def test_powers_Rational(): """Test Rational._eval_power""" # check infinity assert Rational(1, 2)**S.Infinity == 0 assert Rational(3, 2)**S.Infinity == S.Infinity assert Rational(-1, 2)**S.Infinity == 0 assert Rational(-3, 2) ** S.Infinity == \ S.Infinity + S.Infinity * S.ImaginaryUnit # check Nan assert Rational(3, 4)**S.NaN == S.NaN assert Rational(-2, 3)**S.NaN == S.NaN # exact roots on numerator assert sqrt(Rational(4, 3)) == 2 * sqrt(3) / 3 assert Rational(4, 3)**Rational(3, 2) == 8 * sqrt(3) / 9 assert sqrt(Rational(-4, 3)) == I * 2 * sqrt(3) / 3 assert Rational(-4, 3)**Rational(3, 2) == -I * 8 * sqrt(3) / 9 assert Rational(27, 2)**Rational(1, 3) == 3 * (2**Rational(2, 3)) / 2 assert Rational(5**3, 8**3)**Rational(4, 3) == Rational(5**4, 8**4) # exact root on denominator assert sqrt(Rational(1, 4)) == Rational(1, 2) assert sqrt(Rational(1, -4)) == I * Rational(1, 2) assert sqrt(Rational(3, 4)) == sqrt(3) / 2 assert sqrt(Rational(3, -4)) == I * sqrt(3) / 2 assert Rational(5, 27)**Rational(1, 3) == (5**Rational(1, 3)) / 3 # not exact roots assert sqrt(Rational(1, 2)) == sqrt(2) / 2 assert sqrt(Rational(-4, 7)) == I * sqrt(Rational(4, 7)) assert Rational(-3, 2)**Rational(-7, 3) == \ -4*(-1)**Rational(2, 3)*2**Rational(1, 3)*3**Rational(2, 3)/27 assert Rational(-3, 2)**Rational(-2, 3) == \ -(-1)**Rational(1, 3)*2**Rational(2, 3)*3**Rational(1, 3)/3 # negative integer power and negative rational base assert Rational(-2, 3)**Rational(-2, 1) == Rational(9, 4) a = Rational(1, 10) assert a**Float(a, 2) == Float(a, 2)**Float(a, 2) assert Rational(-2, 3)**Symbol('', even=True) == \ Rational(2, 3)**Symbol('', even=True)
def test_calculate_series(): # needs gruntz calculate_series to go to n = 32 assert limit(x**Rational(77, 3)/(1 + x**Rational(77, 3)), x, oo) == 1 # needs gruntz calculate_series to go to n = 128 assert limit(x**101.1/(1 + x**101.1), x, oo) == 1
def test_issue_10610(): assert limit(3**x*3**(-x - 1)*(x + 1)**2/x**2, x, oo) == Rational(1, 3)
def test_pretty_sqrt(): expr = sqrt(2) ascii_str = \ """\ ___\n\ \/ 2 \ """ ucode_str = \ u"""\ ⎽⎽⎽\n\ ╲╱ 2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = 2**Rational(1, 3) ascii_str = \ """\ 3 ___\n\ \/ 2 \ """ ucode_str = \ u"""\ 3 ⎽⎽⎽\n\ ╲╱ 2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = 2**Rational(1, 1000) ascii_str = \ """\ 1000___\n\ \/ 2 \ """ ucode_str = \ u"""\ 1000⎽⎽⎽\n\ ╲╱ 2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = sqrt(x**2 + 1) ascii_str = \ """\ ________\n\ / 2 \n\ \/ 1 + x \ """ ucode_str = \ u"""\ ⎽⎽⎽⎽⎽⎽⎽⎽\n\ ╱ 2 \n\ ╲╱ 1 + x \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (1 + sqrt(5))**Rational(1, 3) ascii_str = \ """\ ___________\n\ 3 / ___ \n\ \/ 1 + \/ 5 \ """ ucode_str = \ u"""\ ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽\n\ 3 ╱ ⎽⎽⎽ \n\ ╲╱ 1 + ╲╱ 5 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = 2**(1 / x) ascii_str = \ """\ x ___\n\ \/ 2 \ """ ucode_str = \ u"""\ x ⎽⎽⎽\n\ ╲╱ 2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = sqrt(2 + pi) ascii_str = \ """\ ________\n\ \/ 2 + pi \ """ ucode_str = \ u"""\ ⎽⎽⎽⎽⎽⎽⎽\n\ ╲╱ 2 + π \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (2 + (1 + x**2) / (2 + x))**Rational( 1, 4) + (1 + x**Rational(1, 1000)) / sqrt(3 + x**2) ascii_str = \ """\ ____________\n\ 1000___ / 2 \n\ 1 + \/ x / 1 + x \n\ ----------- + 4 / 2 + ------ \n\ ________ \/ 2 + x \n\ / 2 \n\ \/ 3 + x \ """ ucode_str = \ u"""\ ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽\n\ 1000⎽⎽⎽ ╱ 2 \n\ 1 + ╲╱ x ╱ 1 + x \n\ ─────────── + 4 ╱ 2 + ────── \n\ ⎽⎽⎽⎽⎽⎽⎽⎽ ╲╱ 2 + x \n\ ╱ 2 \n\ ╲╱ 3 + x \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str
def test_pretty_basic(): assert pretty(-Rational(1) / 2) == '-1/2' assert pretty( -Rational(13)/22 ) == \ """\ 13\n\ - --\n\ 22\ """ expr = oo ascii_str = \ """\ oo\ """ ucode_str = \ u"""\ ∞\ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (x**2) ascii_str = \ """\ 2\n\ x \ """ ucode_str = \ u"""\ 2\n\ x \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = 1 / x ascii_str = \ """\ 1\n\ -\n\ x\ """ ucode_str = \ u"""\ 1\n\ ─\n\ x\ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = y * x**-2 ascii_str = \ """\ y \n\ --\n\ 2\n\ x \ """ ucode_str = \ u"""\ y \n\ ──\n\ 2\n\ x \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = x**Rational(-5, 2) ascii_str = \ """\ 1 \n\ ----\n\ 5/2\n\ x \ """ ucode_str = \ u"""\ 1 \n\ ────\n\ 5/2\n\ x \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (-2)**x ascii_str = \ """\ x\n\ (-2) \ """ ucode_str = \ u"""\ x\n\ (-2) \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str # See issue 1824 expr = Pow(3, 1, evaluate=False) ascii_str = \ """\ 1\n\ 3 \ """ ucode_str = \ u"""\ 1\n\ 3 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (x**2 + x + 1) ascii_str_1 = \ """\ 2\n\ 1 + x + x \ """ ascii_str_2 = \ """\ 2 \n\ x + x + 1\ """ ascii_str_3 = \ """\ 2 \n\ x + 1 + x\ """ ucode_str_1 = \ u"""\ 2\n\ 1 + x + x \ """ ucode_str_2 = \ u"""\ 2 \n\ x + x + 1\ """ ucode_str_3 = \ u"""\ 2 \n\ x + 1 + x\ """ assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3] assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3] expr = 1 - x ascii_str_1 = \ """\ 1 - x\ """ ascii_str_2 = \ """\ -x + 1\ """ ucode_str_1 = \ u"""\ 1 - x\ """ ucode_str_2 = \ u"""\ -x + 1\ """ assert pretty(expr) in [ascii_str_1, ascii_str_2] assert upretty(expr) in [ucode_str_1, ucode_str_2] expr = 1 - 2 * x ascii_str_1 = \ """\ 1 - 2*x\ """ ascii_str_2 = \ """\ -2*x + 1\ """ ucode_str_1 = \ u"""\ 1 - 2⋅x\ """ ucode_str_2 = \ u"""\ -2⋅x + 1\ """ assert pretty(expr) in [ascii_str_1, ascii_str_2] assert upretty(expr) in [ucode_str_1, ucode_str_2] expr = x / y ascii_str = \ """\ x\n\ -\n\ y\ """ ucode_str = \ u"""\ x\n\ ─\n\ y\ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = -x / y ascii_str = \ """\ -x\n\ --\n\ y \ """ ucode_str = \ u"""\ -x\n\ ──\n\ y \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = (x + 2) / y ascii_str_1 = \ """\ 2 + x\n\ -----\n\ y \ """ ascii_str_2 = \ """\ x + 2\n\ -----\n\ y \ """ ucode_str_1 = \ u"""\ 2 + x\n\ ─────\n\ y \ """ ucode_str_2 = \ u"""\ x + 2\n\ ─────\n\ y \ """ assert pretty(expr) in [ascii_str_1, ascii_str_2] assert upretty(expr) in [ucode_str_1, ucode_str_2] expr = (1 + x) * y ascii_str_1 = \ """\ y*(1 + x)\ """ ascii_str_2 = \ """\ (1 + x)*y\ """ ascii_str_3 = \ """\ y*(x + 1)\ """ ucode_str_1 = \ u"""\ y⋅(1 + x)\ """ ucode_str_2 = \ u"""\ (1 + x)⋅y\ """ ucode_str_3 = \ u"""\ y⋅(x + 1)\ """ assert pretty(expr) in [ascii_str_1, ascii_str_2, ascii_str_3] assert upretty(expr) in [ucode_str_1, ucode_str_2, ucode_str_3] # Test for correct placement of the negative sign expr = -5 * x / (x + 10) ascii_str_1 = \ """\ -5*x \n\ ------\n\ 10 + x\ """ ascii_str_2 = \ """\ -5*x \n\ ------\n\ x + 10\ """ ucode_str_1 = \ u"""\ -5⋅x \n\ ──────\n\ 10 + x\ """ ucode_str_2 = \ u"""\ -5⋅x \n\ ──────\n\ x + 10\ """ assert pretty(expr) in [ascii_str_1, ascii_str_2] assert upretty(expr) in [ucode_str_1, ucode_str_2] expr = 1 - Rational(3, 2) * (x + 1) ascii_str = \ """\ 3*x\n\ -1/2 - ---\n\ 2 \ """ ucode_str = \ u"""\ 3⋅x\n\ -1/2 - ───\n\ 2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str
def test_simplify_relational(): assert simplify(x*(y + 1) - x*y - x + 1 < x) == (x > 1) assert simplify(x*(y + 1) - x*y - x - 1 < x) == (x > -1) assert simplify(x < x*(y + 1) - x*y - x + 1) == (x < 1) r = S.One < x # canonical operations are not the same as simplification, # so if there is no simplification, canonicalization will # be done unless the measure forbids it assert simplify(r) == r.canonical assert simplify(r, ratio=0) != r.canonical # this is not a random test; in _eval_simplify # this will simplify to S.false and that is the # reason for the 'if r.is_Relational' in Relational's # _eval_simplify routine assert simplify(-(2**(pi*Rational(3, 2)) + 6**pi)**(1/pi) + 2*(2**(pi/2) + 3**pi)**(1/pi) < 0) is S.false # canonical at least assert Eq(y, x).simplify() == Eq(x, y) assert Eq(x - 1, 0).simplify() == Eq(x, 1) assert Eq(x - 1, x).simplify() == S.false assert Eq(2*x - 1, x).simplify() == Eq(x, 1) assert Eq(2*x, 4).simplify() == Eq(x, 2) z = cos(1)**2 + sin(1)**2 - 1 # z.is_zero is None assert Eq(z*x, 0).simplify() == S.true assert Ne(y, x).simplify() == Ne(x, y) assert Ne(x - 1, 0).simplify() == Ne(x, 1) assert Ne(x - 1, x).simplify() == S.true assert Ne(2*x - 1, x).simplify() == Ne(x, 1) assert Ne(2*x, 4).simplify() == Ne(x, 2) assert Ne(z*x, 0).simplify() == S.false # No real-valued assumptions assert Ge(y, x).simplify() == Le(x, y) assert Ge(x - 1, 0).simplify() == Ge(x, 1) assert Ge(x - 1, x).simplify() == S.false assert Ge(2*x - 1, x).simplify() == Ge(x, 1) assert Ge(2*x, 4).simplify() == Ge(x, 2) assert Ge(z*x, 0).simplify() == S.true assert Ge(x, -2).simplify() == Ge(x, -2) assert Ge(-x, -2).simplify() == Le(x, 2) assert Ge(x, 2).simplify() == Ge(x, 2) assert Ge(-x, 2).simplify() == Le(x, -2) assert Le(y, x).simplify() == Ge(x, y) assert Le(x - 1, 0).simplify() == Le(x, 1) assert Le(x - 1, x).simplify() == S.true assert Le(2*x - 1, x).simplify() == Le(x, 1) assert Le(2*x, 4).simplify() == Le(x, 2) assert Le(z*x, 0).simplify() == S.true assert Le(x, -2).simplify() == Le(x, -2) assert Le(-x, -2).simplify() == Ge(x, 2) assert Le(x, 2).simplify() == Le(x, 2) assert Le(-x, 2).simplify() == Ge(x, -2) assert Gt(y, x).simplify() == Lt(x, y) assert Gt(x - 1, 0).simplify() == Gt(x, 1) assert Gt(x - 1, x).simplify() == S.false assert Gt(2*x - 1, x).simplify() == Gt(x, 1) assert Gt(2*x, 4).simplify() == Gt(x, 2) assert Gt(z*x, 0).simplify() == S.false assert Gt(x, -2).simplify() == Gt(x, -2) assert Gt(-x, -2).simplify() == Lt(x, 2) assert Gt(x, 2).simplify() == Gt(x, 2) assert Gt(-x, 2).simplify() == Lt(x, -2) assert Lt(y, x).simplify() == Gt(x, y) assert Lt(x - 1, 0).simplify() == Lt(x, 1) assert Lt(x - 1, x).simplify() == S.true assert Lt(2*x - 1, x).simplify() == Lt(x, 1) assert Lt(2*x, 4).simplify() == Lt(x, 2) assert Lt(z*x, 0).simplify() == S.false assert Lt(x, -2).simplify() == Lt(x, -2) assert Lt(-x, -2).simplify() == Gt(x, 2) assert Lt(x, 2).simplify() == Lt(x, 2) assert Lt(-x, 2).simplify() == Gt(x, -2)
def particles(self, model): totalGroup = list(model.gaugeGroups) totalGroup = r' $\times$ '.join(totalGroup) # Fermions fermions = [] for name, f in model.Fermions.items(): if f.conj: continue gen = str(f.gen) if type(f.gen) == int else '$'+self.totex(f.gen)+'$' rep = [0]*len(model.gaugeGroups) for g, qnb in f.Qnb.items(): gPos = list(model.gaugeGroups).index(g) if model.gaugeGroups[g].abelian: repName = ('+' if qnb > 0 else '') + self.totex(Rational(qnb)) else: repName = model.gaugeGroups[g].repName(qnb) # Fill Dynkin dic for r,v in model.gaugeGroups[g].repDic.items(): self.dynkin[str(list(r))] = v[4] self.dynkin[str(tuple(r))] = v[4] rep[gPos] = '$' + repName + '$' repStr = '('+', '.join(rep)+')' if len(rep) > 1 else rep[0] fermions.append(('$'+self.totex(Symbol(name))+'$', gen, repStr)) self.string += ('\n' + r""" \subsection{Fermions} """) if fermions != []: self.string += (r""" \begin{table}[h] \renewcommand{\arraystretch}{1.15} \centering \begin{tabular}{c@{\hskip .66cm}c@{\hskip .66cm}c} \hline Name & Generations & """ + totalGroup + r"""\\ \hline \\ [-2ex] """ + " \\\\[.2cm]\n".join([r' & '.join(f) for f in fermions]) + r""" \\[.1cm] \hline \end{tabular} \end{table}""") else: self.string += "\n\\emph{None.}\n" # Scalars scalars = [] for name, s in model.Particles.items(): if name in model.Fermions or s.conj: continue gen = str(s.gen) if type(s.gen) == int else '$'+self.totex(s.gen)+'$' rep = [0]*len(model.gaugeGroups) for g, qnb in s.Qnb.items(): gPos = list(model.gaugeGroups).index(g) if model.gaugeGroups[g].abelian: repName = ('+' if qnb > 0 else '') + self.totex(Rational(qnb)) else: repName = model.gaugeGroups[g].repName(qnb) rep[gPos] = '$' + repName + '$' # Norm*(re + im) formatting if s.cplx: if isinstance(s.norm, Mul) and len(s.norm.args) == 2 and s.norm.args[1]**(-2) == s.norm.args[0]: expr = '\\frac{1}{'+self.totex(s.norm.args[1])+'}' elif s.norm != 1: expr = self.totex(s.norm) else: expr = '' real = ' + i\\, '.join([self.totex(r._name) for r in s.realFields]) if s.norm != 1: real = ' \\left(' + real + '\\right)' expr = '$' + expr + real + '$' else: expr = '/' repStr = '('+', '.join(rep)+')' if len(rep) > 1 else rep[0] scalars.append(('$'+self.totex(Symbol(name))+'$', str(s.cplx), expr, gen, repStr)) self.string += ('\n' + r""" \subsection{Scalars} """) if scalars != []: self.string += (r""" \begin{table}[h] \renewcommand{\arraystretch}{1.15} \centering \begin{tabular}{c@{\hskip .66cm}c@{\hskip .66cm}ccc} \hline Name & Complex & Expression & Generations & """ + totalGroup + r"""\\ \hline \\ [-2ex] """ + " \\\\[.2cm]\n".join([r' & '.join(s) for s in scalars]) + r""" \\[.1cm] \hline \end{tabular} \end{table}""") else: self.string += "\n\\emph{None.}\n"
def test_sympify1(): assert sympify("x") == Symbol("x") assert sympify(" x") == Symbol("x") assert sympify(" x ") == Symbol("x") # 1778 n1 = Rational(1, 2) assert sympify('--.5') == n1 assert sympify('-1/2') == -n1 assert sympify('-+--.5') == -n1 assert sympify('-.[3]') == Rational(-1, 3) assert sympify('.[3]') == Rational(1, 3) assert sympify('+.[3]') == Rational(1, 3) assert sympify('+0.[3]*10**-2') == Rational(1, 300) assert sympify('.[052631578947368421]') == Rational(1, 19) assert sympify('.0[526315789473684210]') == Rational(1, 19) assert sympify('.034[56]') == Rational(1711, 49500) # options to make reals into rationals assert sympify('1.22[345]', rational=1) == \ 1 + Rational(22, 100) + Rational(345, 99900) assert sympify('2/2.6', rational=1) == Rational(10, 13) assert sympify('2.6/2', rational=1) == Rational(13, 10) assert sympify('2.6e2/17', rational=1) == Rational(260, 17) assert sympify('2.6e+2/17', rational=1) == Rational(260, 17) assert sympify('2.6e-2/17', rational=1) == Rational(26, 17000) assert sympify('2.1+3/4', rational=1) == Rational(21, 10) + Rational(3, 4) assert sympify('2.234456', rational=1) == Rational(279307, 125000) assert sympify('2.234456e23', rational=1) == 223445600000000000000000 assert sympify('2.234456e-23', rational=1) == Rational(279307, 12500000000000000000000000000) assert sympify('-2.234456e-23', rational=1) == Rational(-279307, 12500000000000000000000000000) assert sympify('12345678901/17', rational=1) == Rational(12345678901, 17) assert sympify('1/.3 + x', rational=1) == Rational(10, 3) + x # make sure longs in fractions work assert sympify('222222222222/11111111111') == Rational(222222222222, 11111111111) # ... even if they come from repetend notation assert sympify('1/.2[123456789012]') == Rational(333333333333, 70781892967) # ... or from high precision reals assert sympify('.1234567890123456', rational=1) == Rational(19290123283179, 156250000000000)
def test_construct_domain(): assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)]) assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)]) assert construct_domain([S.One, S(2), S(3)], field=True) == ( QQ, [QQ(1), QQ(2), QQ(3)], ) assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)]) result = construct_domain([3.14, 1, S.Half]) assert isinstance(result[0], RealField) assert result[1] == [RR(3.14), RR(1.0), RR(0.5)] assert construct_domain([3.14, sqrt(2)], extension=None) == ( EX, [EX(3.14), EX(sqrt(2))], ) assert construct_domain([3.14, sqrt(2)], extension=True) == ( EX, [EX(3.14), EX(sqrt(2))], ) assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))]) assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))]) assert construct_domain([x, sqrt(x), sqrt(y)]) == ( EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))], ) alg = QQ.algebraic_field(sqrt(2)) assert construct_domain([7, S.Half, sqrt(2)], extension=True) == ( alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))], ) alg = QQ.algebraic_field(sqrt(2) + sqrt(3)) assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == ( alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))], ) dom = ZZ[x] assert construct_domain([2 * x, 3]) == (dom, [dom.convert(2 * x), dom.convert(3)]) dom = ZZ[x, y] assert construct_domain([2 * x, 3 * y]) == ( dom, [dom.convert(2 * x), dom.convert(3 * y)], ) dom = QQ[x] assert construct_domain([x / 2, 3]) == (dom, [dom.convert(x / 2), dom.convert(3)]) dom = QQ[x, y] assert construct_domain([x / 2, 3 * y]) == ( dom, [dom.convert(x / 2), dom.convert(3 * y)], ) dom = RR[x] assert construct_domain([x / 2, 3.5]) == ( dom, [dom.convert(x / 2), dom.convert(3.5)], ) dom = RR[x, y] assert construct_domain([x / 2, 3.5 * y]) == ( dom, [dom.convert(x / 2), dom.convert(3.5 * y)], ) dom = ZZ.frac_field(x) assert construct_domain([2 / x, 3]) == (dom, [dom.convert(2 / x), dom.convert(3)]) dom = ZZ.frac_field(x, y) assert construct_domain([2 / x, 3 * y]) == ( dom, [dom.convert(2 / x), dom.convert(3 * y)], ) dom = RR.frac_field(x) assert construct_domain([2 / x, 3.5]) == ( dom, [dom.convert(2 / x), dom.convert(3.5)], ) dom = RR.frac_field(x, y) assert construct_domain([2 / x, 3.5 * y]) == ( dom, [dom.convert(2 / x), dom.convert(3.5 * y)], ) dom = RealField(prec=336)[x] assert construct_domain([pi.evalf(100) * x]) == ( dom, [dom.convert(pi.evalf(100) * x)], ) assert construct_domain(2) == (ZZ, ZZ(2)) assert construct_domain(S(2) / 3) == (QQ, QQ(2, 3)) assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3)) assert construct_domain({}) == (ZZ, {})
def test_normalize_theta_set(): # Interval assert normalize_theta_set(Interval(pi, 2*pi)) == \ Union(FiniteSet(0), Interval.Ropen(pi, 2*pi)) assert normalize_theta_set(Interval(pi * Rational(9, 2), 5 * pi)) == Interval(pi / 2, pi) assert normalize_theta_set(Interval(pi * Rational(-3, 2), pi / 2)) == Interval.Ropen(0, 2 * pi) assert normalize_theta_set(Interval.open(pi*Rational(-3, 2), pi/2)) == \ Union(Interval.Ropen(0, pi/2), Interval.open(pi/2, 2*pi)) assert normalize_theta_set(Interval.open(pi*Rational(-7, 2), pi*Rational(-3, 2))) == \ Union(Interval.Ropen(0, pi/2), Interval.open(pi/2, 2*pi)) assert normalize_theta_set(Interval(-pi/2, pi/2)) == \ Union(Interval(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi)) assert normalize_theta_set(Interval.open(-pi/2, pi/2)) == \ Union(Interval.Ropen(0, pi/2), Interval.open(pi*Rational(3, 2), 2*pi)) assert normalize_theta_set(Interval(-4 * pi, 3 * pi)) == Interval.Ropen(0, 2 * pi) assert normalize_theta_set(Interval(pi * Rational(-3, 2), -pi / 2)) == Interval( pi / 2, pi * Rational(3, 2)) assert normalize_theta_set(Interval.open(0, 2 * pi)) == Interval.open( 0, 2 * pi) assert normalize_theta_set(Interval.Ropen(-pi/2, pi/2)) == \ Union(Interval.Ropen(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi)) assert normalize_theta_set(Interval.Lopen(-pi/2, pi/2)) == \ Union(Interval(0, pi/2), Interval.open(pi*Rational(3, 2), 2*pi)) assert normalize_theta_set(Interval(-pi/2, pi/2)) == \ Union(Interval(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi)) assert normalize_theta_set(Interval.open( 4 * pi, pi * Rational(9, 2))) == Interval.open(0, pi / 2) assert normalize_theta_set(Interval.Lopen( 4 * pi, pi * Rational(9, 2))) == Interval.Lopen(0, pi / 2) assert normalize_theta_set(Interval.Ropen( 4 * pi, pi * Rational(9, 2))) == Interval.Ropen(0, pi / 2) assert normalize_theta_set(Interval.open(3*pi, 5*pi)) == \ Union(Interval.Ropen(0, pi), Interval.open(pi, 2*pi)) # FiniteSet assert normalize_theta_set(FiniteSet(0, pi, 3 * pi)) == FiniteSet(0, pi) assert normalize_theta_set(FiniteSet(0, pi / 2, pi, 2 * pi)) == FiniteSet(0, pi / 2, pi) assert normalize_theta_set(FiniteSet(0, -pi / 2, -pi, -2 * pi)) == FiniteSet( 0, pi, pi * Rational(3, 2)) assert normalize_theta_set(FiniteSet(pi*Rational(-3, 2), pi/2)) == \ FiniteSet(pi/2) assert normalize_theta_set(FiniteSet(2 * pi)) == FiniteSet(0) # Unions assert normalize_theta_set(Union(Interval(0, pi/3), Interval(pi/2, pi))) == \ Union(Interval(0, pi/3), Interval(pi/2, pi)) assert normalize_theta_set(Union(Interval(0, pi), Interval(2*pi, pi*Rational(7, 3)))) == \ Interval(0, pi) # ValueError for non-real sets raises(ValueError, lambda: normalize_theta_set(S.Complexes)) # NotImplementedError for subset of reals raises(NotImplementedError, lambda: normalize_theta_set(Interval(0, 1))) # NotImplementedError without pi as coefficient raises(NotImplementedError, lambda: normalize_theta_set(Interval(1, 2 * pi))) raises(NotImplementedError, lambda: normalize_theta_set(Interval(2 * pi, 10))) raises(NotImplementedError, lambda: normalize_theta_set(FiniteSet(0, 3, 3 * pi)))
def test_RootOf___new__(): assert RootOf(x, 0) == 0 assert RootOf(x, -1) == 0 assert RootOf(x, S.Zero) == 0 assert RootOf(x - 1, 0) == 1 assert RootOf(x - 1, -1) == 1 assert RootOf(x + 1, 0) == -1 assert RootOf(x + 1, -1) == -1 assert RootOf(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2) assert RootOf(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2) assert RootOf(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2) assert RootOf(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2) r = RootOf(x**2 + 2*x + 3, 0, radicals=False) assert isinstance(r, RootOf) is True r = RootOf(x**2 + 2*x + 3, 1, radicals=False) assert isinstance(r, RootOf) is True r = RootOf(x**2 + 2*x + 3, -1, radicals=False) assert isinstance(r, RootOf) is True r = RootOf(x**2 + 2*x + 3, -2, radicals=False) assert isinstance(r, RootOf) is True assert RootOf((x - 1)*(x + 1), 0, radicals=False) == -1 assert RootOf((x - 1)*(x + 1), 1, radicals=False) == 1 assert RootOf((x - 1)*(x + 1), -1, radicals=False) == 1 assert RootOf((x - 1)*(x + 1), -2, radicals=False) == -1 assert RootOf((x - 1)*(x + 1), 0, radicals=True) == -1 assert RootOf((x - 1)*(x + 1), 1, radicals=True) == 1 assert RootOf((x - 1)*(x + 1), -1, radicals=True) == 1 assert RootOf((x - 1)*(x + 1), -2, radicals=True) == -1 assert RootOf((x - 1)*(x**3 + x + 3), 0) == RootOf(x**3 + x + 3, 0) assert RootOf((x - 1)*(x**3 + x + 3), 1) == 1 assert RootOf((x - 1)*(x**3 + x + 3), 2) == RootOf(x**3 + x + 3, 1) assert RootOf((x - 1)*(x**3 + x + 3), 3) == RootOf(x**3 + x + 3, 2) assert RootOf((x - 1)*(x**3 + x + 3), -1) == RootOf(x**3 + x + 3, 2) assert RootOf((x - 1)*(x**3 + x + 3), -2) == RootOf(x**3 + x + 3, 1) assert RootOf((x - 1)*(x**3 + x + 3), -3) == 1 assert RootOf((x - 1)*(x**3 + x + 3), -4) == RootOf(x**3 + x + 3, 0) assert RootOf(x**4 + 3*x**3, 0) == -3 assert RootOf(x**4 + 3*x**3, 1) == 0 assert RootOf(x**4 + 3*x**3, 2) == 0 assert RootOf(x**4 + 3*x**3, 3) == 0 raises(GeneratorsNeeded, lambda: RootOf(0, 0)) raises(GeneratorsNeeded, lambda: RootOf(1, 0)) raises(PolynomialError, lambda: RootOf(Poly(0, x), 0)) raises(PolynomialError, lambda: RootOf(Poly(1, x), 0)) raises(PolynomialError, lambda: RootOf(x - y, 0)) raises(NotImplementedError, lambda: RootOf(x**3 - x + sqrt(2), 0)) raises(NotImplementedError, lambda: RootOf(x**3 - x + I, 0)) raises(IndexError, lambda: RootOf(x**2 - 1, -4)) raises(IndexError, lambda: RootOf(x**2 - 1, -3)) raises(IndexError, lambda: RootOf(x**2 - 1, 2)) raises(IndexError, lambda: RootOf(x**2 - 1, 3)) raises(ValueError, lambda: RootOf(x**2 - 1, x)) assert RootOf(Poly(x - y, x), 0) == y assert RootOf(Poly(x**2 - y, x), 0) == -sqrt(y) assert RootOf(Poly(x**2 - y, x), 1) == sqrt(y) assert RootOf(Poly(x**3 - y, x), 0) == y**Rational(1, 3) assert RootOf(y*x**3 + y*x + 2*y, x, 0) == -1 raises(NotImplementedError, lambda: RootOf(x**3 + x + 2*y, x, 0)) assert RootOf(x**3 + x + 1, 0).is_commutative is True
def test_ImageSet(): raises(ValueError, lambda: ImageSet(x, S.Integers)) assert ImageSet(Lambda(x, 1), S.Integers) == FiniteSet(1) assert ImageSet(Lambda(x, y), S.Integers) == {y} assert ImageSet(Lambda(x, 1), S.EmptySet) == S.EmptySet empty = Intersection(FiniteSet(log(2) / pi), S.Integers) assert unchanged(ImageSet, Lambda(x, 1), empty) # issue #17471 squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert (1, 2) not in harmonics assert harmonics.is_iterable assert imageset(x, -x, Interval(0, 1)) == Interval(-1, 0) assert ImageSet(Lambda(x, x**2), Interval(0, 2)).doit() == Interval(0, 4) assert ImageSet(Lambda((x, y), 2 * x), {4}, {3}).doit() == FiniteSet(8) assert (ImageSet(Lambda((x, y), x + y), {1, 2, 3}, {10, 20, 30}).doit() == FiniteSet(11, 12, 13, 21, 22, 23, 31, 32, 33)) c = Interval(1, 3) * Interval(1, 3) assert Tuple(2, 6) in ImageSet(Lambda(((x, y), ), (x, 2 * y)), c) assert Tuple(2, S.Half) in ImageSet(Lambda(((x, y), ), (x, 1 / y)), c) assert Tuple(2, -2) not in ImageSet(Lambda(((x, y), ), (x, y**2)), c) assert Tuple(2, -2) in ImageSet(Lambda(((x, y), ), (x, -2)), c) c3 = ProductSet(Interval(3, 7), Interval(8, 11), Interval(5, 9)) assert Tuple(8, 3, 9) in ImageSet(Lambda(((t, y, x), ), (y, t, x)), c3) assert Tuple(Rational(1, 8), 3, 9) in ImageSet(Lambda(((t, y, x), ), (1 / y, t, x)), c3) assert 2 / pi not in ImageSet(Lambda(((x, y), ), 2 / x), c) assert 2 / S(100) not in ImageSet(Lambda(((x, y), ), 2 / x), c) assert Rational(2, 3) in ImageSet(Lambda(((x, y), ), 2 / x), c) S1 = imageset(lambda x, y: x + y, S.Integers, S.Naturals) assert S1.base_pset == ProductSet(S.Integers, S.Naturals) assert S1.base_sets == (S.Integers, S.Naturals) # Passing a set instead of a FiniteSet shouldn't raise assert unchanged(ImageSet, Lambda(x, x**2), {1, 2, 3}) S2 = ImageSet(Lambda(((x, y), ), x + y), {(1, 2), (3, 4)}) assert 3 in S2.doit() # FIXME: This doesn't yet work: #assert 3 in S2 assert S2._contains(3) is None raises(TypeError, lambda: ImageSet(Lambda(x, x**2), 1))
def killing(x, y): """Killing inner product in Lie algebras.""" return -Rational(1, 2) * (x * y).trace()
def test_exp(): x = Symbol('x', integer=True) assert refine(exp(pi*I*2*x)) == 1 assert refine(exp(pi*I*2*(x + Rational(1, 2)))) == -1 assert refine(exp(pi*I*2*(x + Rational(1, 4)))) == I assert refine(exp(pi*I*2*(x + Rational(3, 4)))) == -I
def test_frac(): assert isinstance(frac(x), frac) assert frac(oo) == AccumBounds(0, 1) assert frac(-oo) == AccumBounds(0, 1) assert frac(zoo) is nan assert frac(n) == 0 assert frac(nan) is nan assert frac(Rational(4, 3)) == Rational(1, 3) assert frac(-Rational(4, 3)) == Rational(2, 3) assert frac(Rational(-4, 3)) == Rational(2, 3) r = Symbol('r', real=True) assert frac(I * r) == I * frac(r) assert frac(1 + I * r) == I * frac(r) assert frac(0.5 + I * r) == 0.5 + I * frac(r) assert frac(n + I * r) == I * frac(r) assert frac(n + I * k) == 0 assert unchanged(frac, x + I * x) assert frac(x + I * n) == frac(x) assert frac(x).rewrite(floor) == x - floor(x) assert frac(x).rewrite(ceiling) == x + ceiling(-x) assert frac(y).rewrite(floor).subs(y, pi) == frac(pi) assert frac(y).rewrite(floor).subs(y, -E) == frac(-E) assert frac(y).rewrite(ceiling).subs(y, -pi) == frac(-pi) assert frac(y).rewrite(ceiling).subs(y, E) == frac(E) assert Eq(frac(y), y - floor(y)) assert Eq(frac(y), y + ceiling(-y)) r = Symbol('r', real=True) p_i = Symbol('p_i', integer=True, positive=True) n_i = Symbol('p_i', integer=True, negative=True) np_i = Symbol('np_i', integer=True, nonpositive=True) nn_i = Symbol('nn_i', integer=True, nonnegative=True) p_r = Symbol('p_r', real=True, positive=True) n_r = Symbol('n_r', real=True, negative=True) np_r = Symbol('np_r', real=True, nonpositive=True) nn_r = Symbol('nn_r', real=True, nonnegative=True) # Real frac argument, integer rhs assert frac(r) <= p_i assert not frac(r) <= n_i assert (frac(r) <= np_i).has(Le) assert (frac(r) <= nn_i).has(Le) assert frac(r) < p_i assert not frac(r) < n_i assert not frac(r) < np_i assert (frac(r) < nn_i).has(Lt) assert not frac(r) >= p_i assert frac(r) >= n_i assert frac(r) >= np_i assert (frac(r) >= nn_i).has(Ge) assert not frac(r) > p_i assert frac(r) > n_i assert (frac(r) > np_i).has(Gt) assert (frac(r) > nn_i).has(Gt) assert not Eq(frac(r), p_i) assert not Eq(frac(r), n_i) assert Eq(frac(r), np_i).has(Eq) assert Eq(frac(r), nn_i).has(Eq) assert Ne(frac(r), p_i) assert Ne(frac(r), n_i) assert Ne(frac(r), np_i).has(Ne) assert Ne(frac(r), nn_i).has(Ne) # Real frac argument, real rhs assert (frac(r) <= p_r).has(Le) assert not frac(r) <= n_r assert (frac(r) <= np_r).has(Le) assert (frac(r) <= nn_r).has(Le) assert (frac(r) < p_r).has(Lt) assert not frac(r) < n_r assert not frac(r) < np_r assert (frac(r) < nn_r).has(Lt) assert (frac(r) >= p_r).has(Ge) assert frac(r) >= n_r assert frac(r) >= np_r assert (frac(r) >= nn_r).has(Ge) assert (frac(r) > p_r).has(Gt) assert frac(r) > n_r assert (frac(r) > np_r).has(Gt) assert (frac(r) > nn_r).has(Gt) assert not Eq(frac(r), n_r) assert Eq(frac(r), p_r).has(Eq) assert Eq(frac(r), np_r).has(Eq) assert Eq(frac(r), nn_r).has(Eq) assert Ne(frac(r), p_r).has(Ne) assert Ne(frac(r), n_r) assert Ne(frac(r), np_r).has(Ne) assert Ne(frac(r), nn_r).has(Ne) # Real frac argument, +/- oo rhs assert frac(r) < oo assert frac(r) <= oo assert not frac(r) > oo assert not frac(r) >= oo assert not frac(r) < -oo assert not frac(r) <= -oo assert frac(r) > -oo assert frac(r) >= -oo assert frac(r) < 1 assert frac(r) <= 1 assert not frac(r) > 1 assert not frac(r) >= 1 assert not frac(r) < 0 assert (frac(r) <= 0).has(Le) assert (frac(r) > 0).has(Gt) assert frac(r) >= 0 # Some test for numbers assert frac(r) <= sqrt(2) assert (frac(r) <= sqrt(3) - sqrt(2)).has(Le) assert not frac(r) <= sqrt(2) - sqrt(3) assert not frac(r) >= sqrt(2) assert (frac(r) >= sqrt(3) - sqrt(2)).has(Ge) assert frac(r) >= sqrt(2) - sqrt(3) assert not Eq(frac(r), sqrt(2)) assert Eq(frac(r), sqrt(3) - sqrt(2)).has(Eq) assert not Eq(frac(r), sqrt(2) - sqrt(3)) assert Ne(frac(r), sqrt(2)) assert Ne(frac(r), sqrt(3) - sqrt(2)).has(Ne) assert Ne(frac(r), sqrt(2) - sqrt(3)) assert frac(p_i, evaluate=False).is_zero assert frac(p_i, evaluate=False).is_finite assert frac(p_i, evaluate=False).is_integer assert frac(p_i, evaluate=False).is_real assert frac(r).is_finite assert frac(r).is_real assert frac(r).is_zero is None assert frac(r).is_integer is None assert frac(oo).is_finite assert frac(oo).is_real
def test_issue461(): assert integrate(x**Rational(3,2), x) == 2*x**Rational(5,2)/5 assert integrate(x**Rational(1,2), x) == 2*x**Rational(3,2)/3 assert integrate(x**Rational(-3,2), x) == -2*x**Rational(-1,2)
def test_nested_floor_ceiling(): assert floor(-floor(ceiling(x**3) / y)) == -floor(ceiling(x**3) / y) assert ceiling(-floor(ceiling(x**3) / y)) == -floor(ceiling(x**3) / y) assert floor(ceiling(-floor(x**Rational(7, 2) / y))) == -floor( x**Rational(7, 2) / y) assert -ceiling(-ceiling(floor(x) / y)) == ceiling(floor(x) / y)
def test_issue519(): assert integrate(pi*x**Rational(1,2),x) == 2*pi*x**Rational(3,2)/3 assert integrate(pi*x**Rational(1,2) + E*x**Rational(3,2),x) == \ 2*pi*x**Rational(3,2)/3 + \ 2*E *x**Rational(5,2)/5
def test_polynomial_relation_simplification(): assert Ge(3*x*(x + 1) + 4, 3*x).simplify() in [Ge(x**2, -Rational(4,3)), Le(-x**2, Rational(4, 3))] assert Le(-(3*x*(x + 1) + 4), -3*x).simplify() in [Ge(x**2, -Rational(4,3)), Le(-x**2, Rational(4, 3))] assert ((x**2+3)*(x**2-1)+3*x >= 2*x**2).simplify() in [(x**4 + 3*x >= 3), (-x**4 - 3*x <= -3)]
def test_issue565(): assert integrate(-1./2 * x * sin(n * pi * x/2), [x, -2, 0]) == 2*cos(pi*n)/(pi*n) assert integrate(-Rational(1)/2 * x * sin(n * pi * x/2), [x, -2, 0]) \ == 2*cos(pi*n)/(pi*n)
def RGEs(self, model): Printer.RGE = True self.string += "\n\n\\section{Renormalization Group Equations}\n" translation = {'GaugeCouplings': 'Gauge couplings', 'Yukawas': 'Yukawa couplings', 'QuarticTerms': 'Quartic couplings', 'TrilinearTerms' : 'Trilinear couplings', 'ScalarMasses': 'Scalar mass couplings', 'FermionMasses': 'Fermion mass couplings', 'Vevs': 'Vacuum-expectation values'} self.string += '\\subsection{Convention}\n\\begin{equation*}\n' if self.betaFactor == 1: beta = r'\mu \frac{d X}{d \mu}' else: X = Symbol('X') if self.betaFactor == Rational(1,2): beta = r'\mu^2 \frac{d X}{d \mu^2}' elif isinstance(self.betaFactor, Mul) and Rational(1,2) in self.betaFactor.args: self.betaFactor *= Rational(2) beta = r'\mu^2 \frac{d}{d \mu^2}\left(' + latex(self.betaFactor*X) + r'\right)' else: beta = r'\mu \frac{d}{d \mu}\left(' + latex(self.betaFactor*X) + r'\right)' self.string += r'\beta\left(X\right) \equiv ' + beta if model.betaExponent(Symbol('n')) != 0: self.string += r'\equiv' + '+'.join([latex(sympify(f'1/(4*pi)**({model.betaExponent(n)})', evaluate=False))+'\\beta^{('+str(n)+')}(X)' for n in range(1, 1+max(model.nLoops))]) else: self.string += r'\equiv' + '+'.join(['\\beta^{('+str(n)+')}(X)' for n in range(1, 1+max(model.nLoops))]) self.string += '\n\\end{equation*}\n' if ( ('sub' in model.substitutions and model.substitutions['sub'] != {}) or ('yukMat' in model.substitutions and model.substitutions['yukMat'] != {}) or ('zero' in model.substitutions and model.substitutions['zero'] != {}) ): self.string += '\\subsection{Definitions and substitutions}\n' if 'zero' in model.substitutions and model.substitutions['zero'] != {}: nPerLine = 5 i = 0 n0 = len(model.substitutions['zero']) n = n0 while n > 0: if n > nPerLine: group = list(model.substitutions['zero'].items())[i:i+nPerLine] n -= nPerLine i += nPerLine else: group = list(model.substitutions['zero'].items())[i:] n = 0 self.string += '\\begin{equation*}\n' self.string += ' \\quad,\\quad '.join([self.totex(v) + '=' + self.totex(0) for k,v in group]) + '\n' self.string += '\\end{equation*}\n' if 'yukMat' in model.substitutions and model.substitutions['yukMat'] != {}: nPerLine = 3 i = 0 n0 = len(model.substitutions['yukMat']) n = n0 while n > 0: if n > nPerLine: group = list(model.substitutions['yukMat'].items())[i:i+nPerLine] n -= nPerLine i += nPerLine else: group = list(model.substitutions['yukMat'].items())[i:] n = 0 self.string += '\\begin{equation*}\n' self.string += ' \\quad,\\quad '.join([self.totex(model.allCouplings[k][1]) + '=' + self.totex(v[1]) for k,v in group]) + '\n' self.string += '\\end{equation*}\n' if 'sub' in model.substitutions and model.substitutions['sub'] != {}: nPerLine = 3 i = 0 n0 = len(model.substitutions['sub']) n = n0 while n > 0: if n > nPerLine: group = list(model.substitutions['sub'].items())[i:i+nPerLine] n -= nPerLine i += nPerLine else: group = list(model.substitutions['sub'].items())[i:] n = 0 self.string += '\\begin{equation*}\n' self.string += ' \\quad,\\quad '.join([self.totex(v[0]) + '\equiv' + self.totex(v[1], baseMul=True) for k,v in group]) + '\n' self.string += '\\end{equation*}\n' for cType, dic in model.couplingRGEs.items(): if dic == {}: continue if 'Anomalous' in cType: continue self.string += "\n\n\\subsection{" + translation[cType] + "}\n{\\allowdisplaybreaks\n" if cType in model.NonZeroCouplingRGEs: self.string += r'\emph{\textbf{Warning:} The following couplings were set to 0 in the model file, but have a non-zero \mbox{$\beta$-function}.' self.string += '\n' + r'This may lead to an inconsistent RG flow, except if these couplings can be safely approximated to 0 at all considered energy scales : ' + '\n}\n' self.string += r'\begin{center}' + '\n' self.string += '$' + '$, $'.join([(str(c) if str(c) not in self.latex else self.latex[str(c)]) for c in model.NonZeroCouplingRGEs[cType][0]]) + '$ .\n' self.string += r'\end{center}' + '\n' if cType == 'Vevs': self.string += self.vevs(model) for c in dic[0]: for n in range(model.loopDic[cType]): RGE = dic[n][c] cSymb = model.allCouplings[c][1] self.string += "\n\\begin{align*}\n\\begin{autobreak}\n" if type(RGE) != list: self.string += '\\beta^{('+str(n+1)+')}(' + self.totex(cSymb) + ') =' self.string += self.totex(RGE, sort=True, cType=cType) else: self.string += '\\left.\\beta^{('+str(n+1)+')}(' + self.totex(cSymb) + ')\\right\\rvert_{i j} =' totalRGE = RGE[0] for k,v in RGE[1].items(): if not isMinus(v.args[0] if isinstance(v, Add) else v): totalRGE += Delta(Symbol('i'), k[0], Symbol('j'), k[1])*v else: totalRGE -= Delta(Symbol('i'), k[0], Symbol('j'), k[1])*(-1*v) self.string += self.totex(expand(totalRGE), sort=True, cType=cType) self.string += "\n\\end{autobreak}\n\\end{align*}" if cType not in model.NonZeroCouplingRGEs: self.string += "\n}" continue dic = model.NonZeroCouplingRGEs[cType] for c in dic[0]: for n in range(model.loopDic[cType]): RGE = dic[n][c] self.string += "\n\\begin{align*}\n\\begin{autobreak}\n" self.string += '\\beta^{('+str(n+1)+')}(' + self.latex[str(c)] + ') =' self.string += self.totex(RGE, sort=True, cType=cType).replace('\\left(', '\\big(').replace('\\right)', '\\big)') self.string += "\n\\end{autobreak}\n\\end{align*}" self.string += "\n}"
def issue_1785(): assert integrate(sqrt(x)*(1+x)) == 2*x**Rational(3, 2)/3 + 2*x**Rational(5, 2)/5 assert integrate(x**x*(1+log(x))) is not None
def test_pretty_integrals(): expr = Integral(log(x), x) ascii_str = \ """\ / \n\ | \n\ | log(x) dx\n\ | \n\ / \ """ ucode_str = \ u"""\ ⌠ \n\ ⎮ log(x) dx\n\ ⌡ \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(x**2, x) ascii_str = \ """\ / \n\ | \n\ | 2 \n\ | x dx\n\ | \n\ / \ """ ucode_str = \ u"""\ ⌠ \n\ ⎮ 2 \n\ ⎮ x dx\n\ ⌡ \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral((sin(x))**2 / (tan(x))**2) ascii_str = \ """\ / \n\ | \n\ | 2 \n\ | sin (x) \n\ | ------- dx\n\ | 2 \n\ | tan (x) \n\ | \n\ / \ """ ucode_str = \ u"""\ ⌠ \n\ ⎮ 2 \n\ ⎮ sin (x) \n\ ⎮ ─────── dx\n\ ⎮ 2 \n\ ⎮ tan (x) \n\ ⌡ \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(x**(2**x), x) ascii_str = \ """\ / \n\ | \n\ | / x\ \n\ | \\2 / \n\ | x dx\n\ | \n\ / \ """ ucode_str = \ u"""\ ⌠ \n\ ⎮ ⎛ x⎞ \n\ ⎮ ⎝2 ⎠ \n\ ⎮ x dx\n\ ⌡ \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(x**2, (x, 1, 2)) ascii_str = \ """\ 2 \n\ / \n\ | \n\ | 2 \n\ | x dx\n\ | \n\ / \n\ 1 \ """ ucode_str = \ u"""\ 2 \n\ ⌠ \n\ ⎮ 2 \n\ ⎮ x dx\n\ ⌡ \n\ 1 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(x**2, (x, Rational(1, 2), 10)) ascii_str = \ """\ 10 \n\ / \n\ | \n\ | 2 \n\ | x dx\n\ | \n\ / \n\ 1/2 \ """ ucode_str = \ u"""\ 10 \n\ ⌠ \n\ ⎮ 2 \n\ ⎮ x dx\n\ ⌡ \n\ 1/2 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(x**2 * y**2, x, y) ascii_str = \ """\ / / \n\ | | \n\ | | 2 2 \n\ | | x *y dx dy\n\ | | \n\ / / \ """ ucode_str = \ u"""\ ⌠ ⌠ \n\ ⎮ ⎮ 2 2 \n\ ⎮ ⎮ x ⋅y dx dy\n\ ⌡ ⌡ \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = Integral(sin(th) / cos(ph), (th, 0, pi), (ph, 0, 2 * pi)) ascii_str = \ """\ 2*pi pi \n\ / / \n\ | | \n\ | | sin(theta) \n\ | | ---------- d(theta) d(phi)\n\ | | cos(phi) \n\ | | \n\ / / \n\ 0 0 \ """ ucode_str = \ u"""\ 2⋅π π \n\ ⌠ ⌠ \n\ ⎮ ⎮ sin(θ) \n\ ⎮ ⎮ ────── dθ dφ\n\ ⎮ ⎮ cos(φ) \n\ ⌡ ⌡ \n\ 0 0 \ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str
def test_multiple_integration(): assert integrate((x**2)*(y**2), (x,0,1), (y,-1,2)) == Rational(1) assert integrate((y**2)*(x**2), x, y) == Rational(1,9)*(x**3)*(y**3) assert integrate(1/(x+3)/(1+x)**3, x) == -S(1)/8*log(3 + x) + S(1)/8*log(1 + x) + x/(4 + 8*x + 4*x**2)
def test_issue_4090(): assert limit(1/(x + 3), x, 2) == Rational(1, 5) assert limit(1/(x + pi), x, 2) == S.One/(2 + pi) assert limit(log(x)/(x**2 + 3), x, 2) == log(2)/7 assert limit(log(x)/(x**2 + pi), x, 2) == log(2)/(4 + pi)
def test_floor(): assert floor(nan) is nan assert floor(oo) is oo assert floor(-oo) is -oo assert floor(zoo) is zoo assert floor(0) == 0 assert floor(1) == 1 assert floor(-1) == -1 assert floor(E) == 2 assert floor(-E) == -3 assert floor(2 * E) == 5 assert floor(-2 * E) == -6 assert floor(pi) == 3 assert floor(-pi) == -4 assert floor(S.Half) == 0 assert floor(Rational(-1, 2)) == -1 assert floor(Rational(7, 3)) == 2 assert floor(Rational(-7, 3)) == -3 assert floor(-Rational(7, 3)) == -3 assert floor(Float(17.0)) == 17 assert floor(-Float(17.0)) == -17 assert floor(Float(7.69)) == 7 assert floor(-Float(7.69)) == -8 assert floor(I) == I assert floor(-I) == -I e = floor(i) assert e.func is floor and e.args[0] == i assert floor(oo * I) == oo * I assert floor(-oo * I) == -oo * I assert floor(exp(I * pi / 4) * oo) == exp(I * pi / 4) * oo assert floor(2 * I) == 2 * I assert floor(-2 * I) == -2 * I assert floor(I / 2) == 0 assert floor(-I / 2) == -I assert floor(E + 17) == 19 assert floor(pi + 2) == 5 assert floor(E + pi) == 5 assert floor(I + pi) == 3 + I assert floor(floor(pi)) == 3 assert floor(floor(y)) == floor(y) assert floor(floor(x)) == floor(x) assert unchanged(floor, x) assert unchanged(floor, 2 * x) assert unchanged(floor, k * x) assert floor(k) == k assert floor(2 * k) == 2 * k assert floor(k * n) == k * n assert unchanged(floor, k / 2) assert unchanged(floor, x + y) assert floor(x + 3) == floor(x) + 3 assert floor(x + k) == floor(x) + k assert floor(y + 3) == floor(y) + 3 assert floor(y + k) == floor(y) + k assert floor(3 + I * y + pi) == 6 + floor(y) * I assert floor(k + n) == k + n assert unchanged(floor, x * I) assert floor(k * I) == k * I assert floor(Rational(23, 10) - E * I) == 2 - 3 * I assert floor(sin(1)) == 0 assert floor(sin(-1)) == -1 assert floor(exp(2)) == 7 assert floor(log(8) / log(2)) != 2 assert int(floor(log(8) / log(2)).evalf(chop=True)) == 3 assert floor(factorial(50)/exp(1)) == \ 11188719610782480504630258070757734324011354208865721592720336800 assert (floor(y) < y) == False assert (floor(y) <= y) == True assert (floor(y) > y) == False assert (floor(y) >= y) == False assert (floor(x) <= x).is_Relational # x could be non-real assert (floor(x) > x).is_Relational assert (floor(x) <= y).is_Relational # arg is not same as rhs assert (floor(x) > y).is_Relational assert (floor(y) <= oo) == True assert (floor(y) < oo) == True assert (floor(y) >= -oo) == True assert (floor(y) > -oo) == True assert floor(y).rewrite(frac) == y - frac(y) assert floor(y).rewrite(ceiling) == -ceiling(-y) assert floor(y).rewrite(frac).subs(y, -pi) == floor(-pi) assert floor(y).rewrite(frac).subs(y, E) == floor(E) assert floor(y).rewrite(ceiling).subs(y, E) == -ceiling(-E) assert floor(y).rewrite(ceiling).subs(y, -pi) == -ceiling(pi) assert Eq(floor(y), y - frac(y)) assert Eq(floor(y), -ceiling(-y)) neg = Symbol('neg', negative=True) nn = Symbol('nn', nonnegative=True) pos = Symbol('pos', positive=True) np = Symbol('np', nonpositive=True) assert (floor(neg) < 0) == True assert (floor(neg) <= 0) == True assert (floor(neg) > 0) == False assert (floor(neg) >= 0) == False assert (floor(neg) <= -1) == True assert (floor(neg) >= -3) == (neg >= -3) assert (floor(neg) < 5) == (neg < 5) assert (floor(nn) < 0) == False assert (floor(nn) >= 0) == True assert (floor(pos) < 0) == False assert (floor(pos) <= 0) == (pos < 1) assert (floor(pos) > 0) == (pos >= 1) assert (floor(pos) >= 0) == True assert (floor(pos) >= 3) == (pos >= 3) assert (floor(np) <= 0) == True assert (floor(np) > 0) == False assert floor(neg).is_negative == True assert floor(neg).is_nonnegative == False assert floor(nn).is_negative == False assert floor(nn).is_nonnegative == True assert floor(pos).is_negative == False assert floor(pos).is_nonnegative == True assert floor(np).is_negative is None assert floor(np).is_nonnegative is None assert (floor(7, evaluate=False) >= 7) == True assert (floor(7, evaluate=False) > 7) == False assert (floor(7, evaluate=False) <= 7) == True assert (floor(7, evaluate=False) < 7) == False assert (floor(7, evaluate=False) >= 6) == True assert (floor(7, evaluate=False) > 6) == True assert (floor(7, evaluate=False) <= 6) == False assert (floor(7, evaluate=False) < 6) == False assert (floor(7, evaluate=False) >= 8) == False assert (floor(7, evaluate=False) > 8) == False assert (floor(7, evaluate=False) <= 8) == True assert (floor(7, evaluate=False) < 8) == True assert (floor(x) <= 5.5) == Le(floor(x), 5.5, evaluate=False) assert (floor(x) >= -3.2) == Ge(floor(x), -3.2, evaluate=False) assert (floor(x) < 2.9) == Lt(floor(x), 2.9, evaluate=False) assert (floor(x) > -1.7) == Gt(floor(x), -1.7, evaluate=False) assert (floor(y) <= 5.5) == (y < 6) assert (floor(y) >= -3.2) == (y >= -3) assert (floor(y) < 2.9) == (y < 3) assert (floor(y) > -1.7) == (y >= -1) assert (floor(y) <= n) == (y < n + 1) assert (floor(y) >= n) == (y >= n) assert (floor(y) < n) == (y < n) assert (floor(y) > n) == (y >= n + 1)
def test_issue_6560(): e = (5*x**3/4 - x*Rational(3, 4) + (y*(3*x**2/2 - S.Half) + 35*x**4/8 - 15*x**2/4 + Rational(3, 8))/(2*(y + 1))) assert limit(e, y, oo) == (5*x**3 + 3*x**2 - 3*x - 1)/4
def test_ceiling(): assert ceiling(nan) is nan assert ceiling(oo) is oo assert ceiling(-oo) is -oo assert ceiling(zoo) is zoo assert ceiling(0) == 0 assert ceiling(1) == 1 assert ceiling(-1) == -1 assert ceiling(E) == 3 assert ceiling(-E) == -2 assert ceiling(2 * E) == 6 assert ceiling(-2 * E) == -5 assert ceiling(pi) == 4 assert ceiling(-pi) == -3 assert ceiling(S.Half) == 1 assert ceiling(Rational(-1, 2)) == 0 assert ceiling(Rational(7, 3)) == 3 assert ceiling(-Rational(7, 3)) == -2 assert ceiling(Float(17.0)) == 17 assert ceiling(-Float(17.0)) == -17 assert ceiling(Float(7.69)) == 8 assert ceiling(-Float(7.69)) == -7 assert ceiling(I) == I assert ceiling(-I) == -I e = ceiling(i) assert e.func is ceiling and e.args[0] == i assert ceiling(oo * I) == oo * I assert ceiling(-oo * I) == -oo * I assert ceiling(exp(I * pi / 4) * oo) == exp(I * pi / 4) * oo assert ceiling(2 * I) == 2 * I assert ceiling(-2 * I) == -2 * I assert ceiling(I / 2) == I assert ceiling(-I / 2) == 0 assert ceiling(E + 17) == 20 assert ceiling(pi + 2) == 6 assert ceiling(E + pi) == 6 assert ceiling(I + pi) == I + 4 assert ceiling(ceiling(pi)) == 4 assert ceiling(ceiling(y)) == ceiling(y) assert ceiling(ceiling(x)) == ceiling(x) assert unchanged(ceiling, x) assert unchanged(ceiling, 2 * x) assert unchanged(ceiling, k * x) assert ceiling(k) == k assert ceiling(2 * k) == 2 * k assert ceiling(k * n) == k * n assert unchanged(ceiling, k / 2) assert unchanged(ceiling, x + y) assert ceiling(x + 3) == ceiling(x) + 3 assert ceiling(x + k) == ceiling(x) + k assert ceiling(y + 3) == ceiling(y) + 3 assert ceiling(y + k) == ceiling(y) + k assert ceiling(3 + pi + y * I) == 7 + ceiling(y) * I assert ceiling(k + n) == k + n assert unchanged(ceiling, x * I) assert ceiling(k * I) == k * I assert ceiling(Rational(23, 10) - E * I) == 3 - 2 * I assert ceiling(sin(1)) == 1 assert ceiling(sin(-1)) == 0 assert ceiling(exp(2)) == 8 assert ceiling(-log(8) / log(2)) != -2 assert int(ceiling(-log(8) / log(2)).evalf(chop=True)) == -3 assert ceiling(factorial(50)/exp(1)) == \ 11188719610782480504630258070757734324011354208865721592720336801 assert (ceiling(y) >= y) == True assert (ceiling(y) > y) == False assert (ceiling(y) < y) == False assert (ceiling(y) <= y) == False assert (ceiling(x) >= x).is_Relational # x could be non-real assert (ceiling(x) < x).is_Relational assert (ceiling(x) >= y).is_Relational # arg is not same as rhs assert (ceiling(x) < y).is_Relational assert (ceiling(y) >= -oo) == True assert (ceiling(y) > -oo) == True assert (ceiling(y) <= oo) == True assert (ceiling(y) < oo) == True assert ceiling(y).rewrite(floor) == -floor(-y) assert ceiling(y).rewrite(frac) == y + frac(-y) assert ceiling(y).rewrite(floor).subs(y, -pi) == -floor(pi) assert ceiling(y).rewrite(floor).subs(y, E) == -floor(-E) assert ceiling(y).rewrite(frac).subs(y, pi) == ceiling(pi) assert ceiling(y).rewrite(frac).subs(y, -E) == ceiling(-E) assert Eq(ceiling(y), y + frac(-y)) assert Eq(ceiling(y), -floor(-y)) neg = Symbol('neg', negative=True) nn = Symbol('nn', nonnegative=True) pos = Symbol('pos', positive=True) np = Symbol('np', nonpositive=True) assert (ceiling(neg) <= 0) == True assert (ceiling(neg) < 0) == (neg <= -1) assert (ceiling(neg) > 0) == False assert (ceiling(neg) >= 0) == (neg > -1) assert (ceiling(neg) > -3) == (neg > -3) assert (ceiling(neg) <= 10) == (neg <= 10) assert (ceiling(nn) < 0) == False assert (ceiling(nn) >= 0) == True assert (ceiling(pos) < 0) == False assert (ceiling(pos) <= 0) == False assert (ceiling(pos) > 0) == True assert (ceiling(pos) >= 0) == True assert (ceiling(pos) >= 1) == True assert (ceiling(pos) > 5) == (pos > 5) assert (ceiling(np) <= 0) == True assert (ceiling(np) > 0) == False assert ceiling(neg).is_positive == False assert ceiling(neg).is_nonpositive == True assert ceiling(nn).is_positive is None assert ceiling(nn).is_nonpositive is None assert ceiling(pos).is_positive == True assert ceiling(pos).is_nonpositive == False assert ceiling(np).is_positive == False assert ceiling(np).is_nonpositive == True assert (ceiling(7, evaluate=False) >= 7) == True assert (ceiling(7, evaluate=False) > 7) == False assert (ceiling(7, evaluate=False) <= 7) == True assert (ceiling(7, evaluate=False) < 7) == False assert (ceiling(7, evaluate=False) >= 6) == True assert (ceiling(7, evaluate=False) > 6) == True assert (ceiling(7, evaluate=False) <= 6) == False assert (ceiling(7, evaluate=False) < 6) == False assert (ceiling(7, evaluate=False) >= 8) == False assert (ceiling(7, evaluate=False) > 8) == False assert (ceiling(7, evaluate=False) <= 8) == True assert (ceiling(7, evaluate=False) < 8) == True assert (ceiling(x) <= 5.5) == Le(ceiling(x), 5.5, evaluate=False) assert (ceiling(x) >= -3.2) == Ge(ceiling(x), -3.2, evaluate=False) assert (ceiling(x) < 2.9) == Lt(ceiling(x), 2.9, evaluate=False) assert (ceiling(x) > -1.7) == Gt(ceiling(x), -1.7, evaluate=False) assert (ceiling(y) <= 5.5) == (y <= 5) assert (ceiling(y) >= -3.2) == (y > -4) assert (ceiling(y) < 2.9) == (y <= 2) assert (ceiling(y) > -1.7) == (y > -2) assert (ceiling(y) <= n) == (y <= n) assert (ceiling(y) >= n) == (y > n - 1) assert (ceiling(y) < n) == (y <= n - 1) assert (ceiling(y) > n) == (y > n)
def test_issue_13332(): assert limit(sqrt(30)*5**(-5*x - 1)*(46656*x)**x*(5*x + 2)**(5*x + 5*S.Half) * (6*x + 2)**(-6*x - 5*S.Half), x, oo) == Rational(25, 36)
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