# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/CREVIOS/Python-machine-learning-math/blob/master/ML_Calculus(basic_01).ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="7xW2mT70J8ND" colab_type="code" colab={}
from IPython.display import HTML
def typeset():
  """MathJax initialization for the current cell.
  
  This installs and configures MathJax for the current output.
  """
  display(HTML('''
      <script src="https://www.gstatic.com/external_hosted/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full,Safe&delayStartupUntil=configured"></script>
      <script>
        (() => {
          const mathjax = window.MathJax;
          mathjax.Hub.Config({
          'tex2jax': {
            'inlineMath': [['$', '$'], ['\\(', '\\)']],
            'displayMath': [['$$', '$$'], ['\\[', '\\]']],
            'processEscapes': true,
            'processEnvironments': true,
            'skipTags': ['script', 'noscript', 'style', 'textarea', 'code'],
            'displayAlign': 'center',
          },
          'HTML-CSS': {
            'styles': {'.MathJax_Display': {'margin': 0}},
            'linebreaks': {'automatic': true},
            // Disable to prevent OTF font loading, which aren't part of our
            // distribution.
            'imageFont': null,
          },
          'messageStyle': 'none'
        });
        mathjax.Hub.Configured();
      })();
      </script>
      '''))


# + id="H3cZBkwIK5dB" colab_type="code" colab={}
from IPython.display import HTML, Math
def mview(e):
  display(HTML("<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/"
         "latest.js?config=default'></script>"))
  return Math(latex(e))


# + [markdown] id="egbkkp0pJ73W" colab_type="text"
# Those a

# + id="YfrJSpwqI4LK" colab_type="code" outputId="2019a7ee-0798-48d7-dfcd-626220af0c00" colab={"base_uri": "https://localhost:8080/", "height": 34}
from sympy import *
import numpy as np
import math

x = Symbol('x')
y = Symbol('y')

limit(1/x**2,x,0)

# + id="e8Q7pJDDJG9o" colab_type="code" outputId="8385abb3-6d8c-46b5-ab28-1893eeb061f9" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Find the limit of Functions
from sympy import Limit, Symbol, S

Limit(1/x, x, S.Infinity)

# + id="djVd-DJTJIK9" colab_type="code" outputId="53365c2c-25c2-476f-b26f-cfb1c3bd14dc" colab={"base_uri": "https://localhost:8080/", "height": 34}

# To find the value
l = Limit(1/x, x, S.Infinity)
l.doit()

# + id="hrMdIibKJKo3" colab_type="code" outputId="765df1b4-8f56-4c13-87d8-9639c0dc5d92" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Differentiation
diff(15*x**100-3*x**12+5*x-46)

# + id="N8BDoxrsJNkO" colab_type="code" outputId="65cd92fe-4fde-4a04-bfd0-4e7f8f25be8c" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Constant
diff(99)

# + id="2K6lgN4FJQLv" colab_type="code" outputId="7b8bb6fb-99cc-44d2-86a1-aaea8b4c419c" colab={"base_uri": "https://localhost:8080/", "height": 34}

# Multiplication by Constant
diff(3*x)

# + id="E1HTyxYYJSc1" colab_type="code" outputId="045b9cb0-b3fa-42d1-e199-8b95ac059e7d" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Power Rule
diff(x**3)

# + id="aSbdtpeKJUpt" colab_type="code" outputId="33558206-8642-4d1d-d5d7-b05470bb7017" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Sum Rule
diff(x**2+3*x)

# + id="Sl37toAmJXAt" colab_type="code" outputId="15ea583e-d652-4574-ad8a-128eed476ff8" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Difference Rule
diff(x**2-3*x)

# + id="ILm_ixYxJY84" colab_type="code" outputId="6ff80976-dd45-4a90-a14b-fbfad2e08786" colab={"base_uri": "https://localhost:8080/", "height": 34}

# Product Rule
diff(x**2*x)

# + id="O4BuFjdsJblO" colab_type="code" outputId="f9d96412-f77b-4e64-8260-5037eafb981c" colab={"base_uri": "https://localhost:8080/", "height": 34}
# Chain Rule
diff(ln(x**2))

# + id="w4xS7sdYJeD9" colab_type="code" outputId="b635f0f6-4ba6-42f5-ba51-98d705427c56" colab={"base_uri": "https://localhost:8080/", "height": 34}

# Example
diff(9*(x+x**2))

# + id="8ec1T99ZJiXV" colab_type="code" colab={}
# Matrix Calculus
a = diff(3*x**2*y,x)
b = diff(3*x**2*y,y)
c = diff(2*x+8*y**7,x)
d = diff(2*x+y**8,y)

# + id="rYCsHSwyJjZn" colab_type="code" outputId="1175643c-3090-475a-a46a-97f2385aab33" colab={"base_uri": "https://localhost:8080/", "height": 85}
print(a)
print(b)
print(c)
print(d)

# + id="SQLFNB2IJlzw" colab_type="code" outputId="838cebf5-b18b-454f-a7df-8e03d4a9351b" colab={"base_uri": "https://localhost:8080/", "height": 51}
import numpy as np
Matrix_Calculus = np.matrix([[a, c], [b,d]])
Matrix_Calculus

# + id="rJJ6MpLRJoiV" colab_type="code" outputId="717d1cda-c52d-45d2-e93c-98cee60c4f07" colab={"base_uri": "https://localhost:8080/", "height": 92}

# Chain Rule
mview(diff(ln(sin(x**3)**2)))

# + id="Nlaru9MeJq_d" colab_type="code" outputId="bb878d44-b4cd-4ca5-93c9-0b9644b73e8e" colab={"base_uri": "https://localhost:8080/", "height": 95}

# Sigmoid
# d/dx S(x)=S(x)(1−S(x)) 
mview(diff(1/(1+math.e**-x),x))

# + id="afFjSaSxKfks" colab_type="code" colab={}


# + id="Lbh4oxhfKjHf" colab_type="code" outputId="b2606441-39bd-4848-82e3-ac1c52f0496e" colab={"base_uri": "https://localhost:8080/", "height": 86}
e = Integral(cos(x)**2, (x, 0, pi))
mview(e)

# + id="iiBHKEBBJta8" colab_type="code" outputId="9bcd48bc-878e-4bcf-a585-2af5fa6f3ddb" colab={"base_uri": "https://localhost:8080/", "height": 62}

import sympy
sympy.init_printing()
mview(sympy.integrate(2*x, (x, 1, 0)))

# + id="gst5T1l4Jv29" colab_type="code" outputId="1c1ab6a6-556c-42f6-c3f2-30173270dfc6" colab={"base_uri": "https://localhost:8080/", "height": 113}
# Jacobian
from sympy import sin, cos, Matrix
from sympy.abc import rho, phi

X = Matrix([rho*cos(phi), rho*sin(phi), rho**2])
Y = Matrix([rho, phi])

mview(X.jacobian(Y))

# + id="-iCiGZM9J69-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 65} outputId="17a28cc5-17e7-42ea-8cb1-21fa20adf0a9"
eq = x**2*y + 3/4*x*y + 10
mview(eq)

# + id="zMwUkjM3MOCh" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 113} outputId="51668856-6040-47f4-c777-904a69906b8f"
# Jacobian matrix 
x, y, z = symbols('x y z')
mview(Matrix([sin(x) + y, cos(y) + x, z]).jacobian([x, y, z]))

# + id="PBhYeEwrMR7v" colab_type="code" colab={}


# + id="CiowDvk5MSDY" colab_type="code" colab={}


# + id="X4aDUFptMSGP" colab_type="code" colab={}


# + id="NS1u7sTsMSLc" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 88} outputId="985d6f26-acaa-49bf-cc4e-91cdae6f84ff"
# (x,y)=(0,0)
x1 = -y
x2 = x - 2*y * (2-x**2)
J = sympy.Matrix([x1,x2])
mview(J.jacobian([x,y]))

# + id="ttcvCfzqMSJe" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 87} outputId="f80bdef8-2922-451e-cca9-652ab1bd91d6"
mview(J.jacobian([x,y]).subs([(x,0), (y,0)]))

# + id="7-xvL80-Mf7u" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 62} outputId="7b182cbb-b4bd-4759-a25b-b13febe5446d"
# Derivatives of x
de_x = diff(x**2*y + 3/4*x*y + 10, x)
mview(de_x)

# + id="2TntES2lMlGg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 65} outputId="a1489238-2c16-401b-c2b2-029882e58146"
# Derivatives of y
de_y = diff(x**2*y + 3/4*x*y + 10, y)
mview(de_y)

# + id="_r6J01BnMr1p" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 87} outputId="26c9db65-7740-42a9-9858-408378625097"
# Example
F = sympy.Matrix([de_x,de_y])
mview(F.jacobian([x,y]))

# + id="60OILzjtMvY2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 306} outputId="1dfd8adb-f211-49c7-ff87-0e2473c0d5d1"
# Hessian
from sympy import Function, hessian, pprint
from sympy.abc import x, y
f = Function('f')(x, y)
g1 = Function('g')(x, y)
g2 = x**2+y**2
pprint(hessian(f, (x,y), [g1, g2]))

# + id="73YobGYHM2ZV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="279dfbff-0342-4476-b605-18f14a2706f9"

import numpy as np
import matplotlib.pyplot as plt

def tanh(x):
    return np.tanh(x)

X = np.linspace(-5, 5, 100)
plt.plot(X, tanh(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Neural Networks - Activation Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\tanh{(x)}}$', fontsize=16)
plt.show()


# + id="xBPfz6N-M7K9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="0125ea24-a08f-40b6-9cc4-8c27e611ab1d"
def sigma(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

X = np.linspace(-5, 5, 100)
plt.plot(X, sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}$', fontsize=16)
plt.show()


# + id="PrvDXGM2NAAN" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="b4c3fa09-b2cb-44f1-89d6-2a69b050d9e2"
def derivatives_sigma(x):
    return 1 / (np.cosh(x))**2

X = np.linspace(-5, 5, 100)
plt.plot(X, derivatives_sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{1}{cosh^2(x)}$', fontsize=16)
plt.show()


# + id="vZ1ptEfsNDa2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="4c888df4-8751-4f10-ef51-e86edf818f1c"

def derivatives_sigma(x):
    return 4 / (np.exp(x)+np.exp(-x))**2

X = np.linspace(-5, 5, 100)
plt.plot(X, derivatives_sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{4}{(e^{x}+e^{-x})^2}$', fontsize=16)
plt.show()

# + id="OrV1pRWtNETe" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 86} outputId="0303cfd4-cfcf-49af-ba9d-552173dc35ca"
# Newton_Raphson
# f(x) - the function of the polynomial
from sympy import *

def f(x):
    function = x**3 - x - 1
    return function

def derivative(x): #function to find the derivative of the polynomial
    derivative = diff(f(x), x)
    return derivative

def Newton_Raphson(x):
	return (x - (f(x) / derivative(x)))

mview(Newton_Raphson(x))

# + id="4468V9d9NF8m" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 89} outputId="d1553bd0-4152-443b-a265-fcd137dea856"
# Advanced Chain Rule
# F(y)=ln(1−5y2+y3)
from sympy import *

y = symbols('y')
F = symbols('F', cls=Function)


mview(Derivative(ln(1 - 5*y**2 + y**3)).doit())

# + id="y90JljphNPWT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 89} outputId="688c454d-5a4e-4a29-ed3e-c2ccb7cab1d9"
mview(diff(ln(1 - 5*y**2 + y**3)))

StarcoderdataJupyterScriptsDedupFiltered