# 4.26. Linear Algebra¶

## 4.26.1. Linear Algebra¶

• np.sign()

• np.abs()

• np.sqrt()

• np.power()

## 4.26.2. Logarithms¶

• np.log()

• np.log10()

• np.exp()

## 4.26.3. Vector and matrix mathematics¶

### 4.26.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


### 4.26.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

Listing 4.87. 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

Listing 4.88. 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]])


### 4.26.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.]])

Listing 4.89. 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)


### 4.26.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()

Listing 4.90. Vector cross-product
import numpy as np

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

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

Listing 4.91. One vector with dimension 2
import numpy as np

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

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


## 4.26.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]])


## 4.26.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]])


## 4.26.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]])


## 4.26.7. Linear Algebra¶

Table 4.8. 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 4.9. 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 4.10. Tensor operations

Function

Description

tensorsolve

Solve a linear tensor equation

tensorinv

Calculate an inverse of a tensor

Table 4.11. Exceptions

Function

Description

LinAlgError

Indicates a failed linear algebra operation

## 4.26.8. Assignments¶

### 4.26.8.1. Euclidean distance 2D¶

• Complexity level: easy

• Lines of code to write: 5 lines

• Estimated time of completion: 15 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

Input
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 = ...
return ...


Figure 4.12. Calculate Euclidean distance in Cartesian coordinate system

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

### 4.26.8.2. Euclidean distance n dimensions¶

• Complexity level: easy

• Lines of code to write: 10 lines

• Estimated time of completion: 15 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

Input
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
"""
return ...

Hint
• $$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)