# 5.4. Linear Algebra¶

## 5.4.1. Linear Algebra¶

• np.sign()

• np.abs()

• np.sqrt()

• np.power()

## 5.4.2. Logarithms¶

• np.log()

• np.log10()

• np.exp()

## 5.4.3. Vector and matrix mathematics¶

### 5.4.3.1. Determinant of a square matrix¶

import numpy as np

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

np.linalg.det(a)
# -53.999999999999993


### 5.4.3.2. Inner product¶

• Compute inner product of two vectors

• np.inner()

• Ordinary inner product of vectors for 1-D arrays (without complex conjugation)

• In higher dimensions a sum product over the last axes

Code 5.142. Ordinary inner product for vectors
import numpy as np

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

np.inner(a, b)
# 2

Code 5.143. Multidimensional example
import numpy as np

a = np.arange(24).reshape((2,3,4))
b = np.arange(4)

np.inner(a, b)
# array([[ 14,  38,  62],
#        [ 86, 110, 134]])


### 5.4.3.3. Outer product¶

• Compute the outer product of two vectors

• np.outer()

import numpy as np

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

np.outer(a, b)
# array([[ 2., 2., 1.],
#        [ 8., 8., 4.],
#        [ 0., 0., 0.]])

Code 5.144. An example using a "vector" of letters
import numpy as np

a = np.array(['a', 'b', 'c'], dtype=object)

np.outer(a, [1, 2, 3])
# array([['a', 'aa', 'aaa'],
#        ['b', 'bb', 'bbb'],
#        ['c', 'cc', 'ccc']], dtype=object)


### 5.4.3.4. Cross product¶

• The cross product of a and b in R^3 is a vector perpendicular to both a and b

• np.cross()

Code 5.145. Vector cross-product
import numpy as np

x = [1, 2, 3]
y = [4, 5, 6]

np.cross(x, y)
# array([-3,  6, -3])

Code 5.146. One vector with dimension 2
import numpy as np

x = [1, 2]
y = [4, 5, 6]

np.cross(x, y)
# array([12, -6, -3])


## 5.4.4. Eigenvalues and vectors of a square matrix¶

• Each of a set of values of a parameter for which a differential equation has a nonzero solution (an eigenfunction) under given conditions

• Any number such that a given matrix minus that number times the identity matrix has a zero determinant

import numpy as np

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

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

vals
# array([ 9. , 2.44948974, -2.44948974])

vecs
# array([[-0.3538921 , -0.56786837, 0.27843404],
#        [-0.88473024, 0.44024287, -0.89787873],
#        [-0.30333608, 0.69549388, 0.34101066]])


## 5.4.5. Inverse of a square matrix¶

import numpy as np

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

np.linalg.inv(a)
# array([[ 0.14814815, 0.07407407, -0.25925926],
#        [ 0.2037037 , -0.14814815, 0.51851852],
#        [-0.27777778, 0.11111111, 0.11111111]])

import numpy as np

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

np.dot(a, b)
# array([[ 1.00000000e+00, 5.55111512e-17, 2.22044605e-16],
#        [ 0.00000000e+00, 1.00000000e+00, 5.55111512e-16],
#        [ 1.11022302e-16, 0.00000000e+00, 1.00000000e+00]])


## 5.4.6. Singular value decomposition of a matrix¶

import numpy as np

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

U, s, Vh = np.linalg.svd(a)

U
# array([[-0.6113829 , -0.79133492],
#        [-0.79133492, 0.6113829 ]])

s
# array([ 7.46791327, 2.86884495])

Vh
# array([[-0.61169129, -0.45753324, -0.64536587],
#        [ 0.78971838, -0.40129005, -0.46401635],
#        [-0.046676 , -0.79349205, 0.60678804]])


## 5.4.7. Linear Algebra¶

Table 5.13. 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

Table 5.14. 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

Table 5.15. Tensor operations

Function

Description

tensorsolve

Solve a linear tensor equation

tensorinv

Calculate an inverse of a tensor

Table 5.16. Exceptions

Function

Description

LinAlgError

Indicates a failed linear algebra operation

## 5.4.8. Assignments¶

### 5.4.8.1. Numpy Algebra Euclidean 2D¶

• Assignment: Numpy Algebra Euclidean 2D

• Last update: 2020-10-01

• Complexity: easy

• Lines of code: 5 lines

• Estimated time: 5 min

English:
1. Use code from "Input" section (see below)

2. Given are two points A: tuple[int, int] and B: tuple[int, int]

3. Coordinates are in cartesian system

4. Points A and B are in two dimensional space

5. Calculate distance between points using Euclidean algorithm

6. Function must pass doctest

Polish:
1. Użyj kodu z sekcji "Input" (patrz poniżej)

2. Dane są dwa punkty A: tuple[int, int] i B: tuple[int, int]

3. Koordynaty są w systemie kartezjańskim

4. Punkty A i B są w dwuwymiarowej przestrzeni

5. Oblicz odległość między nimi wykorzystując algorytm Euklidesa

6. Funkcja musi przechodzić doctest

Given:
def euclidean_distance(A, B):
"""
>>> A = (1, 0)
>>> B = (0, 1)
>>> euclidean_distance(A, B)
1.4142135623730951

>>> euclidean_distance((0,0), (1,0))
1.0

>>> euclidean_distance((0,0), (1,1))
1.4142135623730951

>>> euclidean_distance((0,1), (1,1))
1.0

>>> euclidean_distance((0,10), (1,1))
9.055385138137417
"""
x1 = ...
y1 = ...
x2 = ...
y2 = ...


Figure 5.5. Calculate Euclidean distance in Cartesian coordinate system

Hints:
• $$distance(a, b) = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$

### 5.4.8.2. Numpy Algebra Euclidean Ndim¶

• Assignment: Numpy Algebra Euclidean Ndim

• Last update: 2020-10-01

• Complexity: easy

• Lines of code: 10 lines

• Estimated time: 8 min

English:
1. Use code from "Input" section (see below)

2. Given are two points A: Sequence[int] and B: Sequence[int]

3. Coordinates are in cartesian system

4. Points A and B are in N-dimensional space

5. Points A and B must be in the same space

6. Calculate distance between points using Euclidean algorithm

7. Function must pass doctest

Polish:
1. Użyj kodu z sekcji "Input" (patrz poniżej)

2. Dane są dwa punkty A: Sequence[int] i B: Sequence[int]

3. Koordynaty są w systemie kartezjańskim

4. Punkty A i B są w N-wymiarowej przestrzeni

5. Punkty A i B muszą być w tej samej przestrzeni

6. Oblicz odległość między nimi wykorzystując algorytm Euklidesa

7. Funkcja musi przechodzić doctest

Given:
def euclidean_distance(A, B):
"""
>>> euclidean_distance((0,0,1,0,1), (1,1))
Traceback (most recent call last):
ValueError: Points must be in the same dimensions

>>> A = (0,1,0,1)
>>> B = (1,1,0,0)
>>> euclidean_distance(A, B)
1.4142135623730951

>>> euclidean_distance((0,0,0), (0,0,0))
0.0

>>> euclidean_distance((0,0,0), (1,1,1))
1.7320508075688772

>>> euclidean_distance((0,1,0,1), (1,1,0,0))
1.4142135623730951

>>> euclidean_distance((0,0,1,0,1), (1,1,0,0,1))
1.7320508075688772
"""

Hints:
• import doctest

• $$distance(a, b) = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + ... + (n_2 - n_1)^2}$$

• for n1, n2 in zip(A, B)