MATH01, Basic concepts
Back to the previous page
List of posts to read before reading this article
Contents
- Functions and Methods for Operating
- Columns vector
- Row vector
- Value of a corresponding element
- Matrix multiplication
- Transpose of a matrix
- Adjoint of a matrix
- Trace (sum of diagonal elements) of a matrix
- Determinant of a matrix
- Inverse of a matrix
- LU decomposition of a matrix
- Linear system of equations in the form Mx = v, using LU factorization
- QR decomposition of a matrix
- Linear system of equations in the form Mx = v, using QR factorization
- Diagonalization of a matrix M
- Norm of a matrix
- Set of vectors that span the null space of a Matrix
- Rank of a matrix
- Singular values of a matrix
- Linear system of equations in the form Mx = v
Functions and Methods for Operating
Columns vector
INPUT
import sympy
#sympy.init_printing()
sympy.Matrix([1, 2])
OUTPUT
: \(\left[\begin{matrix}1\\2\end{matrix}\right]\)
Matrix([
[1],
[2]])
Row vector
INPUT
import sympy
#sympy.init_printing()
sympy.Matrix([[1, 2]])
OUTPUT
: \(\left[\begin{matrix}1 & 2\end{matrix}\right]\)
Matrix([[1, 2]])
Value of a corresponding element
INPUT
import sympy
#sympy.init_printing()
sympy.Matrix(3, 4, lambda m, n: 10 * m + n)
OUTPUT
: \(\left[\begin{matrix}0 & 1 & 2 & 3\\10 & 11 & 12 & 13\\20 & 21 & 22 & 23\end{matrix}\right]\)
Matrix([
[ 0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23]])
Matrix multiplication
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
x_1, x_2 = sympy.symbols("x_1, x_2")
x = sympy.Matrix([x_1, x_2])
M * x
OUTPUT
: \(\left[\begin{matrix}a x_{1} + b x_{2}\\c x_{1} + d x_{2}\end{matrix}\right]\)
Matrix([
[a*x_1 + b*x_2],
[c*x_1 + d*x_2]])
Transpose of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.T
OUTPUT
: \(\left[\begin{matrix}a & c\\b & d\end{matrix}\right]\)
Matrix([
[a, c],
[b, d]])
Adjoint of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.H
OUTPUT
: \(\left[\begin{matrix}\overline{a} & \overline{c}\\\overline{b} & \overline{d}\end{matrix}\right]\)
Matrix([
[conjugate(a), conjugate(c)],
[conjugate(b), conjugate(d)]])
Trace (sum of diagonal elements) of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.trace()
OUTPUT
: \(a + d\)
a + d
Determinant of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.det()
OUTPUT
: \(a d - b c\)
a*d - b*c
Inverse of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.inv()
OUTPUT
: \(\left[\begin{matrix}\frac{d}{a d - b c} & - \frac{b}{a d - b c}\\- \frac{c}{a d - b c} & \frac{a}{a d - b c}\end{matrix}\right]\)
Matrix([
[ d/(a*d - b*c), -b/(a*d - b*c)],
[-c/(a*d - b*c), a/(a*d - b*c)]])
LU decomposition of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.LUdecomposition()
OUTPUT
: \(\left ( \left[\begin{matrix}1 & 0\\\frac{c}{a} & 1\end{matrix}\right], \quad \left[\begin{matrix}a & b\\0 & d - \frac{b c}{a}\end{matrix}\right], \quad \left [ \right ]\right )\)
(Matrix([
[ 1, 0],
[c/a, 1]]), Matrix([
[a, b],
[0, d - b*c/a]]), [])
Linear system of equations in the form Mx = v, using LU factorization
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
v_1, v_2 = sympy.symbols("v_1, v_2")
v = sympy.Matrix([v_1, v_2])
M.LUsolve(v)
OUTPUT
: \(\left[\begin{matrix}\frac{1}{a} \left(- \frac{b \left(v_{2} - \frac{c v_{1}}{a}\right)}{d - \frac{b c}{a}} + v_{1}\right)\\\frac{v_{2} - \frac{c v_{1}}{a}}{d - \frac{b c}{a}}\end{matrix}\right]\)
Matrix([
[(-b*(v_2 - c*v_1/a)/(d - b*c/a) + v_1)/a],
[ (v_2 - c*v_1/a)/(d - b*c/a)]])
QR decomposition of a matrix
INPUT
import sympy
#sympy.init_printing()
M = sympy.Matrix([[1, 2], [3, 4]])
M.QRdecomposition()
OUTPUT
: \(\displaystyle \left( \left[\begin{matrix}\frac{\sqrt{10}}{10} & \frac{3 \sqrt{10}}{10}\\\frac{3 \sqrt{10}}{10} & - \frac{\sqrt{10}}{10}\end{matrix}\right], \ \left[\begin{matrix}\sqrt{10} & \frac{7 \sqrt{10}}{5}\\0 & \frac{\sqrt{10}}{5}\end{matrix}\right]\right)\)
(Matrix([
[ sqrt(10)/10, 3*sqrt(10)/10],
[3*sqrt(10)/10, -sqrt(10)/10]]), Matrix([
[sqrt(10), 7*sqrt(10)/5],
[ 0, sqrt(10)/5]]))
Linear system of equations in the form Mx = v, using QR factorization
INPUT
import sympy
#sympy.init_printing()
M = sympy.Matrix([[1, 2], [3, 4]])
v = sympy.Matrix([5, 6])
M.QRsolve(v)
OUTPUT
: \(\displaystyle \left[\begin{matrix}-4\\\frac{9}{2}\end{matrix}\right]\)
Matrix([
[ -4],
[9/2]])
Diagonalization of a matrix M
INPUT
import sympy
#sympy.init_printing()
M = sympy.Matrix([[1, 2], [3, 4]])
M.diagonalize()
OUTPUT
: \(\displaystyle \left( \left[\begin{matrix}- \frac{\sqrt{33}}{6} - \frac{1}{2} & - \frac{1}{2} + \frac{\sqrt{33}}{6}\\1 & 1\end{matrix}\right], \ \left[\begin{matrix}\frac{5}{2} - \frac{\sqrt{33}}{2} & 0\\0 & \frac{5}{2} + \frac{\sqrt{33}}{2}\end{matrix}\right]\right)\)
(Matrix([
[-sqrt(33)/6 - 1/2, -1/2 + sqrt(33)/6],
[ 1, 1]]), Matrix([
[5/2 - sqrt(33)/2, 0],
[ 0, 5/2 + sqrt(33)/2]]))
Norm of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.norm()
OUTPUT
: \(\sqrt{\left|{a}\right|^{2} + \left|{b}\right|^{2} + \left|{c}\right|^{2} + \left|{d}\right|^{2}}\)
sqrt(Abs(a)**2 + Abs(b)**2 + Abs(c)**2 + Abs(d)**2)
Set of vectors that span the null space of a Matrix
INPUT
import sympy
#sympy.init_printing()
a, b = sympy.symbols("a, b")
M = sympy.Matrix([[a, b], [a, b]])
M.nullspace()
OUTPUT
: \(\displaystyle \left[ \left[\begin{matrix}- \frac{b}{a}\\1\end{matrix}\right]\right]\)
[Matrix([
[-b/a],
[ 1]])]
Rank of a matrix
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.rank()
OUTPUT
: \(2\)
2
Singular values of a matrix
INPUT
import sympy
#sympy.init_printing()
M = sympy.Matrix([[1, 2], [3, 4]])
M.singular_values()
OUTPUT
: \(\displaystyle \left[ \sqrt{\sqrt{221} + 15}, \ \sqrt{15 - \sqrt{221}}\right]\)
[sqrt(sqrt(221) + 15), sqrt(15 - sqrt(221))]
Linear system of equations in the form Mx = v
INPUT
import sympy
#sympy.init_printing()
a, b, c, d = sympy.symbols("a, b, c, d")
M = sympy.Matrix([[a, b], [c, d]])
M.solve(v)
OUTPUT
: \(\left[\begin{matrix}- \frac{b v_{2}}{a d - b c} + \frac{d v_{1}}{a d - b c}\\\frac{a v_{2}}{a d - b c} - \frac{c v_{1}}{a d - b c}\end{matrix}\right]\)
Matrix([
[(-b*(a*v_2 - c*v_1) + v_1*(a*d - b*c))/(a*(a*d - b*c))],
[ (a*v_2 - c*v_1)/(a*d - b*c)]])
List of posts followed by this article
Reference