```python
import sympy as sym
from sympy.physics.units.quantities import Quantity
from IPython.display import display, Math, Latex

# Specially for linear models
class HasforLind:

    
    def __init__(self, function, *args):
        self.variables = []
        self.c = []
        self.L = None
        self.alpha = []
        self.y_star = []
        self.beta = Quantity("β")
        self.lamb = Quantity("λ")
        for variable in args:
            self.variables.append(variable)
        self.function = function
        self.partial_derivative()
        self.calc_length()
        self.calc_alpha()
        self.calc_y_star()
        self.set_equation()
    
    def partial_derivative(self):
        i = 0
        display(Math(r"c_i = \frac{\partial G}{\partial y_i}"))
        for variable in self.variables:
            self.c.append(self.lamb*sym.diff(self.function, variable))
            display(Math(r"c_" + str(i+1) + " = " + str(self.c[i])))
            i += 1
            
    
    def calc_length(self):
        summation = 0
        for c in self.c:
            summation += c**2
        self.L = summation**0.5
        display(Math(r"L = " + str(self.L)))

    def calc_alpha(self):
        display(Math(r"\alpha_i = \frac{c_i}{L}"))
        i = 0
        for c in self.c:
            self.alpha.append(c/self.L)
            display(Math(r"\alpha_" + str(i+1) + " = " + str(self.alpha[i])))
            i += 1
    
    def calc_y_star(self):
        i = 0
        display(Math(r"y^*_i = -\beta \times \alpha_i"))
        for alpha in self.alpha:
            self.y_star.append(-1*alpha*self.beta)
            display(Math(r"y^*_" + str(i+1) + " = " + str(self.y_star[i])))
            i += 1
    
    def set_equation(self):
        arguments = {}
        for index in range(len(self.y_star)):
            arguments[self.variables[index]] = self.y_star[index]
        self.final_equation = self.function.subs(arguments)
        display(Math(str(self.final_equation) + str(" = 0")))

    def get_equation(self):
        return self.final_equation
    
    def get_beta(self):

        solve = sym.solvers.solve(self.get_equation(), self.beta)
        display(Math(str(self.beta) + " = " + str(solve[0])))
        return solve

x_1 = sym.Symbol("x_1")
x_2 = sym.Symbol("x_2")
f = 10*x_1 - 4*x_2 + 12     

hasfor_lind = HasforLind(f, x_1, x_2)
beta = hasfor_lind.get_beta()
```


$\displaystyle c_i = \frac{\partial G}{\partial y_i}$



$\displaystyle c_1 = 10*λ$



$\displaystyle c_2 = -4*λ$



$\displaystyle L = 10.770329614269*λ**1.0$



$\displaystyle \alpha_i = \frac{c_i}{L}$



$\displaystyle \alpha_1 = 0.928476690885259$



$\displaystyle \alpha_2 = -0.371390676354104$



$\displaystyle y^*_i = -\beta \times \alpha_i$



$\displaystyle y^*_1 = -0.928476690885259*β$



$\displaystyle y^*_2 = 0.371390676354104*β$



$\displaystyle 12 - 10.770329614269*β = 0$



$\displaystyle β = 1.11417202906231$


27fdc0f33886716a0aa8083f272a7ec09a70e645
ipynb
Jupyter Notebook
limit-states/Hasfor-Lind/Python/Hasfor_Lind.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/Hasfor-Lind/Python/Hasfor_Lind.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
limit-states/Hasfor-Lind/Python/Hasfor_Lind.ipynb
amirhkiani77/Risk-Management
f6a868200fb90bdae21c14381084f8f01f6faf6c
MIT
Qwen/Qwen-72B
1. YES
2. YES