# Solving some system of equations


```python
import numpy as np
```


```python
# Solving following system of linear equation
# 3x + 2y = 7
# 2x + 3y = 9

x = np.array([[3, 2],[2,3]])
y = np.array([7, 8])

print(np.linalg.solve(x,y))
```

    [ 1.  2.]



```python
# Solving following system of linear equation
# 9x - 17y = -20
# -13x + 7y = -94

x = np.array([[9, -17],[-13,7]])
y = np.array([-20, -94])

print(np.linalg.solve(x,y))
```

    [ 11.   7.]



```python
# 5x - 2y = -13
# 4x + 5y = -6

x = np.array([[5, -2],[4,5]])
y = np.array([-13, -6])

print(np.linalg.solve(x,y))
```

    [-2.33333333  0.66666667]



```python
# 5x + 7y = 11
# 20x - 18y = 39

x = np.array([[5, 7],[20,-18]])
y = np.array([11, 39])

print(np.linalg.solve(x,y))
```

    [ 2.04782609  0.10869565]



```python
# 3x - 2y + z = 7
# x + y + z = 2
# 3x - 2y - z = 3 

x = np.array([[3, -2, 1],[1, 1, 1],[3, -2, -1]])
y = np.array([7, 2, 3])

print(np.linalg.solve(x,y))
```

    [ 1. -1.  2.]



```python
# 5x - 2y = -13
# 4x + 5y = -6

from sympy import *

x, y = symbols(['x', 'y'])
system = [Eq(5*x - 2*y, -7),
          Eq(4*x + 5*y, -8)]

solutions = solve(system, [x, y])
print(solutions)
```

    {x: -17/11, y: -4/11}



```python
# 3x - 2y + z = 7
# x + y + z = 2
# 3x - 2y - z = 3 

from sympy import *

x, y, z = symbols(['x', 'y', 'z'])
system = [Eq(3*x - 2*y + z, 7),
          Eq(x + y + z, 2),
          Eq(3*x - 2*y - z, 3)]

solutions = solve(system, [x, y, z])
print(solutions)
```

    {x: 1, y: -1, z: 2}



```python
x = np.array([[3, -2, 1],[1, 1, 1],[3, -2, -1]])
y = np.array([7, 2, 3])

# linalg.solve is the function of NumPy to solve a system of linear scalar equations 
print("Solutions:\n", np.linalg.solve(x, y))
```

    Solutions:
     [ 1. -1.  2.]


## LU decomposition with SciPy


```python
# LU decomposition with SciPy
import scipy.linalg as linalg # Package for LU decomposition

x = np.array([[3, -2, 1],[1, 1, 1],[3, -2, -1]])
y = np.array([7, 2, 3])


LU = linalg.lu_factor(x)
x = linalg.lu_solve(LU, y) 
print("Solutions:\n",x) 
```

    Solutions:
     [ 1. -1.  2.]



```python
import scipy
x = scipy.array([[3, -2, 1],[1, 1, 1],[3, -2, -1]])
P, L, U = scipy.linalg.lu(x)
```


```python
print("x:\n", x)
print("-"*50)
print("P:\n", P)
print("-"*50)
print("L:\n", L)
print("-"*50)
print("U:\n", U)
```

    x:
     [[ 3 -2  1]
     [ 1  1  1]
     [ 3 -2 -1]]
    --------------------------------------------------
    P:
     [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]]
    --------------------------------------------------
    L:
     [[ 1.          0.          0.        ]
     [ 0.33333333  1.          0.        ]
     [ 1.          0.          1.        ]]
    --------------------------------------------------
    U:
     [[ 3.         -2.          1.        ]
     [ 0.          1.66666667  0.66666667]
     [ 0.          0.         -2.        ]]


## Euclidean 


```python
from scipy.spatial import distance
a = (1, 2, 3)
b = (4, 5, 6)
euc_dist = distance.euclidean(a, b)

print("Euclidean Distance:", euc_dist)
```

    Euclidean Distance: 5.19615242271


## Hadamard Product


```python
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
hp = np.multiply(a,b)

print("Hadamard Product:\n", hp)
```

    Hadamard Product:
     [[ 5 12]
     [21 32]]



```python
# Another method
a * b 
```




    array([[ 5, 12],
           [21, 32]])




```python
# Another method
np.multiply(a,b)
```




    array([[ 5, 12],
           [21, 32]])



## Dot Product


```python
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

dp = x @ y
print('Dot Product:\n', dp)
```

    Dot Product:
     [[19 22]
     [43 50]]



```python
# Another Method
np.dot(x,y)
```




    array([[19, 22],
           [43, 50]])



##  Dot product of vectors

Finding the product of the summation of two vectors and the output will be a single vector.


```python
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
dotproduct = sum(i*j for i,j in zip(x,y))

print('Dot product is : ' , dotproduct)
```

    Dot product is :  [26 44]



```python
x = [1,2,3,4]
y = [5,6,7,8]
dotproduct = sum(i*j for i,j in zip(x,y))

print('Dot product is : ' , dotproduct)
```

    Dot product is :  70


## Inner Product


```python
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

ip = np.inner(x,y)
print('Inner Product:\n', ip)
```

    Inner Product:
     [[17 23]
     [39 53]]


0fa8b2ea21e7033cf9b0a95f5b1e289d183f395c
ipynb
Jupyter Notebook
Mathematics_for_Machine_Learning_Linear_Algebra.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
2019-02-05T04:35:23.000Z
2022-03-21T19:05:15.000Z
Mathematics_for_Machine_Learning_Linear_Algebra.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
Mathematics_for_Machine_Learning_Linear_Algebra.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
2019-08-20T14:50:07.000Z
2022-03-19T19:24:02.000Z
Qwen/Qwen-72B
1. YES
2. YES