def test_root(): n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == cbrt(2) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5)/2 assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(4, 2, 1, evaluate=False) == -Pow(4, Rational(1, 2), evaluate=False) assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2)*I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == cbrt(x) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1/n) assert root(x, -n) == x**(-1/n) assert root(x, n, k) == x**(1/n)*(-1)**(2*k/n)
def test_root(): n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == cbrt(2) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(4, 2, 1, evaluate=False) == -Pow(4, Rational(1, 2), evaluate=False) assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == cbrt(x) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_minimize_poly(): assert minimize([x - 2 * y, x**2 + y**2 <= 1], x, y) == (-sqrt(5), { x: 4 / sqrt(5) - sqrt(5), y: 2 / sqrt(5) })
def test_nocache(clear_imports, monkeypatch): """Regression tests with DIOFANT_USE_CACHE=False. """ monkeypatch.setenv('DIOFANT_USE_CACHE', 'False') from diofant.core.cache import CACHE from diofant.core.symbol import Symbol from diofant.functions import sin, sqrt # test that we don't use cache assert CACHE == [] x = Symbol('x') assert CACHE == [] # issue sympy/sympy#8840 expr = (1 + x) * x # not raises # issue sympy/sympy#9413 (2 * x).is_complex # not raises # see commit c459d18 sin(x + x) # see commit 53dd1eb mx = -Symbol('x', negative=False) assert mx.is_positive is not True px = 2 * Symbol('x', positive=False) assert px.is_positive is not True # see commit 2eaaba2 s = 1 / sqrt(x**2) y = Symbol('y') result = s.subs(sqrt(x**2), y) assert result == 1 / y
def _expr_big_minus(self, a, z, n): if n.is_even: return (1 + z)**a * exp(2 * pi * I * n * a) * sqrt(z) * sin( 2 * a * atan(sqrt(z))) else: return (1 + z)**a*exp(2*pi*I*n*a)*sqrt(z) \ * sin(2*a*atan(sqrt(z)) - 2*pi*a)
def test_functional_diffgeom_ch2(): x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True) x, y = symbols('x, y', extended_real=True) f = Function('f') assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix([sqrt(x0**2 + y0**2), atan2(y0, x0)])) assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix([r0*cos(theta0), r0*sin(theta0)])) assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix( [[cos(theta0), -r0*sin(theta0)], [sin(theta0), r0*cos(theta0)]]) field = f(R2.x, R2.y) p1_in_rect = R2_r.point([x0, y0]) p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)]) assert field.rcall(p1_in_rect) == f(x0, y0) assert field.rcall(p1_in_polar) == f(x0, y0) p_r = R2_r.point([x0, y0]) p_p = R2_p.point([r0, theta0]) assert R2.x(p_r) == x0 assert R2.x(p_p) == r0*cos(theta0) assert R2.r(p_p) == r0 assert R2.r(p_r) == sqrt(x0**2 + y0**2) assert R2.theta(p_r) == atan2(y0, x0) h = R2.x*R2.r**2 + R2.y**3 assert h.rcall(p_r) == x0*(x0**2 + y0**2) + y0**3 assert h.rcall(p_p) == r0**3*sin(theta0)**3 + r0**3*cos(theta0)
def test_functional_diffgeom_ch2(): x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True) x, y = symbols('x, y', extended_real=True) f = Function('f') assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix( [sqrt(x0**2 + y0**2), atan2(y0, x0)])) assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix( [r0 * cos(theta0), r0 * sin(theta0)])) assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix( [[cos(theta0), -r0 * sin(theta0)], [sin(theta0), r0 * cos(theta0)]]) field = f(R2.x, R2.y) p1_in_rect = R2_r.point([x0, y0]) p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)]) assert field.rcall(p1_in_rect) == f(x0, y0) assert field.rcall(p1_in_polar) == f(x0, y0) p_r = R2_r.point([x0, y0]) p_p = R2_p.point([r0, theta0]) assert R2.x(p_r) == x0 assert R2.x(p_p) == r0 * cos(theta0) assert R2.r(p_p) == r0 assert R2.r(p_r) == sqrt(x0**2 + y0**2) assert R2.theta(p_r) == atan2(y0, x0) h = R2.x * R2.r**2 + R2.y**3 assert h.rcall(p_r) == x0 * (x0**2 + y0**2) + y0**3 assert h.rcall(p_p) == r0**3 * sin(theta0)**3 + r0**3 * cos(theta0)
def test_root(): from diofant.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def _expr_big_minus(cls, a, z, n): if n.is_even: return (1 + z)**a * exp(2 * pi * I * n * a) * cos( 2 * a * atan(sqrt(z))) else: return (1 + z)**a * exp( 2 * pi * I * n * a) * cos(2 * a * atan(sqrt(z)) - 2 * pi * a)
def _expr_big(cls, z, n): if n.is_even: return (n - Rational(1, 2)) * pi * I + log( sqrt(z) / 2) + I * asin(1 / sqrt(z)) else: return (n - Rational(1, 2)) * pi * I + log( sqrt(z) / 2) - I * asin(1 / sqrt(z))
def _sqrt_symbolic_denest(a, b, r): """Given an expression, sqrt(a + b*sqrt(b)), return the denested expression or None. Algorithm: If r = ra + rb*sqrt(rr), try replacing sqrt(rr) in ``a`` with (y**2 - ra)/rb, and if the result is a quadratic, ca*y**2 + cb*y + cc, and (cb + b)**2 - 4*ca*cc is 0, then sqrt(a + b*sqrt(r)) can be rewritten as sqrt(ca*(sqrt(r) + (cb + b)/(2*ca))**2). Examples ======== >>> from diofant.simplify.sqrtdenest import _sqrt_symbolic_denest, sqrtdenest >>> from diofant import sqrt, Symbol >>> from diofant.abc import x >>> a, b, r = 16 - 2*sqrt(29), 2, -10*sqrt(29) + 55 >>> _sqrt_symbolic_denest(a, b, r) sqrt(-2*sqrt(29) + 11) + sqrt(5) If the expression is numeric, it will be simplified: >>> w = sqrt(sqrt(sqrt(3) + 1) + 1) + 1 + sqrt(2) >>> sqrtdenest(sqrt((w**2).expand())) 1 + sqrt(2) + sqrt(1 + sqrt(1 + sqrt(3))) Otherwise, it will only be simplified if assumptions allow: >>> w = w.subs(sqrt(3), sqrt(x + 3)) >>> sqrtdenest(sqrt((w**2).expand())) sqrt((sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2))**2) Notice that the argument of the sqrt is a square. If x is made positive then the sqrt of the square is resolved: >>> _.subs(x, Symbol('x', positive=True)) sqrt(sqrt(sqrt(x + 3) + 1) + 1) + 1 + sqrt(2) """ a, b, r = map(sympify, (a, b, r)) rval = _sqrt_match(r) if not rval: return ra, rb, rr = rval if rb: y = Dummy('y', positive=True) try: newa = Poly(a.subs(sqrt(rr), (y**2 - ra) / rb), y) except PolynomialError: return if newa.degree() == 2: ca, cb, cc = newa.all_coeffs() cb += b if _mexpand(cb**2 - 4 * ca * cc).equals(0): z = sqrt(ca * (sqrt(r) + cb / (2 * ca))**2) if z.is_number: z = _mexpand(Mul._from_args(z.as_content_primitive())) return z
def test_solve_lin_sys_2x2_2(): domain = QQ.algebraic_field(sqrt(2)) coeff_ring, A0, A1 = ring('A:2', domain) R, x = ring('x', coeff_ring) expr = (A1 - sqrt(2))*R.one sol = solve_lin_sys(expr.coeffs(), coeff_ring) assert all(isinstance(s, coeff_ring.dtype) for s in sol.values()) assert sol == {A1: coeff_ring.convert(sqrt(2))}
def _expr_big(cls, a, z, n): if n.is_even: return ((sqrt(z) + 1)**(2 * a) * exp(2 * pi * I * n * a) + (sqrt(z) - 1)**(2 * a) * exp(2 * pi * I * (n - 1) * a)) / 2 else: n -= 1 return ((sqrt(z) - 1)**(2 * a) * exp(2 * pi * I * a * (n + 1)) + (sqrt(z) + 1)**(2 * a) * exp(2 * pi * I * a * n)) / 2
def _ans(y): w = sqrt(e + 2*y) arg1 = 3*e + 2*y arg2 = 2*f/w ans = [] for s in [-1, 1]: root = sqrt(-(arg1 + s*arg2)) for t in [-1, 1]: ans.append((s*w - t*root)/2 - aon4) return ans
def test_1_over_x_and_sqrt(): # 1.0 and 0.5 would do something different in regular StrPrinter, # but these are exact in IEEE floating point so no different here. assert mcode(1 / x) == '1./x' assert mcode(x**-1) == mcode(x**-1.0) == '1./x' assert mcode(1 / sqrt(x)) == '1./sqrt(x)' assert mcode(x**Rational(-1, 2)) == mcode(x**-0.5) == '1./sqrt(x)' assert mcode(sqrt(x)) == mcode(x**0.5) == 'sqrt(x)' assert mcode(1 / pi) == '1/pi' assert mcode(pi**-1) == mcode(pi**-1.0) == '1/pi' assert mcode(pi**-0.5) == '1/sqrt(pi)'
def _roots_quartic_euler(p, q, r, a): """ Descartes-Euler solution of the quartic equation Parameters ========== p, q, r: coefficients of ``x**4 + p*x**2 + q*x + r`` a: shift of the roots Notes ===== This is a helper function for ``roots_quartic``. Look for solutions of the form :: ``x1 = sqrt(R) - sqrt(A + B*sqrt(R))`` ``x2 = -sqrt(R) - sqrt(A - B*sqrt(R))`` ``x3 = -sqrt(R) + sqrt(A - B*sqrt(R))`` ``x4 = sqrt(R) + sqrt(A + B*sqrt(R))`` To satisfy the quartic equation one must have ``p = -2*(R + A); q = -4*B*R; r = (R - A)**2 - B**2*R`` so that ``R`` must satisfy the Descartes-Euler resolvent equation ``64*R**3 + 32*p*R**2 + (4*p**2 - 16*r)*R - q**2 = 0`` If the resolvent does not have a rational solution, return None; in that case it is likely that the Ferrari method gives a simpler solution. Examples ======== >>> from diofant import Rational, Integer >>> p, q, r = -Rational(64, 5), -Rational(512, 125), -Rational(1024, 3125) >>> _roots_quartic_euler(p, q, r, Integer(0))[0] -sqrt(32*sqrt(5)/125 + 16/5) + 4*sqrt(5)/5 """ # solve the resolvent equation x = Symbol('x') eq = 64*x**3 + 32*p*x**2 + (4*p**2 - 16*r)*x - q**2 xsols = list(roots(Poly(eq, x), cubics=False).keys()) xsols = [sol for sol in xsols if sol.is_rational] if not xsols: return R = max(xsols) c1 = sqrt(R) B = -q*c1/(4*R) A = -R - p/2 c2 = sqrt(A + B) c3 = sqrt(A - B) return [c1 - c2 - a, -c1 - c3 - a, -c1 + c3 - a, c1 + c2 - a]
def test_1_over_x_and_sqrt(): # 1.0 and 0.5 would do something different in regular StrPrinter, # but these are exact in IEEE floating point so no different here. assert mcode(1/x) == '1./x' assert mcode(x**-1) == mcode(x**-1.0) == '1./x' assert mcode(1/sqrt(x)) == '1./sqrt(x)' assert mcode(x**Rational(-1, 2)) == mcode(x**-0.5) == '1./sqrt(x)' assert mcode(sqrt(x)) == mcode(x**0.5) == 'sqrt(x)' assert mcode(1/pi) == '1/pi' assert mcode(pi**-1) == mcode(pi**-1.0) == '1/pi' assert mcode(pi**-0.5) == '1/sqrt(pi)'
def test_dup_count_complex_roots_9(): R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = -x**3 + sqrt(2) * x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1 R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I)) f = -x**3 + I * x**2 + sqrt(2) * x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1
def test_dup_count_complex_roots_9(): R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = -x**3 + sqrt(2)*x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1 R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I)) f = -x**3 + I*x**2 + sqrt(2)*x - 1 assert R.dup_count_complex_roots(f, a, b) == 2 assert R.dup_count_complex_roots(f, c, d) == 1
def test_catalan(): n = Symbol('n', integer=True) m = Symbol('n', integer=True, positive=True) catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786] for i, c in enumerate(catalans): assert catalan(i) == c assert catalan(n).rewrite(factorial).subs({n: i}) == c assert catalan(n).rewrite(Product).subs({n: i}).doit() == c assert catalan(x) == catalan(x) assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1) assert catalan(Rational(1, 2)).rewrite(gamma) == 8/(3*pi) assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\ 8 / (3 * pi) assert catalan(3*x).rewrite(gamma) == 4**( 3*x)*gamma(3*x + Rational(1, 2))/(sqrt(pi)*gamma(3*x + 2)) assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1) assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1) * factorial(n)) assert isinstance(catalan(n).rewrite(Product), catalan) assert isinstance(catalan(m).rewrite(Product), Product) assert diff(catalan(x), x) == (polygamma( 0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4))*catalan(x) assert catalan(x).evalf() == catalan(x) c = catalan(Rational(1, 2)).evalf() assert str(c) == '0.848826363156775' c = catalan(I).evalf(3) assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
def _sqrt(d): # remove squares from square root since both will be represented # in the results; a similar thing is happening in roots() but # must be duplicated here because not all quadratics are binomials co = [] other = [] for di in Mul.make_args(d): if di.is_Pow and di.exp.is_Integer and di.exp % 2 == 0: co.append(Pow(di.base, di.exp//2)) else: other.append(di) if co: d = Mul(*other) co = Mul(*co) return co*sqrt(d) return sqrt(d)
def test_catalan(): n = Symbol('n', integer=True) m = Symbol('n', integer=True, positive=True) catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786] for i, c in enumerate(catalans): assert catalan(i) == c assert catalan(n).rewrite(factorial).subs(n, i) == c assert catalan(n).rewrite(Product).subs(n, i).doit() == c assert catalan(x) == catalan(x) assert catalan(2 * x).rewrite(binomial) == binomial(4 * x, 2 * x) / (2 * x + 1) assert catalan(Rational(1, 2)).rewrite(gamma) == 8 / (3 * pi) assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\ 8 / (3 * pi) assert catalan(3 * x).rewrite(gamma) == 4**( 3 * x) * gamma(3 * x + Rational(1, 2)) / (sqrt(pi) * gamma(3 * x + 2)) assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2, ), 1) assert catalan(n).rewrite(factorial) == factorial( 2 * n) / (factorial(n + 1) * factorial(n)) assert isinstance(catalan(n).rewrite(Product), catalan) assert isinstance(catalan(m).rewrite(Product), Product) assert diff(catalan(x), x) == (polygamma(0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4)) * catalan(x) assert catalan(x).evalf() == catalan(x) c = catalan(S.Half).evalf() assert str(c) == '0.848826363156775' c = catalan(I).evalf(3) assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
def _expr_big(cls, a, x, n): sgn = -1 if n.is_odd: sgn = 1 n -= 1 return 2**(2*a - 1)*(1 + sgn*I*sqrt(x - 1))**(1 - 2*a) \ * exp(-2*n*pi*I*a)
def _sqrt_numeric_denest(a, b, r, d2): """Helper that denest expr = a + b*sqrt(r), with d2 = a**2 - b**2*r > 0 or returns None if not denested. """ from diofant.simplify.simplify import radsimp depthr = sqrt_depth(r) d = sqrt(d2) vad = a + d # sqrt_depth(res) <= sqrt_depth(vad) + 1 # sqrt_depth(expr) = depthr + 2 # there is denesting if sqrt_depth(vad)+1 < depthr + 2 # if vad**2 is Number there is a fourth root if sqrt_depth(vad) < depthr + 1 or (vad**2).is_Rational: vad1 = radsimp(1 / vad) return (sqrt(vad / 2) + sign(b) * sqrt( (b**2 * r * vad1 / 2).expand())).expand()
def _sqrtdenest1(expr, denester=True): """Return denested expr after denesting with simpler methods or, that failing, using the denester.""" from diofant.simplify.simplify import radsimp if not is_sqrt(expr): return expr a = expr.base if a.is_Atom: return expr val = _sqrt_match(a) if not val: return expr a, b, r = val # try a quick numeric denesting d2 = _mexpand(a**2 - b**2 * r) if d2.is_Rational: if d2.is_positive: z = _sqrt_numeric_denest(a, b, r, d2) if z is not None: return z else: # fourth root case # sqrtdenest(sqrt(3 + 2*sqrt(3))) = # sqrt(2)*3**(1/4)/2 + sqrt(2)*3**(3/4)/2 dr2 = _mexpand(-d2 * r) dr = sqrt(dr2) if dr.is_Rational: z = _sqrt_numeric_denest(_mexpand(b * r), a, r, dr2) if z is not None: return z / root(r, 4) else: z = _sqrt_symbolic_denest(a, b, r) if z is not None: return z if not denester or not is_algebraic(expr): return expr res = sqrt_biquadratic_denest(expr, a, b, r, d2) if res: return res # now call to the denester av0 = [a, b, r, d2] z = _denester([radsimp(expr**2)], av0, 0, sqrt_depth(expr))[0] if av0[1] is None: return expr if z is not None: if sqrt_depth(z) == sqrt_depth( expr) and count_ops(z) > count_ops(expr): return expr return z return expr
def test_nocache(clear_imports, monkeypatch): """Regression tests with DIOFANT_USE_CACHE=False. """ monkeypatch.setenv('DIOFANT_USE_CACHE', 'False') from diofant.core.cache import CACHE from diofant.core.symbol import Symbol from diofant.functions import sin, sqrt, exp, sinh # test that we don't use cache assert CACHE == [] x = Symbol('x') assert CACHE == [] # issue sympy/sympy#8840 (1 + x)*x # not raises # issue sympy/sympy#9413 (2*x).is_complex # not raises # see commit c459d18 sin(x + x) # not raises # see commit 53dd1eb mx = -Symbol('x', negative=False) assert mx.is_positive is not True px = 2*Symbol('x', positive=False) assert px.is_positive is not True # see commit 2eaaba2 s = 1/sqrt(x**2) y = Symbol('y') result = s.subs(sqrt(x**2), y) assert result == 1/y # problem from https://groups.google.com/forum/#!topic/sympy/LkTMQKC_BOw # see commit c459d18 a = Symbol('a', positive=True) f = exp(x*(-a - 1)) g = sinh(x) f*g # not raises
def test_nocache(clear_imports, monkeypatch): """Regression tests with DIOFANT_USE_CACHE=False. """ monkeypatch.setenv('DIOFANT_USE_CACHE', 'False') from diofant.core.cache import CACHE from diofant.core.symbol import Symbol from diofant.functions import sin, sqrt, exp, sinh # test that we don't use cache assert CACHE == [] x = Symbol('x') assert CACHE == [] # issue sympy/sympy#8840 (1 + x)*x # not raises # issue sympy/sympy#9413 (2*x).is_complex # not raises # see commit c459d18 sin(x + x) # not raises # see commit 53dd1eb mx = -Symbol('x', negative=False) assert mx.is_positive is not True px = 2*Symbol('x', positive=False) assert px.is_positive is not True # see commit 2eaaba2 s = 1/sqrt(x**2) y = Symbol('y') result = s.subs({sqrt(x**2): y}) assert result == 1/y # problem from https://groups.google.com/forum/#!topic/sympy/LkTMQKC_BOw # see commit c459d18 a = Symbol('a', positive=True) f = exp(x*(-a - 1)) g = sinh(x) f*g # not raises
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs({x: -8, n: 3}) == -2 assert g.subs({x: 8, n: 3}) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs({x: I, n: 3}) == cbrt(I) assert g.subs({x: -8, n: 2}) == sqrt(-8) assert g.subs({x: I, n: 2}) == sqrt(I)
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs({x: -8, n: 3}) == -2 assert g.subs({x: 8, n: 3}) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs({x: I, n: 3}) == cbrt(I) assert g.subs({x: -8, n: 2}) == sqrt(-8) assert g.subs({x: I, n: 2}) == sqrt(I)
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2 * x assert fibonacci(x).rewrite(sqrt) == ( S.GoldenRatio**x - cos(S.Pi * x) / S.GoldenRatio**x) / sqrt(5) assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt)
def test_MatPow(): A = MatrixSymbol('A', n, n) AA = MatPow(A, 2) assert AA.exp == 2 assert AA.base == A assert (A**n).exp == n assert A**0 == Identity(n) assert A**1 == A assert A**2 == AA assert A**-1 == Inverse(A) assert A**Rational(1, 2) == sqrt(A) pytest.raises(ShapeError, lambda: MatrixSymbol('B', 3, 2)**2)
def test_MatPow(): A = MatrixSymbol('A', n, n) AA = MatPow(A, 2) assert AA.exp == 2 assert AA.base == A assert (A**n).exp == n assert A**0 == Identity(n) assert A**1 == A assert A**2 == AA assert A**-1 == Inverse(A) assert A**Rational(1, 2) == sqrt(A) pytest.raises(ShapeError, lambda: MatrixSymbol('B', 3, 2)**2)
def line_integrate(field, curve, vars): """line_integrate(field, Curve, variables) Compute the line integral. Examples ======== >>> from diofant import Curve, line_integrate, E, ln >>> from diofant.abc import x, y, t >>> C = Curve([E**t + 1, E**t - 1], (t, 0, ln(2))) >>> line_integrate(x + y, C, [x, y]) 3*sqrt(2) See Also ======== diofant.integrals.integrals.integrate diofant.integrals.integrals.Integral """ from diofant.geometry import Curve F = sympify(field) if not F: raise ValueError( "Expecting function specifying field as first argument.") if not isinstance(curve, Curve): raise ValueError("Expecting Curve entity as second argument.") if not is_sequence(vars): raise ValueError("Expecting ordered iterable for variables.") if len(curve.functions) != len(vars): raise ValueError("Field variable size does not match curve dimension.") if curve.parameter in vars: raise ValueError("Curve parameter clashes with field parameters.") # Calculate derivatives for line parameter functions # F(r) -> F(r(t)) and finally F(r(t)*r'(t)) Ft = F dldt = 0 for i, var in enumerate(vars): _f = curve.functions[i] _dn = diff(_f, curve.parameter) # ...arc length dldt = dldt + (_dn * _dn) Ft = Ft.subs(var, _f) Ft = Ft * sqrt(dldt) integral = Integral(Ft, curve.limits).doit(deep=False) return integral
def _expr_big(cls, a, z, n): if n.is_even: return sqrt(z) / 2 * ( (sqrt(z) - 1)**(2 * a) * exp(2 * pi * I * a * (n - 1)) - (sqrt(z) + 1)**(2 * a) * exp(2 * pi * I * a * n)) else: n -= 1 return sqrt(z) / 2 * ( (sqrt(z) - 1)**(2 * a) * exp(2 * pi * I * a * (n + 1)) - (sqrt(z) + 1)**(2 * a) * exp(2 * pi * I * a * n))
def test_pickling_polys_domains(): # TODO: fix pickling of ModularInteger # for c in (PythonFiniteField, PythonFiniteField(17)): # check(c) for c in (PythonIntegerRing, PythonIntegerRing()): check(c) for c in (PythonRationalField, PythonRationalField()): check(c) if HAS_GMPY: from diofant.polys.domains.gmpyintegerring import GMPYIntegerRing from diofant.polys.domains.gmpyrationalfield import GMPYRationalField # TODO: fix pickling of ModularInteger # for c in (GMPYFiniteField, GMPYFiniteField(17)): # check(c) for c in (GMPYIntegerRing, GMPYIntegerRing()): check(c) for c in (GMPYRationalField, GMPYRationalField()): check(c) # TODO: fix pickling of RealElement # for c in (RealField, RealField(100)): # check(c) # TODO: fix pickling of ComplexElement # for c in (ComplexField, ComplexField(100)): # check(c) for c in (AlgebraicField, AlgebraicField(QQ, sqrt(3))): check(c) # TODO: AssertionError # for c in (PolynomialRing, PolynomialRing(ZZ, "x,y,z")): # check(c) # TODO: AttributeError: 'PolyElement' object has no attribute 'ring' # for c in (FractionField, FractionField(ZZ, "x,y,z")): # check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c)
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert lucas(x) == lucas(x, evaluate=False) assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2*x assert fibonacci(x).rewrite(sqrt) == (GoldenRatio**x - cos(pi*x)/GoldenRatio**x)/sqrt(5) assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt) pytest.raises(ValueError, lambda: fibonacci(-2, x)) n = Symbol('n', integer=True) assert isinstance(fibonacci(n, x).rewrite(sqrt), fibonacci)
def test_fibonacci(): assert [fibonacci(n) for n in range(-3, 5)] == [2, -1, 1, 0, 1, 1, 2, 3] assert fibonacci(100) == 354224848179261915075 assert [lucas(n) for n in range(-3, 5)] == [-4, 3, -1, 2, 1, 3, 4, 7] assert lucas(100) == 792070839848372253127 assert fibonacci(1, x) == 1 assert fibonacci(2, x) == x assert fibonacci(3, x) == x**2 + 1 assert fibonacci(4, x) == x**3 + 2 * x assert fibonacci(x).rewrite(sqrt) == ( S.GoldenRatio**x - cos(S.Pi * x) / S.GoldenRatio**x) / sqrt(5) assert fibonacci(x).rewrite('tractable') == fibonacci(x).rewrite(sqrt) pytest.raises(ValueError, lambda: fibonacci(-2, x)) n = Symbol('n', integer=True) assert fibonacci(n, x).rewrite(sqrt).func is fibonacci
def rad_rationalize(num, den): """ Rationalize num/den by removing square roots in the denominator; num and den are sum of terms whose squares are rationals Examples ======== >>> from diofant import sqrt >>> from diofant.simplify.radsimp import rad_rationalize >>> rad_rationalize(sqrt(3), 1 + sqrt(2)/3) (-sqrt(3) + sqrt(6)/3, -7/9) """ if not den.is_Add: return num, den g, a, b = split_surds(den) a = a * sqrt(g) num = _mexpand((a - b) * num) den = _mexpand(a**2 - b**2) return rad_rationalize(num, den)
def split_surds(expr): """ split an expression with terms whose squares are rationals into a sum of terms whose surds squared have gcd equal to g and a sum of terms with surds squared prime with g Examples ======== >>> from diofant import sqrt >>> from diofant.simplify.radsimp import split_surds >>> split_surds(3*sqrt(3) + sqrt(5)/7 + sqrt(6) + sqrt(10) + sqrt(15)) (3, sqrt(2) + sqrt(5) + 3, sqrt(5)/7 + sqrt(10)) """ args = sorted(expr.args, key=default_sort_key) coeff_muls = [x.as_coeff_Mul() for x in args] surds = [x[1]**2 for x in coeff_muls if x[1].is_Pow] surds.sort(key=default_sort_key) g, b1, b2 = _split_gcd(*surds) g2 = g if not b2 and len(b1) >= 2: b1n = [x / g for x in b1] b1n = [x for x in b1n if x != 1] # only a common factor has been factored; split again g1, b1n, b2 = _split_gcd(*b1n) g2 = g * g1 a1v, a2v = [], [] for c, s in coeff_muls: if s.is_Pow and s.exp == S.Half: s1 = s.base if s1 in b1: a1v.append(c * sqrt(s1 / g2)) else: a2v.append(c * s) else: a2v.append(c * s) a = Add(*a1v) b = Add(*a2v) return g2, a, b
def test_Matrix_printing(): # Test returning a Matrix mat = Matrix([x*y, Piecewise((2 + x, y > 0), (y, True)), sin(z)]) A = MatrixSymbol('A', 3, 1) assert ccode(mat, A) == ( "A[0] = x*y;\n" "if (y > 0) {\n" " A[1] = x + 2;\n" "}\n" "else {\n" " A[1] = y;\n" "}\n" "A[2] = sin(z);") # Test using MatrixElements in expressions expr = Piecewise((2*A[2, 0], x > 0), (A[2, 0], True)) + sin(A[1, 0]) + A[0, 0] assert ccode(expr) == ( "((x > 0) ? (\n" " 2*A[2]\n" ")\n" ": (\n" " A[2]\n" ")) + sin(A[1]) + A[0]") # Test using MatrixElements in a Matrix q = MatrixSymbol('q', 5, 1) M = MatrixSymbol('M', 3, 3) m = Matrix([[sin(q[1, 0]), 0, cos(q[2, 0])], [q[1, 0] + q[2, 0], q[3, 0], 5], [2*q[4, 0]/q[1, 0], sqrt(q[0, 0]) + 4, 0]]) assert ccode(m, M) == ( "M[0] = sin(q[1]);\n" "M[1] = 0;\n" "M[2] = cos(q[2]);\n" "M[3] = q[1] + q[2];\n" "M[4] = q[3];\n" "M[5] = 5;\n" "M[6] = 2*q[4]*1.0/q[1];\n" "M[7] = 4 + sqrt(q[0]);\n" "M[8] = 0;")
def test_dmp_gcd(): R, x = ring("x", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2*x + 4 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1) f, g = 2*x + 4, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1) f, g = x**2 + 2*x + 1, 1 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1) f, g = x**2 + 2*x + 1, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) f, g = 2*x**2 + 4*x + 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1) f, g = 2, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1) f, g = 2*x**2 + 4*x + 2, x + 1 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1) f, g = x + 1, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2) f, g = x - 31, x assert R.dmp_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g) f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8 g = x**3 + 6*x**2 + 11*x + 6 h = x**2 + 3*x + 2 cff = x**2 + 5*x + 4 cfg = x + 3 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) f = x**4 - 4 g = x**4 + 4*x**2 + 4 h = x**2 + 2 cff = x**2 - 2 cfg = x**2 + 2 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 h = 1 cff = f cfg = g assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg) R, x = ring("x", QQ) f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5 g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21 h = 1 cff = f cfg = g assert R.dmp_qq_heu_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg) assert R.dup_ff_prs_gcd(R.zero, R.zero) == ([], [], []) R, x = ring("x", ZZ) f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \ + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \ + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \ + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \ - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \ + 289127344604779611146960547954288113529690984687482920704*x**14 \ + 19007977035740498977629742919480623972236450681*x**7 \ + 311973482284542371301330321821976049 g = 365431878023781158602430064717380211405897160759702125019136*x**21 \ + 197599133478719444145775798221171663643171734081650688*x**14 \ - 9504116979659010018253915765478924103928886144*x**7 \ - 311973482284542371301330321821976049 assert R.dmp_zz_heu_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g assert R.dup_rr_prs_gcd(f, R.dmp_diff_in(f, 1, 0))[0] == g R, x = ring("x", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) h = x + 1 assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) R, x = ring("x", ZZ) f = 1317378933230047068160*x + 2945748836994210856960 g = 120352542776360960*x + 269116466014453760 h = 120352542776360960*x + 269116466014453760 cff = 10946 cfg = 1 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) R, x = ring("x", CC) f, g = (x**2 - 1, x**3 - 3*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) R, x, y = ring("x,y", CC) f, g = (x**2 - y, x**3 - y*x + 2) assert R.dmp_inner_gcd(f, g) == (1, f, g) R, x, y = ring("x,y", ZZ) f, g = 0, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0) f, g = 2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0) f, g = -2, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0) f, g = 0, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1) f, g = 0, 2*x + 4 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1) f, g = 2*x + 4, 0 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0) f, g = 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1) f, g = -2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1) f, g = 2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1) f, g = -2, -2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1) f, g = x**2 + 2*x + 1, 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1) f, g = x**2 + 2*x + 1, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) with using(use_simplify_gcd=0): assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2) f, g = 2*x**2 + 4*x + 2, 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1) f, g = 2, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1) f, g = 2*x**2 + 4*x + 2, x + 1 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1) f, g = x + 1, 2*x**2 + 4*x + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2) with using(heu_gcd_max=0): pytest.raises(HeuristicGCDFailed, lambda: R.dmp_zz_heu_gcd(f, g)) f = x**2 + 2*x*y + y**2 g = x**2 + x*y assert R.dmp_rr_prs_gcd(f, g) == (x + y, x + y, x) R, x, y, z, u = ring("x,y,z,u", ZZ) f, g = u**2 + 2*u + 1, 2*u + 2 assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2) f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1 h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1 assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg) assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg) assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff) assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff) R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z = ring("x,y,z", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ)) H, cff, cfg = R.dmp_zz_heu_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g H, cff, cfg = R.dmp_rr_prs_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y, z, u, v = ring("x,y,z,u,v", ZZ) f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ)) H, cff, cfg = R.dmp_inner_gcd(f, g) assert H == h and R.dmp_mul(H, cff) == f \ and R.dmp_mul(H, cfg) == g R, x, y = ring("x,y", QQ) f = x**2/2 + x + QQ(1, 2) g = x/2 + QQ(1, 2) h = x + 1 assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) with using(use_simplify_gcd=0): assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1, 2)) assert R.dmp_ff_prs_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_qq_heu_gcd(R.zero, R.zero) == (0, 0, 0) assert R.dmp_ff_prs_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) assert R.dmp_qq_heu_gcd(R.zero, g) == (x + 1, R.zero, QQ(1, 2)) R, x, y = ring("x,y", RR) f = 2.1*x*y**2 - 2.2*x*y + 2.1*x g = 1.0*x**3 assert R.dmp_ff_prs_gcd(f, g) == \ (1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2) R, x, y = ring("x,y", ZZ) f = (-17434367009167300000000000000000000000000000000000000000000000000000000*x**4*y - 250501827896299135568887342575961783764139560000000000000000000000000000000000000000000*x**3*y - 2440935909299672540738135183426056447877858000000000000000000000000000000*x**3 - 1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000*x**2*y - 26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000*x**2 - 3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000*x*y - 94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800*x - 2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993*y - 113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290) g = (10000000000000000000000000000*x**2 + 71841388839807267676152024786000000000000000*x + 129029628760809605749020969023932901278290735413660734705971) assert (R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (g, -1743436700916730000000000000000000000000000*x**2*y - 12525091394814956778444367128798089188206978000000000000000*x*y - 244093590929967254073813518342605644787785800*x - 22495499028725631994927113634418779135935898997901327211111875586270479483*y - 876801128965234839118530545935732755107147297241756982389990, 1)) R, x = ring("x", ZZ) f, g = x**2 - 1, x**2 - 3*x + 2 assert R.dmp_gcd(f, g) == x - 1 with using(use_heu_gcd=False, fallback_gcd_zz_method='modgcd'): R.dmp_gcd(f, g) == x - 1 R, x = ring("x", QQ) f, g = x**2/2 + x + QQ(1, 2), x/2 + QQ(1, 2) assert R.dmp_gcd(f, g) == x + 1 with using(use_heu_gcd=False): R.dmp_gcd(f, g) == x + 1 R, x, y = ring("x,y", QQ.algebraic_field(sqrt(2))) f, g = (x + sqrt(2)*y)**2, x + sqrt(2)*y assert R.dmp_gcd(f, g) == g with using(gcd_aa_method='modgcd'): assert R.dmp_gcd(f, g) == g
def test_minimize_poly(): assert minimize([x - 2*y, x**2 + y**2 <= 1], x, y)[0] == -sqrt(5)
def test_point(): p = R2_r.point([x, y]) # TODO assert p.free_symbols() == {x, y} assert p.coords(R2_r) == p.coords() == Matrix([x, y]) assert p.coords(R2_p) == Matrix([sqrt(x**2 + y**2), atan2(y, x)])
def test_harmonic_rational(): ne = Integer(6) no = Integer(5) pe = Integer(8) po = Integer(9) qe = Integer(10) qo = Integer(13) Heee = harmonic(ne + pe/qe) Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8))) + Rational(13944145, 4720968)) Heeo = harmonic(ne + pe/qo) Aeeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(2422020029, 702257080)) Heoe = harmonic(ne + po/qe) Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4) + Rational(11818877030, 4286604231) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8))) Heoo = harmonic(ne + po/qo) Aeoo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(11669332571, 3628714320)) Hoee = harmonic(no + pe/qe) Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8))) + Rational(779405, 277704)) Hoeo = harmonic(no + pe/qo) Aoeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(53857323, 16331560)) Hooe = harmonic(no + po/qe) Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8))) + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4) + Rational(486853480, 186374097) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8))) Hooo = harmonic(no + po/qo) Aooo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(383693479, 125128080)) H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo] A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo] for h, a in zip(H, A): e = expand_func(h).doit() assert cancel(e/a) == 1 assert h.evalf() == a.evalf()
def test_ccode_sqrt(): assert ccode(sqrt(x)) == "sqrt(x)" assert ccode(x**0.5) == "sqrt(x)" assert ccode(sqrt(x)) == "sqrt(x)"
def test_dup_isolate_real_roots_sqf(): R, x = ring("x", ZZ) assert R.dup_isolate_real_roots_sqf(0) == [] assert R.dup_isolate_real_roots_sqf(5) == [] assert R.dup_isolate_real_roots_sqf(x**2 + x) == [(-1, -1), (0, 0)] assert R.dup_isolate_real_roots_sqf(x**2 + x, inf=QQ(+1)) == [] assert R.dup_isolate_real_roots_sqf(x**2 + x, sup=QQ(-1)) == [(-1, -1)] assert R.dup_isolate_real_roots_sqf(x**2 + x, sup=QQ(-2)) == [] assert R.dup_isolate_real_roots_sqf(x**2 - x) == [( 0, 0), (1, 1)] assert R.dup_isolate_real_roots_sqf(x**4 + x + 1) == [] i = [(-2, -1), (1, 2)] assert R.dup_isolate_real_roots_sqf(x**2 - 2) == i assert R.dup_isolate_real_roots_sqf(-x**2 + 2) == i assert R.dup_isolate_real_roots_sqf(x - 1) == \ [(1, 1)] assert R.dup_isolate_real_roots_sqf(x**2 - 3*x + 2) == \ [(1, 1), (2, 2)] assert R.dup_isolate_real_roots_sqf(x**3 - 6*x**2 + 11*x - 6) == \ [(1, 1), (2, 2), (3, 3)] assert R.dup_isolate_real_roots_sqf(x**4 - 10*x**3 + 35*x**2 - 50*x + 24) == \ [(1, 1), (2, 2), (3, 3), (4, 4)] assert R.dup_isolate_real_roots_sqf(x**5 - 15*x**4 + 85*x**3 - 225*x**2 + 274*x - 120) == \ [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)] assert R.dup_isolate_real_roots_sqf(x - 10) == \ [(10, 10)] assert R.dup_isolate_real_roots_sqf(x**2 - 30*x + 200) == \ [(10, 10), (20, 20)] assert R.dup_isolate_real_roots_sqf(x**3 - 60*x**2 + 1100*x - 6000) == \ [(10, 10), (20, 20), (30, 30)] assert R.dup_isolate_real_roots_sqf(x**4 - 100*x**3 + 3500*x**2 - 50000*x + 240000) == \ [(10, 10), (20, 20), (30, 30), (40, 40)] assert R.dup_isolate_real_roots_sqf(x**5 - 150*x**4 + 8500*x**3 - 225000*x**2 + 2740000*x - 12000000) == \ [(10, 10), (20, 20), (30, 30), (40, 40), (50, 50)] assert R.dup_isolate_real_roots_sqf(x + 1) == \ [(-1, -1)] assert R.dup_isolate_real_roots_sqf(x**2 + 3*x + 2) == \ [(-2, -2), (-1, -1)] assert R.dup_isolate_real_roots_sqf(x**3 + 6*x**2 + 11*x + 6) == \ [(-3, -3), (-2, -2), (-1, -1)] assert R.dup_isolate_real_roots_sqf(x**4 + 10*x**3 + 35*x**2 + 50*x + 24) == \ [(-4, -4), (-3, -3), (-2, -2), (-1, -1)] assert R.dup_isolate_real_roots_sqf(x**5 + 15*x**4 + 85*x**3 + 225*x**2 + 274*x + 120) == \ [(-5, -5), (-4, -4), (-3, -3), (-2, -2), (-1, -1)] assert R.dup_isolate_real_roots_sqf(x + 10) == \ [(-10, -10)] assert R.dup_isolate_real_roots_sqf(x**2 + 30*x + 200) == \ [(-20, -20), (-10, -10)] assert R.dup_isolate_real_roots_sqf(x**3 + 60*x**2 + 1100*x + 6000) == \ [(-30, -30), (-20, -20), (-10, -10)] assert R.dup_isolate_real_roots_sqf(x**4 + 100*x**3 + 3500*x**2 + 50000*x + 240000) == \ [(-40, -40), (-30, -30), (-20, -20), (-10, -10)] assert R.dup_isolate_real_roots_sqf(x**5 + 150*x**4 + 8500*x**3 + 225000*x**2 + 2740000*x + 12000000) == \ [(-50, -50), (-40, -40), (-30, -30), (-20, -20), (-10, -10)] assert R.dup_isolate_real_roots_sqf(x**2 - 5) == [(-3, -2), (2, 3)] assert R.dup_isolate_real_roots_sqf(x**3 - 5) == [(1, 2)] assert R.dup_isolate_real_roots_sqf(x**4 - 5) == [(-2, -1), (1, 2)] assert R.dup_isolate_real_roots_sqf(x**5 - 5) == [(1, 2)] assert R.dup_isolate_real_roots_sqf(x**6 - 5) == [(-2, -1), (1, 2)] assert R.dup_isolate_real_roots_sqf(x**7 - 5) == [(1, 2)] assert R.dup_isolate_real_roots_sqf(x**8 - 5) == [(-2, -1), (1, 2)] assert R.dup_isolate_real_roots_sqf(x**9 - 5) == [(1, 2)] assert R.dup_isolate_real_roots_sqf(x**2 - 1) == \ [(-1, -1), (1, 1)] assert R.dup_isolate_real_roots_sqf(x**3 + 2*x**2 - x - 2) == \ [(-2, -2), (-1, -1), (1, 1)] assert R.dup_isolate_real_roots_sqf(x**4 - 5*x**2 + 4) == \ [(-2, -2), (-1, -1), (1, 1), (2, 2)] assert R.dup_isolate_real_roots_sqf(x**5 + 3*x**4 - 5*x**3 - 15*x**2 + 4*x + 12) == \ [(-3, -3), (-2, -2), (-1, -1), (1, 1), (2, 2)] assert R.dup_isolate_real_roots_sqf(x**6 - 14*x**4 + 49*x**2 - 36) == \ [(-3, -3), (-2, -2), (-1, -1), (1, 1), (2, 2), (3, 3)] assert R.dup_isolate_real_roots_sqf(2*x**7 + x**6 - 28*x**5 - 14*x**4 + 98*x**3 + 49*x**2 - 72*x - 36) == \ [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (1, 1), (2, 2), (3, 3)] assert R.dup_isolate_real_roots_sqf(4*x**8 - 57*x**6 + 210*x**4 - 193*x**2 + 36) == \ [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (0, 1), (1, 1), (2, 2), (3, 3)] f = 9*x**2 - 2 assert R.dup_isolate_real_roots_sqf(f) == \ [(-1, 0), (0, 1)] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10)) == \ [(QQ(-1, 2), QQ(-3, 7)), (QQ(3, 7), QQ(1, 2))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \ [(QQ(-9, 19), QQ(-8, 17)), (QQ(8, 17), QQ(9, 19))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == \ [(QQ(-33, 70), QQ(-8, 17)), (QQ(8, 17), QQ(33, 70))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10000)) == \ [(QQ(-33, 70), QQ(-107, 227)), (QQ(107, 227), QQ(33, 70))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \ [(QQ(-305, 647), QQ(-272, 577)), (QQ(272, 577), QQ(305, 647))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000000)) == \ [(QQ(-1121, 2378), QQ(-272, 577)), (QQ(272, 577), QQ(1121, 2378))] f = 200100012*x**5 - 700390052*x**4 + 700490079*x**3 - 200240054*x**2 + 40017*x - 2 assert R.dup_isolate_real_roots_sqf(f) == \ [(0, QQ(1, 10002)), (QQ(1, 10002), QQ(1, 10002)), (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \ [(QQ(1, 10003), QQ(1, 10003)), (QQ(1, 10002), QQ(1, 10002)), (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)] a, b, c, d = 10000090000001, 2000100003, 10000300007, 10000005000008 f = 20001600074001600021*x**4 \ + 1700135866278935491773999857*x**3 \ - 2000179008931031182161141026995283662899200197*x**2 \ - 800027600594323913802305066986600025*x \ + 100000950000540000725000008 assert R.dup_isolate_real_roots_sqf(f) == \ [(-a, -a), (-1, 0), (0, 1), (d, d)] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \ [(-QQ(a), -QQ(a)), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (QQ(d), QQ(d))] (u, v), B, C, (s, t) = R.dup_isolate_real_roots_sqf(f, fast=True) assert u < -a < v and B == (-1, 0) and C == (0, 1) and s < d < t assert R.dup_isolate_real_roots_sqf(f, fast=True, eps=QQ(1, 100000000000000000000000000000)) == \ [(-QQ(a), -QQ(a)), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (QQ(d), QQ(d))] f = -10*x**4 + 8*x**3 + 80*x**2 - 32*x - 160 assert R.dup_isolate_real_roots_sqf(f) == \ [(-2, -2), (-2, -1), (2, 2), (2, 3)] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \ [(-2, -2), (-QQ(23, 14), -QQ(18, 11)), (2, 2), (QQ(39, 16), QQ(22, 9))] f = x - 1 assert R.dup_isolate_real_roots_sqf(f, inf=2) == [] assert R.dup_isolate_real_roots_sqf(f, sup=0) == [] assert R.dup_isolate_real_roots_sqf(f) == [(1, 1)] assert R.dup_isolate_real_roots_sqf(f, inf=1) == [(1, 1)] assert R.dup_isolate_real_roots_sqf(f, sup=1) == [(1, 1)] assert R.dup_isolate_real_roots_sqf(f, inf=1, sup=1) == [(1, 1)] f = x**2 - 2 assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 4)) == [] assert R.dup_isolate_real_roots_sqf(f, inf=QQ(7, 5)) == [(QQ(7, 5), QQ(3, 2))] assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 5)) == [(-2, -1)] assert R.dup_isolate_real_roots_sqf(f, sup=QQ(7, 4)) == [(-2, -1), (1, QQ(3, 2))] assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 4)) == [] assert R.dup_isolate_real_roots_sqf(f, sup=-QQ(7, 5)) == [(-QQ(3, 2), -QQ(7, 5))] assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 5)) == [(1, 2)] assert R.dup_isolate_real_roots_sqf(f, inf=-QQ(7, 4)) == [(-QQ(3, 2), -1), (1, 2)] i = [(-2, -1), (1, 2)] assert R.dup_isolate_real_roots_sqf(f, inf=-2) == i assert R.dup_isolate_real_roots_sqf(f, sup=+2) == i assert R.dup_isolate_real_roots_sqf(f, inf=-2, sup=2) == i assert R.dup_isolate_real_roots_sqf(f, inf=+1) == [(+1, +2)] assert R.dup_isolate_real_roots_sqf(f, sup=-1) == [(-2, -1)] R, x = ring("x", QQ) f = 8*x**2/5 - 87374*x/3855 - QQ(17, 771) assert R.dup_isolate_real_roots_sqf(f) == [(-1, 0), (14, 15)] assert [_.as_tuple() for _ in R.dup_isolate_real_roots_sqf(f, blackbox=True)] == [(-1, 0), (14, 15)] R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dup_isolate_real_roots_sqf(x + 3)) R, x = ring("x", QQ.algebraic_field(I)) f = (x - 1)*(x**3 + I*x - 2) assert R.dup_isolate_real_roots_sqf(f) == [(1, 1)] assert R.dup_isolate_real_roots_sqf(f, sup=0) == [] f = (x**2 - 2)*(x**3 - x + I) assert R.dup_isolate_real_roots_sqf(f) == [(QQ(-3, 2), QQ(-4, 3)), (QQ(4, 3), QQ(3, 2))] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 10), inf=0) == [(QQ(7, 5), QQ(10, 7))] assert R.dup_isolate_real_roots_sqf(x) == [(0, 0)] assert R.dup_isolate_real_roots_sqf(x - 1) == [(1, 1)] assert R.dup_isolate_real_roots_sqf(x - I) == [] R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = (-x**3 + sqrt(2)*x - 1)*(x**2 + 1) assert R.dup_isolate_real_roots_sqf(f) == [(-2, -1)] assert R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [(QQ(-74, 51), QQ(-29, 20))] f = (x - sqrt(2))*(x + 2*sqrt(2))*(x - 7 + sqrt(2))*(x + 3*sqrt(2))*(x - 1)*(x + 1 - sqrt(2)) assert R.dup_isolate_real_roots_sqf(f) == [(-5, -4), (-3, -2), (0, 1), (1, 1), (1, 2), (5, 6)] R, x = ring("x", QQ.algebraic_field(sqrt(2), sqrt(3))) f = (x - sqrt(2))*(x - sqrt(3))*(x - 2*sqrt(6))*(x - sqrt(6))*(x**2 + 2) assert R.dup_isolate_real_roots_sqf(f) == [(1, QQ(3, 2)), (QQ(3, 2), 2), (2, 3), (4, 5)] assert (R.dup_isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [(QQ(41, 29), QQ(58, 41)), (QQ(71, 41), QQ(97, 56)), (QQ(169, 69), QQ(49, 20)), (QQ(436, 89), QQ(485, 99))])
def test_dmp_sqf(): R, x = ring("x", ZZ) assert R(0).sqf_part() == 0 assert R(0).is_squarefree is True assert R(7).sqf_part() == 1 assert R(7).is_squarefree is True assert (2*x + 2).sqf_part() == x + 1 assert (2*x + 2).is_squarefree is True assert (x**3 + x + 1).sqf_part() == x**3 + x + 1 assert (x**3 + x + 1).is_squarefree is True assert (-x**3 + x + 1).sqf_part() == x**3 - x - 1 assert (-x**3 + x + 1).is_squarefree is True assert (2*x**3 + 3*x**2).sqf_part() == 2*x**2 + 3*x assert (2*x**3 + 3*x**2).is_squarefree is False assert (-2*x**3 + 3*x**2).sqf_part() == 2*x**2 - 3*x assert (-2*x**3 + 3*x**2).is_squarefree is False assert (x**3 - 3*x - 2).sqf_part() == x**2 - x - 2 assert (x**3 - 3*x - 2).is_squarefree is False assert R(0).sqf_list() == (0, []) assert R(1).sqf_list() == (1, []) assert x.sqf_list() == (1, [(x, 1)]) assert (2*x**2).sqf_list() == (2, [(x, 2)]) assert (3*x**3).sqf_list() == (3, [(x, 3)]) assert (-x**5 + x**4 + x - 1).sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) assert (x**8 + 6*x**6 + 12*x**4 + 8*x**2).sqf_list() == (1, [(x, 2), (x**2 + 2, 3)]) assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)]) R, x = ring("x", QQ) assert (2*x**2 + 4*x + 2).sqf_list() == (2, [(x + 1, 2)]) R, x = ring("x", FF(2)) assert (x**2 + 1).sqf_list() == (1, [(x + 1, 2)]) R, x = ring("x", FF(3)) assert (x**10 + 2*x**7 + 2*x**4 + x).sqf_list() == (1, [(x, 1), (x + 1, 3), (x + 2, 6)]) R1, x = ring("x", ZZ) R2, y = ring("y", FF(3)) f = x**3 + 1 g = y**3 + 1 assert f.sqf_part() == f assert g.sqf_part() == y + 1 assert f.is_squarefree is True assert g.is_squarefree is False R, x = ring("x", FF(5)) f = x**8 + x**7 + 3*x**6 + x**4 + 2*x**2 + 2*x + 1 assert f.sqf_part() == x**2 + 4*x + 3 f = 3*x**2 + 2*x + 4 assert f.is_squarefree is True f = 2*x**6 + 4*x**5 + 4*x**4 + 2*x**3 + 2*x**2 + x + 4 assert f.is_squarefree is False R, x = ring("x", FF(11)) f = x**3 + 5*x**2 + 8*x + 4 assert f.sqf_part() == x**2 + 3*x + 2 assert R(0).is_squarefree is True assert R(0).sqf_list() == (0, []) assert R(1).is_squarefree is True assert R(1).sqf_list() == (1, []) assert (x + 1).is_squarefree is True assert (x + 1).sqf_list() == (1, [(x + 1, 1)]) f = x**11 + 1 assert f.is_squarefree is False assert f.sqf_list() == (1, [(x + 1, 11)]) f = x**3 + 5*x**2 + 8*x + 4 assert f.is_squarefree is False assert f.sqf_list() == (1, [(x + 1, 1), (x + 2, 2)]) R, x = ring('x', FF(3)) f = x**10 + 2*x**7 + 2*x**4 + x assert f.is_squarefree is False assert f.sqf_list() == (1, [(x, 1), (x + 1, 3), (x + 2, 6)]) R, x = ring('x', FF(53)) f = x**6 + 2*x**5 + 5*x**4 + 26*x**3 + 41*x**2 + 39*x + 38 assert f.is_squarefree is True R, x = ring('x', FF(102953)) f = x**15 + x + 1 assert f.is_squarefree is True R, x, y = ring("x,y", ZZ) A = x**4 - 3*x**2 + 6 D = x**6 - 5*x**4 + 5*x**2 + 4 f, g = D, R.dmp_sub(A, R.dmp_mul(R.dmp_diff_in(D, 1, 0), y)) res = R.dmp_resultant(f, g) h = (4*y**2 + 1).drop(x) assert res.sqf_list() == (45796, [(h, 3)]) pytest.raises(DomainError, lambda: (x**2 - 1).sqf_norm()) K = QQ.algebraic_field(sqrt(3)) R, x = ring("x", K) _, X = ring("x", QQ) assert (x**2 - 2).sqf_norm() == (1, x**2 + K([QQ(-2), QQ(0)])*x + 1, X**4 - 10*X**2 + 1) R, x, y = ring("x,y", ZZ) assert R(0).sqf_part() == 0 assert R(0).is_squarefree is True assert R(7).sqf_part() == 1 assert R(7).is_squarefree is True assert R(3).sqf_list() == (3, []) R, x, y, z = ring("x,y,z", ZZ) assert f_0.is_squarefree is True assert (f_0**2).is_squarefree is False assert f_1.is_squarefree is True assert (f_1**2).is_squarefree is False assert f_2.is_squarefree is True assert (f_2**2).is_squarefree is False assert f_3.is_squarefree is True assert (f_3**2).is_squarefree is False assert f_5.is_squarefree is False assert (f_5**2).is_squarefree is False assert f_4.is_squarefree is True assert f_4.sqf_part() == -f_4 assert f_5.sqf_part() == x + y - z R, x, y, z, t = ring("x,y,z,t", ZZ) assert f_6.is_squarefree is True assert f_6.sqf_part() == f_6 R, x = ring("x", ZZ) f = -x**5 + x**4 + x - 1 assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 assert f.sqf_list() == (2, [(x + 1, 2), (x + 2, 3)]) R, x, y = ring("x,y", ZZ) f = -x**5 + x**4 + x - 1 assert f.sqf_list() == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)]) pytest.raises(DomainError, lambda: (x**2 + y**2).sqf_norm()) R, x, y = ring("x,y", FF(2)) pytest.raises(NotImplementedError, lambda: (y**2 + 1).sqf_list()) pytest.raises(NotImplementedError, lambda: (x**3 + 2*x**2*y + x*y**2).sqf_part()) R, x, y = ring("x,y", QQ.algebraic_field(I)) assert (x**2 + 2*I*x - 1).sqf_list() == (1, [(x + I, 2)])
def test_dup_isolate_complex_roots_sqf(): R, x = ring("x", ZZ) f = x**2 - 2*x + 3 assert R.dup_isolate_complex_roots_sqf(f) == \ [((0, -6), (6, 0)), ((0, 0), (6, 6))] assert [r.as_tuple() for r in R.dup_isolate_complex_roots_sqf(f, blackbox=True)] == \ [((0, -6), (6, 0)), ((0, 0), (6, 6))] assert R.dup_isolate_complex_roots_sqf(f, inf=QQ(1), sup=QQ(3)) == [((1, -3), (3, 0)), ((1, 0), (3, 3))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(1), QQ(0)), sup=QQ(3)) == [((1, 0), (3, 3))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(1), QQ(-1, 2)), sup=QQ(3)) == [((1, 0), (3, 3))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(1), QQ(-3)), sup=(QQ(3), QQ(-1))) == [((1, -2), (3, -1))] assert R.dup_isolate_complex_roots_sqf(f, inf=QQ(0), sup=QQ(1, 6)) == [] assert R.dup_isolate_complex_roots_sqf(R.zero) == [] pytest.raises(ValueError, lambda: R.dup_isolate_complex_roots_sqf(f, inf=QQ(1), sup=QQ(1))) assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 10)) == \ [((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))), ((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))] assert R.dup_isolate_complex_roots_sqf(f, eps=QQ(1, 100)) == \ [((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))), ((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))] f = 7*x**4 - 19*x**3 + 20*x**2 + 17*x + 20 assert R.dup_isolate_complex_roots_sqf(f) == \ [((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))), ((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))] R, x = ring("x", QQ) f = x**2/2 - 3*x/7 + 1 assert R.dup_isolate_complex_roots_sqf(f) == [((0, -4), (4, 0)), ((0, 0), (4, 4))] R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dup_isolate_complex_roots_sqf(x, inf=(QQ(-1), QQ(0)), sup=(QQ(1), QQ(1)))) R, x = ring("x", QQ.algebraic_field(I)) f = x**4 + I*x**3 - x + 1 assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(0), QQ(0)), sup=(QQ(1), QQ(1))) == [((0, 0), (1, QQ(1, 2)))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(0), QQ(0)), sup=(QQ(1), QQ(1)), eps=QQ(1, 100)) == [((QQ(79, 128), QQ(19, 64)), (QQ(5, 8), QQ(39, 128)))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(0), QQ(-1)), sup=(QQ(1), QQ(1))) == [((0, -1), (1, QQ(-1, 2))), ((0, 0), (1, QQ(1, 2)))] assert R.dup_isolate_complex_roots_sqf(f, inf=(QQ(0), QQ(-1)), sup=(QQ(1), QQ(1)), eps=QQ(1, 100)) == [((QQ(79, 128), QQ(19, 64)), (QQ(5, 8), QQ(39, 128))), ((QQ(45, 64), QQ(-91, 128)), (QQ(91, 128), QQ(-45, 64)))] g = (x - 1)*f assert R.dup_isolate_complex_roots_sqf(g) == [((QQ(-401, 100), QQ(-401, 100)), (0, 0)), ((QQ(-401, 100), 0), (0, QQ(401, 100))), ((0, QQ(-401, 100)), (QQ(401, 100), 0)), ((0, 0), (QQ(401, 100), QQ(401, 100)))] f = x**7 + I*x**4 - (2 + I)*x**3 - 3*x + 5 assert R.dup_isolate_complex_roots_sqf(f) == [((QQ(-1001, 100), 0), (0, QQ(1001, 100))), ((QQ(-1001, 400), QQ(-1001, 800)), (QQ(-1001, 800), 0)), ((QQ(-1001, 800), QQ(-1001, 800)), (0, 0)), ((0, QQ(-1001, 400)), (QQ(1001, 400), QQ(-1001, 800))), ((0, QQ(-1001, 800)), (QQ(1001, 400), 0)), ((0, 0), (QQ(1001, 400), QQ(1001, 800))), ((0, QQ(1001, 800)), (QQ(1001, 400), QQ(1001, 400)))] R, x = ring("x", QQ.algebraic_field(sqrt(2))) f = -x**3 + sqrt(2)*x - 1 assert R.dup_isolate_complex_roots_sqf(f) == [((0, QQ(-283, 100)), (QQ(283, 100), 0)), ((0, 0), (QQ(283, 100), QQ(283, 100)))] R, x = ring("x", QQ.algebraic_field(sqrt(2)).algebraic_field(I)) f = -x**3 + I*x**2 + sqrt(2)*x - 1 assert R.dup_isolate_complex_roots_sqf(f) == [((QQ(-283, 100), 0), (0, QQ(283, 100))), ((0, QQ(-283, 100)), (QQ(283, 100), 0)), ((0, 0), (QQ(283, 100), QQ(283, 100)))] R, x = ring("x", EX) pytest.raises(DomainError, lambda: R.dup_isolate_complex_roots_sqf(x))