## NumPy Linear Algebra

Linear algebra is central to almost all areas of mathematics and computer science. The data is represented by linear equations such as (a1x1 +……+anxn = b), which are presented in the form of matrices and vectors.

## Matrix and Vector Products

Here are some of the functions of matrix and vector products which are given below:

Function Description
dot(a, b) Computes the dot product of two arrays.
vdot(a, b) Computes the dot product of two vectors.
linalg.multi_dot(a,b,c,d,…) Computes the dot product of multiple arrays at once.
inner(a, b) Computes the inner product of two arrays.
outer(a, b) Computes the outer product of two arrays.
matmul(x1, x2) Computes the matrix product of two arrays.
tensordot(a, b,axes) Computes the tensor dot product of two arrays along the specified axes.
linalg.matrix_power(a, n) Raises a square matrix raised to the given power.
kron(a, b) Computes the Kronecker product of two arrays.
einsum(subscripts,*operand) Evaluates the Einstein summation convention on the operands.

## Example #1

Calculating different type of products of given arrays:

## Code:

```import numpy as np
#creating two arrays a and b
a = np.array([2, 1, 2])
b= np.array([4,5,6])
#dot product
print("Dot Product of a and b:", np.dot(a,b))
#inner product
print("Inner Product of a and b:", np.inner(a,b))
#outer product
print("Outer Product of a and b:", np.outer(a,b))
#Kronecker product
print("Kronecker Product of a and b:", np.kron(a,b))
```

## Output

```Dot Product of a and b: 25
Inner Product of a and b: 25
Outer Product of a and b: [[ 8 10 12]
[ 4 5 6]
[ 8 10 12]]
Kronecker product of a and b: [ 8 10 12 4 5 6 8 10 12]

```

Let’s see how we calculate all the mentioned products:

Dot product is a.b = ai*bi+aj*bj+ak*bk= [2,1,2].[4,5,6] = 2*4+1*5+2*6 = 25

Inner product is a generalization of dot product. So, it is also calculated similarly to a dot product.

Outer Product of two matrices a and b of sizes (m x 1) and (n x 1) is a resultant matrix (m x n). The product is given by a ⛒ b.

a⛒ b = [2*4 2*5 2*6] [1*4 1*5 1*6][2*4 2*5 2*6] =[ 8 10 12] [ 4 5 6 ] [ 8 10 12]

Kronecker product is the generalization of the outer product. It is also given by a ⛒ b [k0,k1,…,kN] = a[i0,i1,…,iN] * b[j0,j1,…,jN] . The result is given in the form of a block matrix.a⛒ b = [2*4 2*5 2*6] [1*4 1*5 1*6][2*4 2*5 2*6] = [8 10 12] [4 5 6] [8 10 12]

a⛒ b = [2 1 2][4 5 6] = [8 10 12 4 5 6 8 10 12]

## Example #2

This is the example of computing the matrix multiplication.

## Code:

```#creating matrix A and matrix B
import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.array([[2,1,3],[4,1,1],[1,2,3]])
#matrix multiplication
print("Multiplication of A and B:", np.matmul(A,B))
#raise matrix A to power of 2, i.e, AXA
print("Matrix A raised to power of 2:", np.linalg.matrix_power(A,2))
```

## Matrix Eigenvalues

Here are some of the functions of matrix eigenvalues which are given below:

Function Description
linalg.eig(a) Computes the eigenvalue of a square matrix.
linalg.eigvals(a) Compute the eigenvalues of any matrix.
linalg.eigh(a) Computes the eigenvalues and eigenvectors of a complex Hermitian and a real symmetric matrix.
linalg.eigvalsh(a) Compute the eigenvalues of a complex Hermitian and real symmetric matrix.

## Example #1

Computing Eigen Values of the given matrix.

## Code:

```import numpy as np
C = np.array([[1,0,0],[0,1,0],[0,0,1]])
print("Array is:",C)
#eigen values
print("Eigen values of Array is:",np.linalg.eig(C))
print("Eigen values of Array is:",np.linalg.eigvals(C))
```

Here are some of the functions of matrix and vector products which are given below:

Function Description
linalg.solve(a, b) Finds the solution of a linear equation.
linalg.tensorsolve(a, b) Finds the solution of a tensor equation.
linalg.pinv(a) Computes the pseudo inverse of an array/matrix.
linalg.tensorinv(a)

linalg.inv(a)

Computes the inverse of an array/matrix.

## Example #2

Solve two linear equations using the matrix.

## Code:

```#Equation 1 : 3x+4y = 7
#Equation 2 : 4x+3y = 7
#creating two arrays, one for solution and one for equations
import numpy as np
A = np.array([[3,4],[4,3]])
B = np.array([7,7])
C = np.linalg.solve(A,B)
# solution of equations
print("Solution of given linear equations is:",C)
```

## Example #3

This is the example of finding the inverse of a matrix.

### Code:

```import numpy as np
#creating a matrix A
A = np.array([[1,1],[0,1]])
#inverse of A
print("Inverse of matrix A is :", np.linalg.inv(A))
```

Apply now for Advanced Python Training Course