# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: math_for_ML
#     language: python
#     name: math_for_ml
# ---

# # Mathematics for Machine Learning: Linear Algebra
# ## Week2
# ## Module 2:
#
# ### Modulus & inner product (vectors)

# #### usefull pakages

# +
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:

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

# ### dot product :

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

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

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

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

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

r.dot(s+t)

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

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

(r-s).dot(r-s)

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


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

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

(r.dot(s))

r.magnitude()*s.magnitude()

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

Tetha

(r.dot(r))

(r.dot(-r))

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

(r.dot(rn))

# ### Projection

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



v2.projection(v1)

# #### Scalar Projection

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

v2.projection(v1,scalar=True)

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

v1.projection(v2,scalar=True)

# ## Changing basis

# 1)

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

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])

v.dot(b1)/(b1.dot(b1))


v.dot(b2)/(b2.dot(b2))

v.dot(b3)/(b3.dot(b3))

v.dot(b3)/(b3.dot(b3))

# ## Vector operations assessment

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)

c2.projection(v1)

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

b1.projection(v,scalar=True)

b2.projection(v,scalar=True)

b3.projection(v,scalar=True)

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

x_n = x + t * v 

x_n



StarcoderdataJupyterScriptsDedupFiltered