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
Q

magnitude of a vector

A
  1. np.linalg.norm(v)
  2. np.sqrt(sum(np.multiply(v,v)))
  3. np.sqrt(np.dot(v,v))
26
Q

Angle between two vectors

A

theta = np.arccos((np.dot(a,b)) / (np.linalg.norm(a) * np.linalg.norm(b)))

27
Q

How to know if two vectors are orthogonal

A

If the dot product of the two vectors is 0

28
Q

How to create just positive random number

A

np.random.rand()

29
Q

create duplicate dependent vector from v1

A

np.random.rand() * v

30
Q

absolute value of a number

A

np.abs(-1)

31
Q

Perform hadamard multiplication

A
  1. np.multiply(v1, v2)
  2. v1 * v2
32
Q

Outer product

A

np.outer(v1, v2)

33
Q

cross product of vectors

A

np.cross(v1, v2)

34
Q

create complex number

A

np.complex(3,4) = 3 + 4j

35
Q

Create complex vector

A

np.array([3, 4i, np.complex(5,2), 2-5i])

36
Q

np.transpose(3 + 4i) =

A

3 + 4i

37
Q

Hermitian transpose of v = 3+4i - write syntax

A

np.transpose(v.conjugate()) * v

38
Q

Unit vectors - mu =

A

1/np.linalg.norm(v)

39
Q

Normalize a vector

A

mu * v= norm_vector
np.linalg.norm(norm_vector) = 1

40
Q

Scalar multiplication

A

2 * v

41
Q

Creating an identity matrix

A

np.eye(3), np.eye(3,5); But identity matrices are only square matrices in practice

42
Q

Creating zeros matrix

A

np.zeros((3,3)), np.zeros((3,5))

43
Q

creating diagonal matrix

A

np.diag([1,2,3,4,5]); Can create rectangular matrices as well

44
Q

Creating upper triangular matrix

A

s = np.random.randn(5,5)
ut = np.triu(s); Can create rectangular matrices as well

45
Q

Creating lower triangular matrix

A

s = np.random.randn(5,5)
tl = np.tril(s); Can create rectangular matrices as well

46
Q

Concatenate two matrices

A

np.concatenate((A,B), axis = 1)

47
Q

Addition of matrices

A

A + B; Both the matrices must be of same size

48
Q

Shifting a matrix

A

d = np.random.randn(3,3)
si = 0.3 * eye(3)
shiftedD = d + si

49
Q

Create 2x2 matrix

A

np.array([[2,3], [3,4]])

50
Q

Convert np array to matrix

A

c = np.array([1, 2+9j, 3, 4])
m = np.matrix(c)

51
Q

Transposes on matrix

A
  1. np.matrix(c).H
  2. np.matrix(c).T
52
Q

Two applications of diag keyword

A
  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
Q

Calculate trace

A
  1. np.trace(matrix)
  2. np.sum(np.diag(matrix)))
  3. sum(np.diag(matrix)))
54
Q

Properties that prove trace is a linear operator

A
  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
Q

Create a matrix with values from 1 to 12 and shape 3X4

A

a = np.reshape(np.arange(1,13), (3,4), ‘F’)

56
Q

Third parameter in np.reshape

A

F - column
C - row - default

57
Q

Broadcasting row vectors

A

m = np.reshape(np.nrange(1,13), (3,4), ‘F’)
a = np.array([10, 20, 30, 40])
m+a

58
Q

Broadcasting column vectors

A

a + r
a+ c

59
Q

Creating a symmetric matrix from a non-symmetric matrix a

A

a * a.T/m ** 2

60
Q

Create a random integer matrix

A

np.random.randint(1,12, (m,m))

61
Q

Hadamard matrix multiplication

A
  1. np.multiply( a, b)
  2. a*b
    a@b - regular matrix multiplication
62
Q

Hadamard multiplication rule

A

Both the matrices must be of same size