# 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.4. 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.5. 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

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


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.6. 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.]])


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.7. Cross product¶

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

• np.cross()

Vector cross-product:

import numpy as np

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

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


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.8. 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.9. 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.10. 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.11. Linear Algebra¶

Table 5.2. 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.3. 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.4. Tensor operations

Function

Description

tensorsolve

Solve a linear tensor equation

tensorinv

Calculate an inverse of a tensor

Table 5.5. Exceptions

Function

Description

LinAlgError

Indicates a failed linear algebra operation

## 5.4.12. Assignments¶

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

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

"""
* Assignment: Numpy Algebra Euclidean 2D
* Complexity: easy
* Lines of code: 6 lines
* Time: 5 min

English:
1. Given are two points a: tuple[int, int] and b: tuple[int, int]
2. Coordinates are in cartesian system
3. Points a and b are in two dimensional space
4. Calculate distance between points using Euclidean algorithm
5. Run doctests - all must succeed

Polish:
1. Dane są dwa punkty a: tuple[int, int] i b: tuple[int, int]
2. Koordynaty są w systemie kartezjańskim
3. Punkty a i b są w dwuwymiarowej przestrzeni
4. Oblicz odległość między nimi wykorzystując algorytm Euklidesa
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0

>>> 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
"""

from math import sqrt

def euclidean_distance(a, b):
pass


"""

* Assignment: Numpy Algebra Euclidean Ndim
* Complexity: easy
* Lines of code: 7 lines
* Time: 8 min

English:
1. Given are two points a: Sequence[int] and b: Sequence[int]
2. Coordinates are in cartesian system
3. Points a and b are in n-dimensional space
4. Points a and b must be in the same space
5. Calculate distance between points using Euclidean algorithm
6. Run doctests - all must succeed

Polish:
1. Dane są dwa punkty a: Sequence[int] i b: Sequence[int]
2. Koordynaty są w systemie kartezjańskim
3. Punkty a i b są w n-wymiarowej przestrzeni
4. Punkty b i b muszą być w tej samej przestrzeni
5. Oblicz odległość między nimi wykorzystując algorytm Euklidesa
6. Uruchom doctesty - wszystkie muszą się powieść

Hints:
* for n1,n2 in zip(a,b)

Tests:
>>> import sys; sys.tracebacklimit = 0

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

>>> 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
"""

from math import sqrt

def euclidean_distance(a, b):
pass