Linear Algebra Python Flashcards

1
Q

Initialize a vector

A
  1. v = [3,2]
  2. v1 = np.array([3,2])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Addition of two vectors

A

v1 + v2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Scalar multiplication

A

2 * v

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Transpose of a vector

A
  1. np.transpose(v)
  2. v.T
  3. v.T.T
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Dot product approaches

A
  1. np.dot(v1, v2)
  2. np.matmul(v1, v2)
  3. sum(np.multiply(v1, v2))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create one random number

A

np.random.rand/ np.random.randn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create random vector of size 10

A

np.random.randn(10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

create a 3 * 1 matrix of random numbers

A

np.random.randn(3,1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Get the dimensions of a matrix

A

mat.ndim

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Get the number of elements in a matrix

A

mat.size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Get the size of the matrix - 4X6

A

mat.shape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Just get the number of rows of a matrix

A

mat.shape[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Just get the number of columns of a matrix

A

mat.shape[1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Get the first column of a matrix

A

mat[: , 0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Get the first and third columns of a matrix

A

mat[: , [0,2]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Get the one to third columns of a matrix

A

mat[: , 0:3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Repeat 1st column twice in a matrix

A

mat[:, [0,0]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Get the first row of a matrix

A

mat[0, :]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Get the first and third rows of a matrix

A

mat[[0,2], :]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Get 1 to third rows in a matrix

A

mat[0:3 , :]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Repeat 1st row twice in a matrix

A

mat[[0,0], :]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Repeat 1st row n times in a matrix

A

mat[[0] * n , :]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Create a zero vector of size 4

A

np.zeros(4) # 4 row vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

convert 3X1 matrix to 3x1 vector

A
  1. mat = np.random.randn(3,1)
  2. arr = np.asarray(mat) # 2D array
  3. vector = np.squeeze(arr) # 1d array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
magnitude of a vector
1. np.linalg.norm(v) 2. np.sqrt(sum(np.multiply(v,v))) 3. np.sqrt(np.dot(v,v))
26
Angle between two vectors
theta = np.arccos((np.dot(a,b)) / (np.linalg.norm(a) * np.linalg.norm(b)))
27
How to know if two vectors are orthogonal
If the dot product of the two vectors is 0
28
How to create just positive random number
np.random.rand()
29
create duplicate dependent vector from v1
np.random.rand() * v
30
absolute value of a number
np.abs(-1)
31
Perform hadamard multiplication
1. np.multiply(v1, v2) 2. v1 * v2
32
Outer product
np.outer(v1, v2)
33
cross product of vectors
np.cross(v1, v2)
34
create complex number
np.complex(3,4) = 3 + 4j
35
Create complex vector
np.array([3, 4i, np.complex(5,2), 2-5i])
36
np.transpose(3 + 4i) =
3 + 4i
37
Hermitian transpose of v = 3+4i - write syntax
np.transpose(v.conjugate()) * v
38
Unit vectors - mu =
1/np.linalg.norm(v)
39
Normalize a vector
mu * v= norm_vector np.linalg.norm(norm_vector) = 1
40
Scalar multiplication
2 * v
41
Creating an identity matrix
np.eye(3), np.eye(3,5); But identity matrices are only square matrices in practice
42
Creating zeros matrix
np.zeros((3,3)), np.zeros((3,5))
43
creating diagonal matrix
np.diag([1,2,3,4,5]); Can create rectangular matrices as well
44
Creating upper triangular matrix
s = np.random.randn(5,5) ut = np.triu(s); Can create rectangular matrices as well
45
Creating lower triangular matrix
s = np.random.randn(5,5) tl = np.tril(s); Can create rectangular matrices as well
46
Concatenate two matrices
np.concatenate((A,B), axis = 1)
47
Addition of matrices
A + B; Both the matrices must be of same size
48
Shifting a matrix
d = np.random.randn(3,3) si = 0.3 * eye(3) shiftedD = d + si
49
Create 2x2 matrix
np.array([[2,3], [3,4]])
50
Convert np array to matrix
c = np.array([1, 2+9j, 3, 4]) m = np.matrix(c)
51
Transposes on matrix
1. np.matrix(c).H 2. np.matrix(c).T
52
Two applications of diag keyword
1. d = np.diag(matrix) - creates a row vector with the diagonal elements as row vector 2. x = np.diag(d) - creates a diagonal matrix with the elements of the row vector as diagonal elements.
53
Calculate trace
1. np.trace(matrix) 2. np.sum(np.diag(matrix))) 3. sum(np.diag(matrix)))
54
Properties that prove trace is a linear operator
1. np.trace(a+b) = np.trace(a) + np.trace(b) 2. l * np.trace(a) = np.trace(l*a) closed under both addition and subtraction
55
Create a matrix with values from 1 to 12 and shape 3X4
a = np.reshape(np.arange(1,13), (3,4), 'F')
56
Third parameter in np.reshape
F - column C - row - default
57
Broadcasting row vectors
m = np.reshape(np.nrange(1,13), (3,4), 'F') a = np.array([10, 20, 30, 40]) m+a
58
Broadcasting column vectors
a + r a+ c
59
Creating a symmetric matrix from a non-symmetric matrix a
a * a.T/m ** 2
60
Create a random integer matrix
np.random.randint(1,12, (m,m))
61
Hadamard matrix multiplication
1. np.multiply( a, b) 2. a*b a@b - regular matrix multiplication
62
Hadamard multiplication rule
Both the matrices must be of same size