# Mathematics for Machine Learning: Linear Algebra
## Week2
## Module 2:

### Modulus & inner product (vectors)

#### usefull pakages


```python
from sympy import solve, Poly, Eq, Function, exp, sqrt, simplify,acos

from sympy.abc import x, y, z, a, b, c, d , e, f
from sympy.vector import CoordSys3D

import numpy as np
```

### lenght of the vector:


```python
C = CoordSys3D('C') # cartesian coordinate
r = x*C.i + y*C.j + z*C.k
sqrt(r.dot(r))
```




$\displaystyle \sqrt{x^{2} + y^{2} + z^{2}}$



### dot product :


```python
s = a*C.i + b*C.j + c*C.k
r.dot(s)
```




$\displaystyle a x + b y + c z$



#### commutative so s.r = r.s


```python
assert r.dot(s) == s.dot(r) 
```

#### distributive: r.(s+t) == r.s + r.t


```python
t = d*C.i + e*C.j + f*C.k
```


```python
r.dot(s+t)
```




$\displaystyle x \left(a + d\right) + y \left(b + e\right) + z \left(c + f\right)$




```python
r.dot(s) + r.dot(t)
```




$\displaystyle a x + b y + c z + d x + e y + f z$




```python
assert r.dot(s+t).equals(r.dot(s) + r.dot(t))
```


```python
(r-s).dot(r-s)
```




$\displaystyle \left(- a + x\right)^{2} + \left(- b + y\right)^{2} + \left(- c + z\right)^{2}$




```python
from sympy.physics.vector import *
N = ReferenceFrame('N')

```


```python
(a*N.x+b*N.y).magnitude()
```




$\displaystyle \sqrt{a^{2} + b^{2}}$




```python
s=(a*N.x+b*N.y)
r=(x*N.x + y*N.y) 
```


```python
(r.dot(s))
```




$\displaystyle a x + b y$




```python
r.magnitude()*s.magnitude()
```




$\displaystyle \sqrt{a^{2} + b^{2}} \sqrt{x^{2} + y^{2}}$




```python
Tetha = acos((r.dot(s))/(r.magnitude()*s.magnitude()))
```


```python
Tetha
```




$\displaystyle \operatorname{acos}{\left(\frac{a x + b y}{\sqrt{a^{2} + b^{2}} \sqrt{x^{2} + y^{2}}} \right)}$




```python
(r.dot(r))
```




$\displaystyle x^{2} + y^{2}$




```python
(r.dot(-r))
```




$\displaystyle - x^{2} - y^{2}$




```python
 rn = (y*N.x + -x*N.y)  #normal 
```


```python
(r.dot(rn))
```




$\displaystyle 0$



### Projection


```python
i, j, k = C.base_vectors()
v1 = i + j + k
v2 = 3*i + 4*j
v1.projection(v2)
```




$\displaystyle (\frac{7}{3})\mathbf{\hat{i}_{C}} + (\frac{7}{3})\mathbf{\hat{j}_{C}} + (\frac{7}{3})\mathbf{\hat{k}_{C}}$




```python

```


```python
v2.projection(v1)
```




$\displaystyle (\frac{21}{25})\mathbf{\hat{i}_{C}} + (\frac{28}{25})\mathbf{\hat{j}_{C}}$



#### Scalar Projection


```python
v2.projection(v1,scalar=True) == v1.dot(v2)/(v2.magnitude()**2)
```




    True




```python
v2.projection(v1,scalar=True)
```




$\displaystyle \frac{7}{25}$




```python
v1.projection(v2,scalar=True) == v2.dot(v1)/(v1.magnitude()**2)
```




    True




```python
v1.projection(v2,scalar=True)
```




$\displaystyle \frac{7}{3}$



## Changing basis

1)


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


```python
b1 = np.array([1,0,0,0])
b2 = np.array([0,2,-1,0])
b3 = np.array([0,1,2,0])
b4 = np.array([0,0,0,0])
```


```python
v.dot(b1)/(b1.dot(b1))

```




    1.0




```python
v.dot(b2)/(b2.dot(b2))
```




    0.0




```python
v.dot(b3)/(b3.dot(b3))
```




    1.0




```python
v.dot(b3)/(b3.dot(b3))
```




    1.0



## Vector operations assessment


```python
v1 = 2*i + j
c = 3*i -4 *j
#c2 = i-j
#assert c.dot(c2) == 0
assert v1.dot(c)/(c.dot(c)) == c.projection(v1,scalar=True)
c.projection(v1,scalar=True)
```




$\displaystyle \frac{2}{25}$




```python
c2.projection(v1)
```


```python
v = -4*i-3*j+8*k
b1 = i+2*j+3*k
b2 = -2*i+1*j
b3 = -3*i-6*j+5*k
```


```python
b1.projection(v,scalar=True)
```




$\displaystyle 1$




```python
b2.projection(v,scalar=True)
```




$\displaystyle 1$




```python
b3.projection(v,scalar=True)
```




$\displaystyle 1$




```python
x = np.array([3, 2, 4])
v = np.array([-1, 2, -3])
t = 2
```


```python
x_n = x + t * v 
```


```python
x_n
```




    array([ 1,  6, -2])




```python

```

d95a95531823b0a3436fb00c8373679d9f509eae
ipynb
Jupyter Notebook
week2/Module_2.ipynb
FarhadManiCodes/Math_for_ML_Coursera
68f06d7be417d625f60a7257e81242084c0cc7d9
MIT
week2/Module_2.ipynb
FarhadManiCodes/Math_for_ML_Coursera
68f06d7be417d625f60a7257e81242084c0cc7d9
MIT
week2/Module_2.ipynb
FarhadManiCodes/Math_for_ML_Coursera
68f06d7be417d625f60a7257e81242084c0cc7d9
MIT
Qwen/Qwen-72B
1. YES
2. YES