没有合适的资源?快使用搜索试试~ 我知道了~
天文学与计算机:符号计算在科学研究中的应用
天文 学 与 计算机 42 ( 2023 )100666Full length 长度Article 文章sympy 2c 的:From symbolic 相关Expressions 表达两 个几乎C/C ++ Functions 函数而Ode Solvers 的PythonU。 Schmitt A A,B,∗, B 。 莫斯B, C . S 。 Lorenz B, a 。 饥饿BA AScientific 的IT服务 ,ETH苏黎世 ,Binzmühlestrasse 附近 的 旅游130, CH - 8092 :苏黎世 ,Switzerland 瑞士B 1 、 粒子 物理 与 天体 物理 研究 所 , 苏黎世 , Wolfgang-Pauli - Strasse 27 , CH - 8093 , 苏黎世Switzerland 瑞士A A R T i c l ein F o ;article 文章History :3 月 23 日 收到2022接受 的5 11 月202211 月 23 日 online2022Keywords:Codegeneration ODEsolver Python代码生成ComputerAlgebraA B ST RA C t计算机代数系统在科学领域扮演着重要的角色,因为它们促进了发展新的理论模型。结果符号方程通常是以一种编译的编程语言来提供用于实际应用的快速和便携式代码。我们describe sympy 2c的一个新的Python包被设计为在符号发展和理论模型的数字实现之间架起差距。sympy 2c的Symbolic EquationsImplemented in the sympy的Python包到C/C++代码优化使用符号转换。该函数可作为Python中的扩展模块使用。sympy 2c的is used within the使用中PyCOSMO的Python包来解决爱因斯坦—玻尔兹曼方程,一个大的ODE系统,描述宇宙中线性扰动的演变。在回顾了功能和使用情况后sympy 2c的“我们描述了其实施和优化策略。This includes,in particular,a novel approach to generate optimized ODEsolvers making use of在特定的情况下,对生成优化的方法symbolic Jacobian matrix的透明度。Wedemonstrate its performance using the Einstein—Boltzmann equations as a test case.我们用爱因斯坦证明了他的表现——作为测试案例。sympy 2c的1.对于不同的计算物理区域具有一般性和潜在的用途。sympy2c的ispubliclyavailableat公开https://cosmology.ethz.ch/research/software-lab/sympy2c.html。©2022 the作者 ( S ) 。 Published 出版By Elsevier B . V 。 this IS AN open 开放access 访问Article 文章Under the cc by License(http://creativecommons.org/licenses/by/4.0/).1. introduction介绍ComputerAlgebra 的 Systems 【案 例】, SUCH ASMathematica ( Inc . , , 2021 ) 以 及sympy 的 ( 马 华 etal. , ,2017( 重 定向 自 Important Role in Science )原则SUCH AS Mathematics而理论 性物理 学 , 因为 它们 促进 了 新的 或 修改 的 理论 的 发展 。大多 数 情况 下 , 结果 方程 在 一个 公司 实现 - piledProgramming Language在令两 个提供 商差一 点 , 鲁 棒 and portable codes for practical 相 关 文 章applications 。In this paper , we describe 我们 描述sympy 2c 的, 一 个新 的 Python 包 被 设计 为 在 符号 发展 和 之间 架起 一 座 桥梁the数字 化implementation 执行of A A理论 性model . for这个 目的 ,sympy 2c 的python cas 中 实现 符号 运算sympy 的C/C + +代码 , 可以 这样 做used from Python AS AN extension模块 。开发 The Development of sympy 2c 的第 一 个 在 " Field ofComputa " 的 论坛 里 发言tional cosmology 评论AS part 部分ofthe Python package PyCOSMO 的1(莫斯et al. , ,2021� ;张学 良 等 。, ,2017� ;塔西 塔诺 和 阿尔 。, ,2020). TheConcept 的of the Just-In - Time 的 评论compiler hope 希望(行为et阿尔,2015)∗通讯 作者 : Scientific IT Services , 苏黎世 联邦 理工 学院 , Binzmüh -lestrasse 130 , CH - 8092 Zurich ,瑞士 .Email address:作者 : Schmitt@id.ethz.ch( 你 。 施米特 ( Schmitt ) 。1 https://cosmology.ethz.ch/research/software-lab/PyCosmo.htmlhttps://doi.org/10.1016/j.ascom.2022.100666preceded the development of发展sympy 2c的。其他 特点 ,PyCOSMO 的offers A A几乎solver for爱因斯坦 - 玻尔兹曼equation , A Large 大 系统 of Odes Which describes 描 述theevolution 的of linear 线性涡轮 - Turbations在the Universe(Ma而Bertschinger 。,1995; Dodelson 的, ,2003).改进 代码 结构PyCOSMO 的and to make the code generator available to afraudience , we separated the code creation part from theother functionalities of 我们 将 代码 生成 器 与 其他 功能 分开了 。PyCOSMO 的然后 创建 一 个 单独 的 Python 包sympy 2c的。sympy 2c 的 extends the basic C/C++ code generationfunction—alities of基本的C/C++代码生成功能—sympy的例如,通过支持特殊功能,数字集成,插值和ODE的数字解决方案。Weparticularly optimized the 我 们 特 别 优 化 sympy 2c 的 codegenerator for high dimensional stiff 使 用 稀 疏 Jacobian 矩 阵 的ODE A direct solver for general sparse linear systems ( 一 个针对 一般 缐 性 稀疏 系统 的 直接 解决 方案 ) 就 在 用户 的 心中 。ODE solver而威尔be describe 描述下面在细节 。 sympy 2c的Publicly 公开 的available 可 得at https://Co ;smo ;lo ;gy。ethz。Ch/reseA ArCh/so;FtwA Are-lA AB/sympy2C。html。本文是作为后续组织的。在 Section 2我们 introduce the角色ofPython 在 ScientificProgramming, PerformanceRelatedAspects and the Role of 相关 方面 及 作用sympy 2c 的在 这个背景 下 。在 Section 3 " 我们 提供 了 一 个 概述 的 功能 提供sympy 2c 的如何 使用 图书馆 ?We list resources to access the我 们 列 出 资 源sympy 2c 的 package , its source code anddocumentation in Section ( 章节 中 的 文件 ) 4。在 SEC-TION5,We Discuss the implementation 执行而Optimization 优化Details2213-1337/© 2022 年 The Author ( s ) 2013 年 12 月 21 日@下午 12 时 45 分 i = =http://creativecommons.org/licenses/by/4.0/).Contents lists available 可 提供ScienceDirect 科学天文 学 和ComputingJournal 新闻主页 :www.elsevier.com/locate/ascomU。 施密特,B。 Moss,C.S. 劳伦斯和阿尔天文 学而Computing 42( 2023 )1006662ofsympy 2c 的 。in Section 6 , We demonstrate thePerformance ofsympy 2c的。在 Section 7 We summarize our 我们 总结结论 。2. Fast Scientific Computation With 快速 科学 计算pythonPython是一种解释型、高级、通用的编程语言,专注于可编程性和高效编程。今天,它在许多科学学科中扮演着重要的角色。其在科学领域取得成功的因素包括其许可证、使用C/C++的可扩展性以及高质量和易于使用的科学软件包。Fundamental Packages 作 为NumPy 而scipy 的 《 Fea -Tured 》在多 学科Scientific Journals(哈里斯et阿尔,2020� ;塔 尼 斯 等 。 , , 2020 ) . Python Libraries for MachineLearning ( 机器 学习 库 )AS TensorFlow(Developers 开发 者,2022),PyTorch(Paszke et阿尔, ,2019) 以及scikit-learn 教育(佩特罗 等 。, ,2011使用 心得 体会(拉 舒 吉 和 阿尔,2020).另 一 个 重要 的 包装sympy 的Open source . com . puter的 。Algebra系统( CAS )Written在Python 的 。sympy 的CAN be used directly as a Python library , and does notimplement its own programming language , but can beconsidered as an embodied domain-specific language . ( 你 可以 直接 作为 一 个 Python 库 , 而 不是 使用 它 自己 的 编程 语言 , 但 可以 作为 一 个 嵌入 式 特定 领域 的 语言 。HUDAK, ,1997). embedding symbolic compilations 相关Into Python allows 的extending sympy 的在Python所以 usingsympy 的together with other python libraries , without the 在没有 其他 的 Python 库 的 情况 下困难caused By crossing 相关Language Barriers 。 this Embed - Ding 的Into Python ISCrucial 的for the发展of sympy 2c 的而is also used by similartools such as 的 相关 工具devito 。(卢 波 宁 et al. , ,2020).Python is,in particular,widely used in astrophysics.(Python在局部物理中被使用) Exam ples包括astrophy(美联航等。, ,2013� ;价格 Whelan et阿尔,2018),A A Pythonpackage for Astronomy 天文 学that帐户more than 5800 Citations at WEB of Science2AS of March2022年,“未来事件的数据处理管道”(and the data processingpipelines of the Event Horizon)Telescope望远镜)(阿吉和阿尔, ,2019《 The New York Observatory 》 (Abbottet阿尔,2016)而the Legacy 遗产Survey 调查of space而Time( LSST )(朱 睿et al. , ,2015). 2013 年 12 月 21 日@上午 11 时 26 分 i'm reader to the article (Faes, ,2012 2013 年 , Python 在 天文 学 和 天文 学 中 的 作用Science 。自Python以来,它是一种解释型和动态类型的专业编程语言,它提供了极大的灵活性并支持敏捷开发过程。This , however ,also implies reduced speed . ( 这 , However , 也 是 降低 速度 的 影响 )而HiGHER Memory 记忆Consumption 消费during的 runtime 时 间 , 这 对 许 多 科 学 家 都 有 影 响 。applications 。toCircumvent 环 境 这 个 , Solutions 两 个 increase 增 加Execution 执行Speed Have Been开发 的而CAN be classified 的AS Follows :Implementation of parts of the code in a lower level lan -guage , 在 一 个 较 低 级别 的 LAN 中 实现 代码 部分SUCH AS C/C + + 或 休 息 , 或 binding Python 两 个existing 存在C/C + + code . for 例 如 , large 大 部 分 ofNumPy(哈里斯et阿尔, ,2020) 以及scipy 的(塔 尼斯 等 。, ,2020) , 则 实数 a 等于 (史密斯 和 阿尔 。, ,2002) 及 其他Established 的数字 化图书馆 。notable 什么tools两 个SIM - 复Bindings Between Python而 lower Level 语 言 包 括 Cython ( Behnel et 阿 尔 ,2011),swig(Beazley 的,1996) ,Pybind 11 的(雅各 和 艾尔 。, ,2017 ) ,f2 py 的(Peterson, ,2009) 或PyO 3 的。usingJust-In - Time 的 评论compilation AS提供By hope 希望(行为et al. , ,2015) ,数字(Lam et al. 。, ,2015)或PyPy(雷 和Pedroni 的,2006).automaticTranslation of Python code两 个C + + ,AS支持 ByPythran(Guelton 的,2018).2 https://clarivate.com/products/web-of-science/···U。 施密特,B。 Moss,C.S. 劳伦斯和阿尔天文 学而Computing 42( 2023 )1006663=the sympy 2c的package fits in the third category but,incon—trast to the mentioned tools,创建C/C++代码from sympy的expression rather than from existing python functions.(表达比现有的Python函数更高)sympy 的is already to translatebasic expressions to C code , but this 已经 可以 翻译 基本 的C 代码 , 但是 这个Functionality 功能IS Limited 。 for例如 ,sympy 的code世代 Does不是支持特 悬Functions 函数而故称Cannot generate Code for Integrals Anywhere 其它 地方 集成代码sympy 的它 不 可能 与 象征 性 的 反 衍生 工具 竞争 。包装 The package PYODESYS ( Dahlgren 。, ,2018C/C + + 评估 右手 边 的 类似 方法of AN ODE而its Jacobian 的Matrix 。 PYODESYS delegates 代 表 these functions toexisting ODE solvers , so as 这 些 函 数 存 在 或 者 作 为Pygslodeiv 2 的(Bjorn 的, ,2020不 节约 就 不要 帐户 。The Function autowrap的of sympy的其他语言包括:Allowscompilation of expressions to dif—ferent back—ends such as Cor FORTRAN. However , autowrap 的 没 有ex - plicitsymbolic 解 决 方 案 , 因 此 不 足 以 被 使 用 within 其 中PyCOSMO 的(马斯克 et al. , ,2021� ;张 学 良 等 。, ,2017� ;- -西塔 和 阿尔, ,2020). sympy 2c的首页〉外文书〉文学〉西洋文学〉Diagers from the Mentioned Toolsbyoffering a very fast ODE solver by considering sparsity in theJacobian 在 Jacobian 中 提供 非常 快速 的 ODE 解决 方案而Byimplementing 实施routines for数字 化融合 与 扩展插值 。3. Functionalities功能在本节中,我们描述并演示了主要的函数—tionalities ofsympy 2c的。For a detailed documentation of the 的 相关 文章 sympy 2c 的 我 们回 到API sympy 2c 的 onlinedocumentation,可在https://cosmo-docs.phys.ethz.ch/sympy2c/ 。 more 主题 : Tails About TheInner Workings sympy 2c 的在 Section 中 跟踪5。3.1. FunctiontheSteps 步骤两 个Create 创建而use A A Function are ASFollows :1. We declare a function by providing the symbolic expres- sion ( 我们 提供 符号 扩展 功能 )Which the FunctionShould 的evaluate而the arguments 函数拿走2. We declare an extension module and add this functionto the ( 我们 将 增加 一 个 扩展 模块 并 将 此 功能 添加到 该 模块 )模块 。3. 我们 触发 了 代码 生成 过程 , 以及 创作 的 复杂 性code .4. We import the compiled python extension 我们 导入 编译的 Python 扩展模块 。5. We can now call the declared C-Function as a functionwithin this module from 我 们 可 以 将 所 定 义 的 C-Function 作为 一 个 函数 调用 。Python 的 。We demonstrate the usage pattern of 使用 模式sympy 2c的在 Listing 1Where we create and use a Python extensionmodule with a function to compute the volume of a cylinder我们 在 哪里 创建 并 使用 一 个 Python 扩展 模块 , 并 使用一 个 函数 来 计算 cylinder 的 卷v h π r2 Given It Height 的评论h以及 半径r:第4步:控制Mathematica,符号不是第一—在Python中的类公民,这是我们必须declarer而h。第 6 行 : 这 是 十 年 扩展模块 。Line 7 : We Declare a function named ( 我们 确定 一个 函 数 名 称 ) volume _ cylinder 的 用 法 WhichComputes the value of h * pi * r ** 2 而 Takesarguments 论点r而h。LINE8 :We add this Function两 个the extension Module 模块LINE10 : here 这 里 We trigger code Generation 一 代 而compilation of the generated 产生code . We故称进口thegenerated 产生扩展 模块 as imported _ module :。12 号 线 : 现在 功能 名称volume _ cylinder 使用 方法is available as part of the 是 什 么 意 思 imported _module :模块 。·····U。 施密特,B。 Moss,C.S. 劳伦斯和阿尔天文 学而Computing 42( 2023 )1006664∫1fromsympy 2c 的进口Symbols ,功能 ,module2进口NumPy AS NP3 其他4 R,h ==使用Symbol(“R H”)56module_decl的使用 = =Module ( )7F=功能 (volume _ cylinder 的 用法,h * np.pi * r ** 2, R ,h )8module _ decl . add( F ) 模块910imported_module:= =module _ decl . compile _ and _ load ( ) 函数11print(12“ Thevolume of A A Cylinder 的of收音 机1而height 2 IS,13imported _ module . volume _ cylinder( 1.0 , 2.0 )14)清单 1 : 功能 声明 示例sympy 2c 的。1fromsympy 2c 的进口Symbols ,功能 ,综合 性 ,2module3进口NumPy AS NP4 的5 t==使用Symbol(“t”)67Integral=积分 Exp ( - )**( 2 ) 、t ,哦 ,oo )81 . 使用 function _ function (《 高斯 》,统筹 )910imported_module:= =module _ decl . compile _ and _ load ( ) 函数11Gauss_Integral(有效)= =imported _ module . gauss ( ) 函数12print(13“ The数字 化error IS,14ABS( gauss _ integral - np . sqrt( np . pi ) )15)清单 2 : 不 确定 的 数值 比较 ( Numerical computationof an indefinite )Integral 。3.2. Integrals整合一个重要的特征sympy 2c的创建C/C++代码用于数字计算集成。sympy 2c的offers a function函数Integral其中包含表达式,用于较低和upper集成限制的集成变量和表达式。Whenused , for example within a . 在 A 中 使 用 symopy 2c 函数, ,sympy 2c 的creates 建立C/C + + code两 个Compute A A数字 化approxima-tion 。这是特别有用的,当一个关闭的形式的反—衍生产品是未知或不 可计 算的sympy的 。 在 国 内 ,sympy 2c 的 CALL Well-Established 的quadpack 问题(Piessens et阿尔,2012 2013 中国移动 通信 系统 (GSL) (Galassi 的, ,2009).这些路由也支持对不确定的积分的合并。The Function Integral from sympy 2c 的 Return ASymbolic 符号Function Which CAN be used AS any Otherexpression but威尔迟 来 的 Be Translated两 个A A Routine的for the数字 化近似 approximation of the Integral 。Listing2shows AN示例of how两 个Compute the Gaussian 的3.3. Cubic 系列INTERPOLATIONInterpolation 的Functions 函数CAN be helps 帮助两 个Speed up数字 计算机 。sympy 2c 的offers a function 函数InterpolationFunc-tion 1D 功能Which威尔Create 创建C/C+ + code两 个CALL spline 的INTERPOLATION from GSL。sei INTERPOLATION Functions 函 数 CAN 故 称 be usedwithin the 在Integrand 的或在the限for数字 化一体 化AS发行 - Sented previously 预言在Section 3.2或AS A A term在the right Hand side of the symbolic 相关Presentation 介绍ofAN ODE AS introduced 的后来在 Section 3.4。Listing 3显示 如何 支持 此 功能sympy 2c 的:Line 6 : This declares an interpolation function with thegiven name.这是一个使用given名称的插值函数。它 可以用作 任何 其他 符号功能 、E.G. sympy.sin。Right now this serves as a ' place holderfunction ' and the user must provide appropriate values 正确 的 , 服务 作为 一 个 位置 持有 者 函数 ' , 用户 必须 提供 近 似 值 for the actual interpolation of the compiledextension module ( 英语 : for the actual interpolation ofthe compiled extension module )12 ) .Integral∞◎ ◎ e― ―t2dtwith sympy2c 的:LINE7 :We add the Function F Which uses theINTERPOLATIONFunction而Computes cos _ approx ( t )** 2 )。• Line 6:符号整合的定义∫ ∞ e― ―t2dt。tIS• 第 12 行 : After compilation and import , the module 。··U。 施密特,B。 Moss,C.S. 劳伦斯和阿尔天文 学而Computing 42( 2023 )1006665•∞containsthe一体 化variable而theSymbol 象征OOi s――u∞sedBysympy的两 个Represent 代表 。Line 7 : We Add the function Gauss其中 computes thegiven integral and takes no论点 。Line 10 : 我们 呼叫 功能Gauss from Python , this willexecute the numerical 现在 将 执行 数字一体 化 。A功能set_cos_approx_values的值(此名称来源于我们在第6行中指定的名称)以设置插补。Here we providethe values for the 我们 提供 的 价值x i值 0。0,0。1,。 。 。 ,1。0.第 15 行 : 我们 检查 插 补 对 实际 的 影响结果 .··U。 施密特,B。 Moss,C.S. 劳伦斯和阿尔天文 学而Computing 42( 2023 )10066661from sympy 2c 的进口( Symbols ,功能 ,2InterpolationFunction 1D 的 功能模块 )3进口NumPy AS NP4 的5t =使用 Symbol (“ t ”)6cos_approx方法= =1 , 则 实数 a = (“ cos _ approx ” 的 结果)71 . 使用 function _ function (“ F ”,cos _ approx( t )**( 2 ) 、t )89imported _ module = module _ decl . compile _ and _load ( ) 2 10 1011xi = = np . linspace ( 0 , 1.0 , 1.0 )11 )12set _ cos _ approx _ values ( xi , i , i )NP . COS ( xi )13print(14” INTERPOLATIONerror at x = 0.5 IS,15ABS( imported _ module . f ( 0.5 ) )-NP . CO ( 0.5 )**( 2 ) )16)清单 3 : 使用 插 补functions 。3.4. 普通differential 区别equations 的sympy 2c 的Generates efficient code for the numerical solu -tion of stiff and non-stiff ordinary differential , 即 : 为 定 调子和 非 定 调子 的 有效 代码equation 。Explicit first order ordinary equations are of the form第一顺序微分方程y挑战 性(t)=F(t,y(t)),t≥t0y(t0)==y0∈ rn。The Function F英文 名称 : Is Called The右手 侧 ( Right HandSide )英文 片名 Of The Ode And y0 vector of 矢量初始 条件。NumericalSolutionsofSUCHANODE顾问ofapproximations of 的y(t i( C ) 使用 者 规定 时间t i。计算非常近似的算法一般需要一个用户提供的功能来评估F Vector ofinitial conditions初始条件向量。一些 算法 因此 需要 一 个 用户提供 的 函数 来 评估 Jacobian 矩阵F。sympy 2c 的using the 使用LSODA 3 阿尔梅里亚 (Petzold的, ,1983). Wechose LSODA because it detects stiffness of the ODE and au( LSODA 因为 它 检测 出 事物 的 遗传 性 )tomatically 表面 上英文 名 : switch between the BDF 4 方法 (Curtiss而赫希 菲尔德 Hirschfelder, ,1952) , 则 a10 = a10 , a10 =(Bashforth 的而亚当斯,1883)for非 stiff Time Domains 。 选择 的 另 一 个 原因 LSODA 是 , 它 证明 是 可靠 的 , 有效的 , 在 许多 年 , 一 个 原因 , 为什么 它 是 在 默认 方法土 硝英文 名 : The Ode Solver From scipy 的。 LSODA imple -ments 插件Adaptive step Size Control 控制两 个meet本地errorRequirements and Assure 要求稳定 性 。Weused this method 方法两 个REPLACE 地点the existing 存在手工 crafted BDF Solver from 其他PyCOSMO 的2 、 高 强度 、 高 效率 的 步进 控制 , 如 LSODA 两 级 定单 的 灵敏 度 和自动 适应 检测 结果 。统筹 者 。2013 年 12 月 21 日@下午 1 时 45 分 i = Robertson, ,1966� ;英文 片名 Hairer and Wanner, ,1996 2014 年 12 月 26 日 , 美国 化学 家 委员 会 发布 了 题为 《 A Common Example of a StiffEquation describing the kinetics of an autocat alytic chemicalreaction of three reactants with concentrations 》 的 报 告 。y1, ,y2而y3。这个问题经常被用作比较stiff ODE的解决方案的测试问题。2 . Robertson 问
下载后可阅读完整内容,剩余1页未读,立即下载
cpongm
- 粉丝: 4
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功