python矩阵幂函数 python矩阵乘方( 三 )


f, g = symbols('f g', cls=Function)函数的定义,解微分方程diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))再和dsolve(diffeq,f(x))结合 。得到Eq(f(x), (C1 + C2*x)*exp(x) + cos(x)/2),dsolve(f(x).diff(x)*(1 - sin(f(x))), f(x))解出来Eq(f(x) + cos(f(x)), C1),,
Matrix([[1,-1],[3,4],[0,2]]),,Matrix([1, 2, 3])列表示 。M=Matrix([[1,2,3],[3,2,1]])
N=Matrix([0,1,1])
M*N符合矩阵的乘法 。M.shape显示矩阵的行列数 。
M.row(0)获取M的第0行 。M.col(-1)获取倒数第一列 。
M.col_del(0)删掉第1列 。M.row_del(1)删除第二行,序列是从0开始的 。M = M.row_insert(1, Matrix([[0, 4]]))插入第二行,,M = M.col_insert(0, Matrix([1, -2]))插入第一列 。
M+N矩阵相加,M*N,3*M,M**2,M**-1,N**-1表示求逆 。M.T求转置 。
eye(3)单位 。zeros(2, 3),0矩阵 , ones(3, 2)全1 , diag(1, 2, 3)对角矩阵 。diag(-1, ones(2, 2), Matrix([5, 7, 5]))生成Matrix([
[-1, 0, 0, 0],
[ 0, 1, 1, 0],
[ 0, 1, 1, 0],
[ 0, 0, 0, 5],
[ 0, 0, 0, 7],
[ 0, 0, 0, 5]])矩阵 。
Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]])
一行一行显示, , M.det()求行列式 。M.rref()矩阵化简 。得到结果为Matrix([
[1, 0,1,3],
[0, 1, 2/3, 1/3],
[0, 0,0,0]]), [0, 1]) 。
M = Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]]),M.nullspace()
Columnspace
M.columnspace()和M = Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]])
M = Matrix([[3, -2,4, -2], [5,3, -3, -2], [5, -2,2, -2], [5, -2, -3,3]])和M.eigenvals()得到{3: 1, -2: 1, 5: 2},,This means thatMhas eigenvalues -2, 3, and 5, and that the eigenvalues -2 and 3 have algebraic multiplicity 1 and that the eigenvalue 5 has algebraic multiplicity 2.
P, D = M.diagonalize(),P得Matrix([
[0, 1, 1,0],
[1, 1, 1, -1],
[1, 1, 1,0],
[1, 1, 0,1]]),,D为Matrix([
[-2, 0, 0, 0],
[ 0, 3, 0, 0],
[ 0, 0, 5, 0],
[ 0, 0, 0, 5]])
P*D*P**-1 == M返回为True 。lamda = symbols('lamda') 。
lamda = symbols('lamda')定义变量 , p = M.charpoly(lamda)和factor(p)
expr = x**2 + x*y , srepr(expr)可以将表达式说明计算法则,"Add(Pow(Symbol('x'), Integer(2)), Mul(Symbol('x'), Symbol('y')))" 。。
x = symbols('x')和x = Symbol('x')是一样的 。srepr(x**2)得到"Pow(Symbol('x'), Integer(2))" 。Pow(x, 2)和Mul(x, y)得到x**2 。x*y
type(2)得到class 'int',type(sympify(2))得到class 'sympy.core.numbers.Integer'..srepr(x*y)得到"Mul(Symbol('x'), Symbol('y'))" 。。。
Add(Pow(x, 2), Mul(x, y))得到"Add(Mul(Integer(-1), Pow(Symbol('x'), Integer(2))), Mul(Rational(1, 2), sin(Mul(Symbol('x'), Symbol('y')))), Pow(Symbol('y'), Integer(-1)))" 。。Pow函数为幂次 。
expr = Add(x, x) , expr.func 。。Integer(2).func , class 'sympy.core.numbers.Integer',,Integer(0).func和Integer(-1).func, , ,expr = 3*y**2*x和expr.func得到class 'sympy.core.mul.Mul',,expr.args将表达式分解为得到(3, x, y**2),,expr.func(*expr.args)合并 。expr == expr.func(*expr.args)返回True 。expr.args[2]得到y**2,expr.args[1]得到x , expr.args[0]得到3. 。
expr.args[2].args得到(y, 2) 。。y.args得到空括号 。Integer(2).args得到空括号 。
from sympy import *
E**(I*pi)+1,可以看出,I和E,pi已将在sympy内已定义 。
x=Symbol('x') , ,expand( E**(I*x) )不能展开,expand(exp(I*x),complex=True)可以展开,得到I*exp(-im(x))*sin(re(x)) + exp(-im(x))*cos(re(x)),,x=Symbol("x",real=True)将x定义为实数 。再展开expand(exp(I*x),complex=True)得到 。I*sin(x) + cos(x) 。。
tmp = series(exp(I*x), x, 0, 10)和pprint(tmp)打印出来可读性好,print(tmp)可读性不好 。。pprint将公式用更好看的格式打印出来,,pprint( series( cos(x), x, 0, 10) )

推荐阅读