Python Sympy 模块2024 年 8 月 29 日 | 阅读 15 分钟 SymPy 是一个开源的符号计算 Python 包。它可以作为独立的程序、其他应用程序的库,或者作为一个名为 SymPy Live 或 SymPy Gamma 的实时在线应用程序使用。由于 SymPy 完全用 Python 编写且依赖性最小,因此易于安装和研究。SymPy 凭借其易用性和用一种流行语言编写的简单可扩展的代码库,提供了一个低入门门槛的计算机代数系统。从初等符号算术到微积分、代数、离散数学和量子物理,SymPy 应有尽有。它能够将计算结果格式化为 LaTeX 代码。 首先,SymPy 是完全免费的。它是开源的,并在宽松的 BSD 许可证下发布,允许您修改甚至销售源代码。这与 Maple 或 Mathematica 等流行的商业系统形成对比,后者软件和许可证费用高达数百美元。SymPy 是根据 New BSD 许可证发布的开源软件。Ondrej Ertk 和 Aaron Meurer 是主要开发者。它由 Ondrej Erik 于 2005 年创立。 符号计算涉及对数学对象的符号计算。这意味着数学对象被精确表示而不是粗略表示,并且带有未求值的变量的数学方程将以符号形式保留。其次,SymPy 是用 Python 编写的。绝大多数计算机代数系统都创建自己的语言。 SymPy 则不然。SymPy 完全用 Python 编写并在 Python 中运行。这意味着如果您已经了解 Python,那么入门 SymPy 将会容易得多,因为您已经知道了语法(如果您不了解,Python 也非常容易学习)。Python 已经是一种设计良好且经过实战检验的语言。SymPy 的开发者对其编写数学软件的能力充满信心,但设计计算机语言则是完全不同的事情。通过利用现有语言,我们可以专注于数学的重要方面。 Sage 是另一个使用 Python 作为其编程语言的计算机代数系统。然而,Sage 非常庞大,需要下载超过一千兆字节。SymPy 的优势在于轻量级。它不仅小巧,而且除了 Python 之外没有其他依赖项,这使得它几乎可以在任何地方使用。此外,Sage 和 SymPy 的目标并不相同。 Sage 旨在成为一个功能齐全的数学系统,将所有主要的开源数学系统结合到一个软件包中。当您使用 Sage 的函数(如 integrate)时,它会使用它提供的开源软件包之一。事实上,Sage 中包含了 SymPy。然而,SymPy 旨在成为一个独立的系统,所有功能都在框架内实现。SymPy 还可以用作库,这是一个重要特性。许多计算机代数系统旨在用于交互式场景,但自动化或扩展它们却很困难。您可以在交互式 Python 环境中使用 SymPy,或者轻松地将其导入到您自己的 Python 程序中。SymPy 还提供了 API,可以轻松添加您自己的自定义函数。 一些主要功能
现在让我们看一下 Python 中 sympy 模块的代码。 代码输出 please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 1 enter the integer whose square root you want to calculate 7 ?7??7 equals to 7 To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 2 enter the expression(linear) that you want to form with two variables 2*x + 5*y - 18*x + 34*y Expression is -16*var1 + 39*var2 To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 3 The trigno expression is exp(var1)*sin(var1) The differential expression is exp(var1)*sin(var1) + exp(var1)*cos(var1) To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 4 The trigno expression is exp(var2)*sin(var2) + exp(var2)*cos(var2) The integration expression is 2*exp(var2)*cos(var2) To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 5 Enter the values for the 2X2 matrix Enter values for the first row 3 8 Enter values for the second row 5 9 Matrix is Matrix([[3, 8], [5, 9]]) Eigenvalues of the matrix are {13: 1, -1: 1} To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 5 Enter the values for the 2X2 matrix Enter values for the first row 1 9 Enter values for the second row 4 2 Matrix is Matrix([[1, 9], [4, 2]]) Eigenvals of the matrix are {3/2 - sqrt(145)/2: 1, 3/2 + sqrt(145)/2: 1} To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 1 enter the integer whose square root you want to calculate 98 7??2?7??2 equals to 98 To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivative of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 5 Enter the values for the 2X2 matrix Enter values for the first row 500 600 Enter values for the second row 700 800 Matrix is Matrix([[500, 600], [700, 800]]) Eigenvals of the matrix are {650 - 50*sqrt(177): 1, 650 + 50*sqrt(177): 1} To continue with code execution, type [y] otherwise [n]. y please choose any one of the operations from the listed below the list of operations:: 1. To use the sympy module for calculation of the square root. 2. To use the sympy module for generating dynamic expressions. 3. To use the sympy module to calculate the derivate of the trigonometric expressions. 4. To use the sympy module to calculate the integration of the trigonometric expressions. 5. To use the sympy module to calculate the eigenvalues of a matrix. 6. To exit from the code execution. 6 在上面编写的代码中,我们调用了不同的函数,这些函数是我们自己编写的,每个函数都代表了 Python 提供的该模块的不同用例场景。当需要用户输入时,会提示用户输入,然后对用户提供的输入数据执行一些操作,并将结果数据作为该特定函数返回的类型或结果值打印给用户。对用户提供的输入数据执行的操作取决于数据的输入类型。 优点
因此,在本文中,我们了解了 Sympy 模块在 Python 中的用法,并理解了可以使用该模块执行各种数学运算的用例。 下一主题Smote Python |
我们请求您订阅我们的新闻通讯以获取最新更新。