Basic Math


```python
import math
```


```python
1 + 1 # Addition
```




    2




```python
2 - 1 # Subtraction
```




    1




```python
8 - 9
```




    -1




```python
2 * 8 # Multiplication
```




    16




```python
8 / 4 # Division 
```




    2.0




```python
20 // 6 # Division, removed devimal values
```




    3




```python
20 % 6 # Shows remainder for division
```




    2




```python
3  ** 2 # ** means raised to the power
```




    9




```python
16 ** (1/2) # Squared root  
```




    4.0




```python
8 ** (1/3) # Squared cube
```




    2.0




```python
abs(-8) # Absolute Value
```




    8




```python
7%3 # 7 mod 3 - modulo
```




    1




```python
# round decimal point to 2 places
round(3.1468, 2)
```




    3.15




```python
# Logarithmic of x to base b
# log(x, b)
math.log(64, 2)
```




    6.0



PEMDAS - Parentheses, Exponents, Multiply, Divide, Add, Subtract


```python
3 + 3 * 3
```




    12




```python
(8 - 5) * 2
```




    6




```python
12 - 8 + 4 * 2 - (6 / 3)
```




    10.0



Fractions


```python
from fractions import Fraction
# Fraction(numerator, denominator)

d = (0.25, 0.5, 0.75, 1.25)
for the_decimal in d:
    print(Fraction(the_decimal))
```

    1/4
    1/2
    3/4
    5/4



```python
from decimal import Decimal

f = (1/4, 1/2, 3/4, 7/2)
for the_fraction in f:
    print(Decimal(the_fraction))
```

    0.25
    0.5
    0.75
    3.5



```python
Fraction(1,7) + Fraction(2,7) 
```




    Fraction(3, 7)




```python
Fraction(1/2) + Fraction(1/4) 
```




    Fraction(3, 4)




```python
Fraction(1/2)  + 6 * 3
```




    Fraction(37, 2)




```python
Fraction(1/2) * Fraction(1/4) 
```




    Fraction(1, 8)



Complex Numbers


```python
# For complex number use "j" instead of "i"
# Example complex number is "6 + 3i";however, in Python as "6 + 3j"
a = 4 + 2i # Does not work
type(a)
```


```python
a = 4 + 2j
type(a)
```




    complex




```python
a = complex(4, 8)
print("Complex Number:", a)
```

    Complex Number: (4+8j)



```python
b = 2 + 2j
c = 3 + 3j 
```


```python
a + b
```




    (6+10j)




```python
a - b
```




    (2+6j)




```python
a * b
```




    (-8+24j)




```python
a / b
```




    (3+1j)




```python
# Show which number is real and imaginary numbers
print("Real number:", a.real)
print("Imaginary number:", a.imag)
```

    Real number: 4.0
    Imaginary number: 8.0



```python
from sympy import *

x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
```


```python
a = x*y + x*y
a
```




    2*x*y




```python
c = (x + 6)*(x + 4)
c
```




    (x + 4)*(x + 6)




```python
# Expand the factor
expand(c)
```




    x**2 + 10*x + 24




```python
d = (x+y)**2
expand(d)
```




    x**2 + 2*x*y + y**2




```python
e = (x-y)**2
expand(e)
```




    x**2 - 2*x*y + y**2




```python
g = (x+y)**3
expand(g)
```




    x**3 + 3*x**2*y + 3*x*y**2 + y**3




```python
from sympy import pprint # Shows math expression
pprint(g)
```

           3
    (x + y) 



```python
u = 3*x**3 + 6*x*y - 3*y**3
pprint(u)
```

       3              3
    3⋅x  + 6⋅x⋅y - 3⋅y 



```python
from math import sqrt
import cmath # has complex number
# Quandratic Formula
# https://en.wikipedia.org/wiki/Quadratic_formula
# General Equation ax^2 + bx + c = 0
# x = -b + sqrt(b**2 - 4ac) / 2a
# x = -b - sqrt(b**2 - 4ac) / 2a
a = 2
b = 4
c = 8

# Check if the discriminant is positive, negative, or zero
d = b**2-4*a*c # discriminant

if d < 0:
    print("No Solutions!") # It has imaginary number
elif d == 0:
    x = -b / (2*a)
    print("One solution:", x) # Same solutions
else:
    x1 = (-b+sqrt((b**2)-(4*(a*c))))/(2*a)
    x2 = (-b-sqrt((b**2)-(4*(a*c))))/(2*a)
    print("Two solutions: ", x1, " and", x2)
```

    No Solutions!



```python
a = 9
b = 12
c = 4
d = b**2-4*a*c # discriminant

if d < 0:
    print("No Solutions!")
elif d == 0:
    x = -b / (2*a)
    print("One solution:", x)
else:
    x1 = (-b+sqrt((b**2)-(4*(a*c))))/(2*a)
    x2 = (-b-sqrt((b**2)-(4*(a*c))))/(2*a)
    print("Two solutions: ", x1, " and", x2)
```

    One solution: -0.6666666666666666



```python
a = 1
b = 4
c = 3
d = b**2-4*a*c # discriminant

if d < 0:
    print("No Solutions!")
elif d == 0:
    x = -b / (2*a)
    print("One solution:", x)
else:
    x1 = (-b+sqrt((b**2)-(4*(a*c))))/(2*a)
    x2 = (-b-sqrt((b**2)-(4*(a*c))))/(2*a)
    print("Two solutions: ", x1, " and", x2)
```

    Two solutions:  -1.0  and -3.0



```python
# This shows real and imaginary numbers

a = 3
b = 4
c = 2
d = b**2-4*a*c # discriminant

if d < 0:
    x1=(-b+cmath.sqrt(d))/(2*a)
    x2=(-b-cmath.sqrt(d))/(2*a)
    print("Complex Number")
    print("x1=",x1," and x2=",x2)
elif d > 0:
    x1=(-b+sqrt(d))/(2*a)
    x2=(-b-sqrt(d))/(2*a)
    print("Real Number")
    print("x1=",x1," and x2=",x2)
elif d==0:
    x=-b/(2*a)
    print("Equal to Zero" )
    print("x=",x)
else: 
    print("This is NOT Quadratic Equation!")
```

    Complex Number
    x1= (-0.6666666666666666+0.47140452079103173j)  and x2= (-0.6666666666666666-0.47140452079103173j)



```python

a = 3
b = 4
c = 2
d = b**2-4*a*c # discriminant

if d < 0: 
    x1=(-b+cmath.sqrt(d))/(2*a)
    x2=(-b-cmath.sqrt(d))/(2*a)
    print("Complex Number")
    print("x1=",x1.real, x1.imag,"and x2=",x2.real, x2.imag)
elif d > 0:
    x1=(-b+sqrt(d))/(2*a)
    x2=(-b-sqrt(d))/(2*a)
    print("Real Number")
    print("x1=",x1," and x2=",x2)
elif d==0:
    x=-b/(2*a)
    print("Equal to Zero" )
    print("x=",x)
else: 
    print("This is NOT Quadratic Equation!")
```

    Complex Number
    x1= -0.6666666666666666 0.47140452079103173 and x2= -0.6666666666666666 -0.47140452079103173


Graphing


```python
import matplotlib.pyplot as plt
```


```python
y = [1,2,3,4,5,6]   # List of numbers
plt.plot(y)         # Plot graph
plt.show()          # Show the plot
```


```python
# Parabola
a = []
b = []
for x in range(-20,20,1):
    y=x**2
    a.append(x)
    b.append(y)

plt.plot(a,b)
plt.show()
```


```python
# Trignometry 
import math

def cos(x):
    return cos(radians(x))
def sin(x):
    return sin(radians(x))
def tan(x):
    return tan(radians(x))
def arc_tan(x):
    return math.degrees(atan(x))
def arc_sine(x):
    return math.degrees(asin(x))
def arc_cos(x):
    return math.degrees(acos(x))

print("Radians")
print(math.cos(180))
print(math.sin(90))
print(math.tan(45))
print("Degrees")
print(arc_tan(1))
print(arc_sine(1))
print(arc_cos(1))
```

    Radians
    -0.5984600690578581
    0.8939966636005579
    1.6197751905438615
    Degrees
    45.0
    90.0
    0.0


abd6e5b6520e44b685aafc2bdbfa67bb37af8ff4
ipynb
Jupyter Notebook
BasicMath.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
2019-02-05T04:35:23.000Z
2022-03-21T19:05:15.000Z
BasicMath.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
BasicMath.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