<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline
escalar = 5.679
escalar
vector = np.array([1,2,3])
print(vector)
matrix = np.array([[1,2],[3,4]])
matrix
tensor = np.array(([
        [[1,2,3],[4,5,6],[6,7,8]],
        [[11,12,13],[14,15,16],[17,18,19]],
        [[21,22,23],[24,25,26],[27,28,29]],
        ]))
%matplotlib inline
plt.imshow(tensor, interpolation='nearest')
plt.show()<jupyter_output><empty_output><jupyter_text>Vectors, matrixes and tensors<jupyter_code>vector.shape
matrix.shape
tensor.shape
tensor.size<jupyter_output><empty_output><jupyter_text>Transposition and Sum<jupyter_code>vector_t = vector.T
vector_t
matrix_t = matrix.T
matrix_t
tensor_t = tensor.T
tensor_t
tensor
a = matrix
b = matrix_t
c = a + b
c
D = matrix + escalar
D
escalar = 5.679
vector = np.array([2,3])
matrix = np.array([[1,2],[3,4],[5,6]])<jupyter_output><empty_output><jupyter_text>Operation with Matrixes<jupyter_code>A = matrix * vector
B = matrix.dot(vector)
A
B
C = A.dot(B)
D = B.dot(A)
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
B = np.array([[2,3],[5,7],[11,13]])
C = A.dot(B)
C
A = np.array([[2,3],[5,7],[11,13]])
B = np.array([[1,3],[2,1]])
C = np.array([[3,1],[4,2]])
# Associative
ABC = A.dot(B.dot(C))
ABC
AB_C = A.dot(B).dot(C)
AB_C
# Distributive

D = A.dot(B+C)
E = (A.dot(B))+(A.dot(C))
print(D)
print(E)
D == E
# Commutative

F = B.dot(C)
G = C.dot(B)
F == G
print(F)
print(G)
A
B
AB_t = A.dot(B).T
B_tA_T = B.T.dot(A.T)
print(AB_t == B_tA_T)
AB_t<jupyter_output><empty_output><jupyter_text>Equations with Matrixes<jupyter_code>x = np.arange(-5,5)
y_1 = 3*x + 5
y_2 = 2*x + 3
plt.figure()

plt.plot(x, y_1)
plt.plot(x, y_2)

plt.xlim(-5,5)
plt.ylim(-5,5)

plt.axvline(x=0, color="grey")
plt.axhline(y=0, color="grey")
A = np.array([[-3, 1], [-2,1]])
A
B = np.array([[5],[3]])
B
sol_1 = np.array([-2, -1])
sol_1
print(A.dot(sol_1))
id = np.eye(4)
id
vector = np.array([[2],[3],[5],[7]])
vector
print(id.dot(vector))
np.set_printoptions(suppress=True)
A = np.array([[3,1],[2,1]])
print(A)
B = np.array([[1],[1]])
inv_A = np.linalg.inv(A)
inv_A
x = inv_A.dot(B)
x
sol_2 = inv_A.dot(np.array([[3],[7]]))
sol_2
print(A.dot(sol_2))<jupyter_output>[[3.]
 [7.]]<jupyter_text>Linear Equations Systems<jupyter_code># Equations to assess:
# y = 3x + 5
# y = -1x + 3
# y = 2x + 1
%matplotlib inline
x = np.arange(-6, 6)

y_1 = 3*x+5
y_2 = -x+3
y_3 = 2*x+1
plt.figure()
plt.plot(x, y_1)
plt.plot(x, y_2)
plt.plot(x, y_3)

plt.xlim(-8,8)
plt.ylim(-8,8)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")
plt.show()
x = np.arange(-6,6)

y_2 = -1*x+3
y_3 = 2*x+1
plt.figure()
plt.plot(x, y_2)
plt.plot(x, y_3)

plt.xlim(-8,8)
plt.ylim(-8,8)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")
plt.show()
x = np.arange(-6,6)

y_3 = 2*x+1
plt.figure()
plt.plot(x, y_3)

plt.xlim(-8,8)
plt.ylim(-8,8)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")
plt.show()<jupyter_output><empty_output><jupyter_text>**Over-determined solutions**: more equations than variables **Determined solutions**: two equations **Indetermined**: one equations and +2 variables<jupyter_code>v1 = np.array([2,5])
v2 = np.array([3,2])
def graficarVectores(vecs, cols, alpha = 1):

  plt.figure()
  plt.axvline(x = 0, color = "grey", zorder = 0)
  plt.axhline(y = 0, color = "grey", zorder = 0)

  for i in range(len(vecs)):
    x = np.concatenate([[0,0], vecs[i]])
    plt.quiver([x[0]],
               [x[1]],
               [x[2]],
               [x[3]],
               angles="xy", scale_units="xy", scale=1,
               color=cols[i],
               alpha = alpha)
graficarVectores([v1, v2], ["orange", "blue"])
plt.xlim(-1, 8)
plt.ylim(-1, 8)
# Linear Combination: Multiply a vector for a esclarar
## V1 x 2 == V1V2
## V2 x 3
v1 = np.array([2,5])
v2 = np.array([3,2])
v1v2 = 2*v1+v2
v1v2
graficarVectores([v1, v2, v1v2], ["orange", "blue", "red"])
plt.xlim(-1,8)
plt.ylim(-1,12)
for a in range(-10,10):
  for b in range(-10,10):
    plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a+v2[1]*b,
                marker = ".",
                color = "orange")
    
plt.xlim(-100,100)
plt.ylim(-100,100)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")

plt.show()
v1 = np.array([1, 1])
v2 = np.array([-1, -1])
for a in range(-10,10):
  for b in range(-10,10):
    plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a+v2[1]*b,
                marker = ".",
                color = "orange")
    
plt.xlim(-25,25)
plt.ylim(-25,25)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")

plt.show()
v1 = np.array([1,0])
v2 = np.array([2,-3])
for a in range(-10,10):
  for b in range(-10,10):
    plt.scatter(v1[0]*a + v2[0]*b, v1[1]*a+v2[1]*b,
                marker = ".",
                color = "orange")
    
plt.xlim(-25,25)
plt.ylim(-25,25)

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")

plt.show()
from mpl_toolkits.mplot3d import Axes3D
v1 = np.array([1,0,0])
v2 = np.array([2,-3,0])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for a in range(-10,10):
  for b in range(-10,10):
    ax.scatter(v1[0]*a + v2[0]*b, v1[1]*a+v2[1]*b, 
               v1[2]*a + v2[2]*b, 
                marker = ".",
                color = "orange")
    
ax.set_xlabel("X Exe")
ax.set_ylabel("Y Exe")
ax.set_zlabel("Z Exe")

plt.axvline(x=0, color = "grey")
plt.axhline(y=0, color = "grey")

plt.show()
v1 = np.array([1,1])
v2 = np.array([-1,-1])
for a in range(-10,10):
  for b in range (-10,10):
    plt.scatter(v1[0]*a+v2[0]*b, v1[1]*a+v2[1]*b,
                marker = ".",
                color = "orange")
    
  plt.xlim(-25,25)
  plt.ylim(-25,25)

  plt.axvline(x=0, color="grey")
  plt.axhline(y=0, color="grey")

  plt.show()
A = np.array (
    [
    [0,1,0,0],
    [0,0,1,0],
    [0,1,1,0],
    [1,0,0,1]]
)
lambdas, V = np.linalg.eig(A.T)

print(A[lambdas == 0, :])<jupyter_output>[[0 1 1 0]]<jupyter_text>Norms Norms are the method by which we can measure a vector. This should be =< 0. We can use them to identify any mistakes while creating classifications.The most common norm is the one that measures a vector from point 0.A norm A is = 0 only if B = 0A triangular norm is greater than norm(v1) and norm(v2).A norm * escalar = abs(a) x norm(a)<jupyter_code>v1 = np.array([2,7])
v2 = np.array([3,5])

print(v1)
print(v2)
v1v2 = v1+v2
v1v2
np.linalg.norm(v1v2)
# Norm(v1v2) <= norm (v1) + norm (v2)

norm_v1 = np.linalg.norm(v1)
norm_v2 = np.linalg.norm(v2)
norm_v1v2 = np.linalg.norm(v1v2)

print(norm_v1v2 <= (norm_v1 + norm_v2))
norm_v1 + norm_v2
v1 = np.array([0,0,2,7])
v2 = np.array([0,0,3,5])

v1_aux = np.array([v1[2],v1[3],v2[2],v2[3]])
v1v2 = np.array([0,0,5,12])
plt.quiver([v1[0], v1_aux[0], v1v2[0]],
           [v1[1], v1_aux[1], v1v2[1]],
           [v1[2], v1_aux[2], v1v2[2]],
           [v1[3], v1_aux[3], v1v2[3]],
           angles = "xy", scale_units = "xy", scale = 1,
           color = sns.color_palette()
           )

plt.xlim(-0.5, 6)
plt.ylim(-0.5, 15)
## Norm Typs

vector = np.array([1,2,0,5,6,0])

print(np.linalg.norm(vector, ord=0))
vector = np.array([1,-1,1,-1,1])

print(np.linalg.norm(vector, ord=1))
vector = np.array([1,1])
print(np.linalg.norm(vector))
print(np.linalg.norm(vector, ord=2))
vector = np.array([1,2,3,4,5,6])
vector
print(np.linalg.norm(vector, ord=2)**2)
print(vector.T.dot(vector))
vector = np.array([1,2,3,-100])
print(np.linalg.norm(vector,ord=np.inf))
## Internal Product

v1 = np.array([0,0,0,3])
v2 = np.array([0,0,3,3])

plt.xlim(-2,6)
plt.ylim(-2,6)

plt.quiver([v1[0], v2[0]],
           [v1[1], v2[1]],
           [v1[2], v2[2]],
           [v1[3], v2[3]],
           angles = "xy", scale_units = "xy", scale = 1,
           color = sns.color_palette()
           )
v1 = np.array([0,3])
v2 = np.array([3,3])

print(v1.T.dot(v2))
norm_v1 = np.linalg.norm(v1)
norm_v2 = np.linalg.norm(v2)

print(norm_v1 * norm_v2 * np.cos(np.deg2rad(45)))<jupyter_output>9.0<jupyter_text>Special Vectors and Matrixes<jupyter_code>vector = np.array([1,2,3,4,5])

matrix = np.diag(vector)

matrix
matrix[0:4,0:3]
matrix[0:3,0:4]
A = np.diag([2,3,4,5])
print(A)
v1 = np.array([[1,1,1,1]])

print(v1)

print(A.dot(v1.T))
A_inv = np.diag([1/2,1/3,1/4,1/5])
A_inv
id = A.dot(A_inv)

id
A_inv_calc = np.linalg.inv(A)
A_inv_calc
A.T
A
symetric = np.array([[1,2,3],
                     [2,-1,7],
                     [3,7,11]])
symetric
symetric.T
## Orthogonal Vectors

x = np.array([0,0,2,2])
y = np.array([0,0,2,-2])

plt.quiver([x[0], y[0]],
           [x[1], y[1]],
           [x[2], y[2]],
           [x[3], y[3]],
           angles = "xy", scale_units = "xy", scale = 1,
           )
plt.xlim(-2,4)
plt.ylim(-3,3)

plt.show()
v1 = np.array([[2,2]])
v2 = np.array([[2,-2]])

print(v1)
print(v2)
v1.dot(v2.T)
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
v1 = np.array([[1,0]])
v2 = np.array([[0,-1]])
v1.dot(v2.T)
print(np.linalg.norm(v1))
print(np.linalg.norm(v2))
## Orthogonal Matrixes Properties

matrix = np.array([[1,0,0],
                   [0,1,0],
                   [0,0,1]
                   ])
matrix
print(matrix[:,0].dot(matrix[:,1]))
print(matrix[:,0].dot(matrix[:,2]))
print(matrix[:,1].dot(matrix[:,2]))
print(np.linalg.norm(matrix[:,0]))
print(np.linalg.norm(matrix[:,1]))
print(np.linalg.norm(matrix[:,2]))
print(matrix[0,:].dot(matrix[1,:]))
print(matrix[0,:].dot(matrix[2,:]))
print(matrix[1,:].dot(matrix[2,:]))
print(np.linalg.norm(matrix[0,:]))
print(np.linalg.norm(matrix[1,:]))
print(np.linalg.norm(matrix[2,:]))
A = np.array([[np.cos(100), -np.sin(100)],
              [np.sin(100), np.cos(100)]])
print(A)
print(np.linalg.norm(A[0,:]))
print(np.linalg.norm(A[1,:]))
print(np.linalg.norm(A[:,0]))
print(np.linalg.norm(A[:,1]))
print(A[0,:].dot(A[1,:]))
print(A[:,0].dot(A[:,1]))
A_t = A.T
print(A_t.dot(A))
print(A.dot(A_t))
A_inv = np.linalg.inv(A)
print(A_inv)
A_t
print(1/A_t.dot(A))<jupyter_output>[[ 1.00000000e+00 -1.25980837e+17]
 [-1.25980837e+17  1.00000000e+00]]<jupyter_text>Determinant and Tracing<jupyter_code>matrix = np.array([[1,2,3],
                   [4,5,6],
                   [7,8,9]
                   ])
print(matrix)
trace = np.trace(matrix)
print(trace)
v1 = np.array([0,1])
v2 = np.array([1,0])

graficarVectores([v1,v2], ["blue","red"])

plt.xlim(-0.15, 1.5)
plt.ylim(-0.15, 1.5)

plt.show()
A = np.array([[2,0],[0,2]])
print(A)
v1_transformed = A.dot(v1)
v2_transformed = A.dot(v2)

graficarVectores([v1_transformed, v2_transformed], ["orange", "green"])

plt.xlim(-0.25, 3)
plt.ylim(-0.25, 3)

plt.show()
det_A = np.linalg.det(A)

print(det_A)
transformed_area = abs((v1_transformed[0]-v2_transformed[0]))*abs((v1_transformed[1]-v2_transformed[1]))
print(transformed_area)
B = A * [-1,1]
print(B)
det_B = np.linalg.det(B)
print(det_B)
v1 = np.array([0,1])
v2 = np.array([1,0])

v1_transformed = B.dot(v1)
v2_transformed = B.dot(v2)

graficarVectores([v1_transformed, v2_transformed, v1, v2], ["blue", "red", "orange", "green"])

plt.xlim(-3, 1.5)
plt.ylim(-0.5, 2.5)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.23381197507450555, 'fraction_numerical': 0.0791113519371444, 'mean_word_length': 3.730457069628364, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '251681', 'license': 'MIT', 'n_tokens_mistral': 5682, 'n_tokens_neox': 5354, 'n_words': 877, 'path': 'Linear_Algebra.ipynb', 'repo_name': 'XavierCarrera/linear_algebra_python', 'size': '11073'}