It is recommended to download the .ipynb file and related resources here. Then you can test the codes in the article interactively while you are reading in Jupyter.
from IPython.display import display, Math, Latex
from sympy import *
init_printing()
from helper import comparator_factory, comparator_eval_factory, comparator_method_factory
x,y,z = symbols('x y z')
func_comparator = comparator_factory('Before applying {}:','After:')
method_comparator = comparator_method_factory('Before calling {}:','After:')
eval_comparator = comparator_eval_factory('Before evaluation:','After evaluation:')
m = Matrix(
[
[1, -1],
[3, 4],
[0, 2]
]
)
m
Pass a single layer list of elements will create a $n \times 1$ matrix.
m = Matrix([1,2,3])
m
Several constructors exist for creating common matrices.
M = eye(3)
M
M = zeros(3,4)
display(M)
M = ones(3,2)
M
M = diag(-1, ones(2, 2), Matrix([5, 7, 5]))
M
To get the shape of a matrix, use shape
M.shape
M.row(0)
To get the last column
M.col(-1)
M = Matrix([[1, 2, 3], [3, 2, 1]])
display('Before deletion:',M)
M.row_del(0)
M.col_del(-1)
display('After deletion:',M)
M = Matrix([[1, 2, 3], [4, 5, 6]])
display('Original matrix:',M)
display('Transposed matrix:',M.T)
M = Matrix([[1, 2, 3], [3, 2, 1]])
display('Before insertion:',M)
M = M.row_insert(0, Matrix([[6,6,6]]))
display('After Insertion:',M)
col_insert()
accepts position and $n \times 1$ matrix.
M = Matrix([[1, 2, 3], [3, 2, 1]])
display('Before insertion:',M)
M = M.col_insert(-1, Matrix([6,6]))
display('After Insertion:',M)
M = Matrix([[1, 3], [-2, 3]])
N = Matrix([[0, 3], [0, 7]])
print('M')
display(M)
print('N')
display(N)
print('M+N')
display(M+N)
M = Matrix([[1, 3], [-2, 3]])
N = Matrix([[0, 3], [0, 7]])
print('M')
display(M)
print('N')
display(N)
print('M-N')
display(M-N)
M = Matrix([[1, 2, 3], [3, 2, 1]])
N = Matrix([0, 1, 1])
print('M')
display(M)
print('N')
display(N)
display('MxN')
display(M*N)
M = Matrix([[1, 3], [-2, 3]])
print('M')
display(M)
print('inverse of M:')
display(M**(-1))
M = Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]])
method_comparator(M,'det')
M = Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]])
M_rref, pivot_columns = M.rref()
print('Original Matrix:')
display(M)
print('Reduced echelon form:')
display(M_rref)
print('Pivot columns:')
display(pivot_columns)
nullspace()
returns a list of column vectors which span the nullspace of a matrix.
M = Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]])
print('Matrix M:')
display(M)
print('Spanning vectors for nullspace of M:')
display(M.nullspace())
columnspace()
returns a list of column vectors which span the columnspace of a matrix.
M = Matrix([[1, 1, 2], [2 ,1 , 3], [3 , 1, 4]])
print('Matrix M:')
display(M)
print('Spanning vectors for columnspace of M:')
display(M.columnspace())
M = Matrix([[3, -2, 4, -2], [5, 3, -3, -2], [5, -2, 2, -2], [5, -2, -3, 3]])
print('Matrix M')
display(M)
print('Eigenvalues and their algebraic multiplicity:')
display(M.eigenvals())
eigenvectors
return a list of tuples of the form (eigenvalue, algebraic multiplicity, [eigenvectors])
M = Matrix(
[
[3, -2, 4, -2],
[5, 3, -3, -2],
[5, -2, 2, -2],
[5, -2, -3, 3]
]
)
print('Matrix M')
display(M)
print('Eigenvalues, their algebraic multiplicity and eigenvectors:')
display(M.eigenvects())
If all you want is the characteristic polymonial, use charpoly()
and factor()
print('Matrix M')
display(M)
lamda = symbols('lamda')
p = M.charpoly(lamda).factor()
print('characteristic polymonial of M')
display(p)
M = Matrix(
[
[3, -2, 4, -2],
[5, 3, -3, -2],
[5, -2, 2, -2],
[5, -2, -3, 3]
]
)
P, D = M.diagonalize()
print('Matrix M')
display(M)
print('Matrix P')
display(P)
print('Matrix D')
display(D)
display(Latex('$PDP^{-1}$'))
display(P*D*P**-1)
eq = Eq(x**2-1, 0)
func_comparator(eq, solveset, x)
If an expresison instead of a Eq instance is passed in solveset()
, it is automatically assumed to be zero.
expr = x**2-1
func_comparator(eq, solveset, x)
Result of the solver can be a infinite set.
eq = Eq(sin(x) - 1)
func_comparator(eq, solveset, x)
Result of the solver can be an empty set.
eq = Eq(exp(x))
func_comparator(eq, solveset, x)
When solver fails to find solutions, it returns an conditional set.
eq = Eq(cos(x) - x)
func_comparator(eq, solveset, x)
solveset()
reports each solution only once.
eq = Eq(x**3 - 6*x**2 + 9*x,0)
func_comparator(eq, solveset, x)
To get the solutions with polymonial equation with multiplicity, use roots()
eq = Eq(x**3 - 6*x**2 + 9*x,0)
func_comparator(eq, roots, x)
ls = [
Eq(x + y + z - 1),
Eq(x + y + 2*z - 3)
]
func_comparator(ls,linsolve, x, z)
List of equatons can be simplified into list of expressions if all of them are equal to zero。
ls = [
x + y + z - 1,
x + y + 2*z - 3
]
M = Matrix(
[
[1, 1, 1, 1],
[1, 1, 2, 3]
]
)
func_comparator(M,linsolve, x, y, z)
M = Matrix(
[
[1, 1, 1, 1],
[1, 1, 2, 3]
]
)
ls = A, b = M[:, :-1], M[:, -1]
func_comparator(M,linsolve, x, y, z)
solvset()
is not capable of solving mutiple variable non-linear system. In this situation, use solve()
instead.
eq_list = [
Eq(x*y - 1, 0),
Eq(x-2, 0)
]
func_comparator(eq_list, solve, x, y)
f = symbols('f', cls = Function)
Then define the differential equation. For example, $f''(x) - 2f'(x) + f(x) = \sin(x)$
diffeq = Eq(f(x).diff(x, 2) - 2*f(x).diff(x) + f(x), sin(x))
Then use dsolve()
to solve the equation.
func_comparator(diffeq, dsolve, f(x))
If the equation cannot be solved explicitly, dsolve()
returns an implicit equation.
diffeq = Eq(f(x).diff(x)*(1 - sin(f(x))), 0)
func_comparator(diffeq, dsolve, f(x))