lagrange-multipliers.ipynb

In [1]:
from sympy import symbols, Function, Eq, Derivative, Rational, UnevaluatedExpr, solve
import xv.km
In [2]:
x, y, z, lamda = symbols('x, y, z, lamda')
k = symbols('k', constant = True)
f = Function('f')
F = Function('F')
g = Function('g')
In [3]:
eq1 = Eq(F(x, y, z, lamda), f(x, y, z) + lamda*(g(x, y, z) - k))
eq1
Out[3]:
$\displaystyle F{\left(x,y,z,\lambda \right)} = \lambda \left(- k + g{\left(x,y,z \right)}\right) + f{\left(x,y,z \right)}$
In [4]:
F_x = Eq(Eq(Derivative(eq1.lhs, x), Derivative(eq1.rhs, x)), 0, evaluate = False)
F_x
Out[4]:
$\displaystyle \frac{\partial}{\partial x} F{\left(x,y,z,\lambda \right)} = \frac{\partial}{\partial x} \left(\lambda \left(- k + g{\left(x,y,z \right)}\right) + f{\left(x,y,z \right)}\right) = 0$
In [5]:
F_y = Eq(Eq(Derivative(eq1.lhs, y), Derivative(eq1.rhs, y)), 0, evaluate = False)
F_y
Out[5]:
$\displaystyle \frac{\partial}{\partial y} F{\left(x,y,z,\lambda \right)} = \frac{\partial}{\partial y} \left(\lambda \left(- k + g{\left(x,y,z \right)}\right) + f{\left(x,y,z \right)}\right) = 0$
In [6]:
F_z = Eq(Eq(Derivative(eq1.lhs, z), Derivative(eq1.rhs, z)), 0, evaluate = False)
F_z
Out[6]:
$\displaystyle \frac{\partial}{\partial z} F{\left(x,y,z,\lambda \right)} = \frac{\partial}{\partial z} \left(\lambda \left(- k + g{\left(x,y,z \right)}\right) + f{\left(x,y,z \right)}\right) = 0$
In [7]:
F_lamda = Eq(Eq(Derivative(eq1.lhs, lamda), Derivative(eq1.rhs, lamda)), 0, evaluate = False)
F_lamda
Out[7]:
$\displaystyle \frac{\partial}{\partial \lambda} F{\left(x,y,z,\lambda \right)} = \frac{\partial}{\partial \lambda} \left(\lambda \left(- k + g{\left(x,y,z \right)}\right) + f{\left(x,y,z \right)}\right) = 0$

Example

Let x, y be real numbers. Find the maximum value of x + y, such that

In [8]:
eq2 = Eq(x**3 + y**3 + UnevaluatedExpr(x+y)/4 - Rational(15, 2), 0)
eq2
Out[8]:
$\displaystyle x^{3} + y^{3} - \frac{15}{2} + \frac{x + y}{4} = 0$
In [9]:
eq3 = Eq(F(x, y, lamda), f(x, y) + lamda*(g(x, y) - k))
eq3
Out[9]:
$\displaystyle F{\left(x,y,\lambda \right)} = \lambda \left(- k + g{\left(x,y \right)}\right) + f{\left(x,y \right)}$
In [10]:
f_eq = Eq(f(x, y), x + y)
f_eq
Out[10]:
$\displaystyle f{\left(x,y \right)} = x + y$
In [11]:
g_eq = Eq(g(x, y), eq2.lhs)
g_eq
Out[11]:
$\displaystyle g{\left(x,y \right)} = x^{3} + y^{3} - \frac{15}{2} + \frac{x + y}{4}$
In [12]:
eq4 = eq3.subs({f_eq.lhs: f_eq.rhs, g_eq.lhs: g_eq.rhs, k: 0})
eq4
Out[12]:
$\displaystyle F{\left(x,y,\lambda \right)} = \lambda \left(x^{3} + y^{3} - \frac{15}{2} + \frac{x + y}{4}\right) + x + y$
In [13]:
F_x = Eq(Derivative(eq4.rhs, x), 0, evaluate = False).doit()
F_x
Out[13]:
$\displaystyle \lambda \left(3 x^{2} + \frac{1}{4}\right) + 1 = 0$
In [14]:
F_y = Eq(Derivative(eq4.rhs, y), 0, evaluate = False).doit()
F_y
Out[14]:
$\displaystyle \lambda \left(3 y^{2} + \frac{1}{4}\right) + 1 = 0$
In [15]:
F_lamda = Eq(Derivative(eq4.rhs, lamda), 0, evaluate = False).doit()
F_lamda
Out[15]:
$\displaystyle x^{3} + \frac{x}{4} + y^{3} + \frac{y}{4} - \frac{15}{2} = 0$
In [16]:
solns = solve((F_x, F_y, F_lamda), x, y, lamda, dict = True)
solns
Out[16]:
[{x: 3/2, y: 3/2, lamda: -1/7},
 {x: -3/4 + sqrt(31)*I/4,
  y: -3/4 + sqrt(31)*I/4,
  lamda: 1/14 - 9*sqrt(31)*I/434},
 {x: -3/4 - sqrt(31)*I/4,
  y: -3/4 - sqrt(31)*I/4,
  lamda: 1/14 + 9*sqrt(31)*I/434}]
In [17]:
for sol in solns:
    for var, val in sol.items():
        display(Eq(var, val))
$\displaystyle x = \frac{3}{2}$
$\displaystyle y = \frac{3}{2}$
$\displaystyle \lambda = - \frac{1}{7}$
$\displaystyle x = - \frac{3}{4} + \frac{\sqrt{31} i}{4}$
$\displaystyle y = - \frac{3}{4} + \frac{\sqrt{31} i}{4}$
$\displaystyle \lambda = \frac{1}{14} - \frac{9 \sqrt{31} i}{434}$
$\displaystyle x = - \frac{3}{4} - \frac{\sqrt{31} i}{4}$
$\displaystyle y = - \frac{3}{4} - \frac{\sqrt{31} i}{4}$
$\displaystyle \lambda = \frac{1}{14} + \frac{9 \sqrt{31} i}{434}$
In [18]:
x_val = solve(F_x, x)[0]
x_val
Out[18]:
$\displaystyle \frac{\sqrt{3} \sqrt{\frac{- \lambda - 4}{\lambda}}}{6}$
In [19]:
y_val = solve(F_y, y)[0]
y_val
Out[19]:
$\displaystyle \frac{\sqrt{3} \sqrt{\frac{- \lambda - 4}{\lambda}}}{6}$
In [20]:
lamda_val = solve(F_lamda.subs({x: x_val, y: y_val}), lamda)[0]
lamda_val
Out[20]:
$\displaystyle - \frac{1}{7}$
In [21]:
x_val.subs(lamda, lamda_val).simplify(force = True)
Out[21]:
$\displaystyle \frac{3}{2}$
In [22]:
y_val.subs(lamda, lamda_val).simplify(force = True)
Out[22]:
$\displaystyle \frac{3}{2}$
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [23]:
def find_lagrange_multipliers(f_eq, g_eq, k = 0):
    f_eq = Eq(f(x, y), f_eq)
    g_eq = Eq(g(x, y), g_eq)
    eq3 = Eq(F(x, y, lamda), f(x, y) + lamda*(g(x, y) - k))
    eq4 = eq3.subs({f_eq.lhs: f_eq.rhs, g_eq.lhs: g_eq.rhs,})
    F_x = Eq(Derivative(eq4.rhs, x), 0, evaluate = False).doit()
    F_y = Eq(Derivative(eq4.rhs, y), 0, evaluate = False).doit()
    F_lamda = Eq(Derivative(eq4.rhs, lamda), 0, evaluate = False).doit()
    solns = solve((F_x, F_y, F_lamda), x, y, lamda, dict = True)
    dict_solns = {x: [], y: []}
    for sol in solns:
        dict_solns[x].append(sol[x])
        dict_solns[y].append(sol[y])
        
    return dict_solns
In [24]:
f_eq = x + y
g_eq = x**3 + y**3 + UnevaluatedExpr(x+y)/4 - Rational(15, 2)
In [25]:
solns = find_lagrange_multipliers(f_eq, g_eq)
solns
Out[25]:
{x: [3/2, -3/4 + sqrt(31)*I/4, -3/4 - sqrt(31)*I/4],
 y: [3/2, -3/4 + sqrt(31)*I/4, -3/4 - sqrt(31)*I/4]}
In [26]:
for i, j in solns.items():
    print('The values of', i)
    for val in j:
        display(Eq(i, val, evaluate = False))
    print('_'*50)
The values of x
$\displaystyle x = \frac{3}{2}$
$\displaystyle x = - \frac{3}{4} + \frac{\sqrt{31} i}{4}$
$\displaystyle x = - \frac{3}{4} - \frac{\sqrt{31} i}{4}$
__________________________________________________
The values of y
$\displaystyle y = \frac{3}{2}$
$\displaystyle y = - \frac{3}{4} + \frac{\sqrt{31} i}{4}$
$\displaystyle y = - \frac{3}{4} - \frac{\sqrt{31} i}{4}$
__________________________________________________
In [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing