分享

SymPy简易教程(三)

 londonKu 2012-09-30

球谐函数:

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", integer=True)

 

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  2

                   x *EulerGamma    π *x

1 - x*EulerGamma + ────────────── + ───── + O(x**3)

                         2            12

Zeta函数:

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]:

        4

  17   π

- ── + ──

  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]:

          2

       3*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)     #注意在使用输入该命令之前,一定要声明f=Function('f')

Out[4]:

   2

  d

─────(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 (x+1).match(p**x)

None

 

可以使用Wild类的‘exclude’参数(排除参数),排除不需要和无意义的匹配结果,来保证结论中的显示是唯一的:

>>>x = Symbol('x')

>>>p = Wild('p', exclude=[1,x])

>>>print (x+1).match(x+p) # 1 is excluded

None

>>>print (x+1).match(p+1) # x is excluded

None

>>>print (x+1).match(x+2+p) # -1 is not excluded

{p_: -1}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多