def test_subs(): from sympy import besseli assert NS('besseli(-x, y) - besseli(x, y)', subs={x: 3.5, y: 20.0}) == \ '-4.92535585957223e-10' assert NS('Piecewise((x, x>0)) + Piecewise((1-x, x>0))', subs={x: 0.1}) == \ '1.00000000000000' raises(TypeError, lambda: x.evalf(subs=(x, 1)))
def test_evalf_bugs(): assert NS(sin(1)+exp(-10**10),10) == NS(sin(1),10) assert NS(exp(10**10)+sin(1),10) == NS(exp(10**10),10) assert NS('log(1+1/10**50)',20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)',10) == '100.0000000' assert NS('log(2)',10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x:'1/10**50'}) == '-0.166666666666667' assert NS(sin(1)+Rational(1,10**100)*I,15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1+I)**2*I, 6) == '-2.00000' d={n: (-1)**Rational(6,7), y: (-1)**Rational(4,7), x: (-1)**Rational(2,7)} assert NS((x*(1+y*(1 + n))).subs(d).evalf(),6) == '0.346011 + 0.433884*I' assert NS(((-I-sqrt(2)*I)**2).evalf()) == '-5.82842712474619' assert NS((1+I)**2*I,15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n, subs={n:.01}) == '19.8100000000000' assert NS(((x - 1)*((1 - x))**1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2*x).n()) == '-2.00000000000000*x' assert NS((-2*x*y).n()) == '-2.00000000000000*x*y'
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y'
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' #1659 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' #1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y' #3561. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * sin(oo)).n() == S.Zero assert (0 / sin(oo)).n() == S.Zero assert (0 * E**(oo)).n() == S.NaN assert (0 / E**(oo)).n() == S.Zero assert (0 + sin(oo)).n() == S.NaN assert (0 - sin(oo)).n() == S.NaN assert (0 + E**(oo)).n() == S.Infinity assert (0 - E**(oo)).n() == S.NegativeInfinity assert (5 * sin(oo)).n() == S.NaN assert (5 / sin(oo)).n() == S.NaN assert (5 * E**(oo)).n() == S.Infinity assert (5 / E**(oo)).n() == S.Zero assert (5 + sin(oo)).n() == S.NaN assert (5 - sin(oo)).n() == S.NaN assert (5 + E**(oo)).n() == S.Infinity assert (5 - E**(oo)).n() == S.NegativeInfinity
def test_evalf_with_zoo(): assert (1 / x).evalf(subs={x: 0}) == zoo # issue 8242 assert (-1 / x).evalf(subs={x: 0}) == zoo # PR 16150 assert (0**x).evalf(subs={x: -1}) == zoo # PR 16150 assert (0**x).evalf(subs={x: -1 + I}) == nan assert Mul(2, Pow(0, -1, evaluate=False), evaluate=False).evalf() == zoo # issue 21147 assert Mul(x, 1 / x, evaluate=False).evalf(subs={x: 0}) == Mul( x, 1 / x, evaluate=False).subs(x, 0) == nan assert Mul(1 / x, 1 / x, evaluate=False).evalf(subs={x: 0}) == zoo assert Mul(1 / x, Abs(1 / x), evaluate=False).evalf(subs={x: 0}) == zoo assert Abs(zoo, evaluate=False).evalf() == oo assert re(zoo, evaluate=False).evalf() == nan assert im(zoo, evaluate=False).evalf() == nan assert Add(zoo, zoo, evaluate=False).evalf() == nan assert Add(oo, zoo, evaluate=False).evalf() == nan assert Pow(zoo, -1, evaluate=False).evalf() == 0 assert Pow(zoo, Rational(-1, 3), evaluate=False).evalf() == 0 assert Pow(zoo, Rational(1, 3), evaluate=False).evalf() == zoo assert Pow(zoo, S.Half, evaluate=False).evalf() == zoo assert Pow(zoo, 2, evaluate=False).evalf() == zoo assert Pow(0, zoo, evaluate=False).evalf() == nan assert log(zoo, evaluate=False).evalf() == zoo assert zoo.evalf(chop=True) == zoo assert x.evalf(subs={x: zoo}) == zoo
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS( '(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational( 1, 10**100)*I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2*I, 6) == '-2.00000' d = {n: ( -1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)} assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2)*I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2*I, 15) == '-2.00000000000000' # issue 4758 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' # issue 4758 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n, subs={n: .01}) == '19.8100000000000' assert NS(((x - 1)*((1 - x))**1000).n() ) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2*x).n()) == '-2.00000000000000*x' assert NS((-2*x*y).n()) == '-2.00000000000000*x*y' assert cos(x).n(subs={x: 1+I}) == cos(x).subs(x, 1+I).n() # issue 6660. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0*sin(oo)).n() == S.Zero assert (0/sin(oo)).n() == S.Zero assert (0*E**(oo)).n() == S.NaN assert (0/E**(oo)).n() == S.Zero assert (0+sin(oo)).n() == S.NaN assert (0-sin(oo)).n() == S.NaN assert (0+E**(oo)).n() == S.Infinity assert (0-E**(oo)).n() == S.NegativeInfinity assert (5*sin(oo)).n() == S.NaN assert (5/sin(oo)).n() == S.NaN assert (5*E**(oo)).n() == S.Infinity assert (5/E**(oo)).n() == S.Zero assert (5+sin(oo)).n() == S.NaN assert (5-sin(oo)).n() == S.NaN assert (5+E**(oo)).n() == S.Infinity assert (5-E**(oo)).n() == S.NegativeInfinity #issue 7416 assert as_mpmath(0.0, 10, {'chop': True}) == 0
def test_evalf_bugs(): assert NS(sin(1) + exp(-10 ** 10), 10) == NS(sin(1), 10) assert NS(exp(10 ** 10) + sin(1), 10) == NS(exp(10 ** 10), 10) assert NS("log(1+1/10**50)", 20) == "1.0000000000000000000e-50" assert NS("log(10**100,10)", 10) == "100.0000000" assert NS("log(2)", 10) == "0.6931471806" assert NS("(sin(x)-x)/x**3", 15, subs={x: "1/10**50"}) == "-0.166666666666667" assert NS(sin(1) + Rational(1, 10 ** 100) * I, 15) == "0.841470984807897 + 1.00000000000000e-100*I" assert x.evalf() == x assert NS((1 + I) ** 2 * I, 6) == "-2.00000" d = {n: (-1) ** Rational(6, 7), y: (-1) ** Rational(4, 7), x: (-1) ** Rational(2, 7)} assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == "0.346011 + 0.433884*I" assert NS(((-I - sqrt(2) * I) ** 2).evalf()) == "-5.82842712474619" assert NS((1 + I) ** 2 * I, 15) == "-2.00000000000000" # 1659 (1/2): assert NS(pi.evalf(69) - pi) == "-4.43863937855894e-71" # 1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n ** 25 - 477638700 * n ** 37 - 19 * n, subs={n: 0.01}) == "19.8100000000000" assert NS(((x - 1) * ((1 - x)) ** 1000).n()) == "(-x + 1.00000000000000)**1000*(x - 1.00000000000000)" assert NS((-x).n()) == "-x" assert NS((-2 * x).n()) == "-2.00000000000000*x" assert NS((-2 * x * y).n()) == "-2.00000000000000*x*y" assert cos(x).n(subs={x: 1 + I}) == cos(x).subs(x, 1 + I).n() # 3561. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * sin(oo)).n() == S.Zero assert (0 / sin(oo)).n() == S.Zero assert (0 * E ** (oo)).n() == S.NaN assert (0 / E ** (oo)).n() == S.Zero assert (0 + sin(oo)).n() == S.NaN assert (0 - sin(oo)).n() == S.NaN assert (0 + E ** (oo)).n() == S.Infinity assert (0 - E ** (oo)).n() == S.NegativeInfinity assert (5 * sin(oo)).n() == S.NaN assert (5 / sin(oo)).n() == S.NaN assert (5 * E ** (oo)).n() == S.Infinity assert (5 / E ** (oo)).n() == S.Zero assert (5 + sin(oo)).n() == S.NaN assert (5 - sin(oo)).n() == S.NaN assert (5 + E ** (oo)).n() == S.Infinity assert (5 - E ** (oo)).n() == S.NegativeInfinity
def test_eigenvals(): ''' It takes the solutions given by the script written in Matlab which ut can be seen in eigenvals.m. The results of the scripts are written in eigenvals.txt which Python will read to check if the results of the library are the same. If you want to add more tests, you can add them in the script in Matlab. Python will do everything automatically. ''' def item_in_list(lst, it): for item in lst: if item.startswith(it): return True return False successful_cases = parse_eigenvalues() for case in successful_cases: m = Matrix(case[0]) eigenvals = [str(x.evalf()) for x in m.eigenvals(multiple=True)] assert len(eigenvals) == len(case[1]) and set( map(lambda x: item_in_list(eigenvals, x), case[1])) == {True} raises(NonSquareMatrixError, lambda: m12.eigenvals()) raises(NonSquareMatrixError, lambda: m32.eigenvals())
def test_evalf_bugs(): assert NS(sin(1) + exp(-10 ** 10), 10) == NS(sin(1), 10) assert NS(exp(10 ** 10) + sin(1), 10) == NS(exp(10 ** 10), 10) assert NS("log(1+1/10**50)", 20) == "1.0000000000000000000e-50" assert NS("log(10**100,10)", 10) == "100.0000000" assert NS("log(2)", 10) == "0.6931471806" assert NS("(sin(x)-x)/x**3", 15, subs={x: "1/10**50"}) == "-0.166666666666667" assert NS(sin(1) + Rational(1, 10 ** 100) * I, 15) == "0.841470984807897 + 1.00000000000000e-100*I" assert x.evalf() == x assert NS((1 + I) ** 2 * I, 6) == "-2.00000 + 2.32831e-10*I" d = {n: (-1) ** Rational(6, 7), y: (-1) ** Rational(4, 7), x: (-1) ** Rational(2, 7)} assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == "0.346011 + 0.433884*I" assert NS(((-I - sqrt(2) * I) ** 2).evalf()) == "-5.82842712474619" assert NS((1 + I) ** 2 * I, 15) == "-2.00000000000000 + 2.16840434497101e-19*I" # 1659 (1/2): assert NS(pi.evalf(69) - pi) == "-4.43863937855894e-71" # 1659 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n ** 25 - 477638700 * n ** 37 - 19 * n, subs={n: 0.01}) == "19.8100000000000" assert NS(((x - 1) * ((1 - x)) ** 1000).n()) == "(-x + 1.00000000000000)**1000*(x - 1.00000000000000)" assert NS((-x).n()) == "-x" assert NS((-2 * x).n()) == "-2.00000000000000*x" assert NS((-2 * x * y).n()) == "-2.00000000000000*x*y"
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('expand_log(log(1+1/10**50))', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' # issue 4758 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' # issue 4758 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * ((1 - x))** 1000).n()) == '(1.00000000000000 - x)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y' assert cos(x).n(subs={x: 1 + I}) == cos(x).subs(x, 1 + I).n() # issue 6660. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * E**(oo)).n() == S.NaN assert (0 / E**(oo)).n() == S.Zero assert (0 + E**(oo)).n() == S.Infinity assert (0 - E**(oo)).n() == S.NegativeInfinity assert (5 * E**(oo)).n() == S.Infinity assert (5 / E**(oo)).n() == S.Zero assert (5 + E**(oo)).n() == S.Infinity assert (5 - E**(oo)).n() == S.NegativeInfinity #issue 7416 assert as_mpmath(0.0, 10, {'chop': True}) == 0 #issue 5412 assert ((oo * I).n() == S.Infinity * I) assert ((oo + oo * I).n() == S.Infinity + S.Infinity * I) #issue 11518 assert NS(2 * x**2.5, 5) == '2.0000*x**2.5000' #issue 13076 assert NS(Mul(Max(0, y), x, evaluate=False).evalf()) == 'x*Max(0, y)'
def test_evalf_bugs(): assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10) assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10) assert NS('expand_log(log(1+1/10**50))', 20) == '1.0000000000000000000e-50' assert NS('log(10**100,10)', 10) == '100.0000000' assert NS('log(2)', 10) == '0.6931471806' assert NS('(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667' assert NS(sin(1) + Rational(1, 10**100) * I, 15) == '0.841470984807897 + 1.00000000000000e-100*I' assert x.evalf() == x assert NS((1 + I)**2 * I, 6) == '-2.00000' d = { n: (-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7) } assert NS((x * (1 + y * (1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I' assert NS(((-I - sqrt(2) * I)**2).evalf()) == '-5.82842712474619' assert NS((1 + I)**2 * I, 15) == '-2.00000000000000' # issue 4758 (1/2): assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71' # issue 4758 (2/2): With the bug present, this still only fails if the # terms are in the order given here. This is not generally the case, # because the order depends on the hashes of the terms. assert NS(20 - 5008329267844 * n**25 - 477638700 * n**37 - 19 * n, subs={n: .01}) == '19.8100000000000' assert NS( ((x - 1) * (1 - x)** 1000).n()) == '(1.00000000000000 - x)**1000*(x - 1.00000000000000)' assert NS((-x).n()) == '-x' assert NS((-2 * x).n()) == '-2.00000000000000*x' assert NS((-2 * x * y).n()) == '-2.00000000000000*x*y' assert cos(x).n(subs={x: 1 + I}) == cos(x).subs(x, 1 + I).n() # issue 6660. Also NaN != mpmath.nan # In this order: # 0*nan, 0/nan, 0*inf, 0/inf # 0+nan, 0-nan, 0+inf, 0-inf # >>> n = Some Number # n*nan, n/nan, n*inf, n/inf # n+nan, n-nan, n+inf, n-inf assert (0 * E**(oo)).n() is S.NaN assert (0 / E**(oo)).n() is S.Zero assert (0 + E**(oo)).n() is S.Infinity assert (0 - E**(oo)).n() is S.NegativeInfinity assert (5 * E**(oo)).n() is S.Infinity assert (5 / E**(oo)).n() is S.Zero assert (5 + E**(oo)).n() is S.Infinity assert (5 - E**(oo)).n() is S.NegativeInfinity #issue 7416 assert as_mpmath(0.0, 10, {'chop': True}) == 0 #issue 5412 assert ((oo * I).n() == S.Infinity * I) assert ((oo + oo * I).n() == S.Infinity + S.Infinity * I) #issue 11518 assert NS(2 * x**2.5, 5) == '2.0000*x**2.5000' #issue 13076 assert NS(Mul(Max(0, y), x, evaluate=False).evalf()) == 'x*Max(0, y)' #issue 18516 assert NS( log( S(3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 ) / 36360291795869936842385267079543319118023385026001623040346035832580600191583895484198508262979388783308179702534403855752855931517013066142992430916562025780021771247847643450125342836565813209972590371590152578728008385990139795377610001 ).evalf(15, chop=True)) == '-oo'
def test_subs(): assert NS("besseli(-x, y) - besseli(x, y)", subs={x: 3.5, y: 20.0}) == "-4.92535585957223e-10" assert NS("Piecewise((x, x>0)) + Piecewise((1-x, x>0))", subs={x: 0.1}) == "1.00000000000000" raises(TypeError, lambda: x.evalf(subs=(x, 1)))