AMA3724 Topic - 0 Note
AMA3724 Topic - 0 Note
Matrix
An m × n matrix A is a rectangular array of numbers with m rows and n
columns
a11 a12 ··· a1n
a21 a22 ··· a2n
A = [aij ] =
... .. .. .
..
. . .
am1 am2 ··· amn
The element aij is the entry at the ith row and the jth column of A and is
called the (i, j)th entry of A.
" #
2
1 2 1 −2 4
A= 1 1 −1 B= 7 C= D=
3 4 5 0 3
| {z } 8
1×3 | {z }
|{z} | {z }
2×2 2×3
3×1
Basic matrix operations P. 3 / 44
Download Anaconda:
https://www.anaconda.com/products/individual
https://docs.sympy.org/latest/tutorials/intro-tutorial/matrices.html
Basic matrix operations P. 7 / 44
[9]: B = np.array([[2],[7],[8]]); B
[3]: B = sp.Matrix([[2],[7],[8]]); B
[4]:
h i
1 2 [10]: array([[1, 2],
3 4 [3, 4]])
[5]:
h i
1 −2 4 [11]: array([[ 1, -2, 4],
5 0 3 [ 5, 0, 3]])
[6]: ((1, 3), (3, 1), (2, 2), (2, 3)) [12]: ((1, 3), (3, 1), (2, 2), (2, 3))
3. c(A + B) = cA + cB,
4. (c + d)A = cA + dA,
5. c(dA) = (cd)A,
0 ··· 0
.. .. ..
6. A + 0 = A. Here, 0 = . . . is the m × n zero matrix.
0 ··· 0
Basic matrix operations P. 9 / 44
4 0 5 1 1 1
Example 1 For A = and B = ,
−1 3 2 3 5 7
[4]:
h i
5 1 6
2 8 9
[2]: A = sp.Matrix([[4,0,5],[-1,3,2]]); A
[5]: A-B
[2]:
h i
4 0 5
[5]:
h i
−1 3 2 3 −1 4
−4 −2 −5
[3]: B = sp.Matrix([[1,1,1],[3,5,7]]); B
[6]: 2*B
[3]: [6]:
h i h i
1 1 1 2 2 2
3 5 7 6 10 14
h i h i h i
4 0 5 1 1 1 5 1 6
A+B = −1 3 2 + 3 5 7 = 2 8 9
h i h i h i
4 0 5 1 1 1 3 −1 4
A−B = −1 3 2 − 3 5 7 = −4 −2 −5
h i h i
1 1 1 2 2 2
2B = 2· 3 5 7 = 6 10 14
Matrix Multiplication
For any m × n matrix A = [aij ] and n × p matrix B = [bij ], the product of A
n
X
and B, denoted by AB, is the m × p matrix with aik bkj as the (i, j)th
k=1
entry. Equivalently, if C = [cij ] = AB, then
n
X
cij = aik bkj = ai1 b1j + ai2 b2j + · · · + ain bnj .
k=1
"
4
#
4
Example 2 For A = 2 3 ,B= ,C= 2 3 4 , and D = −1 ,
−1
2×1 1
1×2 z }| { 1×1
z }| {
4
z }| {
AB = 2 3 = 2 · 4 + 3 · (−1) = 5
−1
"
4
#
CD = 2 3 4 −1 = 2 · 4 + 3 · (−1) + 4 · 1 = 9
| {z } 1 | {z }
1×3 | {z } 1×1
3×1
Basic matrix operations P. 11 / 44
2 3 4 3 6 4 3
Example 3 For A = ,B= , and C = .
1 −5 1 −2 3 1 −2
AB [1]: import sympy as sp
h ih i A = sp.Matrix([[2,3],[1,-5]])
2 3 4 3 6 B = sp.Matrix([[4,3,6],[1,-2,3]])
= 1 −5 1 −2 3 C = sp.Matrix([[4,-3],[1,2]])
| {z } | {z }
2×2 2×3
h i [2]: A
2·4+3·1 2 · 3 + 3 · (−2) 2·6+3·3
= 1 · 4 + (−5) · 1 1 · 3 + (−5) · (−2) 1 · 6 + (−5) · 3 [2]:
h i
2 3
| {z } 1 −5
2×3
11 0 21 [3]: B
= .
−1 13 −9 [3]:
h i
4 3 6
1 −2 3
4 3 6 2 3
BA = is NOT defined. [4]: C
1 −2 3 1 −5
[4]:
| {z } | {z } h i
2×3 2×2 4 −3
1 2
2 3 4 −3 11 0 [5]:
AC = = # Matrix Multiplication
1 −5 1 2 −1 −13 A@B
[5]:
h i
4 −3 2 3 5 27 11 0 21
CA = = 6= AC −1 13 −9
1 2 1 −5 4 −7
[6]: B@A
---------------------------------------------------------------------------
ShapeError Traceback (most recent call last)
/var/folders/wh/1v8jgf9d5gbfykybbcypgbt40000gn/T/ipykernel_25582/2247667524.py in <module>
----> 1 B@A
/opt/anaconda3/lib/python3.9/site-packages/sympy/core/decorators.py in binary_op_wrapper(self,␣
,→other)
134 if f is not None:
135 return f(self)
--> 136 return func(self, other)
137 return binary_op_wrapper
138 return priority_decorator
/opt/anaconda3/lib/python3.9/site-packages/sympy/core/decorators.py in binary_op_wrapper(self,␣
,→other)
134 if f is not None:
135 return f(self)
--> 136 return func(self, other)
137 return binary_op_wrapper
138 return priority_decorator
ShapeError: Matrix size mismatch: (2, 3) * (2, 2).
Basic matrix operations P. 13 / 44
[7]:
Power of A # Power
A**3
If A is an n × n matrix and k is a positive integer,
[7]:
h i
then 5 66
22 −149
Ak = A| · A ·{zA · · · A} .
k copies [8]: A**10
[8]:
h i
1110172 −8172999
Transpose of A −2724333 20180503
[13]:
h i
T T 2 1 4 1 5 4 5 4
A C = = 27 −7
3 −5 −3 2 27 −7
[14]: C.T@A.T
4 1 2 1 11 −1
C T AT = = [14]:
h
11 −1
i
−3 2 3 −5 0 −13 0 −13
Determinant P. 15 / 44
Determinant of 2 × 2 / 3 × 3 matrices
The determinant of a 2 × 2 matrix is defined and denoted by
a11 a12 a11 a12
det A = |A| = det = = a11 a22 − a12 a21 .
a21 a22 a21 a22
Cofactor
Let A be an n × n matrix.
1. Denote by Aij the (n − 1) × (n − 1) matrix obtained from A by deleting
its ith row and jth column.
2. The (i, j)-th cofactor Cij is defined by
Determinant P. 16 / 44
Determinant
For any n ≥ 2, the determinant of an n × n matrix A = [aij ] is defined by
n
X
det A = a11 C11 + a12 C12 + a13 C13 + a14 C14 + · · · + a1n C1n = a1j C1j .
j=1
It can also be computed by a cofactor expansion across any row or down any
column.
I The expansion across the ith row is
n
X
det A = ai1 Ci1 + ai2 Ci2 + ai3 Ci3 + ai4 Ci4 + · · · + ain Cin = aij Cij .
j=1
Properties of determinant
For any n × n matrices A and B and scalar c,
Determinant P. 18 / 44
−2
1 5 0
2 0 4 −1
Example 5 Suppose A = .
3 1 0 7
0 4 −2 0
det A
0 4 −1 2 4 −1 2 0 −1 2 0 4
= 1· 1 0 7 − (−2) · 3 0 7 +5· 3 1 7 −0· 3 1 0
4 −2 0 0 −2 0 0 4 0 0 4 −2
−2 5 0 1 5 0 1 −2 0 1 −2 5
= −0 · 0 4 −1 + 4 · 2 4 −1 − (−2) · 2 0 −1 + 0 · 2 0 4
1 0 7 3 0 7 3 1 7 3 1 0
Adjoint
For any n × n matrix A, the matrix of cofactors
A · adj A = (det A) In .
Determinant P. 20 / 44
[2]: A = sp.Matrix([[1,-2,5,0],[2,0,4,-1],[3,1,0,7],[0,4,-2,0]]); A
[2]:
" #
1 −2 5 0
2 0 4 −1
3 1 0 7
0 4 −2 0
[3]: −158
[4]: # Adjugate @ SymPy
A.adjugate()
[4]:
" #
114 −112 −16 61
−34 14 2 −57
−68 28 4 −35
−44 46 −16 −18
[5]: A = np.array([[1,-2,5,0],[2,0,4,-1],[3,1,0,7],[0,4,-2,0]]); A
[6]: -158.00000000000003
Determinant P. 21 / 44
[2]:
2 2 6 2 2
7 9 6 3 2
7 2 1 2 1
9 4 4 5 0
5 2 2 6 7
[3]: −4422
[4]: print("The computation time is :", end-start)
[6]: −146836563203882402681100099842173195419442530298048364881649588442071600424473837277364531
[7]: print("The computation time is :", end-start)
Determinant P. 22 / 44
[9]: 20678.999999999978
[12]: 6.309923345255805e+279
Nonsingular matrices
I An n × n matrix A is said to be nonsingular (or invertible) if there is
the unique inverse, denoted by A−1 , such that
Inverse Formula
A matrix A is nonsingular if and only if det A 6= 0 and the inverse, if exists, is
equal to
1
A−1 = adj A.
det A
Nonsingular matrices P. 24 / 44
(2 × 2 case) :
−1 1 C11 C21 1 a22 −a12
A = =
det A C12 C22 det A −a21 a11
(3 × 3 case) :
C11 C21 C31
" #
1
A−1 = C12 C22 C32
det A
C13 C23 C33
a22 a23 a12 a13 a12 a13
−
a32 a33 a32 a33 a22 a23
1 a21 a23 a11 a13 a11 a13
= − a −
det A a33 a31 a33 a21 a23
31
a 21 a22 a11 a12 a11 a12
−
a31 a32 a31 a32 a21 a22
Nonsingular matrices P. 25 / 44
[2]: A = sp.Matrix([[1,-2,5,0],[2,0,4,-1],[3,1,0,7],[0,4,-2,0]]); A
[2]:
" #
1 −2 5 0
2 0 4 −1
3 1 0 7
0 4 −2 0
[3]:
"− 57 56 8 61
− 158
#
79 79 79
17 7
− 79 1
− 79 57
79 158
34 14
− 79 2
− 79 35
79 158
22 23
− 79 8 9
79 79 79
[4]: A = np.array([[1,-2,5,0],[2,0,4,-1],[3,1,0,7],[0,4,-2,0]]); A
Partitioned matrices P. 26 / 44
A= 1 5 −2 3 −1 2 . [2]:
h
2 −3 1
i
0 −4 −2 7 −1 3 1 5 −2
Partitioned matrices P. 28 / 44
Block Multiplication
Suppose
A11 ··· A1s B11 ··· B1t
. .. .. . .. ..
A = [Aij ] = .. . . and B = [Bij ] = .. . .
Ar1 ··· Ars Bs1 ··· Bst
In particular, if b1
b2
A= a1 a2 ··· as and B =
..
.
.
bs
Then
AB = a1 b1 + a2 b2 + · · · + as bs .
Partitioned matrices P. 29 / 44
Example
6 Assume that A, B, X, and Y are square
matrices. If the matrix
A C X Z
is invertible and its inverse is . Find X, Y , Z in terms of A,
0 B 0 Y
B, and C.
X Z A C
Solution. Since is the inverse of ,
0 Y 0 B
I 0 A C X Z AX AZ + CY
=I= = .
0 I 0 B 0 Y 0 BY
[3]: b = sp.Matrix([[0],[8],[-9]]); b
[3]:
0
8
−9
[3]: b = np.array([[0],[8],[-9]]); b
[4]: array([[29.],
[16.],
[ 3.]])
x2 − 4x3 = 8
Example 8 Solve the system 2x1 − 3x2 + 2x3 = 1
5x1 − 8x2 + 7x3 = 1
[2]:
0 1 −4
2 −3 2 [2]: array([[ 0, 1, -4],
5 −8 7 [ 2, -3, 2],
[ 5, -8, 7]])
[3]: b = sp.Matrix([[8],[1],[1]]); b
[3]: b = np.array([[8],[1],[1]]); b
[3]:
8
1
1 [3]: array([[8],
[1],
[4]: # Solve Ax = b @ SymPy [1]])
x1, x2, x3 = sp.symbols('x1 x2 x3')
sp.linsolve((A,b),x1,x2,x3)
[4]: # Solve Ax = b @ NumPy
np.linalg.solve(A,b)
[4]: ∅
[8]: b = np.array([[-5],[9],[15]]); b
[8]: array([[-5],
[ 9],
[15]])
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
/var/folders/wh/1v8jgf9d5gbfykybbcypgbt40000gn/T/ipykernel_26654/351369431.py in <module>
1 # np.linalg.solve cannot solve infinity many solution case
----> 2 np.linalg.solve(A,b)
/opt/anaconda3/lib/python3.9/site-packages/numpy/linalg/linalg.py in solve(a, b)
378 a, _ = _makearray(a)
379 _assert_stacked_2d(a)
--> 380 _assert_stacked_square(a)
381 b, wrap = _makearray(b)
382 t, result_t = _commonType(a, b)
x1 − x3
(
2x2 + = 0
2x2 − 8x3 = 8
−4x1 + 5x2 + 9x3 = −9
[1]:
Solution. Consider the augmented matrix 0 3 −6 6 4 −5
3 −7 8 −5 8 9
A b . 3 −9 12 −9 6 15
[2]: # RREF
0 3 −6 6 4 −5 1 0 −2 3 0 −24
A.rref()
3 −7 8 −5 8 9 ∼ 0 1 −2 2 0 −7 .
3 −9 12 −9 6 15 0 0 0 0 1 4
[2]: (Matrix([
The associated system now is [1, 0, -2, 3, 0, -24],
[0, 1, -2, 2, 0, -7],
[0, 0, 0, 0, 1, 4]]),
x1 − −24
(
2x3 + = 3x4 (0, 1, 4))
x2 − 2x3 + = 2x4 −7
x5 = 4
So the general solution of the system is
x1 −24 + 2x3 − 3x4
x1
= −24 + 2x3 − 3x4 x2 −7 + 2x3 − 2x4
x2 = −7 + 2x3 − 2x4 ⇐⇒ x = x3 = x3 .
x5 =
4 x4 x4
x3 , x4 are free. x5 4
[1]:
0 3 −6 6 4
3 −7 8 −5 8 [4]: (Matrix([
3 −9 12 −9 6 [1, 0, -2, 3, 0],
[0, 1, -2, 2, 0],
[0, 0, 0, 0, 1]]),
(0, 1, 4))
[2]: # define 3x1 zero vector
b = sp.zeros(3,1); b
Method 3
Homogeneous system P. 44 / 44
Homogeneous System
I A system of linear equation is said to be homogeneous if it can be
written in the form
Ax = 0,
where A is an m × n matrix and 0 is the zero vector in Rm .
I A homogeneous system Ax = 0 always has at least one solution,
namely, x = 0. This zero solution is usually called the trivial solution.
I A nonzero vector x that satisfies Ax = 0 is called a nontrivial solution.