def test_conv1(): x = Symbol("x") assert x._sympy_() == sympy.Symbol("x") assert x._sympy_() != sympy.Symbol("y") x = Symbol("y") assert x._sympy_() != sympy.Symbol("x") assert x._sympy_() == sympy.Symbol("y")
def test_n(): x = Symbol("x") raises(RuntimeError, lambda: (x.n())) x = 2 + I raises(RuntimeError, lambda: (x.n(real=True))) x = sqrt(Integer(4)) y = RealDouble(2.0) assert x.n(real=True) == y x = 1 + 2*I y = 1.0 + 2.0*I assert x.n() == y try: from symengine import RealMPFR x = sqrt(Integer(2)) y = RealMPFR('1.41421356237309504880169', 75) assert x.n(75, real=True) == y except ImportError: x = sqrt(Integer(2)) raises(ValueError, lambda: (x.n(75, real=True))) try: from symengine import ComplexMPC x = sqrt(Integer(2)) + 3*I y = ComplexMPC('1.41421356237309504880169', '3.0', 75) assert x.n(75) == y except ImportError: x = sqrt(Integer(2)) raises(ValueError, lambda: (x.n(75)))
def test_log(): x = Symbol("x") x1 = sympy.Symbol("x") assert log(x) == log(x1) assert log(x)._sympy_() == sympy.log(x1) assert sympify(sympy.log(x1)) == log(x) y = Symbol("y") y1 = sympy.Symbol("y") assert log(x, y) == log(x, y1) assert log(x1, y) == log(x1, y1) assert log(x, y)._sympy_() == sympy.log(x1, y1) assert sympify(sympy.log(x1, y1)) == log(x, y)
def q42(self, t=Symbol('t', positive=True)): # term1 expr1_q42 = self.integrate(self.p4(self.rr)**2, (self.rr, 0, self.u)) expr2_q42 = self.integrate( self.p1(self.u) * self.p3(self.u) * expr1_q42, (self.u, 0, self.s)) expr3_q42 = self.integrate( 2 * self.p1(self.s) * self.p3(self.s) * expr2_q42, (self.s, 0, t)) # term2 expr4_q42 = self.integrate( self.rho * self.p1(self.rr) * self.p4(self.rr), (self.rr, 0, self.u)) expr5_q42 = self.integrate( self.rho * self.p3(self.u) * self.p4(self.u) * expr4_q42, (self.u, 0, self.s)) expr6_q42 = self.integrate( 2 * self.p1(self.s) * self.p3(self.s) * expr5_q42, (self.s, 0, t)) # term3 expr7_q42 = self.integrate( self.rho * self.p1(self.u) * self.p4(self.u), (self.u, 0, self.s)) expr8_q42 = self.integrate( self.p3(self.s)**2 * expr7_q42**2, (self.s, 0, t)) return self.simplify(expr3_q42 + expr6_q42 + expr8_q42)
def test_make_args(): x = Symbol("x") y = Symbol("y") z = Symbol("z") assert Add.make_args(x) == (x,) assert Mul.make_args(x) == (x,) assert Add.make_args(x*y*z) == (x*y*z,) assert Mul.make_args(x*y*z) == (x*y*z).args assert Add.make_args(x + y + z) == (x + y + z).args assert Mul.make_args(x + y + z) == (x + y + z,) assert Add.make_args((x + y)**z) == ((x + y)**z,) assert Mul.make_args((x + y)**z) == ((x + y)**z,)
def build_constraints(mod, variables, conds, parameters=None): internal_constraints = mod.get_internal_constraints() internal_constraints = [ INTERNAL_CONSTRAINT_SCALING * x for x in internal_constraints ] multiphase_constraints = mod.get_multiphase_constraints(conds) multiphase_constraints = [ MULTIPHASE_CONSTRAINT_SCALING * x for x in multiphase_constraints ] # TODO: Conditions needing Hessians should probably have a 'second-order' tag or something need_hess = any( type(c) in v.CONDITIONS_REQUIRING_HESSIANS for c in conds.keys()) cf_output = _build_constraint_functions(variables, internal_constraints, include_hess=need_hess, parameters=parameters) internal_cons = cf_output.cons_func internal_jac = cf_output.cons_jac internal_cons_hess = cf_output.cons_hess result_build = _build_constraint_functions(variables + [Symbol('NP')], multiphase_constraints, include_hess=False, parameters=parameters) multiphase_cons = result_build.cons_func multiphase_jac = result_build.cons_jac return ConstraintTuple(internal_cons=internal_cons, internal_jac=internal_jac, internal_cons_hess=internal_cons_hess, multiphase_cons=multiphase_cons, multiphase_jac=multiphase_jac, num_internal_cons=len(internal_constraints), num_multiphase_cons=len(multiphase_constraints))
def build_constraints(mod, variables, conds, parameters=None): internal_constraints = mod.get_internal_constraints() internal_constraints = [ INTERNAL_CONSTRAINT_SCALING * x for x in internal_constraints ] multiphase_constraints = mod.get_multiphase_constraints(conds) multiphase_constraints = [ MULTIPHASE_CONSTRAINT_SCALING * x for x in multiphase_constraints ] cf_output = _build_constraint_functions(variables, internal_constraints, parameters=parameters) internal_cons_func = cf_output.cons_func internal_cons_jac = cf_output.cons_jac internal_cons_hess = cf_output.cons_hess result_build = _build_constraint_functions(variables + [Symbol('NP')], multiphase_constraints, parameters=parameters) multiphase_cons_func = result_build.cons_func multiphase_cons_jac = result_build.cons_jac multiphase_cons_hess = result_build.cons_hess return ConstraintTuple(internal_cons_func=internal_cons_func, internal_cons_jac=internal_cons_jac, internal_cons_hess=internal_cons_hess, multiphase_cons_func=multiphase_cons_func, multiphase_cons_jac=multiphase_cons_jac, multiphase_cons_hess=multiphase_cons_hess, num_internal_cons=len(internal_constraints), num_multiphase_cons=len(multiphase_constraints))
def test_Subs(): x = Symbol("x") y = Symbol("y") _x = Symbol("_xi_1") f = function_symbol("f", 2*x) assert str(f.diff(x)) == "2*Subs(Derivative(f(_xi_1), _xi_1), (_xi_1), (2*x))" # TODO: fix me # assert f.diff(x) == 2 * Subs(Derivative(function_symbol("f", _x), _x), [_x], [2 * x]) assert Subs(Derivative(function_symbol("f", x, y), x), [x, y], [_x, x]) \ == Subs(Derivative(function_symbol("f", x, y), x), [y, x], [x, _x]) s = f.diff(x)/2 _xi_1 = Symbol("_xi_1") assert s.expr == Derivative(function_symbol("f", _xi_1), _xi_1) assert s.variables == (_xi_1,) assert s.point == (2*x,)
def test_derivative(): x = Symbol("x") y = Symbol("y") f = function_symbol("f", x) assert f.diff(x) == function_symbol("f", x).diff(x) assert f.diff(x).diff(x) == function_symbol("f", x).diff(x).diff(x) assert f.diff(y) == 0 assert f.diff(x).args == (f, x) assert f.diff(x).diff(x).args == (f, x, x) assert f.diff(x, 0) == f assert f.diff(x, 0) == Derivative(function_symbol("f", x), x, 0) raises(ValueError, lambda: f.diff(0)) raises(ValueError, lambda: f.diff(x, 0, 0)) raises(ValueError, lambda: f.diff(x, y, 0, 0, x)) g = function_symbol("f", y) assert g.diff(x) == 0 assert g.diff(y) == function_symbol("f", y).diff(y) assert g.diff(y).diff(y) == function_symbol("f", y).diff(y).diff(y) assert f - function_symbol("f", x) == 0 f = function_symbol("f", x, y) assert f.diff(x).diff(y) == function_symbol("f", x, y).diff(x).diff(y) assert f.diff(Symbol("z")) == 0 s = Derivative(function_symbol("f", x), x) assert s.expr == function_symbol("f", x) assert s.variables == (x, ) fxy = Function("f")(x, y) g = Derivative(Function("f")(x, y), x, 2, y, 1) assert g == fxy.diff(x, x, y) assert g == fxy.diff(y, 1, x, 2) assert g == fxy.diff(y, x, 2) h = Derivative(Function("f")(x, y), x, 0, y, 1) assert h == fxy.diff(x, 0, y) assert h == fxy.diff(y, x, 0) i = Derivative(Function("f")(x, y), x, 0, y, 1, x, 1) assert i == fxy.diff(x, 0, y, x, 1) assert i == fxy.diff(x, 0, y, x) assert i == fxy.diff(y, x) assert i == fxy.diff(y, 1, x, 1) assert i == fxy.diff(y, 1, x)
def test_conv11(): x = sympy.Symbol("x") y = sympy.Symbol("y") x1 = Symbol("x") y1 = Symbol("y") f = sympy.Function("f") f1 = Function("f") e1 = diff(f(2 * x, y), x) e2 = diff(f1(2 * x1, y1), x1) e3 = diff(f1(2 * x1, y1), y1) assert sympify(e1) == e2 assert sympify(e1) != e3 assert e2._sympy_() == e1 assert e3._sympy_() != e1
def test_abs(): x = Symbol("x") e1 = abs(sympy.Symbol("x")) e2 = abs(x) assert sympify(e1) == e2 assert e1 == e2._sympy_() e1 = abs(2 * sympy.Symbol("x")) e2 = 2 * abs(x) assert sympify(e1) == e2 assert e1 == e2._sympy_() y = Symbol("y") e1 = abs(sympy.Symbol("y") * sympy.Symbol("x")) e2 = abs(y * x) assert sympify(e1) == e2 assert e1 == e2._sympy_()
def test_conv11(): x = sympy.Symbol("x") y = sympy.Symbol("y") x1 = Symbol("x") y1 = Symbol("y") e1 = sympy.Subs(sympy.Derivative(sympy.Function("f")(x, y), x), [x, y], [y, y]) e2 = Subs(Derivative(function_symbol("f", x1, y1), [x1]), [x1, y1], [y1, y1]) e3 = Subs(Derivative(function_symbol("f", x1, y1), [x1]), [y1, x1], [x1, y1]) assert sympify(e1) == e2 assert sympify(e1) != e3 assert e2._sympy_() == e1 assert e3._sympy_() != e1
def test_symbol_names_are_propagated_through_symbols_and_parameters(): """A map of old symbol names to new symbol names should propagate through symbol and parameter SymPy expressions""" tdb_propagate_str = """$ Mangled function names should propagate through other symbols and parameters ELEMENT A PH 0 0 0 ! FUNCTION FN1 298.15 -42; 6000 N ! FUNCTION FN2 298.15 FN1#; 6000 N ! PARAMETER G(PH,A;0) 298.15 FN1# + FN2#; 6000 N ! """ test_dbf = Database.from_string(tdb_propagate_str, fmt='tdb') rename_map = {'FN1': 'RENAMED_FN1', 'FN2': 'RENAMED_FN2'} _apply_new_symbol_names(test_dbf, rename_map) assert 'RENAMED_FN1' in test_dbf.symbols assert 'FN1' not in test_dbf.symbols # check that the old key was removed assert test_dbf.symbols['RENAMED_FN2'] == Piecewise( (Symbol('RENAMED_FN1'), And(v.T < 6000.0, v.T >= 298.15)), (0, True)) assert test_dbf._parameters.all()[0]['parameter'] == Piecewise( (Symbol('RENAMED_FN1') + Symbol('RENAMED_FN2'), And(v.T < 6000.0, v.T >= 298.15)), (0, True))
def test_symbol(): x = Symbol("x") assert x.name == "x" assert str(x) == "x" assert str(x) != "y" assert repr(x) == str(x) # Verify the successful use of slots. assert not hasattr(x, "__dict__") assert not hasattr(x, "__weakref__")
def f_St(self, x=Symbol('x'), t=Symbol('t', positive=True), order=3, simplify=True, from_cache=True): if from_cache and self.cached_order == order and self.cached_f_St is not None: return self.cached_f_St if simplify: output = self.simplify( self.f_Xt(x / self.F(t) - 1, t, order, simplify) / self.F(t)) else: output = self.f_Xt(x / self.F(t) - 1, t, order, simplify, from_cache) / self.F(t) self.cached_order = order self.cached_f_St = output return output
def test_conv8b(): e1 = sympy.Function("f")(sympy.Symbol("x")) e2 = sympy.Function("g")(sympy.Symbol("x"), sympy.Symbol("y")) assert sympify(e1) == function_symbol("f", Symbol("x")) assert sympify(e2) != function_symbol("f", Symbol("x")) assert sympify(e2) == function_symbol("g", Symbol("x"), Symbol("y")) e3 = sympy.Function("q")(sympy.Symbol("t")) assert sympify(e3) == function_symbol("q", Symbol("t")) assert sympify(e3) != function_symbol("f", Symbol("t")) assert sympify(e3) != function_symbol("q", Symbol("t"), Symbol("t"))
def test_conv12b(): x = sympy.Symbol("x") y = sympy.Symbol("y") assert sympify(sympy.sinh(x / 3)) == sinh(Symbol("x") / 3) assert sympify(sympy.cosh(x / 3)) == cosh(Symbol("x") / 3) assert sympify(sympy.tanh(x / 3)) == tanh(Symbol("x") / 3) assert sympify(sympy.coth(x / 3)) == coth(Symbol("x") / 3) assert sympify(sympy.asinh(x / 3)) == asinh(Symbol("x") / 3) assert sympify(sympy.acosh(x / 3)) == acosh(Symbol("x") / 3) assert sympify(sympy.atanh(x / 3)) == atanh(Symbol("x") / 3) assert sympify(sympy.acoth(x / 3)) == acoth(Symbol("x") / 3)
def q1(self, t=Symbol('t', positive=True)): expr1_q1 = self.integrate( self.sigma0(self.u) * self.p1(self.u), (self.u, 0, self.s)) expr2_q1 = self.integrate( self.p1(self.s) * self.p2(self.s) * expr1_q1, (self.s, 0, t)) expr3_q1 = self.integrate(self.rho * self.p1(self.u) * self.p4(self.u), (self.u, 0, self.s)) expr4_q1 = self.integrate( self.p1(self.s) * self.p3(self.s) * expr3_q1, (self.s, 0, t)) return self.simplify(expr2_q1 + expr4_q1)
def test_slots(): x = Dummy('x') # Verify the successful use of slots. assert not hasattr(x, "__dict__") assert not hasattr(x, "__weakref__") x1 = Symbol('x') # Verify the successful use of slots. assert not hasattr(x, "__dict__") assert not hasattr(x, "__weakref__")
def q22(self, t=Symbol('t', positive=True)): expr1_q22 = self.integrate( self.rho * self.p1(self.rr) * self.p4(self.rr), (self.rr, 0, self.u)) expr2_q22 = self.integrate( self.rho * self.p1(self.u) * self.gamma0_v(self.u) * expr1_q22, (self.u, 0, self.s)) expr3_q22 = self.integrate( self.p1(self.s) * self.p3(self.s) * expr2_q22, (self.s, 0, t)) return self.simplify(expr3_q22)
def test_f(): x = Symbol("x") y = Symbol("y") z = Symbol("z") f = function_symbol("f", x) g = function_symbol("g", x) assert f != g f = function_symbol("f", x) g = function_symbol("f", x) assert f == g f = function_symbol("f", x, y) g = function_symbol("f", y, x) assert f != g f = function_symbol("f", x, y) g = function_symbol("f", x, y) assert f == g
def test_exp(): x = Symbol("x") e1 = sympy.exp(sympy.Symbol("x")) e2 = exp(x) assert sympify(e1) == e2 assert e1 == e2._sympy_() e1 = sympy.exp(sympy.Symbol("x")).diff(sympy.Symbol("x")) e2 = exp(x).diff(x) assert sympify(e1) == e2 assert e1 == e2._sympy_()
def test_derivative(): x = Symbol("x") y = Symbol("y") f = function_symbol("f", x) assert f.diff(x) == function_symbol("f", x).diff(x) assert f.diff(x).diff(x) == function_symbol("f", x).diff(x).diff(x) assert f.diff(y) == 0 assert f.diff(x).args == (f, x) assert f.diff(x).diff(x).args == (f, x, x) g = function_symbol("f", y) assert g.diff(x) == 0 assert g.diff(y) == function_symbol("f", y).diff(y) assert g.diff(y).diff(y) == function_symbol("f", y).diff(y).diff(y) assert f - function_symbol("f", x) == 0 f = function_symbol("f", x, y) assert f.diff(x).diff(y) == function_symbol("f", x, y).diff(x).diff(y) assert f.diff(Symbol("z")) == 0
def test_tuples_lists(): x = sympy.Symbol("x") y = sympy.Symbol("y") z = sympy.Symbol("z") L = [x, y, z, x * y, z**y] t = (x, y, z, x * y, z**y) x = Symbol("x") y = Symbol("y") z = Symbol("z") l2 = [x, y, z, x * y, z**y] t2 = (x, y, z, x * y, z**y) assert sympify(L) == l2 assert sympify(t) == t2 assert sympify(L) != t2 assert sympify(t) != l2 assert L == l2 assert t == t2 assert L != t2 assert t != l2
def test_conv12(): x = Symbol("x") y = Symbol("y") assert sinh(x / 3) == sinh(sympy.Symbol("x") / 3) assert cosh(x / 3) == cosh(sympy.Symbol("x") / 3) assert tanh(x / 3) == tanh(sympy.Symbol("x") / 3) assert coth(x / 3) == coth(sympy.Symbol("x") / 3) assert asinh(x / 3) == asinh(sympy.Symbol("x") / 3) assert acosh(x / 3) == acosh(sympy.Symbol("x") / 3) assert atanh(x / 3) == atanh(sympy.Symbol("x") / 3) assert acoth(x / 3) == acoth(sympy.Symbol("x") / 3) assert sinh(x / 3)._sympy_() == sympy.sinh(sympy.Symbol("x") / 3) assert cosh(x / 3)._sympy_() == sympy.cosh(sympy.Symbol("x") / 3) assert tanh(x / 3)._sympy_() == sympy.tanh(sympy.Symbol("x") / 3) assert coth(x / 3)._sympy_() == sympy.coth(sympy.Symbol("x") / 3) assert asinh(x / 3)._sympy_() == sympy.asinh(sympy.Symbol("x") / 3) assert acosh(x / 3)._sympy_() == sympy.acosh(sympy.Symbol("x") / 3) assert atanh(x / 3)._sympy_() == sympy.atanh(sympy.Symbol("x") / 3) assert acoth(x / 3)._sympy_() == sympy.acoth(sympy.Symbol("x") / 3)
def test_unevaluated_expr(): x = Symbol("x") t = UnevaluatedExpr(x) assert x + t != 2 * x assert not t.is_number assert not t.is_integer assert not t.is_finite t = UnevaluatedExpr(1) assert t.is_number assert t.is_integer assert t.is_finite
def test_abs(): x = Symbol("x") e = abs(x) assert e == abs(x) assert e != cos(x) assert abs(5) == 5 assert abs(-5) == 5 assert abs(Integer(5) / 3) == Integer(5) / 3 assert abs(-Integer(5) / 3) == Integer(5) / 3 assert abs(Integer(5) / 3 + x) != Integer(5) / 3 assert abs(Integer(5) / 3 + x) == abs(Integer(5) / 3 + x)
def test_conv10(): A = DenseMatrix(1, 4, [Integer(1), Integer(2), Integer(3), Integer(4)]) assert (A._sympy_() == sympy.Matrix(1, 4, [ sympy.Integer(1), sympy.Integer(2), sympy.Integer(3), sympy.Integer(4) ])) B = DenseMatrix(4, 1, [Symbol("x"), Symbol("y"), Symbol("z"), Symbol("t")]) assert (B._sympy_() == sympy.Matrix(4, 1, [ sympy.Symbol("x"), sympy.Symbol("y"), sympy.Symbol("z"), sympy.Symbol("t") ])) C = DenseMatrix( 2, 2, [Integer(5), Symbol("x"), function_symbol("f", Symbol("x")), 1 + I]) assert (C._sympy_() == sympy.Matrix( [[5, sympy.Symbol("x")], [sympy.Function("f")(sympy.Symbol("x")), 1 + sympy.I]]))
def test_var(): var("a") assert a == Symbol("a") var("b bb cc zz _x") assert b == Symbol("b") assert bb == Symbol("bb") assert cc == Symbol("cc") assert zz == Symbol("zz") assert _x == Symbol("_x") v = var(['d', 'e', 'fg']) assert d == Symbol('d') assert e == Symbol('e') assert fg == Symbol('fg') # check return value assert v == [d, e, fg]
def q21(self, t=Symbol('t', positive=True)): expr1_q21 = self.integrate( self.sigma0(self.rr) * self.p1(self.rr), (self.rr, 0, self.u)) expr2_q21 = self.integrate( self.sigma0(self.u) * self.p1(self.u) * expr1_q21, (self.u, 0, self.s)) expr3_q21 = self.integrate( self.p1(self.s) * self.p5(self.s) * expr2_q21, (self.s, 0, t)) expr4_q21 = self.integrate( self.p1(self.u) * self.p8(self.u) * expr1_q21, (self.u, 0, self.s)) expr5_q21 = self.integrate( self.p1(self.s) * self.p2(self.s) * expr4_q21, (self.s, 0, t)) return self.simplify(expr3_q21 + expr5_q21)
def q5(self, t=Symbol('t', positive=True)): expr1_q5 = self.integrate(self.sigma0(self.u)**2, (self.u, 0, self.s)) expr2_q5 = self.integrate( self.p2(self.s)**2 * expr1_q5, (self.s, 0, t)) expr3_q5 = self.integrate(self.p4(self.u)**2, (self.u, 0, self.s)) expr4_q5 = self.integrate( self.p3(self.s)**2 * expr3_q5, (self.s, 0, t)) expr5_q5 = self.integrate( self.rho * self.sigma0(self.u) * self.p4(self.u), (self.u, 0, self.s)) expr6_q5 = self.integrate( 2 * self.p2(self.s) * self.p3(self.s) * expr5_q5, (self.s, 0, t)) return self.simplify(expr2_q5 + expr4_q5 + expr6_q5)