```python
import numpy as np
from scipy import linalg as la
import sympy as sp
```

Starting from L9

Vectors x1 and x2 are independent if c1x1+c2x2 <>0

Vectors v1,v2,...,vn are columns of A. They are independant if nullspace of A is only zero vector(r=n, no free variables). They are dependant if Ac=0 for some nonzero c(r<n, there are free variables).

Vectors v1,...,vl span a space means: the space consists of all combs of those vectors.

Basis for a vector space is a sequence of vectors v1,v2,...,vd with 2 properties: 1. They are independant; 2. They span the space.

I3 is a basis for R^3


```python
I3 = np.identity(3)
I3
```




    array([[ 1.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.]])




```python
Z = np.zeros(3)
Z
```




    array([ 0.,  0.,  0.])



The only vector  that gives zeros:


```python
np.dot(I3,Z)
```




    array([ 0.,  0.,  0.])



Another basis:


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




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



n x n matrix that is invertible.


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




    array([[ 0.125,  2.125, -0.625],
           [-0.625,  0.375,  0.125],
           [ 0.375, -0.625,  0.125]])




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




    8.0000000000000018



Every basis for the space has the same number of vectors and this number is the dimension of this space.


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




    3



Rank is a number of pivot columns and it is a dimension of the columnspace. C(A)

The dimension of a Null Space is the number of free variables, total - pivot variables.

4 Fundamental subspaces:

    Columnspace C(A) in R^m
    nullspace N(A) in R^n
    rowspace C(A^T) in R^n
    nullspace of A^T = N(A^T) (Left NullSpace) in R^m
    
    

Just a lyrical digression. One can do a PLU transformation using scipy.linalg.lu.


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

P,L,U
```




    (array([[ 0.,  1.,  0.],
            [ 0.,  0.,  1.],
            [ 1.,  0.,  0.]]), array([[ 1. ,  0. ,  0. ],
            [ 0.5,  1. ,  0. ],
            [ 0.5,  0.6,  1. ]]), array([[  2. ,   7. ,  11. ],
            [  0. ,  -2.5,  -1.5],
            [  0. ,   0. ,  -1.6]]))




```python
A2 = np.array([[1,3,1,4],
              [2,7,3,9],
              [1,5,3,1],
              [1,2,0,8]])
```

Using sympy it is possible to calculate Reduced Row Echelon Form of the Matrix and thus find the basis of the matrix. And understand which rows are linearly independant.


```python
sp.Matrix(A2).rref()
```




    (Matrix([
     [1, 0, -2, 0],
     [0, 1,  1, 0],
     [0, 0,  0, 1],
     [0, 0,  0, 0]]), (0, 1, 3))



In a matrix A2 columns 1, 2 and 4 are linearly independant and form a basis. Thus the rank of this matrix is 3.


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




    3




```python
sp.Matrix(A).rref()
```




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



And here all the columns are linearly independant and form the basis.


```python
sp.Matrix(np.transpose(A2)).rref()
```




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




```python
sp.Matrix(A2).nullspace()
```




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




```python
sp.Matrix(A2).rowspace()
```




    [Matrix([[1, 3, 1, 4]]), Matrix([[0, 1, 1, 1]]), Matrix([[0, 0, 0, -5]])]




```python
sp.Matrix(A2).columnspace()
```




    [Matrix([
     [1],
     [2],
     [1],
     [1]]), Matrix([
     [3],
     [7],
     [5],
     [2]]), Matrix([
     [4],
     [9],
     [1],
     [8]])]



The dim of both column space and row space are rank of matrix - r.


```python
A3 = np.array ([[1,2,3],
               [1,2,3],
               [2,5,8]])
```


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




    2




```python
R = np.array(sp.Matrix(A3).rref()[0])
R
```




    array([[1, 0, -1],
           [0, 1, 2],
           [0, 0, 0]], dtype=object)




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


```python
R4 = np.array(sp.Matrix(A4).rref()[0])
R4
```




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




```python
sp.Matrix(A4).rowspace()
```




    [Matrix([[1, 2, 3, 1]]), Matrix([[0, -1, -1, 0]])]




```python
sp.Matrix(A4).columnspace()
```




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




```python
N41,N42 = np.array(sp.Matrix(A4).nullspace())
np.dot(A4,N41), np.dot(A4,N42)
```




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




```python
lN4 = np.array(sp.Matrix(np.transpose(A4)).nullspace())
np.dot(lN4,A4)
```




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



E matrix, such as EA = R


```python
preE4 = np.rint(np.array(sp.Matrix(np.c_[A4,np.eye(3)]).rref()[0]).astype(np.double))
```


```python
preE4
```




    array([[ 1.,  0.,  1.,  1.,  0.,  2., -1.],
           [ 0.,  1.,  1.,  0.,  0., -1.,  1.],
           [ 0.,  0.,  0.,  0.,  1.,  0., -1.]])




```python
A4
```




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




```python
E4 = preE4[:,4:7]
E4
```




    array([[ 0.,  2., -1.],
           [ 0., -1.,  1.],
           [ 1.,  0., -1.]])




```python
np.dot(E4,A4)
```




    array([[ 1.,  0.,  1.,  1.],
           [ 0.,  1.,  1.,  0.],
           [ 0.,  0.,  0.,  0.]])



Ending Lecture 10 here.

936d59c04110b19579c3cd1683c1222331051cb4
ipynb
Jupyter Notebook
LinAl_003.ipynb
rtgshv/linal101
f520987a6f1e468b3b466c14820e43dada565e43
MIT
LinAl_003.ipynb
rtgshv/linal101
f520987a6f1e468b3b466c14820e43dada565e43
MIT
LinAl_003.ipynb
rtgshv/linal101
f520987a6f1e468b3b466c14820e43dada565e43
MIT
Qwen/Qwen-72B
1. YES
2. YES