# 3. Numpy¶

NumPy jest podstawowym pakiet (dodatkowym) w Pythonie do obliczeń naukowych. Integruje on niskopoziomowe biblioteki takie jak BLAS i LAPACK lub ATLAS. Podstawowe właściwości NumPy to :

• potężny N-wymiarowy obiekt tablicy danych
• rozbudowane funkcje
• narzędzia do integracji z kodem napisanym w C/C++ i Fortranie
• narzędzia do algebry liniowej, transformaty Fouriera czy generator liczb losowych

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

• a powerful N-dimensional array object
• tools for integrating C/C++ and Fortran code
• useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

## 3.1. Import¶

[2]:

import numpy as np


## 3.2. Functions¶

• np.abs()
• np.sign()
• np.sqrt()

### 3.2.1. Exponents and logarithms¶

• np.log()
• np.log10()
• np.exp()

### 3.2.2. Trigonometric functions¶

• np.sin()
• np.cos()
• np.tan()
• np.arcsin()
• np.arccos()
• np.arctan()

### 3.2.3. Hiperbolic functions¶

• np.sinh()
• np.cosh()
• np.tanh()
• np.arcsinh()
• np.arccosh()
• np.arctanh()
[ ]:




## 3.3. Random numbers¶

Mersenne Twister algorithm for pseudorandom number generation

### 3.3.1. Seed the generator¶

[3]:

np.random.seed(293423)
np.random.rand(5)

[3]:

array([0.33677247, 0.52693437, 0.79529578, 0.78867702, 0.02147624])

[3]:

np.random.rand(5)

[3]:

array([0.84612516, 0.0704939 , 0.1526965 , 0.77831701, 0.80821151])


### 3.3.2. Random values in a given shape¶

Random samples from a uniform distribution over [0, 1)

[4]:

np.random.rand(2,3)

[4]:

array([[0.84612516, 0.0704939 , 0.1526965 ],
[0.77831701, 0.80821151, 0.82198398]])

[9]:

np.random.rand(8).reshape((4,2))

[9]:

array([[0.5219473 , 0.11240232],
[0.65049125, 0.67164447],
[0.3756778 , 0.09623913],
[0.88756776, 0.33254026]])


### 3.3.3. Random integers from low (inclusive) to high (exclusive)¶

[6]:

np.random.randint(5, 10)

[6]:

6


### 3.3.4. Random numbers from distributions¶

#### 3.3.4.1. Draw samples from a Poisson distribution¶

Poisson distribution with lambda = 6.0

[176]:

np.random.seed(100)
np.random.poisson(6.0)

[176]:

4

[8]:

np.random.seed(100)
np.random.poisson([6.0, 1.0])

[8]:

array([4, 0])


#### 3.3.4.2. Draw random samples from a normal (Gaussian) distribution¶

Continuous normal (Gaussian) distribution with mean micro=1.5 and standard deviation sigma=4.0:

[9]:

np.random.normal(1.5, 4.)

[9]:

5.425283147804926


Continuous normal (Gaussian) distribution with mean micro=0.0 and standard deviation sigma=1.0:

[10]:

np.random.normal()

[10]:

0.5142188413943821

[11]:

np.random.normal(size=5)

[11]:

array([ 0.22117967, -1.07004333, -0.18949583,  0.25500144, -0.45802699])


### 3.3.5. Modify a sequence in-place by shuffling its contents¶

[12]:

arr = np.arange(10)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

np.random.shuffle(arr)

[13]:

arr

[13]:

array([0, 2, 1, 6, 9, 8, 7, 4, 3, 5])


Multi-dimensional arrays are only shuffled along the first axis:

[10]:

arr = np.arange(9).reshape((3, 3))
arr

[10]:

array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

[11]:

np.random.shuffle(arr)
arr

[11]:

array([[6, 7, 8],
[3, 4, 5],
[0, 1, 2]])


## 3.4. Polynomials¶

### 3.4.1. Defining polynomial¶

[13]:

np.poly([-1, 1, 10])

[13]:

array([  1., -10.,  -1.,  10.])


### 3.4.2. Calculating roots of a polynomial¶

[15]:

np.roots([1., -10., -1., 10.])

[15]:

array([10., -1.,  1.])


### 3.4.3. Evaluate a polynomial at specific values¶

[18]:

np.polyval([1, -10, -1, 10], 10)

[18]:

0


### 3.4.4. Antiderivative (indefinite integral) of a polynomial¶

[19]:

np.polyder([1./4., 1./3., 1./2., 1., 0.])

[19]:

array([1., 1., 1., 1.])


### 3.4.5. Derivatives¶

[20]:

np.polyint([1, 1, 1, 1])


[20]:

array([0.25      , 0.33333333, 0.5       , 1.        , 0.        ])


### 3.4.6. Least squares polynomial fit¶

[21]:

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [0, 2, 1, 3, 7, 10, 11, 19]

np.polyfit(x, y, 2)

[21]:

array([ 0.375     , -0.88690476,  1.05357143])


### 3.4.7. Polynomial Arithmetic¶

• np.polyadd()
• np.polysub()
• np.polymul()
• np.polydiv()
[22]:

np.polyadd([1, 2], [9, 5, 4])

[22]:

array([9, 6, 6])


## 3.5. Arrays¶

### 3.5.1. Create array¶

From list:

[23]:

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

[23]:

array([1, 2, 3])

[24]:

np.array([1, 4, 5, 8], float)

[24]:

array([1., 4., 5., 8.])


From list of lists:

[25]:

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

[25]:

array([[1, 2],
[3, 4]])


### 3.5.2. Generating arrays¶

[26]:

np.arange(3)

[26]:

array([0, 1, 2])

[27]:

np.arange(3.)

[27]:

array([0., 1., 2.])

[28]:

np.arange(3, 7)

[28]:

array([3, 4, 5, 6])

[29]:

np.arange(3, 12, step=2)

[29]:

array([ 3,  5,  7,  9, 11])

[30]:

np.arange(start=3, stop=12, step=2, dtype=float)

[30]:

array([ 3.,  5.,  7.,  9., 11.])


### 3.5.3. Slicing arrays¶

[17]:

a = np.array([1, 4, 5, 8], float)
a

[17]:

array([1., 4., 5., 8.])

[32]:

a[:2]

[32]:

array([1., 4.])

[35]:

a = np.array([[1, 2, 3], [4, 5, 6]], float)
a

[35]:

array([[1., 2., 3.],
[4., 5., 6.]])

[33]:

a[3]

[33]:

8.0

[34]:

a[0] = 5
a

[34]:

array([5., 4., 5., 8.])

[36]:

a[0][0]
a[0,0]

[36]:

1.0

[37]:

a[0]

[37]:

array([1., 2., 3.])

[38]:

a[1,:]

[38]:

array([4., 5., 6.])

[39]:

a[-1,-2]

[39]:

5.0

[40]:

a[-1,-2:]

[40]:

array([5., 6.])

[41]:

a[-1:,-2:]

[41]:

array([[5., 6.]])


### 3.5.4. Array shape¶

[18]:

a = np.array([[1, 2, 3], [4, 5, 6]], float)
a

[18]:

array([[1., 2., 3.],
[4., 5., 6.]])

[43]:

len(a)

[43]:

2

[44]:

a.shape

[44]:

(2, 3)

[19]:

a.dtype

[19]:

dtype('float64')

[24]:

a.astype(int)
a.dtype

[24]:

dtype('int64')

[23]:

a = a.astype(int)
a.dtype

[23]:

dtype('int64')

[48]:

6 in a

[48]:

True

[49]:

0 in a

[49]:

False

[25]:

a = np.array(range(10), float)

[26]:

a = a.reshape((5, 2))
a

[26]:

array([[0., 1.],
[2., 3.],
[4., 5.],
[6., 7.],
[8., 9.]])

[27]:

a.reshape((2, 5))

[27]:

array([[0., 1., 2., 3., 4.],
[5., 6., 7., 8., 9.]])

[28]:

a

[28]:

array([[0., 1.],
[2., 3.],
[4., 5.],
[6., 7.],
[8., 9.]])

[54]:

a = np.array([1, 2, 3], float)

[55]:

b = a
c = a.copy()

[56]:

a[0] = 0

[57]:

b

[57]:

array([0., 2., 3.])

[58]:

c

[58]:

array([1., 2., 3.])

[59]:

a = np.array([[1, 2], [3, 4]], float)
a.tolist()

[59]:

[[1.0, 2.0], [3.0, 4.0]]

[60]:

list(a)

[60]:

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

[61]:

a = np.array([1, 2, 3], float)

[62]:

s = a.tostring()
s

[62]:

b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\x00\[email protected]'

[63]:

np.frombuffer(s)

[63]:

array([1., 2., 3.])


### 3.5.5. Array Data¶

[183]:

n1 = np.array([1,2,3])
n2 = np.array([[1,2],[3,4]])

print(f'Wymiar: n1: {n1.ndim}, n2: {n2.ndim}')

print(f'Kształt: n1: {n1.shape}, n2: {n2.shape}')

print(f'Rozmiar: n1: {n1.size}, n2: {n2.size}')

print(f'Typ: n1: {n1.dtype}, n2: {n2.dtype}')

print(f'Rozmiar elementu (w bajtach): n1: {n1.itemsize}, n2: {n2.itemsize}')

print(f'Wskaźnik do danych: n1: {n1.data}, n2: {n2.data}')

Wymiar: n1: 1, n2: 2
Kształt: n1: (3,), n2: (2, 2)
Rozmiar: n1: 3, n2: 4
Typ: n1: int64, n2: int64
Rozmiar elementu (w bajtach): n1: 8, n2: 8
Wskaźnik do danych: n1: <memory at 0x117c49ac8>, n2: <memory at 0x117de8048>


W przeciwieństwie do kolekcji, tablice mogą mieć tylko jeden typ elementu, choć może być złożony

https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

[184]:

for v in [1, 1., 1j]:
a = np.array([v])
print('Tablica: {}, typ: {}'.format(a, a.dtype))

Tablica: [1], typ: int64
Tablica: [1.], typ: float64
Tablica: [0.+1.j], typ: complex128


### 3.5.6. Array modification¶

[64]:

a = np.arange(3)
a

[64]:

array([0, 1, 2])

[65]:

a.fill(0)
a

[65]:

array([0, 0, 0])

[178]:

a = np.array(range(6), float).reshape((2, 3))
a

[178]:

array([[0., 1., 2.],
[3., 4., 5.]])

[179]:

a.transpose()

[179]:

array([[0., 3.],
[1., 4.],
[2., 5.]])

[180]:

a.T

[180]:

array([[0., 3.],
[1., 4.],
[2., 5.]])

[68]:

a = np.array([[1, 2, 3], [4, 5, 6]], float)
a

[68]:

array([[1., 2., 3.],
[4., 5., 6.]])

[69]:

a.flatten()

[69]:

array([1., 2., 3., 4., 5., 6.])

[70]:

a = np.array(range(6), float).reshape((2, 3, 1))
a

[70]:

array([[[0.],
[1.],
[2.]],

[[3.],
[4.],
[5.]]])

[71]:

a.squeeze()

[71]:

array([[0., 1., 2.],
[3., 4., 5.]])


### 3.5.7. Math operations¶

[72]:

a = np.array([1,2,3], float)
b = np.array([5,2,6], float)

[73]:

a + b

[73]:

array([6., 4., 9.])

[74]:

a - b

[74]:

array([-4.,  0., -3.])

[75]:

a * b

[75]:

array([ 5.,  4., 18.])

[76]:

b / a

[76]:

array([5., 1., 2.])

[77]:

a % b

[77]:

array([1., 0., 3.])

[78]:

b ** a

[78]:

array([  5.,   4., 216.])

[79]:

a = np.array([[1,2], [3,4]], float)
b = np.array([[2,0], [1,3]], float)

[80]:

a * b

[80]:

array([[ 2.,  0.],
[ 3., 12.]])


### 3.5.8. Array concatenation¶

[181]:

a = np.array([1,2], float)
b = np.array([3,4,5,6], float)
c = np.array([7,8,9], float)

np.concatenate((a, b, c))

[181]:

array([1., 2., 3., 4., 5., 6., 7., 8., 9.])


### 3.5.9. Array multiplication¶

[81]:

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

a @ b

[81]:

array([[4, 1],
[2, 2]])

[82]:

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

a * b

[82]:

array([[4, 0],
[0, 2]])


### 3.5.10. Shape operations¶

[31]:

a = np.array([[1, 2], [3, 4], [5, 6]], float)
a

[31]:

array([[1., 2.],
[3., 4.],
[5., 6.]])

[32]:

b = np.array([-1, 3], float)
b

[32]:

array([-1.,  3.])

[33]:

a + b

[33]:

array([[0., 5.],
[2., 7.],
[4., 9.]])

[34]:

a = np.zeros((2,2), float)
a

[34]:

array([[0., 0.],
[0., 0.]])

[35]:

b = np.array([-1., 3.], float)
b

[35]:

array([-1.,  3.])

[36]:

a + b

[36]:

array([[-1.,  3.],
[-1.,  3.]])

[38]:

a + b [np.newaxis, :]

[38]:

array([[-1.,  3.],
[-1.,  3.]])

[90]:

a + b [:, np.newaxis]

[90]:

array([[-1., -1.],
[ 3.,  3.]])


### 3.5.11. Other operations¶

[91]:

a = np.array([1.1, 1.5, 1.9], float)

[92]:

np.sqrt(a)

[92]:

array([1.04880885, 1.22474487, 1.37840488])

[93]:

np.floor(a)

[93]:

array([1., 1., 1.])

[94]:

np.ceil(a)

[94]:

array([2., 2., 2.])

[95]:

np.rint(a)

[95]:

array([1., 2., 2.])

[96]:

np.pi

[96]:

3.141592653589793

[97]:

np.e

[97]:

2.718281828459045

[98]:

np.nan

[98]:

nan

[99]:

np.inf

[99]:

inf


### 3.5.12. Array iteriation¶

[100]:

a = np.array([1, 4, 5], int)

for x in a:
print(x)

1
4
5

[41]:

a = np.array([[1, 2], [3, 4], [5, 6]], float)

for x in a:
print(x)

[1. 2.]
[3. 4.]
[5. 6.]


### 3.5.13. Array operations¶

[102]:

a = np.array([2, 4, 3], float)
a

[102]:

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

[103]:

a.sum()

[103]:

9.0

[104]:

a.prod()

[104]:

24.0

[105]:

a = np.array([2, 1, 9], float)
a

[105]:

array([2., 1., 9.])

[106]:

a.mean()

[106]:

4.0

[107]:

a.var()

[107]:

12.666666666666666

[108]:

a.std()

[108]:

3.559026084010437

[109]:

a.min()

[109]:

1.0

[110]:

a.max()

[110]:

9.0

[111]:

a.argmin()

[111]:

1

[112]:

a.argmax()

[112]:

2

[113]:

a = np.array([6, 2, 5, -1, 0], float)
sorted(a)

[113]:

[-1.0, 0.0, 2.0, 5.0, 6.0]

[114]:

a.sort()
a

[114]:

array([-1.,  0.,  2.,  5.,  6.])

[115]:

a = np.array([6, 2, 5, -1, 0], float)

a.clip(0, 5)

[115]:

array([5., 2., 5., 0., 0.])

[116]:

a = np.array([1, 1, 4, 5, 5, 5, 7], float)

np.unique(a)

[116]:

array([1., 4., 5., 7.])

[117]:

a = np.array([[1, 2], [3, 4]], float)

a.diagonal()

[117]:

array([1., 4.])


### 3.5.14. Comparison operators¶

[118]:

a = np.array([1, 3, 0], float)
b = np.array([0, 3, 2], float)

[119]:

a > b

[119]:

array([ True, False, False])

[120]:

a == b

[120]:

array([False,  True, False])

[121]:

a <= b

[121]:

array([False,  True,  True])

[122]:

c = a > b
c

[122]:

array([ True, False, False])

[123]:

any(c)

[123]:

True

[124]:

all(c)

[124]:

False

[44]:

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

np.logical_and(a > 0, a < 3)

[44]:

array([ True, False, False])

[126]:

a = np.array([True, False, True], bool)

np.logical_not(a)

[126]:

array([False,  True, False])

[127]:

a = np.array([True, False, True], bool)
b = np.array([False, True, False], bool)

np.logical_or(a, b)

[127]:

array([ True,  True,  True])


### 3.5.15. Where¶

[45]:

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

np.where(a != 0)

[45]:

(array([0, 1]),)

[129]:

b = np.array([1, 0, 3, 4, 0], float)

np.where(b != 0)

[129]:

(array([0, 2, 3]),)

[130]:

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

np.where(a != 0, a, None)  # for element a != 0 return such element, otherwise None


[130]:

array([1.0, 3.0, None], dtype=object)

[131]:

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

np.where(a != 0, a ** 2, a)

[131]:

array([1., 9., 0.])

[132]:

np.where(a != 0, a ** 2, 10)

[132]:

array([ 1.,  9., 10.])


### 3.5.16. Nonzero¶

[133]:

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

[133]:

(array([0, 1]), array([1, 0]))


### 3.5.17. IsFinite and IsNaN¶

[134]:

a = np.array([1, np.NaN, np.Inf], float)

[135]:

np.isnan(a)

[135]:

array([False,  True, False])

[136]:

np.isfinite(a)

[136]:

array([ True, False, False])


### 3.5.18. Array item selection and manipulation¶

[46]:

a = np.array([[6, 4], [5, 9]], float)
a

[46]:

array([[6., 4.],
[5., 9.]])

[138]:

a >= 6

[138]:

array([[ True, False],
[False,  True]])

[139]:

a[a >= 6]

[139]:

array([6., 9.])

[140]:

a[np.logical_and(a > 5, a < 9)]

[140]:

array([6.])

[47]:

a = np.array([2, 4, 6, 8], float)
b = np.array([0, 0, 1, 3, 2, 1], int)
a[b]

[47]:

array([2., 2., 4., 8., 6., 4.])

[142]:

a = np.array([2, 4, 6, 8], float)

a[[0, 0, 1, 3, 2, 1]]

[142]:

array([2., 2., 4., 8., 6., 4.])

[52]:

a = np.array([[1, 4], [9, 16]], float)
b = np.array([0, 0, 1, 1, 0], int)
c = np.array([0, 1, 1, 1, 1], int)

a[b,c]

[52]:

array([ 1.,  4., 16., 16.,  4.])

[144]:

a = np.array([2, 4, 6, 8], float)
b = np.array([0, 0, 1, 3, 2, 1], int)

a.take(b)

[144]:

array([2., 2., 4., 8., 6., 4.])

[145]:

a = np.array([[0, 1], [2, 3]], float)
b = np.array([0, 0, 1], int)

a.take(b, axis=0)

[145]:

array([[0., 1.],
[0., 1.],
[2., 3.]])

[146]:

a.take(b, axis=1)

[146]:

array([[0., 0., 1.],
[2., 2., 3.]])

[147]:

a = np.array([0, 1, 2, 3, 4, 5], float)
b = np.array([9, 8, 7], float)

a.put([0, 3], b)
a

[147]:

array([9., 1., 2., 8., 4., 5.])

[148]:

a = np.array([0, 1, 2, 3, 4, 5], float)

a.put([0, 3], 5)
a

[148]:

array([5., 1., 2., 5., 4., 5.])


## 3.6. Statistics¶

### 3.6.1. Compute the median along the specified axis¶

[149]:

a = np.array([1, 4, 3, 8, 9, 2, 3], float)

np.median(a)

[149]:

3.0


### 3.6.2. Estimate a covariance matrix¶

[150]:

a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float)

np.cov(a)

[150]:

array([[0.91666667, 2.08333333],
[2.08333333, 8.91666667]])

[151]:

np.cov(a, ddof=0)

[151]:

array([[0.6875, 1.5625],
[1.5625, 6.6875]])


### 3.6.3. Pearson product-moment correlation coefficients¶

[152]:

a = np.array([[1, 2, 1, 3], [5, 3, 1, 8]], float)

np.corrcoef(a)

[152]:

array([[1.        , 0.72870505],
[0.72870505, 1.        ]])


## 3.7. Vector and matrix mathematics¶

[153]:

a = np.array([[0, 1], [2, 3]], float)
b = np.array([2, 3], float)
c = np.array([[1, 1], [4, 0]], float)

[154]:

np.dot(b, a)

[154]:

array([ 6., 11.])

[155]:

np.dot(a, b)

[155]:

array([ 3., 13.])

[156]:

np.dot(a, c)

[156]:

array([[ 4.,  0.],
[14.,  2.]])

[157]:

np.dot(c, a)

[157]:

array([[2., 4.],
[0., 4.]])

[158]:

a = np.array([1, 4, 0], float)
b = np.array([2, 2, 1], float)

[159]:

np.outer(a, b)

[159]:

array([[2., 2., 1.],
[8., 8., 4.],
[0., 0., 0.]])

[160]:

np.inner(a, b)

[160]:

10.0

[161]:

np.cross(a, b)

[161]:

array([ 4., -1., -6.])


### 3.7.1. Linear algebra¶

[162]:

a = np.array([[4, 2, 0], [9, 3, 7], [1, 2, 1]], float)

np.linalg.det(a)

[162]:

-48.00000000000003

[163]:

a = np.array([[4, 2, 0], [9, 3, 7], [1, 2, 1]], float)

vals, vecs = np.linalg.eig(a)

[164]:

vals

[164]:

array([ 8.85591316,  1.9391628 , -2.79507597])

[165]:

vecs

[165]:

array([[-0.3663565 , -0.54736745,  0.25928158],
[-0.88949768,  0.5640176 , -0.88091903],
[-0.27308752,  0.61828231,  0.39592263]])

[166]:

a = np.array([[4, 2, 0], [9, 3, 7], [1, 2, 1]], float)
b = np.linalg.inv(a)
b

[166]:

array([[ 0.22916667,  0.04166667, -0.29166667],
[ 0.04166667, -0.08333333,  0.58333333],
[-0.3125    ,  0.125     ,  0.125     ]])

[167]:

np.dot(a, b)

[167]:

array([[1.00000000e+00, 5.55111512e-17, 0.00000000e+00],
[0.00000000e+00, 1.00000000e+00, 2.22044605e-16],
[0.00000000e+00, 1.38777878e-17, 1.00000000e+00]])


## 3.8. Matrix¶

Numpy ma również typ macierzy matrix. Jest on bardzo podobny do tablicy ale podstawowe operacje wykonywane są w sposób macierzowy a nie tablicowy.

[168]:

a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])

[169]:

a * b

[169]:

matrix([[19, 22],
[43, 50]])

[170]:

a @ b

[170]:

matrix([[19, 22],
[43, 50]])

[171]:

a ** 2

[171]:

matrix([[ 7, 10],
[15, 22]])

[173]:

a * 2

[173]:

matrix([[2, 4],
[6, 8]])

[185]:

a = np.diag([3,4])
a

[185]:

array([[3, 0],
[0, 4]])


## 3.9. Linear Algebra¶

[187]:

a = np.matrix([[1,2], [3,4]])
print('det(a) = {}'.format(np.linalg.det(a)))

det(a) = -2.0000000000000004


### 3.9.1. Linear algebra basics¶

Function Description
norm Vector or matrix norm
inv Inverse of a square matrix
solve Solve a linear system of equations
det Determinant of a square matrix
slogdet Logarithm of the determinant of a square matrix
lstsq Solve linear least-squares problem
pinv Pseudo-inverse (Moore-Penrose) calculated using a singular value decomposition
matrix_power Integer power of a square matrix
matrix_rank Calculate matrix rank using an SVD-based method

### 3.9.2. Eigenvalues and decompositions¶

| Function | Description | |———-|—————————————————-| | eig | Eigenvalues and vectors of a square matrix | | eigh | Eigenvalues and eigenvectors of a Hermitian matrix | | eigvals | Eigenvalues of a square matrix | | eigvalsh | Eigenvalues of a Hermitian matrix | | qr | QR decomposition of a matrix | | svd | Singular value decomposition of a matrix | | cholesky | Cholesky decomposition of a matrix |

### 3.9.3. Tensor operations¶

Function Description
tensorsolve Solve a linear tensor equation
tensorinv Calculate an inverse of a tensor

### 3.9.4. Exceptions¶

Function Description
LinAlgError Indicates a failed linear algebra operation

## 3.10. Assignments¶

### 3.10.1. Matrix multiplication¶

• Używając numpy oraz operatora @ oraz *
• Czym się różnią?
def matrix_multiplication(A, B):
"""
>>> import numpy as np

>>> A = np.array([[1, 0], [0, 1]])
>>> B = [[4, 1], [2, 2]]
>>> matrix_multiplication(A, B)
[[4, 1], [2, 2]]

>>> A = [[1,0,1,0], [0,1,1,0], [3,2,1,0], [4,1,2,0]]
>>> B = np.matrix([[4,1], [2,2], [5,1], [2,3]])
>>> matrix_multiplication(A, B)
[[9, 2], [7, 3], [21, 8], [28, 8]]
"""
pass


• Filename: numpy-matrix-mul.py
• Lines of code to write: 2 lines
• Estimated time of completion: 5 min

### 3.10.2. Sum of inner matrix¶

• Wygeneruj macierz (16x16) randomowych intów o wartościach od 10 do 100
• Przekonwertuj macierz na typ float
• Transponuj ją
• Policz sumę środkowych (4x4) elementów macierzy
• Wyświetl wartość (skalar) sumy, a nie nie wektor

• Filename: numpy-sum.py
• Lines of code to write: 4 lines
• Estimated time of completion: 5 min

### 3.10.3. Szukanie liczby¶

• Mamy liczbę trzycyfrową.
• Jeżeli od liczny dziesiątek odejmiemy liczbę jedności otrzymamy 6.
• Jeżeli do liczby dziesiątek dodamy liczbę jedności otrzymamy 10.
• Znajdź wszystkie liczby trzycyfrowe spełniające ten warunek
• Znajdź liczby trzycyfrowe podzielne przez 3

#### 3.10.3.1. Hints¶

• Ax=B
• x=A−1B
liczba_dziesiatek - liczba_jednosci = 6
liczba_dziesiatek + liczba_jednosci = 10

liczba_dziesiatek = liczba_jednosci + 6
liczba_dziesiatek + liczba_jednosci = 10

liczba_dziesiatek = liczba_jednosci + 6
(liczba_jednosci + 6) + liczba_jednosci 10

liczba_dziesiatek = liczba_jednosci + 6
2 * liczba_jednosci + 6 = 10

liczba_dziesiatek = liczba_jednosci + 6
liczba_jednosci = 8 / 2

liczba_dziesiatek = 2 + 6
liczba_jednosci = 2

liczba_dziesiatek = 8
liczba_jednosci = 2

x1 - x2 = 6
x1 + x2 = 10

x1 = 6 + x2
6 + x2 + x2 = 10

2 * x2 = 4
x2 = 2
x1 = 8

import numpy as np

A = np.matrix([[1, -1], [1, 1]])
# matrix([[ 1, -1],
#        [ 1,  1]])

B = np.matrix([6, 10]).T  # Transpose matrix
# matrix([[ 6],
#        [10]])

x = A**(-1) * B
# matrix([[8.],
#        [2.]])

A*x == B
# matrix([[ True],
#        [ True]])

res1 = np.arange(1, 10)*100 + 10*x[0,0] + 1*x[1,0]
# array([182., 282., 382., 482., 582., 682., 782., 882., 982.])

res1[res1 % 3 == 0]
# array([282., 582., 882.])

m = res1 % 3 == 0
# array([False,  True, False, False,  True, False, False,  True, False])

res1[m]
# array([282., 582., 882.])

res2 = res1[m]
# array([282., 582., 882.])

[ ]: