In [2]:
import numpy as np
from numpy.linalg import *
rg = matrix_rank
from IPython.display import display, Math, Latex, Markdown
from sympy import *
pr = lambda s: display(Markdown('$'+str(latex(s))+'$'))
def pmatrix(a, intro='',ending='',row=False):
if len(a.shape) > 2:
raise ValueError('pmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{pmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{pmatrix}']
if row:
return(intro+'\n'.join(rv)+ending)
else:
display(Latex('$$'+intro+'\n'.join(rv)+ending+'$$'))
In [6]:
C = np.array([[1,2],
[0,1]])
pmatrix(C, intro=r'C_{2\times 2}=')
D = np.array([[3,1],
[1,0]])
pmatrix(D, intro=r'D_{2\times 2}=')
B = np.array([[5,1],
[5,2]])
pmatrix(B, intro=r'B_{2\times 2}=')
In [11]:
A = np.array([[5,6],
[3,4]])
pmatrix(rg(A), intro=r'rg(A)=')
In [12]:
pmatrix(inv(C), intro=r'C^{-1}=')
pmatrix(B.T, intro=r'C^{T}=')
pmatrix(B.dot(C), intro=r'BC=')
9
pmatrix(rg(B), intro=r'rg(C)=')
pmatrix(det(B), intro=r'det(C)=')
In [18]:
A = np.array([[2,6],
[1,3]])
#pmatrix(rg(B), intro=r'rg(B)=')
pmatrix(rg(A), intro=r'rg(A)=')
#pmatrix(rg(A.dot(B)), intro=r'rg(AB)=')
In [10]:
a1 = Symbol('a_{12}')
b1 = Symbol('b_{11}')
c1 = Symbol('c_{22}')
d1 = Symbol('d_{21}')
X =np.array([[a1,b1],
[c1,d1]])
B = np.array([[5,1],
[5,2]])
C1 = np.array([[1,1],
[1,2]])
D1 = np.array([[2,1],
[1,0]])
C2 = np.array([[1,-1],
[0,1]])
D2 = np.array([[1,1],
[0,1]])
pmatrix(B.reshape((4, 1)), intro="X=")
In [4]:
pmatrix( (C1.dot(X)).dot(D1))
A = (C1.dot(X)).dot(D1) + (C2.dot(X)).dot(D2)
pmatrix(A)
F = np.array([[3,1,1,1],
[2,1,0,-1],
[2,1,5,2],
[1,0,3,1]])
pmatrix(F, ending=pmatrix(X.reshape((4, 1)),row=True)+"="+pmatrix(B.reshape((4, 1)),row=True))
pmatrix(rg(F), intro=r'rg(F)=')
print("Зничит есть нормальное решение!)")
In [12]:
from sympy import Matrix, solve_linear_system
from sympy.abc import a,b,c,d
Examlpe: x + 4 y == 2 -2 x + y == 14
from sympy import Matrix, solve_linear_system from sympy.abc import x, y
system = Matrix(( (1, 4, 2), (-2, 1, 14))) solve_linear_system(system, x, y)
In [14]:
system = Matrix(( (3,1,1,1,5), (2,1,0,-1,1), (2,1,5,2,5),(1,0,3,1,2) ))
x = solve_linear_system(system, a,b,c,d)
X =np.array([[x[a],x[b]],[x[c],x[d]] ])
In [15]:
pmatrix(X,intro="X=")
In [ ]:
In [34]:
x = Symbol('x')
y = Symbol('y')
pr(integrate(sqrt(4*x-x**2), x))
In [ ]: