0% found this document useful (0 votes)
41 views46 pages

Lec3 Structural Data Types in Python

The document provides an overview of key concepts in matrix notation, including scalars, vectors, matrices, matrix operations like addition/subtraction, multiplication with scalars, and matrix multiplication. It also covers transpose, determinant, identity matrix, and inverse matrix. Formulas are given for computing determinants of small matrices and inverses of 1x1 and 2x2 matrices.

Uploaded by

Shanti Grover
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
41 views46 pages

Lec3 Structural Data Types in Python

The document provides an overview of key concepts in matrix notation, including scalars, vectors, matrices, matrix operations like addition/subtraction, multiplication with scalars, and matrix multiplication. It also covers transpose, determinant, identity matrix, and inverse matrix. Formulas are given for computing determinants of small matrices and inverses of 1x1 and 2x2 matrices.

Uploaded by

Shanti Grover
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 46

Matrix Notations

 Concepts
 A scalar is a numerical value
 Example: 4, 100, 4.5, 5, 2/3, …
 Variables for scalars use symbols x, y, z, …
 A vector is a sequence of values
 Row vector: (2, 6, 3), (50, 4.5, ½, 54), …

6
3
0
 Column vector: 4.2 , −5 , …
6 1/2
 Variables for vectors use symbols x, y, z, …
 Without mentioned otherwise, vector variables always denote
column vectors
 In text composition, we write “…x = (2,1,3)T…”
2
Matrix Notations
 Concepts
 A matrix is block of values with rows and columns
 Examples:
2 0.4
 A 3x2 matrix: 8 3
−3 0

3 7 0 1 3
2 1 4 5 9
 A 4x5 matrix:
5 −2 7 7 2
1 0 2 3 1
 Variables for matrixes use symbols X, Y, Z, …
 For any matrix A, the entry at the ith row and jth column is
denoted as Aij (or A(i, j)): A24 = 5, A32 = -2
3
Matrix Notations
 Concepts
 A row vector with n values can be viewed as a 1xn matrix
 examples
 Row vector (2, 6, 3) viewed as 1x3 matrix

 Row vector (50, 4.5, ½, 54) viewed as 1x4 matrix

 A Column vector with n values can be viewed as nx1 matrix


 Examples
3
 Column vector 4.2 viewed as 3x1 matrix
6
6
0
 Column vector −5 viewed as 4x1 matrix
1/2
 A 1 x 1 matrix can be written simply as a scale: (a) = a 4
Matrix Notations
 Matrixes can be represented in different ways
 In the most general case, an m x n matrix is represented as
𝑎11 ⋯ 𝑎1𝑛
A= ⋯ ⋯ ⋯
𝑎𝑚1 ⋯ 𝑎𝑚𝑛

5
Matrix Addition/Subtraction
 Addition and subtraction between two matrices
 Operands must have same shape
 Operation performed on each matching element pair
 Example:
2 5 0 10 2 15
 8 3 + 1 5 = 9 8
1 0 2 0 3 0

4 1 2 1 5 2 3 −4 0
 - =
3 6 3 2 6 7 1 0 −4

6
Multiplication with Scaler
 Multiplication between matrix and scalar
 Multiply the scalar to all the elements in the matrix
 Example
2 5 4 10
 2 8 3 = 16 6
1 0 2 0

1 5 2 5 25 10
 5=
2 6 7 10 30 35

7
Matrix Multiplication
 Matrix multiplication AB
 Column count of A must match the row count of B
 Let A be a pxq matrix, and B be a qxr matrix, then AB = C
where
 C is a pxr matrix
 Cij = sum of the products of the matching elements in the ith row
of A and jth column of B
𝑎11 ⋯ 𝑎1𝑞 𝑏11 ⋯ 𝑎1𝑟
 Let 𝐀 = ⋯ ⋯ ⋯ , 𝐁= ⋯ ⋯ ⋯
𝑎𝑝1 ⋯ 𝑎𝑝𝑞 𝑎𝑞1 ⋯ 𝑎𝑞𝑟

σ𝑞𝑖=1 𝑎1𝑖 𝑏𝑖1 ⋯ σ𝑞𝑖=1 𝑎1𝑖 𝑏𝑖𝑟


 Then 𝐀𝐁 = ⋯ ⋯ ⋯
𝑞 𝑞
σ𝑖=1 𝑎𝑝𝑖 𝑏𝑖1 ⋯ σ𝑖=1 𝑎𝑝𝑖 𝑏𝑖𝑟
8
Matrix Multiplication
 Example:

1 10
1 3 2 8 25
 1 5 =
0 2 1 2 0 4 10

2x3 3x2 2x2

1 10
 0, 2, 5 1 5 = (12, 10)
2 0
1x3 1x2
3x2

9
Matrix Multiplication
2
 1 3 2 0 =8 (called inner product)
3
2 2 6 4
 0 1 3 2 = 0 0 0 (called outer product)
3 3 9 6

1 3 2 1 2

0 2 1 4 5
2x3 2x2

Not the same, operation undefined!

10
Transpose
 Transpose
 The transpose of pxq matrix A is AT = B where
 B is a qxp matrix
 The ith row of A is identical to the ith column of B (or equivalently, the ith
column of A is identical to the ith row of B)
 A more concise definition would be:
 AT = B if Aij = Bji

𝑎11 ⋯ 𝑎1𝑞 𝑎11 ⋯ 𝑎𝑝1


 Let 𝐀 = ⋯ ⋯ ⋯ , then 𝐀T = ⋯ ⋯ ⋯
𝑎𝑝1 ⋯ 𝑎𝑝𝑞 𝑎1𝑞 ⋯ 𝑎𝑝𝑞
 Or let 𝐀 = 𝐀 𝟏 𝐀 𝟐 ⋯ 𝐀 𝐪 where Ai is px1 column vector

𝐀𝐓𝟏
 Then 𝐀𝐓 =
𝐀𝐓𝟐 where 𝐀𝐓𝐢 is 1xp row vector

𝐀𝐓𝐪
11
Transpose
 Examples
1
𝑇
 1 2 3 = 2
1x3
3
3x1
𝑇
2
 0 = 2 0 3
3
1x3
3x1

𝑇 1 0
1 3 2
 = 3 2
0 2 1
2 1
2x3 3x2

12
Transpose
𝑇
1 2 3 1 4 7 0
4 5 6 = 2 5

7 8 9 8 0
3 6 9 0
0 0 0

4x3 3x4

 Note: for any 1 x 1 matrix (a), we have


 𝑎 𝑇= 𝑎
 This is a special case of symmetric matrix (detail omitted)

13
Determinant of Square Matrix
 For any square matrix A, its determinant is a scalar,
denoted as |A|
 The determinants of the matrices with the first few lowest
shapes are calculated as follows:
 |(a)| = a
𝑎 𝑏
 = 𝑎𝑑 − 𝑏𝑐
𝑐 𝑑
𝑎11 𝑎12 𝑎13
 𝑎21 𝑎22 𝑎23 = 𝑎11 𝑎22 𝑎33 + 𝑎21 𝑎32 𝑎13 + 𝑎31 𝑎23 𝑎12
𝑎31 𝑎32 𝑎33
− 𝑎13 𝑎22 𝑎31 − 𝑎23 𝑎32 𝑎11 − 𝑎33 𝑎21 𝑎12

 Note: matrices not in square shapes have no determinants


14
Determinant of Square Matrix
 Examples
 |3| = 3
3 2
 = 34 − 21 = 10
1 4
1 2 3
 2 1 0
2 4 6

= 116 +243 + 202 − 312 − 041 − 622

= 30 – 30 = 0

15
Determinant of Square Matrix
 A matrix with non-zero determinant is said to be in full rank,
otherwise not
3 2
 Matrices (2) and are in full rank
1 4

1 2 3
 2 1 0 is not in full rank
2 4 6
1 2 3
Proof: 2 1 0 = 1*1*6 + 2*4*3 + 2*0*2 – 3*1*2 – 0*4*1 – 6*2*2
2 4 6
= 6 + 24 + 0 – 6 – 0 -24 = 0

16
Identity Matrix
 An identify matrix is a square matrix where the elements in its
main diagonal are all 1s, and the elements not in its main
diagonal are all 0s
 Examples of identity matrices

1 0 0
1 0
 1 , , 0 1 0 ,…
0 1
0 0 1

 Note: the determinant of any identity matrix is 1


 An nxn identity matrix is denoted as Inxn

17
Identity Matrix
 For any nxn matrix A, AInxn = InxnA = A
 This can be easily verified
𝑎 𝑏 1 0 𝑎 𝑏
 =
𝑐 𝑑 0 1 𝑐 𝑑
1 0 𝑎 𝑏 𝑎 𝑏
 =
0 1 𝑐 𝑑 𝑐 𝑑
𝑎11 𝑎12 𝑎13 1 0 0 𝑎11 𝑎12 𝑎13
 𝑎21 𝑎22 𝑎23 0 1 0 = 𝑎21 𝑎22 𝑎23
𝑎31 𝑎32 𝑎33 0 0 1 𝑎31 𝑎32 𝑎33
1 0 0 𝑎11 𝑎12 𝑎13 𝑎11 𝑎12 𝑎13
 0 1 0 𝑎21 𝑎22 𝑎23 = 𝑎21 𝑎22 𝑎23
0 0 1 𝑎31 𝑎32 𝑎33 𝑎31 𝑎32 𝑎33
 .....
18
Inverse Matrix
 If a square matrix A is in full rank, then it has an inverse
matrix, denoted as A-1, defined as follows:
 AA-1 = A-1A = I
 An analogy of basic algebra: for any value a  0, aa-1 = a-1a = 1
 The inverse matrix for 1x1 and 2x2 matrices are computed as
follows:
 (a)-1 = (1/a)

−1 𝑑 −𝑏
𝑎 𝑏 −𝑐 𝑎 1 𝑑 −𝑏
 = 𝑎 𝑏 =
𝑐 𝑑 𝑎𝑑−𝑏𝑐 −𝑐 𝑎
𝑐 𝑑
 Verify the above matrices fit the definition for the inverse matrix
 Calculations for the inverse matrices of larger sizes are omitted

19
About Numpy Module for Python
 Numpy is a module that must be installed separately from
Python interpreter
 The installation procedure varies depending on the
platform (refer to the idle help manual for detail)
 NumPy’s main object is the homogeneous
multidimensional array
 It is a table of elements (usually numbers), all of the same
type, indexed by a tuple of positive integers
 Dimensions are called axes
 In this course, we never use more than two dimensions
 A two dimensional array is a matrix
20
About Numpy Module
 NumPy’s array class is called ndarray
 Note this is different from the standard Python array class
 It provides more powerful functionalities

21
Attributes of ndarray class
• ndarray.ndim
• the number of axes (dimensions) of the array.
• ndarray.shape
• the dimensions of the array, which is a tuple of integers indicating
the size of the array in each dimension.
• For a matrix with n rows and m columns, shape is (n,m).
• ndarray.size
• the total number of elements of the array.
• This is equal to the product of the elements of shape.
• ndarray.dtype
• an object describing the type of the elements in the array

22
How to create an ndarray
 Use array() function: supply a >>> a.ndim
sequence as argument 1
>>> a.shape
>>> import numpy as np
(3,)
>>> a = np.array([2,3,4])
>>> a
Note: you must supply a sequence:
array([2, 3, 4])
>>> a.dtype
dtype('int64’) >>> a = np.array(1,2,3,4) # WRONG
>>> b = np.array([1.2, 3.5, 5.1]) >>> a = np.array([1,2,3,4]) # RIGHT
>>> b.dtype
dtype('float64')
23
How to create an ndarray (cont.)
>>> a = np.array([[1.5,2,3], [4,5,6]]) >>> b = np.array([[2,3,4]])
>>> a >>> b
array([[ 1.5, 2. , 3. ], array([[2, 3, 4 ]])
[ 4. , 5. , 6. ]]) >>> b.ndim
Note. This is the matrix: 2
1.5 2 3 >>> b.shape
4 5 6
(1,3)
>>> a.ndim
2
>>> a.shape
(2,3)
24
How to create an ndarray (cont.)
>>> a=np.array([2,3,4]) >>> b=np.array([[2,3,4]]) >>> c=np.array([[2],[3],[4]])
>>> c
>>> a >>> b
array([[2],
array([2, 3, 4]) array([[2, 3, 4]]) [3],
>>> a.ndim >>> b.ndim [4]])
1 2 >>> c.ndim
2
>>> a.shape >>> b.shape
>>> c.shape
(3,) (1,3) (3,1)

a 3x1 matrix:
a three dimensional row a 1x3 matrix:
vector:
2
[2, 3, 4]
[2, 3, 4] 3
4
25
Basic Operations
 Arithmetic operators on arrays apply elementwise

>>> a = np.array( [20,30,40,50] )


>>> a
array([20,30,40,50])
>>> b = np.array([0,1,2,3])
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
26
Basic Operations (cont.)
>>> a = np.array([[0, 1, 2], [3, 4, 5]]) >>> b = np.array( [[20,30],[40,50]] )
>>> a >>>b
array([[0, 1, 2], array([[20,30],
[3, 4, 5]]) [40,50]])
>>> a**2 >>> 10*np.sin(b)
array([[0, 1, 4], array([[9.12945251, -9.88031624],
[9, 16, 25]]) [7.4511316 , -2.62374854]])
>>> b<35
array([ [True, True],
[False, False] ])
27
Aggregation Operations
 Defined as methods for ndarray class
>>> a = np.array([[1,2,3],[4,5,6]]) >>> a.sum(axis=0)
>>> a array([5,7,9])
array([[1,2,3], >>> a.sum(axis=1)
[4,5,6]]) array([6,15])
>>>a.sum() >>>a.max(axis=1))
21 array([3,6])
>>>a.max() >>>a.min(axis=0)
6 array([1,2,3])
>>>a.min()
1 The results are all 1D array

28
Math Functions
 All applied on individual entries of the matrix
>>> B = np.array([0,1,2])
>>> B
array([0, 1, 2])
>>> np.exp(B) #exponent for base e = 2.7182818284….
array([ 1. , 2.71828183, 7.3890561 ])
>>> np.sqrt(B)
array([ 0. , 1. , 1.41421356])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([ 2., 0., 6.])

29
Indexing and Slicing
 One-dimensional arrays can be indexed, sliced and
iterated over, much like lists and other Python sequences.
>>> a = np.arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
start stop (exclusive)
8
>>> a[2:8:2] #interpretation: a[2:8:2]
array([ 8, 64, 216]) step
>>> a[:6:2] = -1000 #same as a[0:6:2]
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])

30
For multi-dimensional arrays
 Have one index per axis, given in a tuple separated by commas
>>> a = np.array([[0,1,2,3], >>> a[2,3]
[10,11,12,13], 23
[20,21,22,23], >>> a[0:4, 1]
[30,31,32,33], array([1, 11, 21, 31])
[40,41,42,43]]) >>> a[:, 1]
>>> a array([1, 11, 21, 31, 41])
array([[ 0, 1, 2, 3], >>> a[1:4, 0:3]
[10, 11, 12, 13], array([[10, 11, 12],
[20, 21, 22, 23], [20, 21, 22],
[30, 31, 32, 33], [30, 31, 32]])
[40, 41, 42, 43]])

31
Iterating
 Over all the designated values
>>> a = np.array([1,8,27,64,125,216]) >>> for i in b:
>>> for i in a: print(i)
print(i**(1/3)) [1,2,3]
1 [4,5,6]
2 >>> for i in b:
3 for j in i[0:2]:
4 print(j**2)
5 1
6 4
>>> b = np.array([[1,2,3], 16
[4,5,6]]) 25
32
Stacking together different arrays
>>> a = np.array([[1,2,3], [4,5,6]]) >>> c = np.vstack((a, b))
>>> a >>> c
array([[1,2,3], array([[1,2,3],
[4,5,6]]) [4,5,6],
>>> b = np.array([[6,5,4],[3,2,1]]) [6,5,4],
>>> b [3,2,1]])
array([[6,5,4], >>> d = np.hstack((a,b))
[3,2,1]]) array([[1,2,3, 6,5,4],
[4,5,6, 3,2,1]])

33
Repeat
>>> np.repeat(3, 4) >>> np.repeat(a, 2, axis = 1)
array([3,3,3,3]) array([[1,1,2,2,3,3],
>>> np.repeat([1,2], 4) [4,4,5,5,6,6]])
array([1,1,1,1,2,2,2,2]) >>> np.repeat(a, [1,2], axis = 0)
>>> a = np.array([[1,2,3],[4,5,6]]) array([[1,2,3],
>>> a [4,5,6],
array([[1,2,3], [4,5,6]])
[4,5,6]]) >>> np.repeat(a, [1,2,2], axis=1)
>>> np.repeat(a, 2, axis = 0) array([[1,2,2,3,3],
array([[1,2,3], [4,5,5,6,6]])
[1,2,3], >>>np.repeat(a, [1,2,1],axis=0)
[4,5,6], Error…
[4,5,6]])

34
Compare matrices with same shape
>>>a = np.array([[2,4],[1,2]] >>>d = a > b
>>>b = np.array([[0,4],[1,3]]) >>>d
>>>a array([[True,False],
array([[2,4], [False,False]])
[1,2]])
>>>b
array([[0,4],
[1,3]])
>>>c = a == b
>>>c
array([[False,True],
[True,False]])

35
Compare matrices with difference shapes
>>>a = np.array([[2,4,2],[1,2,3]] >>>d = np.array([[4],[1]])
>>>a >>>d
array([[2,4,2], array([[4],
[1,2,3]]) [1]])
>>>b = np.array([2,2,3]) >>>a == d
>>>b array([[False,True,False],
array([2,2,3]) [True,False,False]])
>>>c = a == b >>>f = np.array([4,1])
>>>c >>>a == f
array([[True,False,False], False
[False,True,True]]) >>>a > f
error…

36
Compare matrices with difference shapes
>>>a = np.array([[2,4,2],[1,2,3]] >>>a + 1 < 4
>>>a array([[True,False,True],
array([[2,4,2], [True,True,False]])
[1,2,3]])
>>>a == 2
array([[True,False,True],
[False,True,False]])
>>>a > 2
array([[False,True,False],
[False,False,True]])

37
Count non-zeros in a matrix
>>>a = np. array([[1,0,2], >>>np.count_nonzero(a, axis=0)
[0,1,0]]) array([1,1,1])
>>>a >>>np.count_nonzero(a,axis=1)
array([[1,0,2], array([2,1])
[0,1,0]])
>>>np.count_nonzero(a)
3

38
Retrieve locations for non-zeros in a matrix
>>>a = np. array([[1,0,2], >>>b = np.array([1,0,0,2,3])
[0,1,0]]) >>>b
>>>a array([1,0,0,2,3])
array([[1,0,2], >>>np.nonzero(b)
[0,1,0]]) (array([0,3,4]))
>>>np.nonzero(a)
(array([0,0,1]), array([0,2,1]))

39
More for operations on non-zeros
>>>a = np. array([[True,False,True], >>>np.nonzero(a)
[False,True,False]]) (array([0,0,1]), array([0,2,1]))
>>>a >>>b = np.array([True,False,True])
array([[True,False,True], >>>b
array([True,False,True])
[False,True,False]])
>>>np.nonzero(b)
>>>np.count_nonzero(a)
(array([0,2]))
3
>>>np.count_nonzero(a, axis=0)  Combining operations on non-
array([1,1,1]) zeros and matrix comparisons,
>>>np.count_nonzero(a,axis=1) you can retrieve counts and
locations for any values and
array([2,1]) their duplicates in a matrix

40
More for Operations on Non-Zeros
>>>a = np.array([1,2,5,2,2,5,0])
 How to find the locations of all the occurrences of 2?
>>>b = a == 2
>>>b
array([False,True,False,True,True,False,False])
>>>c = np.nonzero(b)
>>>c
array([1,3,4])
 [1,3,4] gives the locations of occurrences of 2 in a
 For the general matrix, do it yourselves

41
Input/output
 Input 123
456
>>> a = np.loadtxt(‘C:\\xyz\\infile.txt’)
>>> a
array([[1., 2., 3. ], If you don’t specify
[4. ,5. ,6. ]]) absolute paths, the
input/output files must
be in the current
 output working directory
>>> b = np.array([[3,2,1], [6,5,4]])
>>>savetxt(‘C:\\xyz\\outfile.txt’, b, ‘%d’) 321
654

%d: integer
%p.qf: float, with q digits after decimal
point, and keep at least p places

42
Plotting
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.2)


y = np.sin(x)
plt.plot(x, y)
plt.show()

1D array of values for variable

plt.plot(x, y)

1D array of values for function


43
Plotting
 Make your figure more readable
import matplotlib.pyplot as plt
import numpy as np

def plotme():

x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')

plt.xlabel('x label')
plt.ylabel('y label')

plt.title("Simple Plot")

plt.legend()

plt.show()
44
Programming Setting
 When you place Python program in a file, the best way to
operate on it is to use Python IDLE editor
 You can run the program directly from IDLE shell:
 Add a line to call a function
 Click run →run module
 You can also run any function you create in IDLE shell
 Suppose function abc(*) has been defined in file myfile.py
 Remove any call to abc(*) from myfile.py
 Insert the path ‘C://…/myfile.py’ in sys.path using
sys.path.insert(i, pth), which inserts path pth before index i
 Type import myfile on IDLE shell
 Type myfile.abc(*) to run function abc(*)
 If you want to use debugging tools to debug your
program, you must use the second method
45
Programming Setting
 Always put
import numpy as np
before the main code of your .py file
 If you need to do plotting, place the following line
import matplotlib.pyplot as plt
 If you want to use functions defined in another file,
import it also

46

You might also like