def __str__(self): # pragma: no cover from sympy.printing.str import sstr if self.dom is None: return "%s does not divide %s" % (sstr(self.g), sstr(self.f)) else: return "%s does not divide %s in %s" % (sstr(self.g), sstr(self.f), sstr(self.dom))
def test_extended_domain_in_expr_to_holonomic(): x = symbols('x') p = expr_to_holonomic(1.2 * cos(3.1 * x)) assert p.to_expr() == 1.2 * cos(3.1 * x) assert sstr(p.integrate(x).to_expr()) == '0.387096774193548*sin(3.1*x)' _, Dx = DifferentialOperators(RR.old_poly_ring(x), 'Dx') p = expr_to_holonomic(1.1329138213 * x) q = HolonomicFunction((-1.1329138213) + (1.1329138213 * x) * Dx, x, 0, {1: [1.1329138213]}) assert p == q assert p.to_expr() == 1.1329138213 * x assert sstr(p.integrate((x, 1, 2))) == sstr((1.1329138213 * x).integrate( (x, 1, 2))) y, z = symbols('y, z') p = expr_to_holonomic(sin(x * y * z), x=x) assert p.to_expr() == sin(x * y * z) assert p.integrate(x).to_expr() == (-cos(x * y * z) + 1) / (y * z) p = expr_to_holonomic(sin(x * y + z), x=x).integrate(x).to_expr() q = (cos(z) - cos(x * y + z)) / y assert p == q a = symbols('a') p = expr_to_holonomic(a * x, x) assert p.to_expr() == a * x assert p.integrate(x).to_expr() == a * x**2 / 2 D_2, C_1 = symbols("D_2, C_1") p = expr_to_holonomic(x) + expr_to_holonomic(1.2 * cos(x)) p = p.to_expr().subs(D_2, 0) assert p - x - 1.2 * cos(1.0 * x) == 0 p = expr_to_holonomic(x) * expr_to_holonomic(1.2 * cos(x)) p = p.to_expr().subs(C_1, 0) assert p - 1.2 * x * cos(1.0 * x) == 0
def test_complex_space(): c1 = ComplexSpace(2) assert isinstance(c1, ComplexSpace) assert c1.dimension == 2 assert sstr(c1) == 'C(2)' assert srepr(c1) == 'ComplexSpace(Integer(2))' n = Symbol('n') c2 = ComplexSpace(n) assert isinstance(c2, ComplexSpace) assert c2.dimension == n assert sstr(c2) == 'C(n)' assert srepr(c2) == "ComplexSpace(Symbol('n'))" assert c2.subs(n, 2) == ComplexSpace(2)
def _print_Float(self, e): # we will use StrPrinter's Float printer, but we need to handle the # full_prec ourselves, according to the self._print_level full_prec = self._settings["full_prec"] if full_prec == "auto": full_prec = self._print_level == 1 return prettyForm(sstr(e, full_prec=full_prec))
def test_integrate(): x = symbols('x') R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx') p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 2, 3)) q = '0.166270406994788' assert sstr(p) == q p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr() q = 1 - cos(x) assert p == q p = expr_to_holonomic(sin(x)).integrate((x, 0, 3)) q = 1 - cos(3) assert p == q p = expr_to_holonomic(sin(x) / x, x0=1).integrate((x, 1, 2)) q = '0.659329913368450' assert sstr(p) == q p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 1, 0)) q = '-0.423690480850035' assert sstr(p) == q p = expr_to_holonomic(sin(x) / x) assert p.integrate(x).to_expr() == Si(x) assert p.integrate((x, 0, 2)) == Si(2) p = expr_to_holonomic(sin(x)**2 / x) q = p.to_expr() assert p.integrate(x).to_expr() == q.integrate((x, 0, x)) assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1)) assert expr_to_holonomic(1 / x, x0=1).integrate(x).to_expr() == log(x) p = expr_to_holonomic((x + 1)**3 * exp(-x), x0=-1).integrate(x).to_expr() q = (-x**3 - 6 * x**2 - 15 * x + 6 * exp(x + 1) - 16) * exp(-x) assert p == q p = expr_to_holonomic(cos(x)**2 / x**2, y0={ -2: [1, 0, -1] }).integrate(x).to_expr() q = -Si(2 * x) - cos(x)**2 / x assert p == q p = expr_to_holonomic(sqrt(x**2 + x)).integrate(x).to_expr() q = (x**Rational(3, 2) * (2 * x**2 + 3 * x + 1) - x * sqrt(x + 1) * asinh(sqrt(x))) / (4 * x * sqrt(x + 1)) assert p == q p = expr_to_holonomic(sqrt(x**2 + 1)).integrate(x).to_expr() q = (sqrt(x**2 + 1)).integrate(x) assert (p - q).simplify() == 0 p = expr_to_holonomic(1 / x**2, y0={-2: [1, 0, 0]}) r = expr_to_holonomic(1 / x**2, lenics=3) assert p == r q = expr_to_holonomic(cos(x)**2) assert (r * q).integrate(x).to_expr() == -Si(2 * x) - cos(x)**2 / x
def __call__(self, a): """Construct an element of `self` domain from `a`. """ res = self.dtype(a, self.dom, len(self.gens) - 1, ring=self) # make sure res is actually in our ring if res.denom().terms(order=self.order)[0][0] != (0,)*len(self.gens): from sympy.printing.str import sstr raise CoercionFailed("denominator %s not allowed in %s" % (sstr(res), self)) return res
def new(self, a): """Construct an element of `self` domain from `a`. """ res = self.dtype(a, self.dom, len(self.gens) - 1, ring=self) # make sure res is actually in our ring if res.denom().terms(order=self.order)[0][0] != (0,)*len(self.gens): from sympy.printing.str import sstr raise CoercionFailed("denominator %s not allowed in %s" % (sstr(res), self)) return res
def _sympystr(self, printer, *args): from sympy.printing.str import sstr length = len(self.args) s = '' for i in range(length): if isinstance(self.args[i], (Add, Pow, Mul)): s = s + '(' s = s + sstr(self.args[i]) if isinstance(self.args[i], (Add, Pow, Mul)): s = s + ')' if i != length-1: s = s + 'x' return s
def _sympystr(self, printer, *args): from sympy.printing.str import sstr length = len(self.args) s = '' for i in range(length): if isinstance(self.args[i], (Add, Pow, Mul)): s = s + '(' s = s + sstr(self.args[i]) if isinstance(self.args[i], (Add, Pow, Mul)): s = s + ')' if i != length - 1: s = s + 'x' return s
def test_from_hyper(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') p = hyper([1, 1], [Rational(3, 2)], x**2 / 4) q = HolonomicFunction( (4 * x) + (5 * x**2 - 8) * Dx + (x**3 - 4 * x) * Dx**2, x, 1, [2 * sqrt(3) * pi / 9, -4 * sqrt(3) * pi / 27 + Rational(4, 3)]) r = from_hyper(p) assert r == q p = from_hyper(hyper([1], [Rational(3, 2)], x**2 / 4)) q = HolonomicFunction(-x + (-x**2 / 2 + 2) * Dx + x * Dx**2, x) # x0 = 1 y0 = '[sqrt(pi)*exp(1/4)*erf(1/2), -sqrt(pi)*exp(1/4)*erf(1/2)/2 + 1]' assert sstr(p.y0) == y0 assert q.annihilator == p.annihilator
def test_evalf_rk4(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') # log(1+x) p = HolonomicFunction((1 + x) * Dx**2 + Dx, x, 0, [0, 1]) # path taken is a straight line from 0 to 1, on the real axis r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] s = '0.693146363174626' # approx. equal to log(2) i.e. 0.693147180559945 assert sstr(p.evalf(r)[-1]) == s # path taken is a traingle 0-->1+i-->2 r = [0.1 + 0.1 * I] for i in range(9): r.append(r[-1] + 0.1 + 0.1 * I) for i in range(10): r.append(r[-1] + 0.1 - 0.1 * I) # close to the exact solution 1.09861228866811 # imaginary part also close to zero s = '1.098616 + 1.36083e-7*I' assert sstr(p.evalf(r)[-1].n(7)) == s # sin(x) p = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]) s = '0.90929463522785 + 1.52655665885959e-16*I' assert sstr(p.evalf(r)[-1]) == s # computing sin(pi/2) using this method # using a linear path from 0 to pi/2 r = [0.1] for i in range(14): r.append(r[-1] + 0.1) r.append(pi / 2) s = '0.999999895088917' # close to 1.0 (exact solution) assert sstr(p.evalf(r)[-1]) == s # trying different path, a rectangle (0-->i-->pi/2 + i-->pi/2) # computing the same value sin(pi/2) using different path r = [0.1 * I] for i in range(9): r.append(r[-1] + 0.1 * I) for i in range(15): r.append(r[-1] + 0.1) r.append(pi / 2 + I) for i in range(10): r.append(r[-1] - 0.1 * I) # close to 1.0 s = '1.00000003415141 + 6.11940487991086e-16*I' assert sstr(p.evalf(r)[-1]) == s # cos(x) p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]) # compute cos(pi) along 0-->pi r = [0.05] for i in range(61): r.append(r[-1] + 0.05) r.append(pi) # close to -1 (exact answer) s = '-0.999999993238714' assert sstr(p.evalf(r)[-1]) == s # a rectangular path (0 -> i -> 2+i -> 2) r = [0.1 * I] for i in range(9): r.append(r[-1] + 0.1 * I) for i in range(20): r.append(r[-1] + 0.1) for i in range(10): r.append(r[-1] - 0.1 * I) p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 1]).evalf(r) s = '0.493152791638442 - 1.41553435639707e-15*I' assert sstr(p[-1]) == s
def __str__(f): from sympy.printing.str import sstr return sstr(f.rep)
def __str__(self): from sympy.printing.str import sstr return sstr(self, order=None)
def __repr__(self): from sympy.printing.str import sstr return '[' + ', '.join(sstr(x) for x in self.data) + ']'
def test_hilbert_space(): hs = HilbertSpace() assert isinstance(hs, HilbertSpace) assert sstr(hs) == 'H' assert srepr(hs) == 'HilbertSpace()'
def __repr__(self): from sympy.printing.str import sstr return '<' + ','.join(sstr(x) for [x] in self._module.gens) + '>'
def NS(e, n=15, **options): return sstr(sympify(e).evalf(n, **options), full_prec=True)
def test_evalf_euler(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') # log(1+x) p = HolonomicFunction((1 + x) * Dx**2 + Dx, x, 0, [0, 1]) # path taken is a straight line from 0 to 1, on the real axis r = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] s = '0.699525841805253' # approx. equal to log(2) i.e. 0.693147180559945 assert sstr(p.evalf(r, method='Euler')[-1]) == s # path taken is a traingle 0-->1+i-->2 r = [0.1 + 0.1 * I] for i in range(9): r.append(r[-1] + 0.1 + 0.1 * I) for i in range(10): r.append(r[-1] + 0.1 - 0.1 * I) # close to the exact solution 1.09861228866811 # imaginary part also close to zero s = '1.07530466271334 - 0.0251200594793912*I' assert sstr(p.evalf(r, method='Euler')[-1]) == s # sin(x) p = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1]) s = '0.905546532085401 - 6.93889390390723e-18*I' assert sstr(p.evalf(r, method='Euler')[-1]) == s # computing sin(pi/2) using this method # using a linear path from 0 to pi/2 r = [0.1] for i in range(14): r.append(r[-1] + 0.1) r.append(pi / 2) s = '1.08016557252834' # close to 1.0 (exact solution) assert sstr(p.evalf(r, method='Euler')[-1]) == s # trying different path, a rectangle (0-->i-->pi/2 + i-->pi/2) # computing the same value sin(pi/2) using different path r = [0.1 * I] for i in range(9): r.append(r[-1] + 0.1 * I) for i in range(15): r.append(r[-1] + 0.1) r.append(pi / 2 + I) for i in range(10): r.append(r[-1] - 0.1 * I) # close to 1.0 s = '0.976882381836257 - 1.65557671738537e-16*I' assert sstr(p.evalf(r, method='Euler')[-1]) == s # cos(x) p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]) # compute cos(pi) along 0-->pi r = [0.05] for i in range(61): r.append(r[-1] + 0.05) r.append(pi) # close to -1 (exact answer) s = '-1.08140824719196' assert sstr(p.evalf(r, method='Euler')[-1]) == s # a rectangular path (0 -> i -> 2+i -> 2) r = [0.1 * I] for i in range(9): r.append(r[-1] + 0.1 * I) for i in range(20): r.append(r[-1] + 0.1) for i in range(10): r.append(r[-1] - 0.1 * I) p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 1]).evalf(r, method='Euler') s = '0.501421652861245 - 3.88578058618805e-16*I' assert sstr(p[-1]) == s
def __str__(self): from sympy.printing.str import sstr return sstr(self.data) + " + " + str(self.ring.base_ideal)