上海建设钢结构工程网站网站开发软件是什么专业
上海建设钢结构工程网站,网站开发软件是什么专业,运行怎么打开wordpress,青岛市住房城乡建设局网站SymPy库常用函数 简介 本文抄于https://www.cnblogs.com/baby123/p/6296629.html SymPy是一个符号计算的Python库。它的目标是成为一个全功能的计算机代数系统#xff0c;同时保持代码简 洁、易于理解和扩展。它完全由Python写成#xff0c;不依赖于外部库。SymPy支持符号计算…SymPy库常用函数 简介 本文抄于https://www.cnblogs.com/baby123/p/6296629.html SymPy是一个符号计算的Python库。它的目标是成为一个全功能的计算机代数系统同时保持代码简 洁、易于理解和扩展。它完全由Python写成不依赖于外部库。SymPy支持符号计算、高精度计算、模式匹配、绘图、解方程、微积分、组合数学、离散 数学、几何学、概率与统计、物理学等方面的功能。(来自维基百科的描述) 更多内容请查看本人个人博客https://huiyang865.github.io/2016/08/27/sympy/ Sympy安装方法 安装命令pip install sympy 基本数值类型 实数有理数和整数 SymPy有三个内建的数值类型实数有理数和整数。有理数类用两个整数来表示一个有理数。分子与分母所以Rational(1,2)代表1/2Rational(5,2)代表5/2等等。 from sympy import *
a Rational(1,2)
a
1/2
a*2
1
Rational(2)**50/Rational(10)**50
1/88817841970012523233890533447265625当利用Python的整数计算时要注意一下Python只会截取除法的整数部分 1/2
0
1.0/2
0.5然而你可以 from __future__ import division
1/2 #doctest: SKIP
0.5正确的除法在python3k和isympy中这样做是标准的。 特殊的常数 我们也可以有一些特殊的常数像e和pi它们会被当作符号去对待。1pi不会求得值反而它会保持为1pi例如 pi**2
pi**2
pi.evalf()
3.14159265358979
(piexp(1)).evalf()
5.85987448204884求表达式的浮点数-evalf()函数 正如你看到的evalf()函数可以用求出表达式的浮点数。有一个无穷大的类型被成为oo oo 99999
True
oo 1
oo
If the substitution will be followed by numerical evaluation, it is better to pass the substitution to evalf as(1/x).evalf(subs{x: 3.0}, n21)
0.333333333333333333333
rather than(1/x).subs({x: 3.0}).evalf(21)
0.333333333333333314830Sympy基本使用 定义变量-Symbols函数 对比与其他的计算机代数系统在SymPy中要明确声明符号变量 x symbols(x)x 1
x 1
x,y,zsymbols(x y z)crazy symbols(unrelated)crazy 1
unrelated 1x symbols(x)expr x 1x 2print(expr)
x 1
Changing x to 2 had no effect on expr. This is because x 2 changes the Python variable x to 2, but has no effect on the SymPy Symbol x, which was what we used in creating expr.变量替换subs函数 x symbols(x)expr x 1expr.subs(x, 2)
3from sympy import pi, exp, limit, oofrom sympy.abc import x, y(1 x*y).subs(x, pi)
pi*y 1(1 x*y).subs({x:pi, y:2})
1 2*pi(1 x*y).subs([(x, pi), (y, 2)])
1 2*pireps [(y, x**2), (x, 2)](x y).subs(reps)
6(x y).subs(reversed(reps))
x**2 2(x**2 x**4).subs(x**2, y)
y**2 y(x**2 x**4).xreplace({x**2: y})
x**4 y(x/y).subs([(x, 0), (y, 0)])
0(x/y).subs([(x, 0), (y, 0)], simultaneousTrue)
nan((x y)/y).subs({x y: y, y: x y})
1((x y)/y).subs({x y: y, y: x y}, simultaneousTrue)
y/(x y)limit(x**3 - 3*x, x, oo)
oo调用方式[subs(*args, **kwargs)] 代数 局部的代数式展开使用apart(expr, x): In [1]: 1/( (x2)*(x1) )
Out[1]:1
───────────────
(2 x)*(1 x)
In [2]: apart(1/( (x2)*(x1) ), x)
Out[2]:1 1
───── - ─────
1 x 2 x
In [3]: (x1)/(x-1)
Out[3]:
-(1 x)
────────1 - x
In [4]: apart((x1)/(x-1), x)
Out[4]:2
1 - ─────1 - x代数式的合并 (相当于展开的逆运算)使用together(expr, x) In [7]: together(1/x 1/y 1/z)
Out[7]:
x*y x*z y*z
───────────────x*y*z
In [8]: together(apart((x1)/(x-1), x), x)
Out[8]:
-1 - x
──────
1 - x
In [9]: together(apart(1/( (x2)*(x1) ), x), x)
Out[9]:1
───────────────
(2 x)*(1 x)微积分 极限 在sympy中极限容易求出它们遵循极限语法 limit(function, variable, point) 所以计算x-0时f(x)的极限即limit(f, x, 0) from sympy import *
xSymbol(x)
limit(sin(x)/x, x, 0)
1
limit(x, x, oo)
oo
limit(1/x, x, oo)
0
limit(x**x, x, 0)
1有一些特殊的极限的例子可以阅读文件test_demidovich.py 微分 可以对任意SymPy表达式微分。diff(func, var)。例如 from sympy import *
x Symbol(x)
diff(sin(x), x)
cos(x)
diff(sin(2*x), x)
2*cos(2*x)
diff(tan(x), x)
1 tan(x)**2可以通过以下验证: limit((tan(xy)-tan(x))/y, y, 0)
1 tan(x)**2计算高阶微分 diff(func, var, n) : diff(sin(2*x), x, 1)
2*cos(2*x)
diff(sin(2*x), x, 2)
-4*sin(2*x)
diff(sin(2*x), x, 3)
-8*cos(2*x)级数展开 函数 series(var, point, order) from sympy import *
x Symbol(x)
cos(x).series(x, 0, 10)
1 - x**2/2 x**4/24 - x**6/720 x**8/40320 O(x**10)
(1/cos(x)).series(x, 0, 10)
1 x**2/2 5*x**4/24 61*x**6/720 277*x**8/8064 O(x**10)积分 SymPy支持不定积分超越函数与特殊函数的定积分。SymPy有力的扩展Risch-Norman 算法和模型匹配算法。 from sympy import *
x, y symbols(xy)初等函数 integrate(6*x**5, x)
x**6
integrate(sin(x), x)
-cos(x)
integrate(log(x), x)
-x x*log(x)
integrate(2*x sinh(x), x)
cosh(x) x**2特殊函数 integrate(exp(-x**2)*erf(x), x)
pi**(1/2)*erf(x)**2/4定积分 integrate(x**3, (x, -1, 1))
0
integrate(sin(x), (x, 0, pi/2))
1
integrate(cos(x), (x, -pi/2, pi/2))
2一些广义积分也可以被支持 integrate(exp(-x), (x, 0, oo))
1
integrate(log(x), (x, 0, 1))
-1复数 from sympy import Symbol, exp, I
x Symbol(x)
exp(I*x).expand()
exp(I*x)
exp(I*x).expand(complexTrue)
I*exp(-im(x))*sin(re(x)) cos(re(x))*exp(-im(x))
x Symbol(x, realTrue)
exp(I*x).expand(complexTrue)
I*sin(x) cos(x)函数 三角函数: In [1]: sin(xy).expand(trigTrue)
Out[1]: cos(x)*sin(y) cos(y)*sin(x)
In [2]: cos(xy).expand(trigTrue)
Out[2]: cos(x)*cos(y) - sin(x)*sin(y)
In [3]: sin(I*x)
Out[3]: I*sinh(x)
In [4]: sinh(I*x)
Out[4]: I*sin(x)
In [5]: asinh(I)
Out[5]:
π*I
───2
In [6]: asinh(I*x)
Out[6]: I*asin(x)
In [15]: sin(x).series(x, 0, 10)
Out[15]:3 5 7 9x x x x
x - ── ─── - ──── ────── O(x**10)6 120 5040 362880
In [16]: sinh(x).series(x, 0, 10)
Out[16]:3 5 7 9x x x x
x ── ─── ──── ────── O(x**10)6 120 5040 362880
In [17]: asin(x).series(x, 0, 10)
Out[17]:3 5 7 9x 3*x 5*x 35*x
x ── ──── ──── ───── O(x**10)6 40 112 1152
In [18]: asinh(x).series(x, 0, 10)
Out[18]:3 5 7 9x 3*x 5*x 35*x
x - ── ──── - ──── ───── O(x**10)6 40 112 1152球谐函数 In [1]: from sympy.abc import theta, phi
In [2]: Ylm(1, 0, theta, phi)
Out[2]:————
╲╱ 3 *cos(θ)
────────────——2*╲╱ π
In [3]: Ylm(1, 1, theta, phi)
Out[3]:—— I*φ
-╲╱ 6 *│sin(θ)│*ℯ
────────────────────——4*╲╱ π
In [4]: Ylm(2, 1, theta, phi)
Out[4]:——— I*φ
-╲╱ 30 *│sin(θ)│*cos(θ)*ℯ
────────────────────────────——4*╲╱ π阶乘和伽玛函数 In [1]: x Symbol(x)
In [2]: y Symbol(y, integerTrue)
In [3]: factorial(x)
Out[3]: Γ(1 x)
In [4]: factorial(y)
Out[4]: y!
In [5]: factorial(x).series(x, 0, 3)
Out[5]:2 2 2 2x *EulerGamma π *x
1 - x*EulerGamma ────────────── ───── O(x**3)2 12Zeta函数: In [18]: zeta(4, x)
Out[18]: ζ(4, x)
In [19]: zeta(4, 1)
Out[19]:4
π
──
90
In [20]: zeta(4, 2)
Out[20]:4π
-1 ──90
In [21]: zeta(4, 3)
Out[21]:417 π
- ── ──16 90多项式 In [1]: chebyshevt(2, x)
Out[1]:2
-1 2*x
In [2]: chebyshevt(4, x)
Out[2]:2 4
1 - 8*x 8*x
In [3]: legendre(2, x)
Out[3]:23*x
-1/2 ────2
In [4]: legendre(8, x)
Out[4]:2 4 6 8
35 315*x 3465*x 3003*x 6435*x
─── - ────── ─────── - ─────── ───────
128 32 64 32 128
In [5]: assoc_legendre(2, 1, x)
Out[5]:—————╱ 2
-3*x*╲╱ 1 - x
In [6]: assoc_legendre(2, 2, x)
Out[6]:2
3 - 3*x
In [7]: hermite(3, x)
Out[7]:3
-12*x 8*x微分方程 在isympy中 In [4]: f(x).diff(x, x) f(x) #注意在使用输入该命令之前一定要声明fFunction(f)
Out[4]:2d
─────(f(x)) f(x)
dx dx
In [5]: dsolve(f(x).diff(x, x) f(x), f(x))
Out[5]: C₁*sin(x) C₂*cos(x)代数方程 在isympy中 In [7]: solve(x**4 - 1, x)
Out[7]: [i, 1, -1, -i]
In [8]: solve([x 5*y - 2, -3*x 6*y - 15], [x, y])
Out[8]: {y: 1, x: -3}线性代数 矩阵 矩阵由矩阵类创立建: from sympy import Matrix
Matrix([[1,0], [0,1]])
[1, 0]
[0, 1]不只是数值矩阵亦可为代数矩阵即矩阵中存在符号 x Symbol(x)
y Symbol(y)
A Matrix([[1,x], [y,1]])
A
[1, x]
[y, 1]
A**2
[1 x*y, 2*x]
[ 2*y, 1 x*y]关于矩阵更多的例子请看线性代数教程。 系数匹配 使用 .match()方法引用Wild类来执行表达式的匹配。该方法会返回一个字典。 from sympy import *
x Symbol(x)
p Wild(p)
(5*x**2).match(p*x**2)
{p_: 5}
q Wild(q)
(x**2).match(p*x**q)
{p_: 1, q_: 2}如果匹配不成功则返回None print (x1).match(p**x)
None可以使用Wild类的‘exclude’参数排除参数排除不需要和无意义的匹配结果,来保证结论中的显示是唯一的 x Symbol(x)
p Wild(p, exclude[1,x])
print (x1).match(xp) # 1 is excluded
None
print (x1).match(p1) # x is excluded
None
print (x1).match(x2p) # -1 is not excluded
{p_: -1}打印输出 标准 str(expression)返回如下 from sympy import Integral
from sympy.abc import x
print x**2
x**2
print 1/x
1/x
print Integral(x**2, x)
Integral(x**2, x)Pretty Printing 用pprint函数可以输出不错的ascii艺术 from sympy import Integral, pprint
from sympy.abc import x
pprint(x**2) #doctest: NORMALIZE_WHITESPACE
2
x
pprint(1/x)
1
-
x
pprint(Integral(x**2, x))/
|
| 2
| x dx
|
/[Pretty PrintingWiki]提示在python解释器中为使pretty printing为默认输出使用 $ python
Python 2.5.2 (r252:60911, Jun 25 2008, 17:58:32)
[GCC 4.3.1] on linux2
Type help, copyright, credits or license for more information.from sympy import *import syssys.displayhook pprintvar(x)
xx**3/3
3
x
--
3Integral(x**2, x) #doctest: NORMALIZE_WHITESPACE
/
|
| 2
| x dx
|
/Python printing from sympy.printing.python import python
from sympy import Integral
from sympy.abc import x
print python(x**2)
x Symbol(x)
e x**2
print python(1/x)
x Symbol(x)
e 1/x
print python(Integral(x**2, x))
x Symbol(x)
e Integral(x**2, x)LaTeX printing from sympy import Integral, latex
from sympy.abc import x
latex(x**2)
$x^{2}$
latex(1/x)
$\frac{1}{x}$
latex(Integral(x**2, x))
$\int x^{2}\,dx$MathML from sympy.printing.mathml import mathml
from sympy import Integral, latex
from sympy.abc import x
print mathml(x**2)
applypower/cix/cicn2/cn/apply
print mathml(1/x)
applypower/cix/cicn-1/cn/applyPyglet from sympy import Integral, preview
from sympy.abc import x
preview(Integral(x**2, x)) #doctest:SKIP注解 Isympy默认调用pprint所以这就是为什么看到pretty printing为默认的。 有一个打印的有效模块sympy.printing。用这个模块实现其他的打印 pretty(expr), pretty_print(expr), pprint(expr): 分别返回或者输出表达式的漂亮描述。这是相同latex(expr), print_latex(expr):分别返回或者输出LaTex描写的表达式mathml(expr), print_mathml(expr):分别返回或者输出MathML描写的表达式print_gtk(expr): 表达式打印到Gtkmathview , 这是一个GTK小配件显示MathML代码。Gtkmathview程序是必须的。相关链接 本文转载自于http://blog.csdn.net/pipisorry/article/details/39123247Sympy源码库https://github.com/sympy/sympySymPy’s documentationhttp://docs.sympy.org/latest/index.htmlSymPy-符号运算好帮手http://hyry.dip.jp/tech/book/page/scipy/sympy.htmlSymPy Tutorial(译)http://reverland.org/python/2012/08/30/sympy-tutorial/转载于:https://www.cnblogs.com/3daytears/p/9236175.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/92161.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!