def feq(a, b): """Test if two floating point values are 'equal'.""" t = Float("1.0E-10") return -t < a - b < t
def test_accept_int(): assert Float(4) == 4
def test_divmod(): assert divmod(S(12), S(8)) == Tuple(1, 4) assert divmod(-S(12), S(8)) == Tuple(-2, 4) assert divmod(S(0), S(1)) == Tuple(0, 0) raises(ZeroDivisionError, lambda: divmod(S(0), S(0))) raises(ZeroDivisionError, lambda: divmod(S(1), S(0))) assert divmod(S(12), 8) == Tuple(1, 4) assert divmod(12, S(8)) == Tuple(1, 4) assert divmod(S("2"), S("3/2")) == Tuple(S("1"), S("1/2")) assert divmod(S("3/2"), S("2")) == Tuple(S("0"), S("3/2")) assert divmod(S("2"), S("3.5")) == Tuple(S("0"), S("2")) assert divmod(S("3.5"), S("2")) == Tuple(S("1"), S("1.5")) assert divmod(S("2"), S("1/3")) == Tuple(S("6"), S("0")) assert divmod(S("1/3"), S("2")) == Tuple(S("0"), S("1/3")) assert divmod(S("2"), S("0.1")) == Tuple(S("20"), S("0")) assert divmod(S("0.1"), S("2")) == Tuple(S("0"), S("0.1")) assert divmod(S("2"), 2) == Tuple(S("1"), S("0")) assert divmod(2, S("2")) == Tuple(S("1"), S("0")) assert divmod(S("2"), 1.5) == Tuple(S("1"), S("0.5")) assert divmod(1.5, S("2")) == Tuple(S("0"), S("1.5")) assert divmod(0.3, S("2")) == Tuple(S("0"), S("0.3")) assert divmod(S("3/2"), S("3.5")) == Tuple(S("0"), S("3/2")) assert divmod(S("3.5"), S("3/2")) == Tuple(S("2"), S("0.5")) assert divmod(S("3/2"), S("1/3")) == Tuple(S("4"), Float("1/6")) assert divmod(S("1/3"), S("3/2")) == Tuple(S("0"), S("1/3")) assert divmod(S("3/2"), S("0.1")) == Tuple(S("15"), S("0")) assert divmod(S("0.1"), S("3/2")) == Tuple(S("0"), S("0.1")) assert divmod(S("3/2"), 2) == Tuple(S("0"), S("3/2")) assert divmod(2, S("3/2")) == Tuple(S("1"), S("0.5")) assert divmod(S("3/2"), 1.5) == Tuple(S("1"), S("0")) assert divmod(1.5, S("3/2")) == Tuple(S("1"), S("0")) assert divmod(S("3/2"), 0.3) == Tuple(S("5"), S("0")) assert divmod(0.3, S("3/2")) == Tuple(S("0"), S("0.3")) assert divmod(S("1/3"), S("3.5")) == Tuple(S("0"), S("1/3")) assert divmod(S("3.5"), S("0.1")) == Tuple(S("35"), S("0")) assert divmod(S("0.1"), S("3.5")) == Tuple(S("0"), S("0.1")) assert divmod(S("3.5"), 2) == Tuple(S("1"), S("1.5")) assert divmod(2, S("3.5")) == Tuple(S("0"), S("2")) assert divmod(S("3.5"), 1.5) == Tuple(S("2"), S("0.5")) assert divmod(1.5, S("3.5")) == Tuple(S("0"), S("1.5")) assert divmod(0.3, S("3.5")) == Tuple(S("0"), S("0.3")) assert divmod(S("0.1"), S("1/3")) == Tuple(S("0"), S("0.1")) assert divmod(S("1/3"), 2) == Tuple(S("0"), S("1/3")) assert divmod(2, S("1/3")) == Tuple(S("6"), S("0")) assert divmod(S("1/3"), 1.5) == Tuple(S("0"), S("1/3")) assert divmod(0.3, S("1/3")) == Tuple(S("0"), S("0.3")) assert divmod(S("0.1"), 2) == Tuple(S("0"), S("0.1")) assert divmod(2, S("0.1")) == Tuple(S("20"), S("0")) assert divmod(S("0.1"), 1.5) == Tuple(S("0"), S("0.1")) assert divmod(1.5, S("0.1")) == Tuple(S("15"), S("0")) assert divmod(S("0.1"), 0.3) == Tuple(S("0"), S("0.1")) assert str(divmod(S("2"), 0.3)) == '(6, 0.2)' assert str(divmod(S("3.5"), S("1/3"))) == '(10, 0.166666666666667)' assert str(divmod(S("3.5"), 0.3)) == '(11, 0.2)' assert str(divmod(S("1/3"), S("0.1"))) == '(3, 0.0333333333333333)' assert str(divmod(1.5, S("1/3"))) == '(4, 0.166666666666667)' assert str(divmod(S("1/3"), 0.3)) == '(1, 0.0333333333333333)' assert str(divmod(0.3, S("0.1"))) == '(2, 0.1)' assert divmod(-3, S(2)) == (-2, 1) assert divmod(S(-3), S(2)) == (-2, 1) assert divmod(S(-3), 2) == (-2, 1)
def test_Interval_is_left_unbounded(): assert Interval(3, 4).is_left_unbounded is False assert Interval(-oo, 3).is_left_unbounded is True assert Interval(Float("-inf"), 3).is_left_unbounded is True
def test_limit_with_Float(): k = symbols("k") assert limit(1.0 ** k, k, oo) == 1 assert limit(0.3*1.0**k, k, oo) == Float(0.3)
def test_bug(): x1 = Symbol('x1') x2 = Symbol('x2') y = x1 * x2 assert y.subs(x1, Float(3.0)) == Float(3.0) * x2
def test_function_evalf(): def eq(a, b, eps): return abs(a - b) < eps assert eq(sin(1).evalf(15), Float("0.841470984807897"), 1e-13) assert eq( sin(2).evalf(25), Float("0.9092974268256816953960199", 25), 1e-23) assert eq( sin(1 + I).evalf(15), Float("1.29845758141598") + Float("0.634963914784736") * I, 1e-13) assert eq( exp(1 + I).evalf(15), Float("1.46869393991588") + Float("2.28735528717884239") * I, 1e-13) assert eq( exp(-0.5 + 1.5 * I).evalf(15), Float("0.0429042815937374") + Float("0.605011292285002") * I, 1e-13) assert eq( log(pi + sqrt(2) * I).evalf(15), Float("1.23699044022052") + Float("0.422985442737893") * I, 1e-13) assert eq(cos(100).evalf(15), Float("0.86231887228768"), 1e-13)
def test_Float_eval(): a = Float(3.2) assert (a**2).is_Float
def test_Infinity(): assert oo != 1 assert 1 * oo == oo assert 1 != oo assert oo != -oo assert oo != Symbol("x")**3 assert oo + 1 == oo assert 2 + oo == oo assert 3 * oo + 2 == oo assert S.Half**oo == 0 assert S.Half**(-oo) == oo assert -oo * 3 == -oo assert oo + oo == oo assert -oo + oo * (-5) == -oo assert 1 / oo == 0 assert 1 / (-oo) == 0 assert 8 / oo == 0 assert oo % 2 == nan assert 2 % oo == nan assert oo / oo == nan assert oo / -oo == nan assert -oo / oo == nan assert -oo / -oo == nan assert oo - oo == nan assert oo - -oo == oo assert -oo - oo == -oo assert -oo - -oo == nan assert oo + -oo == nan assert -oo + oo == nan assert oo + oo == oo assert -oo + oo == nan assert oo + -oo == nan assert -oo + -oo == -oo assert oo * oo == oo assert -oo * oo == -oo assert oo * -oo == -oo assert -oo * -oo == oo assert oo / 0 == oo assert -oo / 0 == -oo assert 0 / oo == 0 assert 0 / -oo == 0 assert oo * 0 == nan assert -oo * 0 == nan assert 0 * oo == nan assert 0 * -oo == nan assert oo + 0 == oo assert -oo + 0 == -oo assert 0 + oo == oo assert 0 + -oo == -oo assert oo - 0 == oo assert -oo - 0 == -oo assert 0 - oo == -oo assert 0 - -oo == oo assert oo / 2 == oo assert -oo / 2 == -oo assert oo / -2 == -oo assert -oo / -2 == oo assert oo * 2 == oo assert -oo * 2 == -oo assert oo * -2 == -oo assert 2 / oo == 0 assert 2 / -oo == 0 assert -2 / oo == 0 assert -2 / -oo == 0 assert 2 * oo == oo assert 2 * -oo == -oo assert -2 * oo == -oo assert -2 * -oo == oo assert 2 + oo == oo assert 2 - oo == -oo assert -2 + oo == oo assert -2 - oo == -oo assert 2 + -oo == -oo assert 2 - -oo == oo assert -2 + -oo == -oo assert -2 - -oo == oo assert S(2) + oo == oo assert S(2) - oo == -oo assert oo / I == -oo * I assert -oo / I == oo * I assert oo * float(1) == Float('inf') and (oo * float(1)).is_Float assert -oo * float(1) == Float('-inf') and (-oo * float(1)).is_Float assert oo / float(1) == Float('inf') and (oo / float(1)).is_Float assert -oo / float(1) == Float('-inf') and (-oo / float(1)).is_Float assert oo * float(-1) == Float('-inf') and (oo * float(-1)).is_Float assert -oo * float(-1) == Float('inf') and (-oo * float(-1)).is_Float assert oo / float(-1) == Float('-inf') and (oo / float(-1)).is_Float assert -oo / float(-1) == Float('inf') and (-oo / float(-1)).is_Float assert oo + float(1) == Float('inf') and (oo + float(1)).is_Float assert -oo + float(1) == Float('-inf') and (-oo + float(1)).is_Float assert oo - float(1) == Float('inf') and (oo - float(1)).is_Float assert -oo - float(1) == Float('-inf') and (-oo - float(1)).is_Float assert float(1) * oo == Float('inf') and (float(1) * oo).is_Float assert float(1) * -oo == Float('-inf') and (float(1) * -oo).is_Float assert float(1) / oo == 0 assert float(1) / -oo == 0 assert float(-1) * oo == Float('-inf') and (float(-1) * oo).is_Float assert float(-1) * -oo == Float('inf') and (float(-1) * -oo).is_Float assert float(-1) / oo == 0 assert float(-1) / -oo == 0 assert float(1) + oo == Float('inf') assert float(1) + -oo == Float('-inf') assert float(1) - oo == Float('-inf') assert float(1) - -oo == Float('inf') assert Float('nan') == nan assert nan * 1.0 == nan assert -1.0 * nan == nan assert nan * oo == nan assert nan * -oo == nan assert nan / oo == nan assert nan / -oo == nan assert nan + oo == nan assert nan + -oo == nan assert nan - oo == nan assert nan - -oo == nan assert -oo * S.Zero == nan assert oo * nan == nan assert -oo * nan == nan assert oo / nan == nan assert -oo / nan == nan assert oo + nan == nan assert -oo + nan == nan assert oo - nan == nan assert -oo - nan == nan assert S.Zero * oo == nan assert oo.is_Rational is False assert isinstance(oo, Rational) is False assert S.One / oo == 0 assert -S.One / oo == 0 assert S.One / -oo == 0 assert -S.One / -oo == 0 assert S.One * oo == oo assert -S.One * oo == -oo assert S.One * -oo == -oo assert -S.One * -oo == oo assert S.One / nan == nan assert S.One - -oo == oo assert S.One + nan == nan assert S.One - nan == nan assert nan - S.One == nan assert nan / S.One == nan assert -oo - S.One == -oo
def eq(a, b): t = Float("1.0E-15") return (-t < a - b < t)
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 = Float(2.6) assert (a % .2) == 0 assert (a % 2).round(15) == 0.6 assert (a % 0.5).round(15) == 0.1 # In these two tests, if the precision of m does # not match the precision of the ans, then it is # likely that the change made now gives an answer # with degraded accuracy. r = Rational(500, 41) f = Float('.36', 3) m = r % f ans = Float(r % Rational(f), 3) assert m == ans and m._prec == ans._prec f = Float('8.36', 3) m = f % r ans = Float(Rational(f) % r, 3) assert m == ans and m._prec == ans._prec s = S.Zero assert s % float(1) == S.Zero # No rounding required since these numbers can be represented # exactly. assert Rational(3, 4) % Float(1.1) == 0.75 assert Float(1.5) % Rational(5, 4) == 0.25 assert Rational(5, 4).__rmod__(Float('1.5')) == 0.25 assert Float('1.5').__rmod__(Float('2.75')) == Float('1.25') assert 2.75 % Float('1.5') == Float('1.25') a = Integer(7) b = Integer(4) assert type(a % b) == Integer assert a % b == Integer(3) assert Integer(1) % Rational(2, 3) == Rational(1, 3) assert Rational(7, 5) % Integer(1) == Rational(2, 5) assert Integer(2) % 1.5 == 0.5 assert Integer(3).__rmod__(Integer(10)) == Integer(1) assert Integer(10) % 4 == Integer(2) assert 15 % Integer(4) == Integer(3)
def test_Float(): def eq(a, b): t = Float("1.0E-15") return (-t < a - b < t) a = Float(2)**Float(3) assert eq(a.evalf(), Float(8)) assert eq((pi**-1).evalf(), Float("0.31830988618379067")) a = Float(2)**Float(4) assert eq(a.evalf(), Float(16)) assert (S(.3) == S(.5)) is False x_str = Float((0, '13333333333333', -52, 53)) x2_str = Float((0, '26666666666666', -53, 53)) x_hex = Float((0, long(0x13333333333333), -52, 53)) x_dec = Float((0, 5404319552844595, -52, 53)) x2_hex = Float((0, long(0x13333333333333) * 2, -53, 53)) assert x_str == x_hex == x_dec == x2_hex == Float(1.2) # x2_str and 1.2 are superficially the same assert str(x2_str) == str(Float(1.2)) # but are different at the mpf level assert Float(1.2)._mpf_ == (0, long(5404319552844595), -52, 53) assert x2_str._mpf_ == (0, long(10808639105689190), -53, 53) assert Float((0, long(0), -123, -1)) == Float('nan') assert Float((0, long(0), -456, -2)) == Float('inf') == Float('+inf') assert Float((1, long(0), -789, -3)) == Float('-inf') raises(ValueError, lambda: Float((0, 7, 1, 3), '')) assert Float('+inf').is_bounded is False assert Float('+inf').is_finite is False assert Float('+inf').is_negative is False assert Float('+inf').is_positive is True assert Float('+inf').is_unbounded is True assert Float('+inf').is_zero is False assert Float('-inf').is_bounded is False assert Float('-inf').is_finite is False assert Float('-inf').is_negative is True assert Float('-inf').is_positive is False assert Float('-inf').is_unbounded is True assert Float('-inf').is_zero is False assert Float('0.0').is_bounded is True assert Float('0.0').is_finite is False assert Float('0.0').is_negative is False assert Float('0.0').is_positive is False assert Float('0.0').is_unbounded is False assert Float('0.0').is_zero is True # rationality properties assert Float(1).is_rational is None assert Float(1).is_irrational is None assert sqrt(2).n(15).is_rational is None assert sqrt(2).n(15).is_irrational is None # 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)) i = 12345678901234567890 assert _aresame(Float(12, ''), Float('12', '')) assert _aresame(Float(Integer(i), ''), Float(i, '')) assert _aresame(Float(i, ''), Float(str(i), 20)) assert not _aresame(Float(str(i)), Float(i, '')) # inexact floats (repeating binary = denom not multiple of 2) # cannot have precision greater than 15 assert Float(.125, 22) == .125 assert Float(2.0, 22) == 2 assert float(Float('.12500000000000001', '')) == .125 raises(ValueError, lambda: Float(.12500000000000001, '')) # allow spaces Float('123 456.123 456') == Float('123456.123456') Integer('123 456') == Integer('123456') Rational('123 456.123 456') == Rational('123456.123456') assert Float(' .3e2') == Float('0.3e2') # allow auto precision detection assert Float('.1', '') == Float(.1, 1) assert Float('.125', '') == Float(.125, 3) assert Float('.100', '') == Float(.1, 3) assert Float('2.0', '') == Float('2', 2) raises(ValueError, lambda: Float("12.3d-4", "")) raises(ValueError, lambda: Float(12.3, "")) raises(ValueError, lambda: Float('.')) raises(ValueError, lambda: Float('-.')) zero = Float('0.0') assert Float('-0') == zero assert Float('.0') == zero assert Float('-.0') == zero assert Float('-0.0') == zero assert Float(0.0) == zero assert Float(0) == zero assert Float(0, '') == Float('0', '') assert Float(1) == Float(1.0) assert Float(S.Zero) == zero assert Float(S.One) == Float(1.0) assert Float(decimal.Decimal('0.1'), 3) == Float('.1', 3) assert '{0:.3f}'.format(Float(4.236622)) == '4.237' assert '{0:.35f}'.format(Float( pi.n(40), 40)) == '3.14159265358979323846264338327950288'
def test_issue_6349(): assert Float('23.e3', '')._prec == 10 assert Float('23e3', '')._prec == 20 assert Float('23000', '')._prec == 20 assert Float('-23000', '')._prec == 20
def test_Float_eq(): assert Float(.12, 3) != Float(.12, 4) assert Float(.12, 3) == .12 assert 0.12 == Float(.12, 3) assert Float('.12', 22) != .12
def test_issue_12092(): f = implemented_function('f', lambda x: x**2) assert f(f(2)).evalf() == Float(16)
def test_Mul_Infinity_Zero(): assert 0 * Float('inf') == nan assert 0 * Float('-inf') == nan assert 0 * Float('inf') == nan assert 0 * Float('-inf') == nan assert Float('inf') * 0 == nan assert Float('-inf') * 0 == nan assert Float('inf') * 0 == nan assert Float('-inf') * 0 == nan assert Float(0) * Float('inf') == nan assert Float(0) * Float('-inf') == nan assert Float(0) * Float('inf') == nan assert Float(0) * Float('-inf') == nan assert Float('inf') * Float(0) == nan assert Float('-inf') * Float(0) == nan assert Float('inf') * Float(0) == nan assert Float('-inf') * Float(0) == nan
def __init__(self, model, file_name, **kwargs): self._i_inj_params = set() # add i_inj V = _get_membrane_voltage_var(model, convert=False) i_stim = _get_membrane_stimulus_current(model) if i_stim: # add units units = UnitStore(model.units) picoF = units.add_unit('picoF', 'farad / 1e12') one_over_mv = units.add_unit('one_over_millivolt', '1 / (volt * 1e-3)') one_over_ohm = units.add_unit('one_over_ohm', '1 / ohm') millivolt = units.add_unit('millivolt', 'volt / 1e3') # add variables Scaling = model.add_variable('Scaling', units='dimensionless') Cext = model.add_variable('Cext', units=picoF) Ampl_gain = model.add_variable('Ampl_gain', units='dimensionless') R_seal = model.add_variable('R_seal', units='ohm') g_leak = model.add_variable('g_leak', units=one_over_ohm) leak_comp_perc = model.add_variable('leak_comp_perc', units='dimensionless') E_l = model.add_variable('E_l', units=millivolt) A0_bck = model.add_variable('A0_bck', units='dimensionless') k_bck = model.add_variable('k_bck', units=one_over_mv) Scale_bck = model.add_variable('Scale_bck', units='dimensionless') i_leak_comp = model.add_variable('i_leak_comp', units='dimensionless') i_bck = model.add_variable('i_bck', units='dimensionless') I_ext = model.add_variable('I_ext', units='dimensionless') I_curr = model.add_variable('I', units='dimensionless') i_inj = model.add_variable('i_inj', units='dimensionless') self._i_inj_params = { A0_bck, Ampl_gain, Cext, E_l, I_curr, R_seal, Scale_bck, Scaling, k_bck, leak_comp_perc } # add defining numbers model.add_equation(Eq(Scaling, Float(1.0))) model.add_equation(Eq(A0_bck, Float(1.0278))) model.add_equation(Eq(k_bck, Float(0.0986))) model.add_equation(Eq(R_seal, Float(1.0))) model.add_equation(Eq(Cext, Float(1.0))) model.add_equation(Eq(leak_comp_perc, Float(1.0))) model.add_equation(Eq(E_l, Float(1.0))) model.add_equation(Eq(I_curr, Float(1.0))) model.add_equation(Eq(Ampl_gain, Float(1.0))) model.add_equation(Eq(Scale_bck, Float(1.0))) # add eqs model.add_equation(Eq(g_leak, 1 / R_seal)) model.add_equation( Eq(i_leak_comp, (g_leak / Cext) * (V - E_l) * (leak_comp_perc / 100))) model.add_equation( Eq(i_bck, (Scale_bck / Cext) * A0_bck / (1 + exp_(-k_bck * V)))) model.add_equation(Eq(I_ext, I_curr / (Cext * Ampl_gain))) model.add_equation( Eq(i_inj, Scaling * (I_ext - i_leak_comp - i_bck))) dvdt = next( (eq for eq in model.equations if eq.lhs == Derivative(V, model.get_free_variable())), None) assert dvdt is not None, "Expecting exatctly 1 dvdt equation" model.remove_equation(dvdt) model.add_equation( Eq(dvdt.lhs, dvdt.rhs.replace(i_stim, i_stim + i_inj))) # find dv/dt # add conversions add_conversions(model, use_modifiers=False, skip_chaste_stimulus_conversion=True) model.modifiable_parameters |= set(self._i_inj_params) # For equations containing a piecewise not at the top level, pull it out into it's own equation for eq in tuple(model.equations): # Piecewises cannot be inline, so attach each to a new variable if eq.has(Piecewise): if eq.rhs.has(Piecewise) and ( not isinstance(eq.rhs, Piecewise) or len(eq.rhs.atoms(Piecewise)) > 1): new_rhs = eq.rhs # sort the piecewsies to guarantee consistent output across re-runs for i, pw in enumerate( sorted(eq.rhs.atoms(Piecewise), key=str)): new_pw_lhs = model.add_variable( eq.lhs.name + '_PW_' + str(i), eq.lhs.units) model.add_equation(Eq(new_pw_lhs, pw)) new_rhs = new_rhs.replace(pw, new_pw_lhs) model.remove_equation(eq) model.add_equation(Eq(eq.lhs, new_rhs)) super().__init__(model, file_name, **kwargs) self._templates = ['labview.h', 'labview.c'] self._vars_for_template['model_type'] = 'RushLarsenC' # store info for .c/.h self._vars_for_template['stat_var_name_max_length'] = \ max([len(sv['var']) for sv in self._formatted_state_vars]) self._vars_for_template['unit_name_max_length'] = \ max([len(sv['units']) for sv in self._formatted_state_vars]) self._vars_for_template['components'] = \ tuple([component_name(sv['sympy_var']) for sv in self._formatted_state_vars]) self._vars_for_template['component_name_max_length'] = \ max([len(sv) for sv in self._vars_for_template['components']])
def test_powers_Integer(): """Test Integer._eval_power""" # check infinity assert S(1)**S.Infinity == S.NaN assert S(-1)**S.Infinity == S.NaN assert S(2)**S.Infinity == S.Infinity assert S(-2)**S.Infinity == S.Infinity + S.Infinity * S.ImaginaryUnit assert S(0)**S.Infinity == 0 # check Nan assert S(1)**S.NaN == S.NaN assert S(-1)**S.NaN == S.NaN # check for exact roots assert S(-1)**Rational(6, 5) == -(-1)**(S(1) / 5) assert sqrt(S(4)) == 2 assert sqrt(S(-4)) == I * 2 assert S(16)**Rational(1, 4) == 2 assert S(-16)**Rational(1, 4) == 2 * (-1)**Rational(1, 4) assert S(9)**Rational(3, 2) == 27 assert S(-9)**Rational(3, 2) == -27 * I assert S(27)**Rational(2, 3) == 9 assert S(-27)**Rational(2, 3) == 9 * (S(-1)**Rational(2, 3)) assert (-2)**Rational(-2, 1) == Rational(1, 4) # not exact roots assert sqrt(-3) == I * sqrt(3) assert (3)**(S(3) / 2) == 3 * sqrt(3) assert (-3)**(S(3) / 2) == -3 * sqrt(-3) assert (-3)**(S(5) / 2) == 9 * I * sqrt(3) assert (-3)**(S(7) / 2) == -I * 27 * sqrt(3) assert (2)**(S(3) / 2) == 2 * sqrt(2) assert (2)**(S(-3) / 2) == sqrt(2) / 4 assert (81)**(S(2) / 3) == 9 * (S(3)**(S(2) / 3)) assert (-81)**(S(2) / 3) == 9 * (S(-3)**(S(2) / 3)) assert (-3) ** Rational(-7, 3) == \ -(-1)**Rational(2, 3)*3**Rational(2, 3)/27 assert (-3) ** Rational(-2, 3) == \ -(-1)**Rational(1, 3)*3**Rational(1, 3)/3 # join roots assert sqrt(6) + sqrt(24) == 3 * sqrt(6) assert sqrt(2) * sqrt(3) == sqrt(6) # separate symbols & constansts x = Symbol("x") assert sqrt(49 * x) == 7 * sqrt(x) assert sqrt((3 - sqrt(pi))**2) == 3 - sqrt(pi) # check that it is fast for big numbers assert (2**64 + 1)**Rational(4, 3) assert (2**64 + 1)**Rational(17, 25) # negative rational power and negative base assert (-3) ** Rational(-7, 3) == \ -(-1)**Rational(2, 3)*3**Rational(2, 3)/27 assert (-3) ** Rational(-2, 3) == \ -(-1)**Rational(1, 3)*3**Rational(1, 3)/3 assert S(1234).factors() == {617: 1, 2: 1} assert Rational(2 * 3, 3 * 5 * 7).factors() == {2: 1, 5: -1, 7: -1} # test that eval_power factors numbers bigger than # the current limit in factor_trial_division (2**15) from sympy import nextprime n = nextprime(2**15) assert sqrt(n**2) == n assert sqrt(n**3) == n * sqrt(n) assert sqrt(4 * n) == 2 * sqrt(n) # check that factors of base with powers sharing gcd with power are removed assert (2**4 * 3)**Rational(1, 6) == 2**Rational(2, 3) * 3**Rational(1, 6) assert (2**4 * 3)**Rational( 5, 6) == 8 * 2**Rational(1, 3) * 3**Rational(5, 6) # check that bases sharing a gcd are exptracted assert 2**Rational(1, 3)*3**Rational(1, 4)*6**Rational(1, 5) == \ 2**Rational(8, 15)*3**Rational(9, 20) assert sqrt(8)*24**Rational(1, 3)*6**Rational(1, 5) == \ 4*2**Rational(7, 10)*3**Rational(8, 15) assert sqrt(8)*(-24)**Rational(1, 3)*(-6)**Rational(1, 5) == \ 4*(-3)**Rational(8, 15)*2**Rational(7, 10) assert 2**Rational(1, 3) * 2**Rational(8, 9) == 2 * 2**Rational(2, 9) assert 2**Rational(2, 3) * 6**Rational(1, 3) == 2 * 3**Rational(1, 3) assert 2**Rational(2, 3)*6**Rational(8, 9) == \ 2*2**Rational(5, 9)*3**Rational(8, 9) assert (-2)**Rational(2, S(3)) * (-4)**Rational( 1, S(3)) == -2 * 2**Rational(1, 3) assert 3 * Pow(3, 2, evaluate=False) == 3**3 assert 3 * Pow(3, -1 / S(3), evaluate=False) == 3**(2 / S(3)) assert (-2)**(1/S(3))*(-3)**(1/S(4))*(-5)**(5/S(6)) == \ -(-1)**Rational(5, 12)*2**Rational(1, 3)*3**Rational(1, 4) * \ 5**Rational(5, 6) assert Integer(-2)**Symbol('', even=True) == \ Integer(2)**Symbol('', even=True) assert (-1)**Float(.5) == 1.0 * I
def test_subbug2(): # Ensure this does not cause infinite recursion assert Float(7.7).epsilon_eq(abs(x).subs(x, -7.7))
def test_powers_Float(): assert str((S('-1/10')**S('3/10')).n()) == str(Float(-.1)**(.3))
def test_nsimplify(): x = Symbol("x") assert nsimplify(0) == 0 assert nsimplify(-1) == -1 assert nsimplify(1) == 1 assert nsimplify(1 + x) == 1 + x assert nsimplify(2.7) == Rational(27, 10) assert nsimplify(1 - GoldenRatio) == (1 - sqrt(5)) / 2 assert nsimplify((1 + sqrt(5)) / 4, [GoldenRatio]) == GoldenRatio / 2 assert nsimplify(2 / GoldenRatio, [GoldenRatio]) == 2 * GoldenRatio - 2 assert nsimplify(exp(5*pi*I/3, evaluate=False)) == \ sympify('1/2 - sqrt(3)*I/2') assert nsimplify(sin(3*pi/5, evaluate=False)) == \ sympify('sqrt(sqrt(5)/8 + 5/8)') assert nsimplify(sqrt(atan('1', evaluate=False))*(2 + I), [pi]) == \ sqrt(pi) + sqrt(pi)/2*I assert nsimplify(2 + exp(2 * atan('1/4') * I)) == sympify('49/17 + 8*I/17') assert nsimplify(pi, tolerance=0.01) == Rational(22, 7) assert nsimplify(pi, tolerance=0.001) == Rational(355, 113) assert nsimplify(0.33333, tolerance=1e-4) == Rational(1, 3) assert nsimplify(2.0**(1 / 3.), tolerance=0.001) == Rational(635, 504) assert nsimplify(2.0**(1/3.), tolerance=0.001, full=True) == \ 2**Rational(1, 3) assert nsimplify(x + .5, rational=True) == Rational(1, 2) + x assert nsimplify(1 / .3 + x, rational=True) == Rational(10, 3) + x assert nsimplify(log(3).n(), rational=True) == \ sympify('109861228866811/100000000000000') assert nsimplify(Float(0.272198261287950), [pi, log(2)]) == pi * log(2) / 8 assert nsimplify(Float(0.272198261287950).n(3), [pi, log(2)]) == \ -pi/4 - log(2) + S(7)/4 assert nsimplify(x / 7.0) == x / 7 assert nsimplify(pi / 1e2) == pi / 100 assert nsimplify(pi / 1e2, rational=False) == pi / 100.0 assert nsimplify(pi / 1e-7) == 10000000 * pi assert not nsimplify( factor(-3.0 * z**2 * (z**2)**(-2.5) + 3 * (z**2)**(-1.5))).atoms(Float) e = x**0.0 assert e.is_Pow and nsimplify(x**0.0) == 1 assert nsimplify(3.333333, tolerance=0.1, rational=True) == Rational(10, 3) assert nsimplify(3.333333, tolerance=0.01, rational=True) == Rational(10, 3) assert nsimplify(3.666666, tolerance=0.1, rational=True) == Rational(11, 3) assert nsimplify(3.666666, tolerance=0.01, rational=True) == Rational(11, 3) assert nsimplify(33, tolerance=10, rational=True) == Rational(33) assert nsimplify(33.33, tolerance=10, rational=True) == Rational(30) assert nsimplify(37.76, tolerance=10, rational=True) == Rational(40) assert nsimplify(-203.1) == -S(2031) / 10 assert nsimplify(.2, tolerance=0) == S.One / 5 assert nsimplify(-.2, tolerance=0) == -S.One / 5 assert nsimplify(.2222, tolerance=0) == S(1111) / 5000 assert nsimplify(-.2222, tolerance=0) == -S(1111) / 5000 # issue 7211, PR 4112 assert nsimplify(S(2e-8)) == S(1) / 50000000 # issue 7322 direct test assert nsimplify(1e-42, rational=True) != 0 # issue 10336 inf = Float('inf') infs = (-oo, oo, inf, -inf) for i in infs: ans = sign(i) * oo assert nsimplify(i) == ans assert nsimplify(i + x) == x + ans assert nsimplify(0.33333333, rational=True, rational_conversion='exact') == Rational(0.33333333) # Make sure nsimplify on expressions uses full precision assert nsimplify( pi.evalf(100) * x, rational_conversion='exact').evalf(100) == pi.evalf(100) * x
def _sympy_(self): return Float(1.1)
def test_Interval_is_right_unbounded(): assert Interval(3, 4).is_right_unbounded is False assert Interval(3, oo).is_right_unbounded is True assert Interval(3, Float("+inf")).is_right_unbounded is True
def test_issue_3982(): a = [3, 2.0] assert sympify(a) == [Integer(3), Float(2.0)] assert sympify(tuple(a)) == Tuple(Integer(3), Float(2.0)) assert sympify(set(a)) == FiniteSet(Integer(3), Float(2.0))
def test_log_apply_evalf(): value = (log(3) / log(2) - 1).evalf() assert value.epsilon_eq(Float("0.58496250072115618145373"))
def test_issue_4788(): assert srepr(S(1.0 + 0J)) == srepr(S(1.0)) == srepr(Float(1.0))
def test_dont_accept_str(): assert Float("0.2") != "0.2" assert not (Float("0.2") == "0.2")
def test_sympify_mpmath(): value = sympify(mpmath.mpf(1.0)) assert value == Float(1.0) and type(value) is Float mpmath.mp.dps = 12 assert sympify(mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-12")) == True assert sympify(mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-13")) == False mpmath.mp.dps = 6 assert sympify(mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-5")) == True assert sympify(mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-6")) == False assert sympify(mpmath.mpc(1.0 + 2.0j)) == Float(1.0) + Float(2.0) * I
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(Float('1.23', 15)) == mpmath.mpf('1.23')
def test_polygon(): a, b, c = Point(0, 0), Point(2, 0), Point(3, 3) t = Triangle(a, b, c) assert Polygon(a, Point(1, 0), b, c) == t assert Polygon(Point(1, 0), b, c, a) == t assert Polygon(b, c, a, Point(1, 0)) == t # 2 "remove folded" tests assert Polygon(a, Point(3, 0), b, c) == t assert Polygon(a, b, Point(3, -1), b, c) == t raises(GeometryError, lambda: Polygon((0, 0), (1, 0), (0, 1), (1, 1))) # remove multiple collinear points assert Polygon(Point(-4, 15), Point(-11, 15), Point(-15, 15), Point(-15, 33/5), Point(-15, -87/10), Point(-15, -15), Point(-42/5, -15), Point(-2, -15), Point(7, -15), Point(15, -15), Point(15, -3), Point(15, 10), Point(15, 15)) == \ Polygon(Point(-15,-15), Point(15,-15), Point(15,15), Point(-15,15)) p1 = Polygon( Point(0, 0), Point(3, -1), Point(6, 0), Point(4, 5), Point(2, 3), Point(0, 3)) p2 = Polygon( Point(6, 0), Point(3, -1), Point(0, 0), Point(0, 3), Point(2, 3), Point(4, 5)) p3 = Polygon( Point(0, 0), Point(3, 0), Point(5, 2), Point(4, 4)) p4 = Polygon( Point(0, 0), Point(4, 4), Point(5, 2), Point(3, 0)) p5 = Polygon( Point(0, 0), Point(4, 4), Point(0, 4)) p6 = Polygon( Point(-11, 1), Point(-9, 6.6), Point(-4, -3), Point(-8.4, -8.7)) r = Ray(Point(-9,6.6), Point(-9,5.5)) # # General polygon # assert p1 == p2 assert len(p1.args) == 6 assert len(p1.sides) == 6 assert p1.perimeter == 5 + 2*sqrt(10) + sqrt(29) + sqrt(8) assert p1.area == 22 assert not p1.is_convex() # ensure convex for both CW and CCW point specification assert p3.is_convex() assert p4.is_convex() dict5 = p5.angles assert dict5[Point(0, 0)] == pi / 4 assert dict5[Point(0, 4)] == pi / 2 assert p5.encloses_point(Point(x, y)) is None assert p5.encloses_point(Point(1, 3)) assert p5.encloses_point(Point(0, 0)) is False assert p5.encloses_point(Point(4, 0)) is False p5.plot_interval('x') == [x, 0, 1] assert p5.distance( Polygon(Point(10, 10), Point(14, 14), Point(10, 14))) == 6 * sqrt(2) assert p5.distance( Polygon(Point(1, 8), Point(5, 8), Point(8, 12), Point(1, 12))) == 4 warnings.filterwarnings( "error", message="Polygons may intersect producing erroneous output") raises(UserWarning, lambda: Polygon(Point(0, 0), Point(1, 0), Point(1, 1)).distance( Polygon(Point(0, 0), Point(0, 1), Point(1, 1)))) warnings.filterwarnings( "ignore", message="Polygons may intersect producing erroneous output") assert hash(p5) == hash(Polygon(Point(0, 0), Point(4, 4), Point(0, 4))) assert p5 == Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) assert Polygon(Point(4, 4), Point(0, 4), Point(0, 0)) in p5 assert p5 != Point(0, 4) assert Point(0, 1) in p5 assert p5.arbitrary_point('t').subs(Symbol('t', real=True), 0) == \ Point(0, 0) raises(ValueError, lambda: Polygon( Point(x, 0), Point(0, y), Point(x, y)).arbitrary_point('x')) assert p6.intersection(r) == [Point(-9, 33/5), Point(-9, -84/13)] # # Regular polygon # p1 = RegularPolygon(Point(0, 0), 10, 5) p2 = RegularPolygon(Point(0, 0), 5, 5) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), Point(0, 1), Point(1, 1))) raises(GeometryError, lambda: RegularPolygon(Point(0, 0), 1, 2)) raises(ValueError, lambda: RegularPolygon(Point(0, 0), 1, 2.5)) assert p1 != p2 assert p1.interior_angle == 3*pi/5 assert p1.exterior_angle == 2*pi/5 assert p2.apothem == 5*cos(pi/5) assert p2.circumcenter == p1.circumcenter == Point(0, 0) assert p1.circumradius == p1.radius == 10 assert p2.circumcircle == Circle(Point(0, 0), 5) assert p2.incircle == Circle(Point(0, 0), p2.apothem) assert p2.inradius == p2.apothem == (5 * (1 + sqrt(5)) / 4) p2.spin(pi / 10) dict1 = p2.angles assert dict1[Point(0, 5)] == 3 * pi / 5 assert p1.is_convex() assert p1.rotation == 0 assert p1.encloses_point(Point(0, 0)) assert p1.encloses_point(Point(11, 0)) is False assert p2.encloses_point(Point(0, 4.9)) p1.spin(pi/3) assert p1.rotation == pi/3 assert p1.vertices[0] == Point(5, 5*sqrt(3)) for var in p1.args: if isinstance(var, Point): assert var == Point(0, 0) else: assert var == 5 or var == 10 or var == pi / 3 assert p1 != Point(0, 0) assert p1 != p5 # while spin works in place (notice that rotation is 2pi/3 below) # rotate returns a new object p1_old = p1 assert p1.rotate(pi/3) == RegularPolygon(Point(0, 0), 10, 5, 2*pi/3) assert p1 == p1_old assert p1.area == (-250*sqrt(5) + 1250)/(4*tan(pi/5)) assert p1.length == 20*sqrt(-sqrt(5)/8 + 5/8) assert p1.scale(2, 2) == \ RegularPolygon(p1.center, p1.radius*2, p1._n, p1.rotation) assert RegularPolygon((0, 0), 1, 4).scale(2, 3) == \ Polygon(Point(2, 0), Point(0, 3), Point(-2, 0), Point(0, -3)) assert repr(p1) == str(p1) # # Angles # angles = p4.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) angles = p3.angles assert feq(angles[Point(0, 0)].evalf(), Float("0.7853981633974483")) assert feq(angles[Point(4, 4)].evalf(), Float("1.2490457723982544")) assert feq(angles[Point(5, 2)].evalf(), Float("1.8925468811915388")) assert feq(angles[Point(3, 0)].evalf(), Float("2.3561944901923449")) # # Triangle # p1 = Point(0, 0) p2 = Point(5, 0) p3 = Point(0, 5) t1 = Triangle(p1, p2, p3) t2 = Triangle(p1, p2, Point(Rational(5, 2), sqrt(Rational(75, 4)))) t3 = Triangle(p1, Point(x1, 0), Point(0, x1)) s1 = t1.sides assert Triangle(p1, p2, p1) == Polygon(p1, p2, p1) == Segment(p1, p2) raises(GeometryError, lambda: Triangle(Point(0, 0))) # Basic stuff assert Triangle(p1, p1, p1) == p1 assert Triangle(p2, p2*2, p2*3) == Segment(p2, p2*3) assert t1.area == Rational(25, 2) assert t1.is_right() assert t2.is_right() is False assert t3.is_right() assert p1 in t1 assert t1.sides[0] in t1 assert Segment((0, 0), (1, 0)) in t1 assert Point(5, 5) not in t2 assert t1.is_convex() assert feq(t1.angles[p1].evalf(), pi.evalf()/2) assert t1.is_equilateral() is False assert t2.is_equilateral() assert t3.is_equilateral() is False assert are_similar(t1, t2) is False assert are_similar(t1, t3) assert are_similar(t2, t3) is False assert t1.is_similar(Point(0, 0)) is False # Bisectors bisectors = t1.bisectors() assert bisectors[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) ic = (250 - 125*sqrt(2)) / 50 assert t1.incenter == Point(ic, ic) # Inradius assert t1.inradius == t1.incircle.radius == 5 - 5*sqrt(2)/2 assert t2.inradius == t2.incircle.radius == 5*sqrt(3)/6 assert t3.inradius == t3.incircle.radius == x1**2/((2 + sqrt(2))*Abs(x1)) # Circumcircle assert t1.circumcircle.center == Point(2.5, 2.5) # Medians + Centroid m = t1.medians assert t1.centroid == Point(Rational(5, 3), Rational(5, 3)) assert m[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert t3.medians[p1] == Segment(p1, Point(x1/2, x1/2)) assert intersection(m[p1], m[p2], m[p3]) == [t1.centroid] assert t1.medial == Triangle(Point(2.5, 0), Point(0, 2.5), Point(2.5, 2.5)) # Perpendicular altitudes = t1.altitudes assert altitudes[p1] == Segment(p1, Point(Rational(5, 2), Rational(5, 2))) assert altitudes[p2] == s1[0] assert altitudes[p3] == s1[2] assert t1.orthocenter == p1 t = S('''Triangle( Point(100080156402737/5000000000000, 79782624633431/500000000000), Point(39223884078253/2000000000000, 156345163124289/1000000000000), Point(31241359188437/1250000000000, 338338270939941/1000000000000000))''') assert t.orthocenter == S('''Point(-780660869050599840216997''' '''79471538701955848721853/80368430960602242240789074233100000000000000,''' '''20151573611150265741278060334545897615974257/16073686192120448448157''' '''8148466200000000000)''') # Ensure assert len(intersection(*bisectors.values())) == 1 assert len(intersection(*altitudes.values())) == 1 assert len(intersection(*m.values())) == 1 # Distance p1 = Polygon( Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)) p2 = Polygon( Point(0, Rational(5)/4), Point(1, Rational(5)/4), Point(1, Rational(9)/4), Point(0, Rational(9)/4)) p3 = Polygon( Point(1, 2), Point(2, 2), Point(2, 1)) p4 = Polygon( Point(1, 1), Point(Rational(6)/5, 1), Point(1, Rational(6)/5)) pt1 = Point(half, half) pt2 = Point(1, 1) '''Polygon to Point''' assert p1.distance(pt1) == half assert p1.distance(pt2) == 0 assert p2.distance(pt1) == Rational(3)/4 assert p3.distance(pt2) == sqrt(2)/2 '''Polygon to Polygon''' # p1.distance(p2) emits a warning # First, test the warning warnings.filterwarnings("error", message="Polygons may intersect producing erroneous output") raises(UserWarning, lambda: p1.distance(p2)) # now test the actual output warnings.filterwarnings("ignore", message="Polygons may intersect producing erroneous output") assert p1.distance(p2) == half/2 assert p1.distance(p3) == sqrt(2)/2 assert p3.distance(p4) == (sqrt(2)/2 - sqrt(Rational(2)/25)/2)