# Matrix addition


```python
import numpy as np
# Initializing an array
x = np.array([[1, 1], [2, 2]])
y = np.array([[10, 10], [20, 20]])
```


```python
x+y
```




    array([[11, 11],
           [22, 22]])




```python
np.add(x,y)
```




    array([[11, 11],
           [22, 22]])



# Matrix multiplication


```python
x = np.array([[1, 1], [2, 2]])
```


```python
2*x
```




    array([[2, 2],
           [4, 4]])



# Matrix arithmetic


```python
# subtraction
x-y
```




    array([[ -9,  -9],
           [-18, -18]])




```python
x*y
```




    array([[10, 10],
           [40, 40]])




```python
# division
x/y
```




    array([[ 0.1,  0.1],
           [ 0.1,  0.1]])




```python
# exponential
x**y
```




    array([[      1,       1],
           [1048576, 1048576]], dtype=int32)



# Matrix matrix multiplication


```python
x=np.array([[1, 1], [2, 2]])
y=np.array([[10, 10], [20, 20]])
```


```python
np.dot(x,y)
```




    array([[30, 30],
           [60, 60]])



# Matrix transpose


```python
# Initialize a matrix
A = np.array([[1,2],[3,4]])
A.transpose()
```




    array([[1, 3],
           [2, 4]])



# Matrix inversion


```python
from scipy import linalg
```


```python
A = np.array([[1,2],[3,4]])
```


```python
linalg.inv(A)
```




    array([[-2. ,  1. ],
           [ 1.5, -0.5]])



# Solving linear systems using matrices


```python
from scipy import linalg
```


```python
A=[[1,3,5],[2,5,1],[2,3,8]]
b=[[10],[8],[3]]
```


```python
np.dot(linalg.inv(A),b)
```




    array([[-9.28],
           [ 5.16],
           [ 0.76]])




```python
linalg.solve(A, b)
```




    array([[-9.28],
           [ 5.16],
           [ 0.76]])



# Determinant of a matrix


```python
linalg.det(A)
```




    -25.000000000000004



# Null space of a matrix


```python
import sympy
M = sympy.Matrix([[1, 2, 3, 0, 0], [4, 10, 0, 0, 1]])
```


```python
M.nullspace()
```




    [Matrix([
     [-15],
     [  6],
     [  1],
     [  0],
     [  0]]), Matrix([
     [0],
     [0],
     [0],
     [1],
     [0]]), Matrix([
     [   1],
     [-1/2],
     [   0],
     [   0],
     [   1]])]




```python
x=[
[-15],
[  6],
[  1],
[  0],
[  0]]

```


```python
np.dot(np.array(M),x)
```




    array([[0],
           [0]], dtype=object)



# Range of a matrix


```python
x=np.array([[1,100],[2,200]])
```


```python
np.ptp(x)
```




    199




```python
np.ptp(x,axis=1)
```




    array([ 99, 198])




```python
np.ptp(x,axis=0)
```




    array([  1, 100])



# Rank and nullity of a matrix


```python
a=np.array([[3,2,-1],[2,-3,-5],[-1,-4,-3]])
```


```python
np.linalg.matrix_rank(a)
```




    2




```python
a.shape[1]
```




    3




```python
a.shape[1] - np.linalg.matrix_rank(a) 
```




    1



# LU decomposition of a matrix


```python
A = np.array([[2., 1., 1.], [1., 3., 2.], [1., 0., 0.]])
```


```python
import scipy.linalg as linalg
```


```python
P, L, U = linalg.lu(A)
```


```python
print(L)
```

    [[ 1.   0.   0. ]
     [ 0.5  1.   0. ]
     [ 0.5 -0.2  1. ]]



```python
print(U)
```

    [[ 2.   1.   1. ]
     [ 0.   2.5  1.5]
     [ 0.   0.  -0.2]]



```python
np.dot(L,U)
```




    array([[  2.00000000e+00,   1.00000000e+00,   1.00000000e+00],
           [  1.00000000e+00,   3.00000000e+00,   2.00000000e+00],
           [  1.00000000e+00,   0.00000000e+00,  -2.77555756e-17]])



# QR decomposition of a matrix


```python
import scipy.linalg as linalg 
import numpy as np

```


```python
A = np.array([ [2., 1., 1.], [1., 3., 2.], [1., 0., 0]])
```


```python
Q, R = linalg.qr(A)
```


```python
print(Q)
```

    [[-0.81649658  0.27602622 -0.50709255]
     [-0.40824829 -0.89708523  0.16903085]
     [-0.40824829  0.34503278  0.84515425]]



```python
print(R)
```

    [[-2.44948974 -2.04124145 -1.63299316]
     [ 0.         -2.41522946 -1.51814423]
     [ 0.          0.         -0.16903085]]



```python
print(np.dot(Q,Q.T))
```

    [[  1.00000000e+00  -1.05102508e-17  -3.90671821e-17]
     [ -1.05102508e-17   1.00000000e+00   2.51394744e-17]
     [ -3.90671821e-17   2.51394744e-17   1.00000000e+00]]



```python
print(np.dot(Q,R))
```

    [[  2.00000000e+00   1.00000000e+00   1.00000000e+00]
     [  1.00000000e+00   3.00000000e+00   2.00000000e+00]
     [  1.00000000e+00   1.11022302e-16  -7.63527929e-17]]


# Eigenvalue and eigenvector of a matrix


```python
a = np.array([[1, 2], [3, 4]])
```


```python
la, v = linalg.eig(a)
```


```python
print(la)
```

    [-0.37228132+0.j  5.37228132+0.j]



```python
print(v)
```

    [[-0.82456484 -0.41597356]
     [ 0.56576746 -0.90937671]]



```python
np.dot(a,v)
```




    array([[ 0.30697009, -2.23472698],
           [-0.21062466, -4.88542751]])




```python
np.dot(la,v)
```




    array([ 3.34643208+0.j, -4.73056832+0.j])



# Diagonalizing a matrix


```python
import numpy as np
```


```python
a= np.array([1,2,3])
```


```python
A=np.diag(a)
print(A)
```

    [[1 0 0]
     [0 2 0]
     [0 0 3]]



```python
la, v = linalg.eig(A)
print(la)
print(v)

```

    [ 1.+0.j  2.+0.j  3.+0.j]
    [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]]



```python
a = np.array([[1, 2,0], [2,1,0],[0,0,-3]])
la, v = linalg.eig(a)
print(la)
print(v)
```

    [ 3.+0.j -1.+0.j -3.+0.j]
    [[ 0.70710678 -0.70710678  0.        ]
     [ 0.70710678  0.70710678  0.        ]
     [ 0.          0.          1.        ]]



```python
np.dot(np.dot(np.linalg.inv(v),a),v)
```




    array([[  3.00000000e+00,   4.44089210e-16,   0.00000000e+00],
           [  6.66133815e-16,  -1.00000000e+00,   0.00000000e+00],
           [  0.00000000e+00,   0.00000000e+00,  -3.00000000e+00]])




```python
np.diag(la)
```




    array([[ 3.+0.j,  0.+0.j,  0.+0.j],
           [ 0.+0.j, -1.+0.j,  0.+0.j],
           [ 0.+0.j,  0.+0.j, -3.+0.j]])




```python
np.dot(np.dot(np.linalg.inv(v),a),v)-np.diag(la)
```




    array([[ -4.44089210e-16+0.j,   4.44089210e-16+0.j,   0.00000000e+00+0.j],
           [  6.66133815e-16+0.j,  -4.44089210e-16+0.j,   0.00000000e+00+0.j],
           [  0.00000000e+00+0.j,   0.00000000e+00+0.j,   0.00000000e+00+0.j]])



# Jordan form of a matrix


```python
import numpy as np
from sympy import Matrix


```


```python
a = np.array([[5, 4, 2, 1], [0, 1, -1, -1], [-1, -1, 3, 0], [1, 1, -1, 2]])
```


```python
m = Matrix(a)
```


```python
P, J = m.jordan_form()
```


```python
J
```




    Matrix([
    [1, 0, 0, 0],
    [0, 2, 0, 0],
    [0, 0, 4, 1],
    [0, 0, 0, 4]])



# SVD of a matrix


```python
a = np.array([[1, 2], [3, 4]])
```


```python
from scipy import linalg
```


```python
linalg.svd(a)
```




    (array([[-0.40455358, -0.9145143 ],
            [-0.9145143 ,  0.40455358]]),
     array([ 5.4649857 ,  0.36596619]),
     array([[-0.57604844, -0.81741556],
            [ 0.81741556, -0.57604844]]))




```python
U = np.array([[-0.40455358, -0.9145143 ],
        [-0.9145143 ,  0.40455358]])
sigma = np.array([[ 5.4649857 ,  0],
                  [0,0.36596619]])
V=np.array([[-0.57604844, -0.81741556],
        [ 0.81741556, -0.57604844]])

```


```python
np.dot(np.dot(U,sigma),V)
```




    array([[ 0.99999999,  1.99999998],
           [ 3.00000003,  4.00000001]])



# Sparse matrix


```python
import numpy as np
from scipy import sparse

```


```python
A = np.array([[1,2,0],[0,0,3],[1,0,4]])
```


```python
sA = sparse.csr_matrix(A)
```


```python
print(sA)
```

      (0, 0)	1
      (0, 1)	2
      (1, 2)	3
      (2, 0)	1
      (2, 2)	4



```python
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve
from numpy.linalg import solve, norm
from numpy.random import rand


```


```python
A = lil_matrix((10000, 10000))
```


```python
A[0, :100] = rand(100)
A[1, 100:200] = A[0, :100]

```


```python
A.setdiag(rand(10000))
```


```python
b = rand(10000)
```


```python
A = A.tocsr()
```


```python
x = spsolve(A, b)
```


```python
import time
start_time=time.time()
x = spsolve(A.tocsr(), b)
end_time=time.time()
print(end_time-start_time)
```

    0.00500035285949707



```python
import time
start_time=time.time()
x = solve(A.toarray(), b)
end_time=time.time()
print(end_time-start_time)
```

    6.414999961853027



```python

```

f5b0968da226b62d5c13bde05322ec2a64161d35
ipynb
Jupyter Notebook
Chapter05/Chapter 5 - scipy.ipynb
PacktPublishing/SciPy-Recipes
fdea8e3bd6b161402ed824624819aa788e544eed
MIT
2017-12-28T05:01:19.000Z
2021-11-08T13:10:39.000Z
Chapter05/Chapter 5 - scipy.ipynb
PacktPublishing/SciPy-Recipes
fdea8e3bd6b161402ed824624819aa788e544eed
MIT
Chapter05/Chapter 5 - scipy.ipynb
PacktPublishing/SciPy-Recipes
fdea8e3bd6b161402ed824624819aa788e544eed
MIT
2017-12-24T09:25:55.000Z
2020-02-17T22:57:17.000Z
Qwen/Qwen-72B
1. YES
2. YES