Ejemplo n.º 1
0
def test_mod():
    x = Rational(1, 2)
    y = Rational(3, 4)
    z = Rational(5, 18043)

    assert x % x == 0
    assert x % y == 1 / S(2)
    assert x % z == 3 / S(36086)
    assert y % x == 1 / S(4)
    assert y % y == 0
    assert y % z == 9 / S(72172)
    assert z % x == 5 / S(18043)
    assert z % y == 5 / S(18043)
    assert z % z == 0

    a = Real('2.6')

    assert round(a % Real('0.2'), 15) == 0.2
    assert round(a % 2, 15) == 0.6
    assert round(a % 0.5, 15) == 0.1
    assert Rational(3, 4) % Real(1.1) == 0.75

    a = Integer(7)
    b = Integer(4)

    assert type(a % b) == Integer
Ejemplo n.º 2
0
def test_Real():
    def eq(a, b):
        t = Real("1.0E-15")
        return (-t < a-b < t)

    a = Real(2) ** Real(3)
    assert eq(a.evalf(), Real(8))
    assert eq((pi ** -1).evalf(), Real("0.31830988618379067"))
    a = Real(2) ** Real(4)
    assert eq(a.evalf(), Real(16))
    assert (S(.3) == S(.5)) is False
    x_str = Real((0, '13333333333333L', -52, 53))
    x2_str = Real((0, '26666666666666L', -53, 53))
    x_hex = Real((0, 0x13333333333333L, -52, 53))
    x_dec = Real((0, 5404319552844595L, -52, 53))
    x2_hex = Real((0, 0x13333333333333L*2, -53, 53))
    assert x_str == x_hex == x_dec == x2_hex == Real(1.2)
    # x2_str and 1.2 are superficially the same
    assert str(x2_str) == str(Real(1.2))
    # but are different at the mpf level
    assert Real(1.2)._mpf_ == (0, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

    # do not automatically evalf
    def teq(a):
        assert (a.evalf () == a) is False
        assert (a.evalf () != a) is True
        assert (a == a.evalf()) is False
        assert (a != a.evalf()) is True

    teq(pi)
    teq(2*pi)
    teq(cos(0.1, evaluate=False))
Ejemplo n.º 3
0
def test_Real():
    # NOTE prec is the whole number of decimal digits
    assert str(Real('1.23', prec=1+2))    == '1.23'
    assert str(Real('1.23456789', prec=1+8))  == '1.23456789'
    assert str(Real('1.234567890123456789', prec=1+18))    == '1.234567890123456789'
    assert str(pi.evalf(1+2))   == '3.14'
    assert str(pi.evalf(1+14))  == '3.14159265358979'
    assert str(pi.evalf(1+64))  == '3.1415926535897932384626433832795028841971693993751058209749445923'
Ejemplo n.º 4
0
def test_Real_gcd_lcm_cofactors():
    assert Real(2.0).gcd(Integer(4)) == S.One
    assert Real(2.0).lcm(Integer(4)) == Real(8.0)
    assert Real(2.0).cofactors(Integer(4)) == (S.One, Real(2.0), Integer(4))

    assert Real(2.0).gcd(Rational(1, 2)) == S.One
    assert Real(2.0).lcm(Rational(1, 2)) == Real(1.0)
    assert Real(2.0).cofactors(Rational(1, 2)) == (S.One, Real(2.0),
                                                   Rational(1, 2))
Ejemplo n.º 5
0
def test_issue629():
    x = Symbol("x")
    assert (Rational(1, 2) * array([2 * x, 0]) == array([x, 0])).all()
    assert (Rational(1, 2) + array([2 * x, 0]) == array(
        [2 * x + Rational(1, 2), Rational(1, 2)])).all()
    assert (Real("0.5") * array([2 * x, 0]) == array([Real("1.0") * x,
                                                      0])).all()
    assert (Real("0.5") + array([2 * x, 0]) == array(
        [2 * x + Real("0.5"), Real("0.5")])).all()
Ejemplo n.º 6
0
def test_Real():
    def eq(a, b):
        t = Real("1.0E-15")
        return (-t < a-b < t)

    a = Real(2) ** Real(3)
    assert eq(a.evalf(), Real(8))
    assert eq((pi ** -1).evalf(), Real("0.31830988618379067"))
    a = Real(2) ** Real(4)
    assert eq(a.evalf(), Real(16))
Ejemplo n.º 7
0
def test_RootOf_evalf():
    real = RootOf(x**3 + x + 3, 0).evalf(n=20)

    assert real.epsilon_eq(Real("-1.2134116627622296341"))

    re, im = RootOf(x**3 + x + 3, 1).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Real("0.60670583138111481707"))
    assert im.epsilon_eq(Real("1.45061224918844152650"))

    re, im = RootOf(x**3 + x + 3, 2).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Real("0.60670583138111481707"))
    assert im.epsilon_eq(-Real("1.45061224918844152650"))
Ejemplo n.º 8
0
def test_function_comparable():
    x = Symbol('x')

    assert sin(x).is_comparable == False
    assert cos(x).is_comparable == False

    assert sin(Real('0.1')).is_comparable == True
    assert cos(Real('0.1')).is_comparable == True

    assert sin(E).is_comparable == True
    assert cos(E).is_comparable == True

    assert sin(Rational(1, 3)).is_comparable == True
    assert cos(Rational(1, 3)).is_comparable == True
Ejemplo n.º 9
0
def test_is_number():
    g = WildFunction('g')

    assert Real(3.14).is_number == True
    assert Integer(737).is_number == True
    assert Rational(3, 2).is_number == True
    assert Rational(8).is_number == True
    assert x.is_number == False
    assert (2 * x).is_number == False
    assert (x + y).is_number == False
    assert log(2).is_number == True
    assert log(x).is_number == False
    assert (2 + log(2)).is_number == True
    assert (8 + log(2)).is_number == True
    assert (2 + log(x)).is_number == False
    assert (8 + log(2) + x).is_number == False
    assert (2 * g).is_number == False
    assert (1 + x**2 / x - x).is_number == True

    # test extensibility of .is_number
    # on subinstances of Basic
    class A(Basic):
        pass

    a = A()
    assert a.is_number == False
Ejemplo n.º 10
0
def test_is_number():
    assert Real(3.14).is_number == True
    assert Integer(737).is_number == True
    assert Rational(3, 2).is_number == True
    assert Rational(8).is_number == True
    assert x.is_number == False
    assert (2 * x).is_number == False
    assert (x + y).is_number == False
    assert log(2).is_number == True
    assert log(x).is_number == False
    assert (2 + log(2)).is_number == True
    assert (8 + log(2)).is_number == True
    assert (2 + log(x)).is_number == False
    assert (8 + log(2) + x).is_number == False
    assert (1 + x**2 / x - x).is_number == True
    assert Tuple(Integer(1)).is_number == False
    assert Add(2, x).is_number == False
    assert Mul(3, 4).is_number == True
    assert Pow(log(2), 2).is_number == True
    assert oo.is_number == True
    g = WildFunction('g')
    assert g.is_number == False
    assert (2 * g).is_number == False
    assert (x**2).subs(x, 3).is_number == True

    # test extensibility of .is_number
    # on subinstances of Basic
    class A(Basic):
        pass

    a = A()
    assert a.is_number == False
Ejemplo n.º 11
0
def test_systematic_basic():
    def s(sympy_object, numpy_array):
        x = sympy_object + numpy_array
        x = numpy_array + sympy_object
        x = sympy_object - numpy_array
        x = numpy_array - sympy_object
        x = sympy_object * numpy_array
        x = numpy_array * sympy_object
        x = sympy_object / numpy_array
        x = numpy_array / sympy_object
        x = sympy_object**numpy_array
        x = numpy_array**sympy_object

    x = Symbol("x")
    y = Symbol("y")
    sympy_objs = [
        Rational(2),
        Real("1.3"),
        x,
        y,
        pow(x, y) * y,
        5,
        5.5,
    ]
    numpy_objs = [
        array([1]),
        array([3, 8, -1]),
        array([x, x**2, Rational(5)]),
        array([x / y * sin(y), 5, Rational(5)]),
    ]
    for x in sympy_objs:
        for y in numpy_objs:
            s(x, y)
Ejemplo n.º 12
0
def g(yieldCurve, zeroRates,n, verbose):
    '''
        generates recursively the zero curve 
        expressions eval('(0.06/1.05)+(1.06/(1+x)**2)-1')
        solves these expressions to get the new rate
        for that period
    
    '''
    if len(zeroRates) >= len(yieldCurve):
        print "\n\n\t+zero curve boot strapped [%d iterations]" % (n)
        return
    else:
        legn = ''
        for i in range(0,len(zeroRates),1):
            if i == 0:
                legn = '%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
            else:
                legn = legn + ' +%2.6f/(1+%2.6f)**%d'%(yieldCurve[n], zeroRates[i],i+1)
        legn = legn + '+ (1+%2.6f)/(1+x)**%d-1'%(yieldCurve[n], n+1)
        # solve the expression for this iteration
        if verbose:
            print "-[%d] %s" % (n, legn.strip())
        rate1 = solve(eval(legn), x)
        # Abs here since some solutions can be complex
        rate1 = min([Real(abs(r)) for r in rate1])
        if verbose:
            print "-[%d] solution %2.6f" % (n, float(rate1))
        # stuff the new rate in the results, will be 
        # used by the next iteration
        zeroRates.append(rate1)
        g(yieldCurve, zeroRates,n+1, verbose)
Ejemplo n.º 13
0
def test_hbar():
    assert hbar.is_commutative == True
    assert hbar.is_real == True
    assert hbar.is_positive == True
    assert hbar.is_negative == False
    assert hbar.is_irrational == True

    assert hbar.evalf() == Real(1.05457162e-34)
Ejemplo n.º 14
0
def test_function_evalf():
    def eq(a, b, eps):
        return abs(a - b) < eps

    assert eq(sin(1).evalf(15), Real("0.841470984807897"), 1e-13)
    assert eq(sin(2).evalf(25), Real("0.9092974268256816953960199", 25), 1e-23)
    assert eq(
        sin(1 + I).evalf(15),
        Real("1.29845758141598") + Real("0.634963914784736") * I, 1e-13)
    assert eq(
        exp(1 + I).evalf(15),
        Real("1.46869393991588") + Real("2.28735528717884239") * I, 1e-13)
    assert eq(
        log(pi + sqrt(2) * I).evalf(15),
        Real("1.23699044022052") + Real("0.422985442737893") * I, 1e-13)
    assert eq(cos(100).evalf(15), Real("0.86231887228768"), 1e-13)
Ejemplo n.º 15
0
def E_nl_dirac(n, l, spin_up=True, Z=1, c=Real("137.035999037")):
    """
    Returns the relativistic energy of the state (n, l, spin) in Hartree atomic
    units.

    The energy is calculated from the Dirac equation. The rest mass energy is
    *not* included.

    n, l ...... quantum numbers 'n' and 'l'
    spin_up ... True if the electron spin is up (default), otherwise down
    Z    ...... atomic number (1 for Hydrogen, 2 for Helium, ...)
    c    ...... speed of light in atomic units. Default value is 137.035999037,
                taken from: http://arxiv.org/abs/1012.3627

    Examples::

    >>> from sympy.physics.hydrogen import E_nl_dirac
    >>> E_nl_dirac(1, 0)
    -0.500006656595360

    >>> E_nl_dirac(2, 0)
    -0.125002080189006
    >>> E_nl_dirac(2, 1)
    -0.125000416024704
    >>> E_nl_dirac(2, 1, False)
    -0.125002080189006

    >>> E_nl_dirac(3, 0)
    -0.0555562951740285
    >>> E_nl_dirac(3, 1)
    -0.0555558020932949
    >>> E_nl_dirac(3, 1, False)
    -0.0555562951740285
    >>> E_nl_dirac(3, 2)
    -0.0555556377366884
    >>> E_nl_dirac(3, 2, False)
    -0.0555558020932949


    """
    if not (l >= 0):
        raise ValueError("'l' must be positive or zero")
    if not (n > l):
        raise ValueError("'n' must be greater than 'l'")
    if (l==0 and spin_up is False):
        raise ValueError("Spin must be up for l==0.")
    # skappa is sign*kappa, where sign contains the correct sign
    if spin_up:
        skappa = -l - 1
    else:
        skappa = -l
    c = S(c)
    beta = sqrt(skappa**2 - Z**2/c**2)
    return c**2/sqrt(1+Z**2/(n + skappa + beta)**2/c**2) - c**2
Ejemplo n.º 16
0
def test_sympy_lambda():
    f = lambdify(x, sin(x), "sympy")
    assert f(x) is sin(x)
    prec = 1e-15
    assert -prec < f(Rational(1, 5)).evalf() - Real(str(sin02)) < prec
    try:
        # arctan is in numpy module and should not be available
        f = lambdify(x, arctan(x), "sympy")
        assert False
    except NameError:
        pass
Ejemplo n.º 17
0
def test_lambertw():
    x = Symbol('x')
    assert LambertW(x) == LambertW(x)
    assert LambertW(0) == 0
    assert LambertW(E) == 1
    assert LambertW(-1/E) == -1
    assert LambertW(-log(2)/2) == -log(2)
    assert LambertW(oo) == oo
    assert LambertW(x**2).diff(x) == 2*LambertW(x**2)/x/(1+LambertW(x**2))
    assert LambertW(sqrt(2)).evalf(30).epsilon_eq(
        Real("0.701338383413663009202120278965",30),1e-29)
Ejemplo n.º 18
0
def test_Real():
    def eq(a, b):
        t = Real("1.0E-15")
        return -t < a - b < t

    a = Real(2) ** Real(3)
    assert eq(a.evalf(), Real(8))
    assert eq((pi ** -1).evalf(), Real("0.31830988618379067"))
    a = Real(2) ** Real(4)
    assert eq(a.evalf(), Real(16))
    assert (S(0.3) == S(0.5)) is False
    x_str = Real((0, "13333333333333L", -52, 53))
    x2_str = Real((0, "26666666666666L", -53, 53))
    x_hex = Real((0, 0x13333333333333L, -52, 53))
    x_dec = Real((0, 5404319552844595L, -52, 53))
    x2_hex = Real((0, 0x13333333333333L * 2, -53, 53))
    assert x_str == x_hex == x_dec == x2_hex == Real(1.2)
    # x2_str and 1.2 are superficially the same
    assert str(x2_str) == str(Real(1.2))
    # but are different at the mpf level
    assert Real(1.2)._mpf_ == (0, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)
Ejemplo n.º 19
0
def test_zeta():

    assert zeta(nan) == nan
    assert zeta(x, nan) == nan

    assert zeta(0) == Rational(-1,2)
    assert zeta(0, x) == Rational(1,2) - x

    assert zeta(1) == zoo
    assert zeta(1, 2) == zoo
    assert zeta(1, -7) == zoo
    assert zeta(1, x) == zoo

    assert zeta(2, 0) == pi**2/6
    assert zeta(2, 1) == pi**2/6

    assert zeta(2) == pi**2/6
    assert zeta(4) == pi**4/90
    assert zeta(6) == pi**6/945

    assert zeta(2, 2) == pi**2/6 - 1
    assert zeta(4, 3) == pi**4/90 - Rational(17, 16)
    assert zeta(6, 4) == pi**6/945 - Rational(47449, 46656)

    assert zeta(2, -2) == pi**2/6 + Rational(5, 4)
    assert zeta(4, -3) == pi**4/90 + Rational(1393, 1296)
    assert zeta(6, -4) == pi**6/945 + Rational(3037465, 2985984)

    assert zeta(-1) == -Rational(1, 12)
    assert zeta(-2) == 0
    assert zeta(-3) == Rational(1, 120)
    assert zeta(-4) == 0
    assert zeta(-5) == -Rational(1, 252)

    assert zeta(-1, 3) == -Rational(37, 12)
    assert zeta(-1, 7) == -Rational(253, 12)
    assert zeta(-1, -4) == Rational(119, 12)
    assert zeta(-1, -9) == Rational(539, 12)

    assert zeta(-4, 3) == -17
    assert zeta(-4, -8) == 8772

    assert zeta(0, 0) == -Rational(1, 2)

    assert zeta(0, 1) == -Rational(1, 2)
    assert zeta(0, -1) == Rational(1, 2)

    assert zeta(0, 2) == -Rational(3, 2)
    assert zeta(0, -2) == Rational(3, 2)

    assert zeta(3).evalf(20).epsilon_eq(Real("1.2020569031595942854",20), 1e-19)
Ejemplo n.º 20
0
def test_sympifyit():
    x = Symbol('x')
    y = Symbol('y')

    @_sympifyit('b', NotImplemented)
    def add(a, b):
        return a + b

    assert add(x, 1) == x + 1
    assert add(x, 0.5) == x + Real('0.5')
    assert add(x, y) == x + y

    assert add(x, '1') == NotImplemented

    @_sympifyit('b')
    def add_raises(a, b):
        return a + b

    assert add_raises(x, 1) == x + 1
    assert add_raises(x, 0.5) == x + Real('0.5')
    assert add_raises(x, y) == x + y

    raises(SympifyError, "add_raises(x, '1')")
Ejemplo n.º 21
0
def test_Rational_gcd_lcm_cofactors():
    assert Integer(4).gcd(2) == Integer(2)
    assert Integer(4).lcm(2) == Integer(4)
    assert Integer(4).gcd(Integer(2)) == Integer(2)
    assert Integer(4).lcm(Integer(2)) == Integer(4)

    assert Integer(4).gcd(3) == Integer(1)
    assert Integer(4).lcm(3) == Integer(12)
    assert Integer(4).gcd(Integer(3)) == Integer(1)
    assert Integer(4).lcm(Integer(3)) == Integer(12)

    assert Rational(4, 3).gcd(2) == Rational(2, 3)
    assert Rational(4, 3).lcm(2) == Integer(4)
    assert Rational(4, 3).gcd(Integer(2)) == Rational(2, 3)
    assert Rational(4, 3).lcm(Integer(2)) == Integer(4)

    assert Integer(4).gcd(Rational(2, 9)) == Rational(2, 9)
    assert Integer(4).lcm(Rational(2, 9)) == Integer(4)

    assert Rational(4, 3).gcd(Rational(2, 9)) == Rational(2, 9)
    assert Rational(4, 3).lcm(Rational(2, 9)) == Rational(4, 3)
    assert Rational(4, 5).gcd(Rational(2, 9)) == Rational(2, 45)
    assert Rational(4, 5).lcm(Rational(2, 9)) == Integer(4)

    assert Integer(4).cofactors(2) == (Integer(2), Integer(2), Integer(1))
    assert Integer(4).cofactors(Integer(2)) == (Integer(2), Integer(2),
                                                Integer(1))

    assert Integer(4).gcd(Real(2.0)) == S.One
    assert Integer(4).lcm(Real(2.0)) == Real(8.0)
    assert Integer(4).cofactors(Real(2.0)) == (S.One, Integer(4), Real(2.0))

    assert Rational(1, 2).gcd(Real(2.0)) == S.One
    assert Rational(1, 2).lcm(Real(2.0)) == Real(1.0)
    assert Rational(1,
                    2).cofactors(Real(2.0)) == (S.One, Rational(1,
                                                                2), Real(2.0))
Ejemplo n.º 22
0
def dotest(s):
    x = Symbol("x")
    y = Symbol("y")
    l = [
    Rational(2),
    Real("1.3"),
    x,
    y,
    pow(x,y)*y,
    5,
    5.5
    ]
    for x in l:
        for y in l:
            s(x,y)
Ejemplo n.º 23
0
def test_is_number():
    x, y = symbols('xy')
    g = WildFunction('g')

    assert Real(3.14).is_number == True
    assert Integer(737).is_number == True
    assert Rational(3, 2).is_number == True

    assert x.is_number == False
    assert (2*x).is_number == False

    assert (x + y).is_number == False

    assert log(2).is_number == True
    assert log(x).is_number == False

    assert (2 + log(2)).is_number == True
    assert (2 + log(x)).is_number == False

    assert (2*g).is_number == False
Ejemplo n.º 24
0
def test_Mul():
    assert str(x/y) == "x/y"
    assert str(y/x) == "y/x"
    assert str(x/y/z) == "x/(y*z)"
    assert str((x+1)/(y+2)) == "(1 + x)/(2 + y)"
    assert str(2*x/3)  ==  '2*x/3'
    assert str(-2*x/3)  == '-2*x/3'

    class CustomClass1(Expr):
        pass
    class CustomClass2(Expr):
        pass
    cc1 = CustomClass1(commutative=True)
    cc2 = CustomClass2(commutative=True)
    assert str(Rational(2)*cc1) == '2*CustomClass1()'
    assert str(cc1*Rational(2)) == '2*CustomClass1()'
    assert str(cc1*Real("1.5")) == '1.5*CustomClass1()'
    assert str(cc2*Rational(2)) == '2*CustomClass2()'
    assert str(cc2*Rational(2)*cc1) == '2*CustomClass1()*CustomClass2()'
    assert str(cc1*Rational(2)*cc2) == '2*CustomClass1()*CustomClass2()'
Ejemplo n.º 25
0
def test_as_coeff_Mul():
    Integer(3).as_coeff_Mul() == (Integer(3), Integer(1))
    Rational(3, 4).as_coeff_Mul() == (Rational(3, 4), Integer(1))
    Real(5.0).as_coeff_Mul() == (Real(5.0), Integer(1))

    (Integer(3) * x).as_coeff_Mul() == (Integer(3), x)
    (Rational(3, 4) * x).as_coeff_Mul() == (Rational(3, 4), x)
    (Real(5.0) * x).as_coeff_Mul() == (Real(5.0), x)

    (Integer(3) * x * y).as_coeff_Mul() == (Integer(3), x * y)
    (Rational(3, 4) * x * y).as_coeff_Mul() == (Rational(3, 4), x * y)
    (Real(5.0) * x * y).as_coeff_Mul() == (Real(5.0), x * y)

    (x).as_coeff_Mul() == (S.One, x)
    (x * y).as_coeff_Mul() == (S.One, x * y)
Ejemplo n.º 26
0
def test__sympify():
    x = Symbol('x')
    f = Function('f')

    # positive _sympify
    assert _sympify(x)      is x
    assert _sympify(f)      is f
    assert _sympify(1)      == Integer(1)
    assert _sympify(0.5)    == Real("0.5")
    assert _sympify(1+1j)   == 1 + I

    class A:
        def _sympy_(self):
            return Integer(5)

    a = A()
    assert _sympify(a)      == Integer(5)

    # negative _sympify
    raises(SympifyError, "_sympify('1')")
    raises(SympifyError, "_sympify([1,2,3])")
Ejemplo n.º 27
0
def test_issue883():
    a = [3,2.0]
    assert sympify(a) == [Integer(3), Real(2.0)]
    assert sympify(tuple(a)) == (Integer(3), Real(2.0))
    assert sympify(set(a)) == set([Integer(3), Real(2.0)])
Ejemplo n.º 28
0
 def _sympy_(self):
     return Real(1.1)
Ejemplo n.º 29
0
def test_log_apply_evalf():
    value = (log(3)/log(2)-1).evalf()
    assert value.epsilon_eq(Real("0.58496250072115618145373"))
Ejemplo n.º 30
0
def test_Real():
    sT(Real('1.23', prec=3), "Real('1.22998', prec=3)")
    sT(Real('1.23456789', prec=9), "Real('1.23456788994', prec=9)")
    sT(Real('1.234567890123456789', prec=19),
       "Real('1.234567890123456789013', prec=19)")
    sT(Real('0.60038617995049726', 15), "Real('0.60038617995049726', prec=15)")
Ejemplo n.º 31
0
def test_real_mul():
    Real(0) * pi * x == Real(0)
    Real(1) * pi * x == pi * x
    len((Real(2) * pi * x).args) == 3
Ejemplo n.º 32
0
def test_conversion_to_mpmath():
    assert mpmath.mpmathify(Integer(1)) == mpmath.mpf(1)
    assert mpmath.mpmathify(Rational(1, 2)) == mpmath.mpf(0.5)
    assert mpmath.mpmathify(Real('1.23')) == mpmath.mpf('1.23')
Ejemplo n.º 33
0
def test_dont_accept_str():
    assert Real("0.2") != "0.2"
    assert not (Real("0.2") == "0.2")