基础
from sympy import *
数学格式输出:
init_printing()
添加变量:
x, y, z, a, b, c = symbols('x y z a b c')
声明分数:
Rational(1, 3)
(displaystyle frac{1}{3})
化简式子:
simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
(displaystyle x - 1)
因式分解:
expand((x + 2)*(x - 3))
(displaystyle x^{2} - x - 6)
提取公因式:
factor(x**3 - x**2 + x - 1)
(displaystyle left(x - 1 ight) left(x^{2} + 1 ight))
约分:
cancel((x**2 + 2*x + 1)/(x**2 + x))
(displaystyle frac{x + 1}{x})
裂项:
apart((4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x))
(displaystyle frac{2 x - 1}{x^{2} + x + 1} - frac{1}{x + 4} + frac{3}{x})
变换形式:
tan(x).rewrite(sin)
(displaystyle frac{2 sin^{2}{left(x ight)}}{sin{left(2 x ight)}})
数列求和:
Sum(x ** 2, (x, 1, a)).doit()
(displaystyle frac{a^{3}}{3} + frac{a^{2}}{2} + frac{a}{6})
数列求积:
Product(x**2,(x, 1, a)).doit()
(displaystyle a!^{2})
微积分
求导:
diff(cos(x), x)
(displaystyle - sin{left(x ight)})
求高阶导:
diff(x**4, x, 3)
(displaystyle 24 x)
连续求偏导:
diff(exp(x*y*z), x, y, 2, z, 4)
(displaystyle x^{3} y^{2} left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48 ight) e^{x y z})
不定积分:
integrate(cos(x), x)
(displaystyle sin{left(x ight)})
定积分:
integrate(exp(-x), (x, 0, oo))
(displaystyle 1)
多重积分:
integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
(displaystyle pi)
极限:
limit(sin(x)/x, x, 0)
(displaystyle 1)
泰勒展开(到第4阶):
sin(x).series(x, 0, 4)
(displaystyle x - frac{x^{3}}{6} + Oleft(x^{4} ight))
泰勒展开(在x=6处):
exp(x - 6).series(x, 6)
(displaystyle -5 + frac{left(x - 6 ight)^{2}}{2} + frac{left(x - 6 ight)^{3}}{6} + frac{left(x - 6 ight)^{4}}{24} + frac{left(x - 6 ight)^{5}}{120} + x + Oleft(left(x - 6 ight)^{6}; x ightarrow 6 ight))
矩阵
矩阵求逆:
Matrix([[1, 3], [-2, 3]])**-1
(displaystyle left[egin{matrix}frac{1}{3} & - frac{1}{3}\frac{2}{9} & frac{1}{9}end{matrix} ight])
求转置:
Matrix([[1, 2, 3], [4, 5, 6]]).T
(displaystyle left[egin{matrix}1 & 4\2 & 5\3 & 6end{matrix} ight])
生成单位矩阵:
eye(3)
(displaystyle left[egin{matrix}1 & 0 & 0\0 & 1 & 0\0 & 0 & 1end{matrix} ight])
求行列式:
Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]]).det()
(displaystyle -1)
化成行阶梯形矩阵:
Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]]).rref()
(displaystyle left( left[egin{matrix}1 & 0 & 1 & 3\0 & 1 & frac{2}{3} & frac{1}{3}\0 & 0 & 0 & 0end{matrix} ight], left( 0, 1 ight) ight))
求列向量空间:
Matrix([[1, 1, 2], [2 ,1 , 3], [3 , 1, 4]]).columnspace()
(displaystyle left[ left[egin{matrix}1\2\3end{matrix} ight], left[egin{matrix}1\1\1end{matrix} ight] ight])
M = Matrix([[3, -2, 4, -2], [5, 3, -3, -2], [5, -2, 2, -2], [5, -2, -3, 3]])
求特征值:
M.eigenvals()
(displaystyle left{ -2 : 1, 3 : 1, 5 : 2 ight})
求特征向量:
M.eigenvects()
(displaystyle left[ left( -2, 1, left[ left[egin{matrix}0\1\1\1end{matrix} ight] ight] ight), left( 3, 1, left[ left[egin{matrix}1\1\1\1end{matrix} ight] ight] ight), left( 5, 2, left[ left[egin{matrix}1\1\1\0end{matrix} ight], left[egin{matrix}0\-1\0\1end{matrix} ight] ight] ight) ight])
求对角化矩阵,返回两个矩阵P、D满足(PDP^{-1}=M):
M.diagonalize()
(displaystyle left( left[egin{matrix}0 & 1 & 1 & 0\1 & 1 & 1 & -1\1 & 1 & 1 & 0\1 & 1 & 0 & 1end{matrix} ight], left[egin{matrix}-2 & 0 & 0 & 0\0 & 3 & 0 & 0\0 & 0 & 5 & 0\0 & 0 & 0 & 5end{matrix} ight] ight))
解方程
求解集:
solveset(x**2 - x, x)
(displaystyle left{0, 1 ight})
求解集(显示多少个重根):
roots(x**3 - 6*x**2 + 9*x, x)
(displaystyle left{ 0 : 1, 3 : 2 ight})
求解集(用Eq构造等式):
solveset(Eq(sin(x), 1), x, domain=S.Reals)
(displaystyle left{2 n pi + frac{pi}{2}; |; n in mathbb{Z} ight})
解线性方程组:
linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z))
(displaystyle left{left( - y - 1, y, 2 ight) ight})
解线性方程组(矩阵表示):
linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z))
(displaystyle left{left( - y - 1, y, 2 ight) ight})
解非线性方程组:
nonlinsolve([exp(x) - sin(y), 1/y - 3], [x, y])
(displaystyle left{left( log{left(sin{left(frac{1}{3} ight)} ight)}, frac{1}{3} ight), left( left{2 n i pi + left(log{left(sin{left(frac{1}{3} ight)} ight)}mod{2 i pi} ight); |; n in mathbb{Z} ight}, frac{1}{3} ight) ight})
解微分方程:
f, g = symbols('f g', cls=Function)
dsolve(Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x)), f(x))
(displaystyle f{left(x ight)} = left(C_{1} + C_{2} x ight) e^{x} + frac{cos{left(x ight)}}{2})
解不等式组:
from sympy.solvers.inequalities import *
reduce_inequalities([x <= x ** 2], [x])
(displaystyle left(1 leq x wedge x < infty ight) vee left(x leq 0 wedge -infty < x ight))
逻辑代数
from sympy.logic.boolalg import *
合取范式:
to_cnf(~(x | y) | z)
(displaystyle left(z vee eg x ight) wedge left(z vee eg y ight))
析取范式:
to_dnf(x & (y | z))
(displaystyle left(x wedge y ight) vee left(x wedge z ight))
化简逻辑函数:
simplify_logic((~x & ~y & ~z) | ( ~x & ~y & z))
(displaystyle eg x wedge eg y)
from sympy.logic import *
化简最小项之和为析取范式
minterms = [0, 7]
SOPform([x, y, z], minterms)
(displaystyle left(x wedge y wedge z ight) vee left( eg x wedge eg y wedge eg z ight))
化简最小项之和为合取范式
minterms = [[1, 0, 1], [1, 1, 0], [1, 1, 1]]
POSform([x, y, z], minterms)
(displaystyle x wedge left(y vee z ight))
化简最小项之和为析取范式(第7项任取)
minterms = [[1, 0, 1], [1, 1, 0]]
dontcares = [7]
SOPform([x, y, z], minterms, dontcares)
(displaystyle left(x wedge y ight) vee left(x wedge z ight))
数论
from sympy.ntheory import *
阶乘:
factorial(10)
(displaystyle 3628800)
分解质因数:
factorint(300)
(displaystyle left{ 2 : 2, 3 : 1, 5 : 2 ight})
factorint(300, visual=True)
(displaystyle 2^{2} cdot 3^{1} cdot 5^{2})
求欧拉函数:
totient(25)
(displaystyle 20)
判断质数:
isprime(101)
True
莫比乌斯函数:
mobius(13 * 17 * 5)
(displaystyle -1)
乘法逆元(模后者意义):
mod_inverse(3, 5)
(displaystyle 2)
from sympy.ntheory.factor_ import *
求因子:
divisors(36)
(displaystyle left[ 1, 2, 3, 4, 6, 9, 12, 18, 36 ight])
from sympy.ntheory.modular import *
中国剩余定理解同余方程(模数需互质,前三个数为模数,后三个数为余数,返回第一个数为结果):
crt([99, 97, 95], [49, 76, 65])
(displaystyle left( 639985, 912285 ight))
解同余方程(模数不需互质但比中国剩余定理慢,每个元组第一个数为余数,第二个数为模数,返回第一个数为结果):
solve_congruence((2, 3), (3, 5), (2, 7))
(displaystyle left( 23, 105 ight))
from sympy.ntheory.residue_ntheory import *
求原根(如下2在模19意义下的所有幂占满了0到18):
primitive_root(19)
(displaystyle 2)
求离散对数(如下(7^3 mod 15 = 41)):
discrete_log(41, 15, 7)
(displaystyle 3)