```python
import sympy as sym
from sympy.physics.units.quantities import Quantity
from IPython.display import display, Math, Latex
import pandas as pd
import numpy as np
def func_str(function):
    return str(function)
    function_str = str(function)
    function_str = function_str.replace("**", "^")
    return function_str
class Lagrange:
    def __init__(self, function, *args):
        self.lamb = Quantity("λ")
        self.variables = []
        self.function = function
        for variable in args:
            self.variables.append(variable)
        self.make_delta()
        self.partial_deravative()
        
    def make_delta(self):
        summation = 0
        for variable in self.variables:
            summation += variable**2 
        self.delta =  summation**0.5 + self.lamb * self.function
        display(Math(r"min(\Delta) =  " + func_str(self.delta)))
    
    def partial_deravative(self):
        self.partial_equations = []
        for i in range(len(self.variables) + 1):
            if i == len(self.variables):
                der_function = sym.diff(self.delta, self.lamb)
                self.partial_equations.append(sym.Eq(der_function, 0))
                display(Math(r"\frac{\partial min(\Delta)}{\partial \lambda} = " + func_str(der_function) + str( " = 0")))
            else:
                der_function = sym.diff(self.delta, self.variables[i])
                self.partial_equations.append(sym.Eq(der_function, 0))
                display(Math(r"\frac{\partial min(\Delta)}{\partial y_%d} = " % (i+1) + func_str(der_function) + str( " = 0")))
    
    def solve_equations(self):
        self.unknows = []
        for i in range(len(self.variables) + 1):
            if i == len(self.variables):
                self.unknows.append(self.lamb)
            else:
                self.unknows.append(self.variables[i])
        self.result = sym.solve(self.partial_equations, self.unknows)
        return self.result
    
    def get_beta(self):
        if hasattr(self, "result"):
            return ((float(self.result[0][0])**2) + float((self.result[0][1])**2))**0.5
        else:
            self.result = self.solve_equations()
            return self.get_beta()

        


y_1, y_2 = sym.symbols('y_1 y_2')

f = 10*y_1 - 4*y_2 + 12

lagrange = Lagrange(f, y_1, y_2)
lagrange.get_beta()
```


$\displaystyle min(\Delta) =  λ*(10*y_1 - 4*y_2 + 12) + (y_1**2 + y_2**2)**0.5$



$\displaystyle \frac{\partial min(\Delta)}{\partial y_1} = 10*λ + 1.0*y_1*(y_1**2 + y_2**2)**(-0.5) = 0$



$\displaystyle \frac{\partial min(\Delta)}{\partial y_2} = -4*λ + 1.0*y_2*(y_1**2 + y_2**2)**(-0.5) = 0$



$\displaystyle \frac{\partial min(\Delta)}{\partial \lambda} = 10*y_1 - 4*y_2 + 12 = 0$





    1.1141720290623114




```python
print("hi")
```

b7cb5b63045f1e4325394ab665eca014008e0316
ipynb
Jupyter Notebook
limit-states/Lagrange/Python/Lagrange.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/Lagrange/Python/Lagrange.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/Lagrange/Python/Lagrange.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
Qwen/Qwen-72B
1. YES
2. YES