def evaluate_linear(self, expression): if len(expression) == 0: return "Enter valid expression" try: exp = expression.split("=") sy_exp = kernS(exp[0]) if exp[1].isdigit(): req = int(exp[1]) else: req = kernS(exp[1]) except SyntaxError: return "Not a valid expression" return sy.solveset(sy.Eq(sy_exp, req))
def test_kernS(): s = "-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))" # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert -1 - 2 * (-(-x + 1 / x) / (x * (x - 1 / x) ** 2) - 1 / (x * (x - 1 / x))) == -1 # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = "-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))".replace("x", "_kern") ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 3588 assert kernS("Interval(-1,-2 - 4*(-3))") == Interval(-1, 10) assert kernS("_kern") == Symbol("_kern")
def test_kernS(): s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))' # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert -1 - 2 * (-(-x + 1 / x) / (x * (x - 1 / x)**2) - 1 / (x * (x - 1 / x))) == -1 # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'.replace( 'x', '_kern') ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 3588 assert kernS('Interval(-1,-2 - 4*(-3))') == Interval(-1, 10) assert kernS('_kern') == Symbol('_kern')
def test_kernS(): s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))' # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert -1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) == -1 # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'.replace( 'x', '_kern') ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 6687 assert kernS('Interval(-1,-2 - 4*(-3))') == Interval(-1, 10) assert kernS('_kern') == Symbol('_kern') assert kernS('E**-(x)') == exp(-x) e = 2*(x + y)*y assert kernS(['2*(x + y)*y', ('2*(x + y)*y',)]) == [e, (e,)] assert kernS('-(2*sin(x)**2 + 2*sin(x)*cos(x))*y/2') == \ -y*(2*sin(x)**2 + 2*sin(x)*cos(x))/2
def test_kernS(): s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))' # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert -1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) == -1 # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'.replace( 'x', '_kern') ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 3588 assert kernS('Interval(-1,-2 - 4*(-3))') == Interval(-1, 10) assert kernS('_kern') == Symbol('_kern') assert kernS('E**-(x)') == exp(-x) e = 2*(x + y)*y assert kernS(['2*(x + y)*y', ('2*(x + y)*y',)]) == [e, (e,)] assert kernS('-(2*sin(x)**2 + 2*sin(x)*cos(x))*y/2') == \ -y*(2*sin(x)**2 + 2*sin(x)*cos(x))/2
def test_kernS(): s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))' # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert -1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x))) == -1 # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = '-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))'.replace( 'x', '_kern') ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 6687 assert (kernS('Interval(-1,-2 - 4*(-3))') == Interval(-1, Add(-2, Mul(12, 1, evaluate=False), evaluate=False))) assert kernS('_kern') == Symbol('_kern') assert kernS('E**-(x)') == exp(-x) e = 2*(x + y)*y assert kernS(['2*(x + y)*y', ('2*(x + y)*y',)]) == [e, (e,)] assert kernS('-(2*sin(x)**2 + 2*sin(x)*cos(x))*y/2') == \ -y*(2*sin(x)**2 + 2*sin(x)*cos(x))/2 # issue 15132 assert kernS('(1 - x)/(1 - x*(1-y))') == kernS('(1-x)/(1-(1-y)*x)') assert kernS('(1-2**-(4+1)*(1-y)*x)') == (1 - x*(1 - y)/32) assert kernS('(1-2**(4+1)*(1-y)*x)') == (1 - 32*x*(1 - y)) assert kernS('(1-2.*(1-y)*x)') == 1 - 2.*x*(1 - y) one = kernS('x - (x - 1)') assert one != 1 and one.expand() == 1 assert kernS("(2*x)/(x-1)") == 2*x/(x-1)
# In[ ]: eqn_str = '1 / (1 + x**2 + y**2)' #x = Symbol('x') #y = Symbol('y') #i,j,k = symbols('i, j, k') #kernS(eqn_str) function = sympify(eqn_str) symbols = list(function.free_symbols) print(symbols) sym_val_dict = {k: np.random.rand() for k in symbols} print(sym_val_dict) deriv = function.diff(x) eqn_str2 = 'x+y+x+2*y-i-j*k+1' function2 = kernS(eqn_str2) deriv2 = function2.diff(x) print(deriv) print(deriv2) print(function + function2) print(2 * function2) print(np.square(function2)) function.evalf(subs={x: 0, y: 1}) f = lambdify((x, y), function) print(f(1, 1)) ##inequality = 'x <= 2' ##ineq_func = sympify(inequality) ##ineq_func.evalf(subs = {x: 1}) zero = sympify('0') zero.evalf() + 1 absolute = sympify(-x)
def optimize(self): f1 = kernS(self.function1.get()) f2 = kernS(self.function2.get()) u1 = kernS(self.u1.get()) u2 = kernS(self.u2.get()) e = float(self.e.get()) # d lambda dl = 0.5 f = (u1*f1 + u2*f2).expand() print(f) # let's begin # preparations x1, x2 = symbols("x1 x2") # direction vector ej = [0, 0] # initial approximations for solution matrix and value X = [1, 1] F = f.subs({x1: X[0], x2: X[1]}) print("F " + str(F)) # optimal solution matrix and value, set as initial ones opt_X = X opt_F = F while True: # step 3 # checking if precision conditions met j = 0 while j < len(X): # step 2 # repeating this cycle for each direction toward each variable (x1, ..., xn) # step 1 # optimizing initially approximated solution matrix toward current direction # changing vector direction according to current argument ej[j] = 1 print("ej " + str(ej)) # setting approximations for optimals X = opt_X F = opt_F # forming 3 points for parabola approximation # Y - points, lam - points for parabola, P - f(Yi) Y = [] lam = [] P = [] # first point is current point Y.append(X) lam.append(0) print("Y " + str(Y[0][0]) + " " + str(Y[0][1])) P.append(f.subs({x1: Y[0][0], x2: Y[0][1]})) # second point is current point + offset dl in ej direction Y.append([X[0] + dl * ej[0], X[1] + dl * ej[1]]) lam.append(dl) P.append(f.subs({x1: Y[1][0], x2: Y[1][1]})) # third point is chosen according to first 2 if P[0] < P[1]: Y.append([X[0] - dl * ej[0], X[1] - dl * ej[1]]) lam.append(-1 * dl) P.append(f.subs({x1: Y[2][0], x2: Y[2][1]})) else: Y.append([X[0] + 2 * dl * ej[0], X[1] + 2 * dl * ej[1]]) lam.append(2 * dl) P.append(f.subs({x1: Y[2][0], x2: Y[2][1]})) # setting new lambda print("lam " + str(lam)) print("P " + str(P)) opt_l = parabolic_interpolation(lam, P) print("dl " + str(dl)) # getting new optimal solution matrix and value opt_X = [X[0] + opt_l * ej[0], X[1] + opt_l * ej[1]] opt_F = f.subs({x1: opt_X[0], x2: opt_X[1]}) print("opt_F " + str(opt_F)) # resetting direction ej[j] = 0 j += 1 if (sqrt((opt_X[0]-X[0])**2+(opt_X[1]-X[1])**2) <= e) or (abs(opt_F - F) <= e): # if precision conditions are met, then optimals are found break else: continue # rounding to one-thousandth for output for i in range(0, len(opt_X)): opt_X[i] = round(opt_X[i], 3) opt_F = round(opt_F, 3) answer_string = "Оптимальное значение достигнуто \nв точке " + str(opt_X) + " и равно " + str(opt_F) self.answer.configure(text=answer_string) self.answer.grid(row=self.current_row, column=self.current_col, columnspan=4)
# sympy solution (autosimplification alters the order and the brackets) from sympy import Symbol, sympify scale_f = Symbol("scale_f") Vmax_bA = Symbol("Vmax_bA") Km_A = Symbol("Km_A") e__A = Symbol("e__A") c__A = Symbol("c__A") from sympy.core.sympify import kernS s = 'scale_f * (Vmax_bA / Km_A) * ((e__A - c__A) / (1 + e__A / Km_A + c__A / Km_A))' expr = sympify(s) print(expr) expr = kernS(s) print(expr) expr = sympify("(e__A - c__A)") latex_str = latex(expr, order=None, mode='equation', itex=True, mul_symbol='dot', symbol_names={ e__A: 'A_{(e)}', c__A: 'A_{(c)}', }) print(latex_str) # Equation solution, necessary to make replacements
def test_kernS(): s = "-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))" # when 1497 is fixed, this no longer should pass: the expression # should be unchanged assert (-1 - 2 * (-(-x + 1 / x) / (x * (x - 1 / x)**2) - 1 / (x * (x - 1 / x))) == -1) # sympification should not allow the constant to enter a Mul # or else the structure can change dramatically ss = kernS(s) assert ss != -1 and ss.simplify() == -1 s = "-1 - 2*(-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))".replace( "x", "_kern") ss = kernS(s) assert ss != -1 and ss.simplify() == -1 # issue 6687 assert kernS("Interval(-1,-2 - 4*(-3))") == Interval(-1, 10) assert kernS("_kern") == Symbol("_kern") assert kernS("E**-(x)") == exp(-x) e = 2 * (x + y) * y assert kernS(["2*(x + y)*y", ("2*(x + y)*y", )]) == [e, (e, )] assert (kernS("-(2*sin(x)**2 + 2*sin(x)*cos(x))*y/2") == -y * (2 * sin(x)**2 + 2 * sin(x) * cos(x)) / 2) # issue 15132 assert kernS("(1 - x)/(1 - x*(1-y))") == kernS("(1-x)/(1-(1-y)*x)") assert kernS("(1-2**-(4+1)*(1-y)*x)") == (1 - x * (1 - y) / 32) assert kernS("(1-2**(4+1)*(1-y)*x)") == (1 - 32 * x * (1 - y)) assert kernS("(1-2.*(1-y)*x)") == 1 - 2.0 * x * (1 - y) one = kernS("x - (x - 1)") assert one != 1 and one.expand() == 1
# sympy solution (autosimplification alters the order and the brackets) from sympy import Symbol, sympify scale_f = Symbol("scale_f") Vmax_bA = Symbol("Vmax_bA") Km_A = Symbol("Km_A") e__A = Symbol("e__A") c__A = Symbol("c__A") from sympy.core.sympify import kernS s = 'scale_f * (Vmax_bA / Km_A) * ((e__A - c__A) / (1 + e__A / Km_A + c__A / Km_A))' expr = sympify(s) print(expr) expr = kernS(s) print(expr) expr = sympify("(e__A - c__A)") latex_str = latex(expr, order=None, mode='equation', itex=True, mul_symbol='dot', symbol_names={ e__A: 'A_{(e)}', c__A: 'A_{(c)}', }) print(latex_str) # Equation solution, necessary to make replacements import Equation from Equation import Expression