def test_sets(): x = Integer(2) y = Integer(3) x1 = sympy.Integer(2) y1 = sympy.Integer(3) assert Interval(x, y) == Interval(x1, y1) assert Interval(x1, y) == Interval(x1, y1) assert Interval(x, y)._sympy_() == sympy.Interval(x1, y1) assert sympify(sympy.Interval(x1, y1)) == Interval(x, y) assert sympify(sympy.EmptySet()) == EmptySet() assert sympy.EmptySet() == EmptySet()._sympy_() assert FiniteSet(x, y) == FiniteSet(x1, y1) assert FiniteSet(x1, y) == FiniteSet(x1, y1) assert FiniteSet(x, y)._sympy_() == sympy.FiniteSet(x1, y1) assert sympify(sympy.FiniteSet(x1, y1)) == FiniteSet(x, y) x = Interval(1, 2) y = Interval(2, 3) x1 = sympy.Interval(1, 2) y1 = sympy.Interval(2, 3) assert Union(x, y) == Union(x1, y1) assert Union(x1, y) == Union(x1, y1) assert Union(x, y)._sympy_() == sympy.Union(x1, y1) assert sympify(sympy.Union(x1, y1)) == Union(x, y) assert Complement(x, y) == Complement(x1, y1) assert Complement(x1, y) == Complement(x1, y1) assert Complement(x, y)._sympy_() == sympy.Complement(x1, y1) assert sympify(sympy.Complement(x1, y1)) == Complement(x, 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_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_args(): x = Symbol("x") y = Symbol("y") assert (x**2).args == (x, 2) assert (x**2 + 5).args == (5, x**2) assert set((x**2 + 2*x*y + 5).args) == set((x**2, 2*x*y, Integer(5))) assert (2*x**2).args == (2, x**2) assert set((2*x**2*y).args) == set((Integer(2), x**2, y))
def test_as_numer_denom(): x, y = Rational(17, 26).as_numer_denom() assert x == Integer(17) assert y == Integer(26) x, y = Integer(-5).as_numer_denom() assert x == Integer(-5) assert y == Integer(1)
def test_function_within_function(self): expression = f(f(42)) self.assertEqual(collect_arguments(expression, f), {(Integer(42), ), (f(Integer(42)), )}) self.assertEqual(count_calls(expression, f), 2) self.assertTrue(has_function(expression, f)) self.assertEqual(replace_function(expression, f, g), g(g(42)))
def test_n_mpfr(): 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))) raise SkipTest("No MPFR support")
def test_n_mpc(): 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))) raise SkipTest("No MPC support")
def test_ccode(): x = Symbol("x") y = Symbol("y") assert ccode(x) == "x" assert ccode(x**3) == "pow(x, 3)" assert ccode(x**(y**3)) == "pow(x, pow(y, 3))" assert ccode(x**-1.0) == "pow(x, -1.0)" assert ccode(Max(x, x*x)) == "fmax(x, pow(x, 2))" assert ccode(sin(x)) == "sin(x)" assert ccode(Integer(67)) == "67" assert ccode(Integer(-1)) == "-1"
def test_complex(): i = Integer(5) / 10 + I assert str(i) == "1/2 + I" assert complex(i) == 0.5 + 1j assert i.real == Integer(1) / 2 assert i.imag == 1 i = 0.5 + I assert str(i) == "0.5 + 1.0*I" assert complex(i) == 0.5 + 1j assert i.real == 0.5 assert i.imag == 1.0
def test_arit2(): x = Symbol("x") y = Symbol("y") assert x + x == Integer(2) * x assert x + x != Integer(3) * x assert x + y == y + x assert x + x == 2 * x assert x + x == x * 2 assert x + x + x == 3 * x assert x + y + x + x == 3 * x + y assert not x + x == 3 * x assert not x + x != 2 * x
def test_FunctionWrapper(): import sympy sympy.var("n, m, theta, phi") r = sympy.Ynm(n, m, theta, phi) s = Integer(2) * r assert isinstance(s, Mul) assert isinstance(s.args[1]._sympy_(), sympy.Ynm)
def test_conv10b(): A = sympy.Matrix([[sympy.Symbol("x"), sympy.Symbol("y")], [sympy.Symbol("z"), sympy.Symbol("t")]]) assert sympify(A) == DenseMatrix(2, 2, [Symbol("x"), Symbol("y"), Symbol("z"), Symbol("t")]) B = sympy.Matrix([[1, 2], [3, 4]]) assert sympify(B) == DenseMatrix(2, 2, [Integer(1), Integer(2), Integer(3), Integer(4)]) C = sympy.Matrix([[7, sympy.Symbol("y")], [sympy.Function("g")(sympy.Symbol("z")), 3 + 2*sympy.I]]) assert sympify(C) == DenseMatrix(2, 2, [Integer(7), Symbol("y"), function_symbol("g", Symbol("z")), 3 + 2*I])
def test_FunctionWrapper(): import sympy n, m, theta, phi = sympy.symbols("n, m, theta, phi") r = sympy.Ynm(n, m, theta, phi) s = Integer(2) * r assert isinstance(s, Mul) assert isinstance(s.args[1]._sympy_(), sympy.Ynm) x = symbols("x") e = x + sympy.loggamma(x) assert str(e) == "x + loggamma(x)" assert isinstance(e, Add) assert e + sympy.loggamma(x) == x + 2 * sympy.loggamma(x) f = e.subs({x: 10}) assert f == 10 + log(362880) f = e.subs({x: 2}) assert f == 2 f = e.subs({x: 100}) v = f.n(53, real=True) assert abs(float(v) - 459.13420537) < 1e-7 f = e.diff(x) assert f == 1 + sympy.polygamma(0, x)
def test_integer(): i = Integer(5) assert str(i) == "5" assert int(i) == 5 assert float(i) == 5.0 assert complex(i) == 5.0 + 0j assert i.real == i assert i.imag == S.Zero
def test_conv9(): x = Symbol("x") y = Symbol("y") assert (I)._sympy_() == sympy.I assert (2*I+3)._sympy_() == 2*sympy.I+3 assert (2*I/5+Integer(3)/5)._sympy_() == 2*sympy.I/5+sympy.S(3)/5 assert (x*I+3)._sympy_() == sympy.Symbol("x")*sympy.I + 3 assert (x+I*y)._sympy_() == sympy.Symbol("x") + sympy.I*sympy.Symbol("y")
def test_conv9b(): x = Symbol("x") y = Symbol("y") assert sympify(sympy.I) == I assert sympify(2*sympy.I+3) == 2*I+3 assert sympify(2*sympy.I/5+sympy.S(3)/5) == 2*I/5+Integer(3)/5 assert sympify(sympy.Symbol("x")*sympy.I + 3) == x*I+3 assert sympify(sympy.Symbol("x") + sympy.I*sympy.Symbol("y")) == x+I*y
def test_rational(): i = Integer(5) / 10 assert str(i) == "1/2" assert int(i) == 0 assert float(i) == 0.5 assert complex(i) == 0.5 + 0j assert i.real == i assert i.imag == S.Zero
def test_sets(): x = Integer(2) y = Integer(3) x1 = sympy.Integer(2) y1 = sympy.Integer(3) assert Interval(x, y) == Interval(x1, y1) assert Interval(x1, y) == Interval(x1, y1) assert Interval(x, y)._sympy_() == sympy.Interval(x1, y1) assert sympify(sympy.Interval(x1, y1)) == Interval(x, y) assert sympify(sympy.S.EmptySet) == EmptySet() assert sympy.S.EmptySet == EmptySet()._sympy_() assert sympify(sympy.S.UniversalSet) == UniversalSet() assert sympy.S.UniversalSet == UniversalSet()._sympy_() assert sympify(sympy.S.Reals) == Reals() assert sympy.S.Reals == Reals()._sympy_() assert sympify(sympy.S.Rationals) == Rationals() assert sympy.S.Rationals == Rationals()._sympy_() assert sympify(sympy.S.Integers) == Integers() assert sympy.S.Integers == Integers()._sympy_() assert FiniteSet(x, y) == FiniteSet(x1, y1) assert FiniteSet(x1, y) == FiniteSet(x1, y1) assert FiniteSet(x, y)._sympy_() == sympy.FiniteSet(x1, y1) assert sympify(sympy.FiniteSet(x1, y1)) == FiniteSet(x, y) x = Interval(1, 2) y = Interval(2, 3) x1 = sympy.Interval(1, 2) y1 = sympy.Interval(2, 3) assert Union(x, y) == Union(x1, y1) assert Union(x1, y) == Union(x1, y1) assert Union(x, y)._sympy_() == sympy.Union(x1, y1) assert sympify(sympy.Union(x1, y1)) == Union(x, y) assert Complement(x, y) == Complement(x1, y1) assert Complement(x1, y) == Complement(x1, y1) assert Complement(x, y)._sympy_() == sympy.Complement(x1, y1) assert sympify(sympy.Complement(x1, y1)) == Complement(x, y)
def test_arit1(): x = Symbol("x") y = Symbol("y") e = x + y e = x * y e = Integer(2) * x e = 2 * x e = x + 1 e = 1 + x
def test_as_coefficients_dict(): x = Symbol('x') y = Symbol('y') check = [x, y, x*y, Integer(1)] assert [(3*x + 2*x + y + 3).as_coefficients_dict()[i] for i in check] == \ [5, 1, 0, 3] assert [(3*x*y).as_coefficients_dict()[i] for i in check] == \ [0, 0, 3, 0] assert (3.0*x*y).as_coefficients_dict()[3.0*x*y] == 0 assert (3.0*x*y).as_coefficients_dict()[x*y] == 3.0
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_arit6(): x = Symbol("x") y = Symbol("y") e = x + y assert str(e) == "x + y" or "y + x" e = x * y assert str(e) == "x*y" or "y*x" e = Integer(2) * x assert str(e) == "2*x" e = 2 * x assert str(e) == "2*x"
def test_is_conditions(): i = Integer(-123) assert not i.is_zero assert not i.is_positive assert i.is_negative assert i.is_nonzero assert i.is_nonpositive assert not i.is_nonnegative assert not i.is_complex i = Integer(123) assert not i.is_zero assert i.is_positive assert not i.is_negative assert i.is_nonzero assert not i.is_nonpositive assert i.is_nonnegative assert not i.is_complex i = Integer(0) assert i.is_zero assert not i.is_positive assert not i.is_negative assert not i.is_nonzero assert i.is_nonpositive assert i.is_nonnegative assert not i.is_complex i = Integer(1) + I assert not i.is_zero assert not i.is_positive assert not i.is_negative assert not i.is_nonzero assert not i.is_nonpositive assert not i.is_nonnegative assert i.is_complex assert pi.is_number
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
def test_find_dependent_helpers(self): helpers = [ (q, p), (r, sin(q)), (s, 3 * p + r), (u, Integer(42)), ] control = [ (q, 1), (r, cos(q)), (s, 3 + cos(q)), ] dependent_helpers = find_dependent_helpers(helpers, p) # This check is overly restrictive due to depending on the order and exact form of the result: self.assertListEqual(dependent_helpers, control)
def test_S(): assert S(0) == Integer(0) assert S(1) == Integer(1) assert S(-1) == Integer(-1) assert S(1) / 2 == Rational(1, 2) assert S.One is S(1) assert S.Zero is S(0) assert S.NegativeOne is S(-1) assert S.Half is S(1) / 2 assert S.Pi is pi assert S.NaN is S(0) / 0 assert S.Infinity is -oo * -10 assert S.NegativeInfinity is oo * (-3) assert S.ComplexInfinity is zoo assert S.Exp1 is (E + 1 - 1) assert S.ImaginaryUnit is sqrt(-1) assert S.GoldenRatio * 2 / 2 is GoldenRatio assert S.Catalan * 1 is Catalan assert S.EulerGamma is polygamma(0, 1) * -1 assert S.true is Eq(2, 2) assert S.false is Eq(2, 3) assert S(1) / 0 is zoo assert S.Pi * 1 is pi assert type(S.One) == One
def test_sympify1(): assert sympify(1) == Integer(1) assert sympify(2) != Integer(1) assert sympify(-5) == Integer(-5) assert sympify(Integer(3)) == Integer(3) assert sympify(('0', '0')) == (0, 0) assert sympify(['0', '0']) == [0, 0] assert sympify("3+5") == Integer(8) assert true == sympify(True) assert false == sympify(False)
def solid_harmonics(lmax, scaled=False): """Symbolic real solid harmonics up to order lmax. .. code-block:: python sh = solid_harmonics(6) # Inclusive, so computes up to l = 6 sh[3][-3] # symbolic f-phi angular function Args: lmax (int): highest order angular momentum quantum number scaled (bool): if scaled, includes factor of 1 / (2 * np.pi ** 0.5) """ def _top_sh(lp, kr, sp, sm): return ((2**kr * (2 * lp + 1) / (2 * lp + 2))**0.5 * (_x * sp - (1 - kr) * _y * sm)) def _mid_sh(lp, m, sm, smm): return (((2 * lp + 1) * _z * sm - ((lp + m) * (lp - m))**0.5 * (_x * _x + _y * _y + _z * _z) * smm) / (((lp + m + 1) * (lp - m + 1))**0.5)) def _bot_sh(lp, kr, sp, sm): return ((2**kr * (2 * lp + 1) / (2 * lp + 2))**0.5 * (_y * sp + (1 - kr) * _x * sm)) sh = OrderedDict([(l, OrderedDict([])) for l in range(lmax + 1)]) sh[0][0] = Integer(1) for L in range(1, lmax + 1): lp = L - 1 kr = int(not lp) mls = list(range(-L, L + 1)) sh[L][mls[0]] = _bot_sh(lp, kr, sh[lp][lp], sh[lp][-lp]) for ml in mls[1:-1]: try: rec = sh[lp - 1][ml] except KeyError: rec = sh[lp][ml] sh[L][ml] = _mid_sh(lp, ml, sh[lp][ml], rec) sh[L][mls[-1]] = _top_sh(lp, kr, sh[lp][lp], sh[lp][-lp]) if scaled: for L in range(lmax + 1): for ml in range(-L, L + 1): sh[L][ml] /= (2 * np.pi**0.5) return sh
def test_complex_expression(self): expression = 3**f(42) + 23 - f( 43, 44) + f(45 + a) * sin(g(f(46, 47, 48) + 17) - g(4)) + sin( f(42)) self.assertEqual( collect_arguments(expression, f), {(Integer(42), ), (Integer(43), Integer(44)), (45 + a, ), (Integer(46), Integer(47), Integer(48))}) self.assertEqual(count_calls(expression, f), 5) self.assertTrue(has_function(expression, f)) self.assertEqual( replace_function(expression, f, g), 3**g(42) + 23 - g(43, 44) + g(45 + a) * sin(g(g(46, 47, 48) + 17) - g(4)) + sin(g(42)))
def legendre(n, x): e = Integer(1)/(Integer(2)**n * fact(Integer(n))) * diff((x**2-1)**n, x, n) return e.expand()
def test_conv5(): x = Integer(5) y = Integer(6) assert x._sympy_() == sympy.Integer(5) assert (x/y)._sympy_() == sympy.Integer(5) / sympy.Integer(6)