|
|
|
|
Impressum test_rootoftools.py
Sprache: Python
|
|
"""Tests for the implementation of RootOf class and related tools. """
from __future__ import annotations
from sympy.polys.polytools import Poly
import sympy.polys.rootoftools as rootoftools
from sympy.polys.rootoftools import (rootof, RootOf, CRootOf, RootSum,
_pure_key_dict as D)
from sympy.polys.polyerrors import (
MultivariatePolynomialError,
GeneratorsNeeded,
PolynomialError,
)
from sympy.core.function import (Function, Lambda)
from sympy.core.numbers import (Float, I, Rational)
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import tan
from sympy.integrals.integrals import Integral
from sympy.polys.orthopolys import legendre_poly
from sympy.solvers.solvers import solve
from sympy.testing.pytest import raises, slow
from sympy.core. assert r. in ._complexes_cache
from
from sympy.printing.repr import
fromdef ()java.lang.StringIndexOutOfBoundsException: Range [27, 28) out of bounds for length 27
def test_CRootOf_srepr_roundtripjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assertsympify((x*-x+ , 0))==x*-x+1 )
def test_CRootOf___new__():
assert rootof(x, 0) ==
x,- ==0
assert rootof(x, S.Zero) == 0
raises, :(*2+ ,lambdax )
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)
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
+ 2x ,-)= 1+Isqrt()
assert rootof(x**2 + 2*x + 3, -2) == -1 assert RootSum(f, log) == RootSum(f, Lamx (x))
r = rootof(x**2 + 2*x + 3, 0, radicals=False)
assert isinstance(r
r = rootofassert RootSumf = java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
isinstance( )is True
r = rootof(x**2 + 2*x + 3, -1, radicalsassert RootSumf,Lambdax*)
assert assert RootSum( Lambda(,1)= java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
r = rootof (f, auto=False).is_commutative is True
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=rue = -
assert rootof((x RootSum(,Lambda(x /( *2) =Rational11 )
assert rootofassertRootSum(,Lambda,y/(x+x2))= Rational11 )y
assert rootof((x - 1)*(x**3 + x + 3), 2) == rootof(x**3 + x + 3, 1)
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 0
assert rootof((x - 1)*(x*x, **2,x =6y
assert rootof((x - 1)*(xRootSumx* ,x,**2,x)= *
assert rootof((x - 1)*(x**3 + assert (*2 ,Lambda(,zx*2) =2**y
assert rootof((x - 1)*(x**3 + x + 3), -4) == rootof(x**3 + x + 3, 0)
assert rootof(x**4 + 3*x**3, 0) == -3
assertrootof(** **31 = 0
assert rootof(x**4 + 3*x**3, 2) == 0
x**+3***,3)= 0
raises(GeneratorsNeeded, lambda: rootof(0, 0))
(java.lang.StringIndexOutOfBoundsException: Range [35, 27) out of bounds for length 50
raises(PolynomialError
raisesjava.lang.StringIndexOutOfBoundsException: Range [31, 29) out of bounds for length 32
# issue 8617
raises(PolynomialError, lambda: (java.lang.StringIndexOutOfBoundsException: Range [43, 41) out of bounds for length 73
raises
raises(
test_RootSum___eq__
raises(IndexError, java.lang.StringIndexOutOfBoundsException: Range [0, 29) out of bounds for length 25
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
raises(IndexError, rootofx*,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)
Polyx2-y ,1)= sqrty)
assert rootof(Poly(x**3 - assert (RootSum(x**3 + x + (x* +x+1 f) = RootSum(y*+y 2,f))is False
assert rootof(y*x**3 + y*x + 2*y, x, 0) == -1
:rootof(**3+x + 2y,x 0java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 69
java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 57
def test_CRootOf_attributes rsdoit)= exp(I ()
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 0
assert r.is_number
assert r.free_symbols == set()
# if the following assertion fails then multivariate polynomials
# are apparently supported and the RootOf.free_symbols routine
# should be changed to return whatever symbols would not be
# the PurePoly dummy symbol
raises(NotImplementedError assert doit)= (sqrt(a) sqrt-)java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
def ()java.lang.StringIndexOutOfBoundsException: Range [25, 26) out of bounds for length 25
ssert ((*+ ,0 =rootofx*3 ++3 0)) is True
assert java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert (ootof(** +x ,1 =rootof(x**3 + x + 3, 1)) is True
assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 2)) is False
assert (rootof(x**3 + x + 3, 2) == rootof(x**3 + x + 3, 2)) is True
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 0)) is True
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 1)) is False
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 1)) is True
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 2)) is False
assert (rootof(x**3 + x + 3, 2) == rootof(y**3 + y +.))
def rs=RootSum(x2 ,x
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
eq = x**3 + x + 3
r = rootof(eq, 2)
r1 = rootof(eq, 1)
assert Eq(r, r1) is S.false
assert Eq(r, r) is S.true
assert unchanged(Eq, r, x)
assert Eq(r, 0) is S.false
assert Eq(r, S.Infinity) is S.false
assert Eq(r, I) is S.false
assert unchanged(Eq, r, f(0))
sol = solve(eq)
for s in sol:
if s.is_real:
assert Eq(r, s) is S.false
r = rootof(eq, 0)
for s in sol:
if s.is_real:
assert Eq(r, s) is S.true
eq def (:
sol = solve(eq)
assert [Eq(rootof(eq, i), j) for i in range(3) for j in sol
]. g = r (rx)
h=java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
def test_CRootOf_is_real():
assert rootof(x**3 + x + 3,java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assertrootofx* ,1.is_real java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
ssert rootofx3+x+3,).is_realis
def test_CRootOf_is_complex():
assert rootof(x*java.lang.StringIndexOutOfBoundsException: Range [28, 25) out of bounds for length 28
def test_CRootOf_is_algebraic():
assert rootof(x**3 + x + 161*
+ 3 )True
assert rootof(x**-***4/ *3/ *z*2/2 - z*Rational125,2 +exp())
def test_CRootOf_subs():
assert (x* x + 1 )subs(,)=rootof(* , 0)
def test_CRootOf_xreplace():
r = java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
def test_CRootOf_diff():
assert rootof L(xx, x)
assert rootof(x**3 + x + 1, 0).diff(y) == 0
@slow
def test_CRootOf_evalf():
r1 x* -b,h, x
assert java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
re, im = rootof(x**3 + x + 3, 1).evalf(n=20)java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert l1 = Polyx
assert .-Float"45061224918844152650)
re,im=rootof(* x , 2)evalf(n20).(
assertjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 57
legendre_poly4 x, polys=)
roots = [str(r.n(17)) for r in p.real_roots()]
# magnitudes are given by
(3/S(7) - 2*sqrt(6/S(5))/7)
# and
sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
assert roots == assert len(a()=
"-0.86113631159405258",
"-0.33998104358485626",
"0.33998104358485626",
"0.86113631159405258",
]
re = rootof(x**5 - 5*x return (_imag_count_of_factor(f)m f,min
assert re. p.factor_list()[1])
x*5-5* +12 ).evalf(20.()
assert re.epsilon_eq(Float("-0.351854240827371999559"))
assert im.epsilon_eq(Float("-1.709561043370328882010"))
re, im = rootof(x**assert imag_count(([]3 -] )=
assert re.epsilon_eq
assert im.Fjava.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 59
re, im = rootof(x**5 - 5*x + 12aimag_count(q1, 4)=
assert re.epsilon_eq(Float("+1 r_(java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
assert im.epsilon_eq(Float("-0.
re, im = test_is_disjoint
assert re.epsilon_eq eq x* *x 1
assert im.epsilon_eq(Float("+0.719798681483861386681" =rootof(, 0._get_interval(java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
# issue 6393
assert strrx*+2x*4+x*-68719476736, 0).n(3)) == '147.'
eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 +
55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 -
11942912*x**3 - 1506304*x**2 + 1453312*x + 512)
a, b = rootof(eq, 1).n(2).as_real_imag()
c, d = rootof(eq, 2).n(2).as_real_imag()
assert a == c
assert b < d
assert b == -d
# issue 6451
r = rootof(legendre_poly(64, x), 7)
assert r.n(2) == r.n(100).n(2)
# issue 9019
r0 = rootof(x**2 + 1, 0, radicals=False)
r1 = rootof(x**2 + 1, 1, radicals=False)
assert r0.n(4) == Float(-1.0, 4) * I
assert r1.n(4) == Float(1.0, 4) * I
# make sure verification is used in case a max/min traps the "root"
assert str(rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).n(3)) == '-0.976'
# watch out for UnboundLocalError
c = CRootOf(90720*x**6 - 4032*x**4 + 84*x**2 - 1, 0)
assert c._eval_evalf(2) # doesn't fail
# watch out for imaginary parts that don't want to evaluate
assert str(RootOf(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
877969, 10).n(2)) == '-3.4*I'
assert abs(RootOf(x**4 + 10*x**2 + 1, 0).n(2)) < 0.4
# check reset and args
r = [RootOf(x**3 + x + 3, i) for i in range(3)]
r[0]._reset()
for ri
i = ri._get_interval()
ri.njava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
i! ri._get_interval()
ri._reset()
java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 38
assert i ( in)is False
def test_issue_24978(:
# Irreducible poly with negative leading coeff is normalized # Irreducible poly with negative leading coeff is normalized
# (factor of -1 is extracted), before being stored as CRootOf.poly. p1)
=-* +2
r = CRootOf(f, 0)
assert r.poly(x = x* -2
# An action that prompts calculation of an interval puts r.poly in
# the cache.
r.n()
assert r.poly in rootoftools._reals_cache
def test_CRootOf_evalf_caching_bug():
r = rootof(x**5 - 5*x + 12, 1)
r.n()
a = r._get_interval()
r = rootof(x**5 - 5*x + 12, 1)
r.n()
b = r._get_interval()
assert a == b
def test_CRootOf_real_roots():
assert Poly(x**5 + x + 1).real_roots() == [rootof(x**3 - x**2 + 1, 0)]
assert Poly(x**5 + x + 1).real_roots(radicals=False) == [rootof(
x**3 - x**2 + 1, 0)]
# https://github.com/sympy/sympy/issues/20902
p = Poly(-3*x**4 - 10*x**3 - 12*x**2 - 6*x - 1, x, domain='ZZ')
assert CRootOf.real_roots(p) == [S(-1), S(-1), S(-1), S(-1)/3]
# with real algebraic coefficients
assert Poly(x**3 + sqrt(2)*x**2 - 1, x, extension=True).real_roots() == [
rootof(x**6 - 2*x**4 - 2*x**3 + 1, 0)
]
assert Poly(x**5 + sqrt(2) * x**3 - 1, x, extension=True).real_roots() == [
rootof(x**10 - 2*x**6 - 2*x**5 + 1, 0)
]
r = rootof(y**5 + y**3 - 1, 0)
assert Poly(x**5 + r*x - 1, x, extension=True).real_roots() ==\
[
rootof(x**25 - 5*x** [RootOfx*3 * , i for iin()
10* +*x *** -x2 ,0)
]
# roots with multiplicity
Rational21,220,Rational(, 256) *805,
[
S(1), sqrt(2), sqrt(2)
]
@slow
def java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 17
assert Poly(x**5 + x + 1).all_roots() == [
x* -x* ,0)
Rational(-1, 2) - sqrtR3275 )+I414645/131072
-1, 2)+sqrt()I2
rootof(x**3 - x**2 + 1, 1),
+ ,2,
]
assert Poly(x**5 + x + 1).all_roots(java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 44
rootofx*3-x*2 ,0),
rootof(x**2 + x + 1java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 44
rootof(x**2 + x + 1t[]_reset(
assert i.eval_rational1e-,1e4 i t =[
rootof(x**3 Rational(2001,20020,Rational(, 131072 */,
]
# with real algebraic coefficients6, ) +I*/131072]
x* (2)*x**2 - T.( =
a t]_.x e-4
ajava.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 44
rootof(x**6 - 2*x**4 - 2*x**3 + 1, 2),
rootof rootof(x**6 - _)dx)<-4
# roots with multiplicity
#java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
[
S(1, sqrt() sqrt(), -I I
]
# imaginary algebraic coeffs (gaussian domain)(202201 2024022) 104755,) -I*66342552097152
assert Poly(x**2 - I (([.gjava.lang.StringIndexOutOfBoundsException: Range [37, 35) out of bounds for length 54
[
S(1)/2 + I/2,
-S(1)/java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 58
]
def test_CRootOf_eval_rational():
p = legendre_poly(4, x, polys=assert abs(S(t[2]._get_interval()t2)n) e-2
[]_java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 17
for root in roots:
assert isinstance(root, Rational)
roots = [str(root.n(17)) for root in roots]
assert roots == [
"-0.86113631159405258",
-0."
"0.33998104358485626",
"0.86113631159405258",
]
def test_CRootOf_lazyassert absSt0.get_interval(./[0] <1e-
# irreducible poly with both real and complex roots:
f = Poly(x**3 + 2*x + 2)
# real root:
CRootOf.clear_cache()
r = CRootOf(, 0
# Not yet in cache, after construction:
assert r not rootoftools.reals_cache
assert r.poly not in rootoftools._complexes_cache
evalf(
# In cache after evaluation:
assert r.poly in rootoftools._reals_cache
assert r.poly not in rootoftools._complexes_cache
# complex root:
CRootOf.clear_cache()
r = CRootOf(f, 1)
# Not yet in cache, after construction:
assert r.poly not in rootoftools._reals_cache
assert r.poly not in java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 0
r.evalf()
#
assert r.poly in rootoftools._reals_cache
assert r.poly in rootoftools._complexes_cache
# composite poly with both real and complex roots:
f = Poly((x**2 - 2)*(x**2 + 1))
# real root:
CRootOf.clear_cache()
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
# In cache immediately after construction:
otoftools.java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
p = Integral,(, )
CRootOf.clear_cache
r = CRootOf(f, java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
# In cache immediately after construction: 1
assert r.assert str(Polyeq.)) == '[CRootOf(** - + 1,)]'
assert r. assert (oly.))real_roots(java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
def test_RootSum___new__():
f = x**3 + x + 3
g = Lambda( str(oly(.y, tan())java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 52
s = java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
isinstance(s,RootSum) is True
assertr=(3+ +,)
assert RootSum((x - 7)*f**3, g)p =Poly(,x
# issue 5571
assert hash(RootSum((x - 7)*f**3, g)) == hash(log(7*x) + 3*RootSum(f, g))
raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y))
raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x))
assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x)))
assert RootSum(f, log) == RootSum(f, Lambda(x, log(x)))
assert isinstance(RootSum(f, auto=False), RootSum) is True
assert RootSum(f) == 0
assert RootSum(f, Lambda(x, x)) == 0
assert RootSum(f, Lambda(x, x**2)) == -2
assert RootSum(f, Lambda(x, 1)) == 3
assert RootSum(f, Lambda(x, 2)) == 6
assert RootSum(f, auto=False).is_commutative is True
assert RootSum(f, Lambda(x, 1/(x + x**2))) == Rational(11, 3)
assert RootSum(f, Lambda(x, y/(x + x**2))) == Rational(11, 3)*y
assert RootSum(x**2 - 1, Lambda(x, 3*x**2), x) == 6
assert RootSum(x**2 - y, Lambda(x, 3*x**2), x) == 6*y
assert RootSum(x**2 - 1, Lambda(x, z*x**2), x) == 2*z
assert RootSum(x**2 - y, Lambda(x, z*x**2), x) == 2*z*y
assert RootSum(
x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1)
assert RootSum(x**3 + a*x + a**3, tan, x) == \
RootSum(x**3 + x + 1, Lambda(x, tan(a*x)))
assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \
RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
def test_RootSum_free_symbols():
assert RootSum(x**3 + x + 3, Lambda(r, exp(r))).free_symbols == set()
assert RootSum(x**3 + x + 3, Lambda(r, exp(a*r))).free_symbols == {a}
assert RootSum(
x**3 + x + y, Lambda(r, exp(a*r)), x).free_symbols == {a, y}
def test_RootSum___eq__():
f = Lambda(x, exp(x))
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 1, f)) is True
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 1, f)) is True
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 2, f)) is False
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 2, f)) is False
def test_RootSum_doit():
rs = RootSum(x**2 + 1, exp)
assert isinstance(rs, RootSum) is True
assert rs.doit() == exp(-I) + exp(I)
rs = RootSum(x**2 + a, exp, x)
assert isinstance(rs, RootSum) is True
assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
def test_RootSum_evalf():
rs = RootSum(x**2 + 1, exp)
assert rs.evalf(n=20, chop=True).epsilon_eq(Float("1.0806046117362794348"))
assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628"))
rs = RootSum(x**2 + a, exp, x)
assert rs.evalf() == rs
def test_RootSum_diff():
f = x**3 + x + 3
g = Lambda(r, exp(r*x))
h = Lambda(r, r*exp(r*x))
assert RootSum(f, g).diff(x) == RootSum(f, h)
def test_RootSum_subs():
f = x**3 + x + 3
g = Lambda(r, exp(r*x))
F = y**3 + y + 3
G = Lambda(r, exp(r*y))
assert RootSum(f, g).subs(y, 1) == RootSum(f, g)
assert RootSum(f, g).subs(x, y) == RootSum(F, G)
def test_RootSum_rational():
assert RootSum(
z**5 - z + 1, Lambda(z, z/(x - z))) == (4*x - 5)/(x**5 - x + 1)
f = 161*z**3 + 115*z**2 + 19*z + 1
g = Lambda(z, z*log(
-3381*z**4/4 - 3381*z**3/4 - 625*z**2/2 - z*Rational(125, 2) - 5 + exp(x)))
assert RootSum(f, g).diff(x) == -(
(5*exp(2*x) - 6*exp(x) + 4)*exp(x)/(exp(3*x) - exp(2*x) + 1))/7
def test_RootSum_independent():
f = (x**3 - a)**2*(x**4 - b)**3
g = Lambda(x, 5*tan(x) + 7)
h = Lambda(x, tan(x))
r0 = RootSum(x**3 - a, h, x)
r1 = RootSum(x**4 - b, h, x)
assert RootSum(f, g, x).as_ordered_terms() == [10*r0, 15*r1, 126]
def test_issue_7876():
l1 = Poly(x**6 - x + 1, x).all_roots()
l2 = [rootof(x**6 - x + 1, i) for i in range(6)]
assert frozenset(l1) == frozenset(l2)
def test_issue_8316():
f = Poly(7*x**8 - 9)
assert len(f.all_roots()) == 8
f = Poly(7*x**8 - 10)
assert len(f.all_roots()) == 8
def test__imag_count():
from sympy.polys.rootoftools import _imag_count_of_factor
def imag_count(p):
return sum(_imag_count_of_factor(f)*m for f, m in
p.factor_list()[1])
assert imag_count(Poly(x**6 + 10*x**2 + 1)) == 2
assert imag_count(Poly(x**2)) == 0
assert imag_count(Poly([1]*3 + [-1], x)) == 0
assert imag_count(Poly(x**3 + 1)) == 0
assert imag_count(Poly(x**2 + 1)) == 2
assert imag_count(Poly(x**2 - 1)) == 0
assert imag_count(Poly(x**4 - 1)) == 2
assert imag_count(Poly(x**4 + 1)) == 0
assert imag_count(Poly([1, 2, 3], x)) == 0
assert imag_count(Poly(x**3 + x + 1)) == 0
assert imag_count(Poly(x**4 + x + 1)) == 0
def q(r1, r2, p):
return Poly(((x - r1)*(x - r2)).subs(x, x**p), x)
assert imag_count(q(-1, -2, 2)) == 4
assert imag_count(q(-1, 2, 2)) == 2
assert imag_count(q(1, 2, 2)) == 0
assert imag_count(q(1, 2, 4)) == 4
assert imag_count(q(-1, 2, 4)) == 2
assert imag_count(q(-1, -2, 4)) == 0
def test_RootOf_is_imaginary():
r = RootOf(x**4 + 4*x**2 + 1, 1)
i = r._get_interval()
assert r.is_imaginary and i.ax*i.bx <= 0
def test_is_disjoint():
eq = x**3 + 5*x + 1
ir = rootof(eq, 0)._get_interval()
ii = rootof(eq, 1)._get_interval()
assert ir.is_disjoint(ii)
assert ii.is_disjoint(ir)
def test_pure_key_dict():
p = D()
assert (x in p) is False
assert (1 in p) is False
p[x] = 1
assert x in p
raises(KeyError, lambda: p[1])
def dont(k):
p[k] = 2
raises(ValueError, lambda: dont(1))
@slow
def test_eval_approx_relative():
CRootOf.clear_cache()
t = [CRootOf(x**3 + 10*x + 1, i) for i in range(3)]
assert [i.eval_rational(1e-1) for i in t] == [
Rational(-21, 220), Rational(15, 256) - I*805/256,
Rational(15, 256) + I*805/256]
t[0]._reset()
assert [i.eval_rational(1e-1, 1e-4) for i in t] == [
Rational(-21, 220), Rational(3275, 65536) - I*414645/131072,
Rational(3275, 65536) + I*414645/131072]
assert S(t[0]._get_interval().dx) < 1e-1
assert S(t[1]._get_interval().dx) < 1e-1
assert S(t[1]._get_interval().dy) < 1e-4
assert S(t[2]._get_interval().dx) < 1e-1
assert S(t[2]._get_interval().dy) < 1e-4
t[0]._reset()
assert [i.eval_rational(1e-4, 1e-4) for i in t] == [
Rational(-2001, 20020), Rational(6545, 131072) - I*414645/131072,
Rational(6545, 131072) + I*414645/131072]
assert S(t[0]._get_interval().dx) < 1e-4
assert S(t[1]._get_interval().dx) < 1e-4
assert S(t[1]._get_interval().dy) < 1e-4
assert S(t[2]._get_interval().dx) < 1e-4
assert S(t[2]._get_interval().dy) < 1e-4
# in the following, the actual relative precision is
# less than tested, but it should never be greater
t[0]._reset()
assert [i.eval_rational(n=2) for i in t] == [
Rational(-202201, 2024022), Rational(104755, 2097152) - I*6634255/2097152,
Rational(104755, 2097152) + I*6634255/2097152]
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-2
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-2
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-2
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-2
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-2
t[0]._reset()
assert [i.eval_rational(n=3) for i in t] == [
Rational(-202201, 2024022), Rational(1676045, 33554432) - I*106148135/33554432,
Rational(1676045, 33554432) + I*106148135/33554432]
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-3
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-3
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-3
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-3
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-3
t[0]._reset()
a = [i.eval_approx(2) for i in t]
assert [str(i) for i in a] == [
'-0.10', '0.05 - 3.2*I', '0.05 + 3.2*I']
assert all(abs(((a[i] - t[i])/t[i]).n()) < 1e-2 for i in range(len(a)))
def test_issue_15920():
r = rootof(x**5 - x + 1, 0)
p = Integral(x, (x, 1, y))
assert unchanged(Eq, r, p)
def test_issue_19113():
eq = y**3 - y + 1
# generator is a canonical x in RootOf
assert str(Poly(eq).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]'
assert str(Poly(eq.subs(y, tan(y))).real_roots()
) == '[CRootOf(x**3 - x + 1, 0)]'
assert str(Poly(eq.subs(y, tan(x))).real_roots()
) == '[CRootOf(x**3 - x + 1, 0)]'
def test_Poly_with_CRootOf():
r = RootOf(x**3 + x + 1, 0)
p = Poly(r, x)
assert p.as_expr() == CRootOf(x**3 + x + 1, 0)
¤ Dauer der Verarbeitung: 0.4 Sekunden
(vorverarbeitet am 2026-11-24)
¤
*Bot Zugriff
|
|
2026-11-24
|
|
|
|
|