Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import numpy as np

#vectors
a = np.array([1, 4, 5])
b = np.array([-1, 4, 53])
c = a + b # c is vector (0, 8, 58 )
d = a * b # d is vector (−1, 16, 265)

#arrays
m1 = np.array([[1, 4], [−5, 8]])
m2 = np.array([[7, −6], [−11, 2]])
# msum is array ([[8, −2], [−16, 10]])
msum = m1+m2
# product is array ([[−37, 2], [−123, 46]])
product = np.dot(m1, m2)

#zeros is a null matrix of shape 2x3


zeros = np.zeros((2,3))
#i is an identity matrix of shape 3x3
i = np.identity(3)

#matrix inverse and multiplication


a = np.array([[−1, 3], [3, 5]])
b = np.array([[3], [7]])
inv = np.linalg.inv(a)
x = np.dot(inv,b)

from scipy.linalg import lu


B = np.array([[ 0, 0, 1], [0, 2, 2], [3, 0, 4]])
# PLU
P, L, U = lu(B)
# outputs true
np.allclose(np.dot(np.dot(P,L), U), B)

a = np.array([[1, 0], [0, 1]])


b = np.array([[4, 1], [2, 2]])
np.matmul(a, b) # also can be written as a @ b

a = np.array([[1,2],[3,4]])
# sum_of_elements is 10
sum_of_elements = np.sum(a)

k = -9
abs_k = np.abs(k) #abs_k is 9

# random matrix of size 4x4 with values from -15 to 15


m1 = np.random.randint(low=-15,high=15, size=(4, 4))
#replace all values that are greater than 0 with 1
filted_m1 = np.where(m1 < 0, m1, 1)

You might also like