Home » Blog » Linear Algebra with SciPy

# Linear Algebra with SciPy

## Linear Algebra with SciPy

• Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
• Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
• Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional array.

Now let’s do some test with scipy.linalg,

Calculating determinant of a two-dimensional matrix,

```from scipy import linalg
import numpy as np
#define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass values to det() function
linalg.det( two_d_array )```

Output:

`-7.0`

Inverse Matrix –

```scipy.linalg.inv()
```

Inverse Matrix of Scipy calculates the inverse of any square matrix.

Let’s see,

```from scipy import linalg
import numpy as np
# define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass value to function inv()
linalg.inv( two_d_array )```

Output:

```array( [[-0.28571429,  0.71428571],
[ 0.42857143, -0.57142857]] )```

Eigenvalues and Eigenvector – scipy.linalg.eig()

• The most common problem in linear algebra is eigenvalues and eigenvector which can be easily solved using eig() function.
• Now lets we find the Eigenvalue of (X) and correspond eigenvector of a two-dimensional square matrix.

Example,

```from scipy import linalg
import numpy as np
#define two dimensional array
arr = np.array([[5,4],[6,3]])
#pass value into function
eg_val, eg_vect = linalg.eig(arr)
#get eigenvalues
print(eg_val)
#get eigenvectors
print(eg_vect)```

Output:

```[ 9.+0.j -1.+0.j] #eigenvalues
[ [ 0.70710678 -0.5547002 ] #eigenvectors
[ 0.70710678  0.83205029] ]```

SciPy is built using the optimized ATLAS LAPACK and BLAS libraries. It has very fast linear algebra capabilities. All of these linear algebra routines expect an object that can be converted into a two-dimensional array. A scipy.linalg contains all the functions that are in numpy.linalg. Additionally, scipy.linalg also has some other advanced functions that are not in numpy.linalg. Another advantage of using scipy.linalg over numpy.linalg is that it is always compiled with BLAS/LAPACK support, while for NumPy this is optional. Therefore, the SciPy version might be faster depending on how NumPy was installed.

The scipy.linalg.solve feature solves the linear equation a x + b y = Z, for the unknown x, y values.

```from scipy import linalg
import numpy as np

#Declaring the numpy arrays
a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
b = np.array([2, 4, -1])

#Passing the values to the solve function
x = linalg.solve(a, b)

#printing the result array
x```

Output:

`array([ 2., -2.,  9.])`
```#Declaring the numpy array
A = np.array([[1,2],[3,4]])

#Passing the values to the eig function
l, v = linalg.eig(A)

#printing the result for eigen values
print("Eigen value :  ")
l
```

Output:

```Eigen value :
array([-0.37228132+0.j,  5.37228132+0.j])```
```print("Eigen vector ")
v```

Output:

```Eigen vector
array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]])```
```a = np.random.randn(3, 2) + 1.j*np.random.randn(3, 2)

#Passing the values to the eig function
U, s, Vh = linalg.svd(a)

# printing the result
print (U)
print(Vh)
print(s)```

Output:

```[[-0.6146792 -0.38444967j -0.45050707+0.00623521j  0.51838468+0.05147584j]
[-0.4866983 +0.17985717j  0.43071221-0.73367169j -0.05426512-0.06359483j]
[ 0.39349679+0.22428636j  0.24078844-0.12344339j  0.84824503-0.04603179j]]
[[ 0.94914006+0.j         -0.0251676 -0.31384667j]
[ 0.31485416+0.j          0.07586871+0.94610295j]]
[3.71730062 2.50770185]```