<jupyter_start><jupyter_text>--- VIDEO: A zoo of matrices---<jupyter_code># square vs. rectangular
S = np.random.randn(5,5)
R = np.random.randn(5,2) # 5 rows, 2 columns
print(S), print(' ')
print(R)

# identity
I = np.eye(3)
print(I), print(' ')

# zeros
Z = np.zeros((4,4))
print(Z), print(' ')

# diagonal
D = np.diag([ 1, 2, 3, 5, 2 ])
print(D), print(' ')

# create triangular matrix from full matrices
S = np.random.randn(5,5)
U = np.triu(S)
L = np.tril(S)
print(L), print(' ')

# concatenate matrices (sizes must match!)
A = np.random.randn(3,2)
B = np.random.randn(4,4)
C = np.concatenate((A,B),axis=1)
print(C)<jupyter_output><empty_output><jupyter_text>--- VIDEO: Matrix addition and subtraction---<jupyter_code># create random matrices
A = np.random.randn(5,4)
B = np.random.randn(5,3)
C = np.random.randn(5,4)

# try to add them
A+B
A+C



# "shifting" a matrix
l = .03 # lambda
N = 5  # size of square matrix
D = np.random.randn(N,N) # can only shift a square matrix

Ds = D + l*np.eye(N)
print(D), print(' '), print(Ds)<jupyter_output><empty_output><jupyter_text>--- VIDEO: Matrix-scalar multiplication---<jupyter_code># define matrix and scalar
M = np.array([ [1, 2], [2, 5] ])
s = 2

# pre- and post-multiplication is the same:
print( M*s )
print( s*M )<jupyter_output><empty_output><jupyter_text>VIDEO: Transpose<jupyter_code>M = np.array([ [1,2,3],
               [2,3,4] ])

print(M), print('')
print(M.T), print('') # one transpose
print(M.T.T), print('') # double-transpose returns the original matrix

# can also use the function transpose
print(np.transpose(M))
# warning! be careful when using complex matrices
C = np.array([ [4+1j , 3 , 2-4j] ])

print(C), print('')
print(C.T), print('')
print(np.transpose(C)), print('')

# Note: In MATLAB, the transpose is the Hermitian transpose; 
#       in Python, you need to call the Hermitian explicitly by first converting from an array into a matrix
print(np.matrix(C).H) # note the sign flips!<jupyter_output><empty_output><jupyter_text>--- VIDEO: Diagonal and trace---<jupyter_code>M = np.round( 6*np.random.randn(4,4) )
print(M), print(' ')
# extract the diagonals
d = np.diag(M)

# notice the two ways of using the diag function
d = np.diag(M) # input is matrix, output is vector
D = np.diag(d) # input is vector, output is matrix
print(d)
print(D)

# trace as sum of diagonal elements
tr = np.trace(M)
tr2 = sum( np.diag(M) )
print(tr,tr2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1731477691363078, 'fraction_numerical': 0.020875972165370446, 'mean_word_length': 4.455357142857143, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '581538', 'license': 'MIT', 'n_tokens_mistral': 1005, 'n_tokens_neox': 964, 'n_words': 264, 'path': 'matlab/udemy/Matrices/linalg_matrices.ipynb', 'repo_name': 'hybridmachine/learning', 'size': '2443'}