def test_derivatives_in_spherical_coordinates(): with GA_Printer(): X = (r, th, phi) = symbols("r theta phi") curv = [[r * cos(phi) * sin(th), r * sin(phi) * sin(th), r * cos(th)], [1, r, r * sin(th)]] (er, eth, ephi, grad) = MV.setup("e_r e_theta e_phi", metric="[1,1,1]", coords=X, curv=curv) f = MV("f", "scalar", fct=True) A = MV("A", "vector", fct=True) B = MV("B", "grade2", fct=True) assert str(f) == "f" assert str(A) == "A__r*e_r + A__theta*e_theta + A__phi*e_phi" assert str(B) == "B__rtheta*e_r^e_theta + B__rphi*e_r^e_phi + B__thetaphi*e_theta^e_phi" assert str(grad * f) == "D{r}f*e_r + D{theta}f/r*e_theta + D{phi}f/(r*sin(theta))*e_phi" assert ( str(grad | A) == "D{r}A__r + 2*A__r/r + A__theta*cos(theta)/(r*sin(theta)) + D{theta}A__theta/r + D{phi}A__phi/(r*sin(theta))" ) assert ( str(-MV.I * (grad ^ A)) == "((A__phi*cos(theta)/sin(theta) + D{theta}A__phi - D{phi}A__theta/sin(theta))/r)*e_r + (-D{r}A__phi - A__phi/r + D{phi}A__r/(r*sin(theta)))*e_theta + (D{r}A__theta + A__theta/r - D{theta}A__r/r)*e_phi" ) assert ( str(grad ^ B) == "(D{r}B__thetaphi - B__rphi*cos(theta)/(r*sin(theta)) + 2*B__thetaphi/r - D{theta}B__rphi/r + D{phi}B__rtheta/(r*sin(theta)))*e_r^e_theta^e_phi" ) return
def test_functional_diffgeom_ch2(): x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', real=True) x, y = symbols('x, y', 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 _get_lambda_evaluator(self): fr = self.d_vars[0] t = self.u_interval.v p = self.v_interval.v fx = fr*cos(t)*sin(p) fy = fr*sin(t)*sin(p) fz = fr*cos(p) return lambdify([t,p], [fx,fy,fz])
def rs_sin(p, x, prec): """ Sine of a series Returns the series expansion of the sin of p, about 0. Examples ======== >>> from sympy.polys.domains import QQ >>> from sympy.polys.rings import ring >>> from sympy.polys.ring_series import rs_sin >>> R, x, y = ring('x, y', QQ) >>> rs_sin(x + x*y, x, 4) -1/6*x**3*y**3 - 1/2*x**3*y**2 - 1/2*x**3*y - 1/6*x**3 + x*y + x See Also ======== sin """ R = x.ring if not p: return R(0) if _has_constant_term(p, x): zm = R.zero_monom c = p[zm] c_expr = c.as_expr() if R.domain is EX: t1, t2 = sin(c_expr), cos(c_expr) elif isinstance(c, PolyElement): try: t1, t2 = R(sin(c_expr)), R(cos(c_expr)) except ValueError: raise DomainError("The given series can't be expanded in this " "domain.") else: raise DomainError("The given series can't be expanded in this " "domain") raise NotImplementedError p1 = p - c # Makes use of sympy cos, sin fuctions to evaluate the values of the cos/sin # of the constant term. return rs_sin(p1, x, prec) * t2 + rs_cos(p1, x, prec) * t1 # Series is calculated in terms of tan as its evaluation is fast. if len(p) > 20 and p.ngens == 1: t = rs_tan(p / 2, x, prec) t2 = rs_square(t, x, prec) p1 = rs_series_inversion(1 + t2, x, prec) return rs_mul(p1, 2 * t, x, prec) one = R(1) n = 1 c = [0] for k in range(2, prec + 2, 2): c.append(one / n) c.append(0) n *= -k * (k + 1) return rs_series_from_list(p, c, x, prec)
def test_C99CodePrinter__precision(): n = symbols('n', integer=True) f32_printer = C99CodePrinter(dict(type_aliases={real: float32})) f64_printer = C99CodePrinter(dict(type_aliases={real: float64})) f80_printer = C99CodePrinter(dict(type_aliases={real: float80})) assert f32_printer.doprint(sin(x+2.1)) == 'sinf(x + 2.1F)' assert f64_printer.doprint(sin(x+2.1)) == 'sin(x + 2.1000000000000001)' assert f80_printer.doprint(sin(x+Float('2.0'))) == 'sinl(x + 2.0L)' for printer, suffix in zip([f32_printer, f64_printer, f80_printer], ['f', '', 'l']): def check(expr, ref): assert printer.doprint(expr) == ref.format(s=suffix, S=suffix.upper()) check(Abs(n), 'abs(n)') check(Abs(x + 2.0), 'fabs{s}(x + 2.0{S})') check(sin(x + 4.0)**cos(x - 2.0), 'pow{s}(sin{s}(x + 4.0{S}), cos{s}(x - 2.0{S}))') check(exp(x*8.0), 'exp{s}(8.0{S}*x)') check(exp2(x), 'exp2{s}(x)') check(expm1(x*4.0), 'expm1{s}(4.0{S}*x)') check(Mod(n, 2), '((n) % (2))') check(Mod(2*n + 3, 3*n + 5), '((2*n + 3) % (3*n + 5))') check(Mod(x + 2.0, 3.0), 'fmod{s}(1.0{S}*x + 2.0{S}, 3.0{S})') check(Mod(x, 2.0*x + 3.0), 'fmod{s}(1.0{S}*x, 2.0{S}*x + 3.0{S})') check(log(x/2), 'log{s}((1.0{S}/2.0{S})*x)') check(log10(3*x/2), 'log10{s}((3.0{S}/2.0{S})*x)') check(log2(x*8.0), 'log2{s}(8.0{S}*x)') check(log1p(x), 'log1p{s}(x)') check(2**x, 'pow{s}(2, x)') check(2.0**x, 'pow{s}(2.0{S}, x)') check(x**3, 'pow{s}(x, 3)') check(x**4.0, 'pow{s}(x, 4.0{S})') check(sqrt(3+x), 'sqrt{s}(x + 3)') check(Cbrt(x-2.0), 'cbrt{s}(x - 2.0{S})') check(hypot(x, y), 'hypot{s}(x, y)') check(sin(3.*x + 2.), 'sin{s}(3.0{S}*x + 2.0{S})') check(cos(3.*x - 1.), 'cos{s}(3.0{S}*x - 1.0{S})') check(tan(4.*y + 2.), 'tan{s}(4.0{S}*y + 2.0{S})') check(asin(3.*x + 2.), 'asin{s}(3.0{S}*x + 2.0{S})') check(acos(3.*x + 2.), 'acos{s}(3.0{S}*x + 2.0{S})') check(atan(3.*x + 2.), 'atan{s}(3.0{S}*x + 2.0{S})') check(atan2(3.*x, 2.*y), 'atan2{s}(3.0{S}*x, 2.0{S}*y)') check(sinh(3.*x + 2.), 'sinh{s}(3.0{S}*x + 2.0{S})') check(cosh(3.*x - 1.), 'cosh{s}(3.0{S}*x - 1.0{S})') check(tanh(4.0*y + 2.), 'tanh{s}(4.0{S}*y + 2.0{S})') check(asinh(3.*x + 2.), 'asinh{s}(3.0{S}*x + 2.0{S})') check(acosh(3.*x + 2.), 'acosh{s}(3.0{S}*x + 2.0{S})') check(atanh(3.*x + 2.), 'atanh{s}(3.0{S}*x + 2.0{S})') check(erf(42.*x), 'erf{s}(42.0{S}*x)') check(erfc(42.*x), 'erfc{s}(42.0{S}*x)') check(gamma(x), 'tgamma{s}(x)') check(loggamma(x), 'lgamma{s}(x)') check(ceiling(x + 2.), "ceil{s}(x + 2.0{S})") check(floor(x + 2.), "floor{s}(x + 2.0{S})") check(fma(x, y, -z), 'fma{s}(x, y, -z)') check(Max(x, 8.0, x**4.0), 'fmax{s}(8.0{S}, fmax{s}(x, pow{s}(x, 4.0{S})))') check(Min(x, 2.0), 'fmin{s}(2.0{S}, x)')
def test_trigintegrate_mixed(): assert trigintegrate(sin(x)*sec(x), x) == -log(cos(x)) assert trigintegrate(sin(x)*csc(x), x) == x assert trigintegrate(sin(x)*cot(x), x) == sin(x) assert trigintegrate(cos(x)*sec(x), x) == x assert trigintegrate(cos(x)*csc(x), x) == log(sin(x)) assert trigintegrate(cos(x)*tan(x), x) == -cos(x) assert trigintegrate(cos(x)*cot(x), x) == log(cos(x) - 1)/2 \ - log(cos(x) + 1)/2 + cos(x) assert trigintegrate(cot(x)*cos(x)**2, x) == log(sin(x)) - sin(x)**2/2
def test_bounded(): x, y = symbols('xy') assert ask(x, Q.bounded) == False assert ask(x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x, Q.bounded, Assume(y, Q.bounded)) == False assert ask(x, Q.bounded, Assume(x, Q.complex)) == False assert ask(x+1, Q.bounded) == False assert ask(x+1, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x+y, Q.bounded) == None assert ask(x+y, Q.bounded, Assume(x, Q.bounded)) == False assert ask(x+1, Q.bounded, Assume(x, Q.bounded) & \ Assume(y, Q.bounded)) == True assert ask(2*x, Q.bounded) == False assert ask(2*x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x*y, Q.bounded) == None assert ask(x*y, Q.bounded, Assume(x, Q.bounded)) == False assert ask(x*y, Q.bounded, Assume(x, Q.bounded) & \ Assume(y, Q.bounded)) == True assert ask(x**2, Q.bounded) == False assert ask(2**x, Q.bounded) == False assert ask(2**x, Q.bounded, Assume(x, Q.bounded)) == True assert ask(x**x, Q.bounded) == False assert ask(Rational(1,2) ** x, Q.bounded) == True assert ask(x ** Rational(1,2), Q.bounded) == False # sign function assert ask(sign(x), Q.bounded) == True assert ask(sign(x), Q.bounded, Assume(x, Q.bounded, False)) == True # exponential functions assert ask(log(x), Q.bounded) == False assert ask(log(x), Q.bounded, Assume(x, Q.bounded)) == True assert ask(exp(x), Q.bounded) == False assert ask(exp(x), Q.bounded, Assume(x, Q.bounded)) == True assert ask(exp(2), Q.bounded) == True # trigonometric functions assert ask(sin(x), Q.bounded) == True assert ask(sin(x), Q.bounded, Assume(x, Q.bounded, False)) == True assert ask(cos(x), Q.bounded) == True assert ask(cos(x), Q.bounded, Assume(x, Q.bounded, False)) == True assert ask(2*sin(x), Q.bounded) == True assert ask(sin(x)**2, Q.bounded) == True assert ask(cos(x)**2, Q.bounded) == True assert ask(cos(x) + sin(x), Q.bounded) == True
def test_bounded(): x, y = symbols('x,y') assert ask(Q.bounded(x)) == False assert ask(Q.bounded(x), Q.bounded(x)) == True assert ask(Q.bounded(x), Q.bounded(y)) == False assert ask(Q.bounded(x), Q.complex(x)) == False assert ask(Q.bounded(x+1)) == False assert ask(Q.bounded(x+1), Q.bounded(x)) == True assert ask(Q.bounded(x+y)) == None assert ask(Q.bounded(x+y), Q.bounded(x)) == False assert ask(Q.bounded(x+1), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(2*x)) == False assert ask(Q.bounded(2*x), Q.bounded(x)) == True assert ask(Q.bounded(x*y)) == None assert ask(Q.bounded(x*y), Q.bounded(x)) == False assert ask(Q.bounded(x*y), Q.bounded(x) & Q.bounded(y)) == True assert ask(Q.bounded(x**2)) == False assert ask(Q.bounded(2**x)) == False assert ask(Q.bounded(2**x), Q.bounded(x)) == True assert ask(Q.bounded(x**x)) == False assert ask(Q.bounded(Rational(1,2) ** x)) == None assert ask(Q.bounded(Rational(1,2) ** x), Q.positive(x)) == True assert ask(Q.bounded(Rational(1,2) ** x), Q.negative(x)) == False assert ask(Q.bounded(sqrt(x))) == False # sign function assert ask(Q.bounded(sign(x))) == True assert ask(Q.bounded(sign(x)), ~Q.bounded(x)) == True # exponential functions assert ask(Q.bounded(log(x))) == False assert ask(Q.bounded(log(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(x))) == False assert ask(Q.bounded(exp(x)), Q.bounded(x)) == True assert ask(Q.bounded(exp(2))) == True # trigonometric functions assert ask(Q.bounded(sin(x))) == True assert ask(Q.bounded(sin(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(cos(x))) == True assert ask(Q.bounded(cos(x)), ~Q.bounded(x)) == True assert ask(Q.bounded(2*sin(x))) == True assert ask(Q.bounded(sin(x)**2)) == True assert ask(Q.bounded(cos(x)**2)) == True assert ask(Q.bounded(cos(x) + sin(x))) == True
def test_ccode_Piecewise_deep(): p = ccode(2*Piecewise((x, x < 1), (x + 1, x < 2), (x**2, True))) assert p == ( "2*((x < 1) ? (\n" " x\n" ")\n" ": ((x < 2) ? (\n" " x + 1\n" ")\n" ": (\n" " pow(x, 2)\n" ")))") expr = x*y*z + x**2 + y**2 + Piecewise((0, x < 0.5), (1, True)) + cos(z) - 1 assert ccode(expr) == ( "pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n" " 0\n" ")\n" ": (\n" " 1\n" ")) + cos(z) - 1") assert ccode(expr, assign_to='c') == ( "c = pow(x, 2) + x*y*z + pow(y, 2) + ((x < 0.5) ? (\n" " 0\n" ")\n" ": (\n" " 1\n" ")) + cos(z) - 1;")
def test_functional_diffgeom_ch3(): x0, y0 = symbols('x0, y0', real=True) x, y, t = symbols('x, y, t', real=True) f = Function('f') b1 = Function('b1') b2 = Function('b2') p_r = R2_r.point([x0, y0]) s_field = f(R2.x, R2.y) v_field = b1(R2.x)*R2.e_x + b2(R2.y)*R2.e_y assert v_field.rcall(s_field).rcall(p_r).doit() == b1( x0)*Derivative(f(x0, y0), x0) + b2(y0)*Derivative(f(x0, y0), y0) assert R2.e_x(R2.r**2).rcall(p_r) == 2*x0 v = R2.e_x + 2*R2.e_y s = R2.r**2 + 3*R2.x assert v.rcall(s).rcall(p_r).doit() == 2*x0 + 4*y0 + 3 circ = -R2.y*R2.e_x + R2.x*R2.e_y series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True) series_x, series_y = zip(*series) assert all( [term == cos(t).taylor_term(i, t) for i, term in enumerate(series_x)]) assert all( [term == sin(t).taylor_term(i, t) for i, term in enumerate(series_y)])
def test_functional_diffgeom_ch4(): x0, y0, theta0 = symbols('x0, y0, theta0', real=True) x, y, r, theta = symbols('x, y, r, theta', real=True) r0 = symbols('r0', positive=True) f = Function('f') b1 = Function('b1') b2 = Function('b2') p_r = R2_r.point([x0, y0]) p_p = R2_p.point([r0, theta0]) f_field = b1(R2.x,R2.y)*R2.dx + b2(R2.x,R2.y)*R2.dy assert f_field(R2.e_x)(p_r) == b1(x0, y0) assert f_field(R2.e_y)(p_r) == b2(x0, y0) s_field_r = f(R2.x,R2.y) df = Differential(s_field_r) assert df(R2.e_x)(p_r).doit() == Derivative(f(x0, y0), x0) assert df(R2.e_y)(p_r).doit() == Derivative(f(x0, y0), y0) s_field_p = f(R2.r,R2.theta) df = Differential(s_field_p) assert trigsimp(df(R2.e_x)(p_p).doit()) == cos(theta0)*Derivative(f(r0, theta0), r0) - sin(theta0)*Derivative(f(r0, theta0), theta0)/r0 assert trigsimp(df(R2.e_y)(p_p).doit()) == sin(theta0)*Derivative(f(r0, theta0), r0) + cos(theta0)*Derivative(f(r0, theta0), theta0)/r0 assert R2.dx(R2.e_x)(p_r) == 1 assert R2.dx(R2.e_y)(p_r) == 0 circ = -R2.y*R2.e_x + R2.x*R2.e_y assert R2.dx(circ)(p_r).doit() == -y0 assert R2.dy(circ)(p_r) == x0 assert R2.dr(circ)(p_r) == 0 assert simplify(R2.dtheta(circ)(p_r)) == 1 assert (circ - R2.e_theta)(s_field_r)(p_r) == 0
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) p = rcode(mat, A) assert p == ( "A[0] = x*y;\n" "A[1] = ifelse(y > 0,x + 2,y);\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] p = rcode(expr) assert p == ("ifelse(x > 0,2*A[2],A[2]) + 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 rcode(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]/q[1];\n" "M[7] = sqrt(q[0]) + 4;\n" "M[8] = 0;")
def roots_cubic(f, trig=False): """Returns a list of roots of a cubic polynomial.""" if trig: a, b, c, d = f.all_coeffs() p = (3*a*c - b**2)/3/a**2 q = (2*b**3 - 9*a*b*c + 27*a**2*d)/(27*a**3) D = 18*a*b*c*d - 4*b**3*d + b**2*c**2 - 4*a*c**3 - 27*a**2*d**2 if (D > 0) == True: rv = [] for k in range(3): rv.append(2*sqrt(-p/3)*cos(acos(3*q/2/p*sqrt(-3/p))/3 - k*2*pi/3)) return [i - b/3/a for i in rv] _, a, b, c = f.monic().all_coeffs() if c is S.Zero: x1, x2 = roots([1, a, b], multiple=True) return [x1, S.Zero, x2] p = b - a**2/3 q = c - a*b/3 + 2*a**3/27 pon3 = p/3 aon3 = a/3 if p is S.Zero: if q is S.Zero: return [-aon3]*3 else: if q.is_real: if (q > 0) == True: u1 = -root(q, 3) else: u1 = root(-q, 3) else: u1 = root(-q, 3) elif q is S.Zero: y1, y2 = roots([1, 0, p], multiple=True) return [tmp - aon3 for tmp in [y1, S.Zero, y2]] elif q.is_real and q < 0: u1 = -root(-q/2 + sqrt(q**2/4 + pon3**3), 3) else: u1 = root(q/2 + sqrt(q**2/4 + pon3**3), 3) coeff = I*sqrt(3)/2 u2 = u1*(-S.Half + coeff) u3 = u1*(-S.Half - coeff) if p is S.Zero: return [u1 - aon3, u2 - aon3, u3 - aon3] soln = [ -u1 + pon3/u1 - aon3, -u2 + pon3/u2 - aon3, -u3 + pon3/u3 - aon3 ] return soln
def test_ccode_sign(): expr1, ref1 = sign(x) * y, 'y*(((x) > 0) - ((x) < 0))' expr2, ref2 = sign(cos(x)), '(((cos(x)) > 0) - ((cos(x)) < 0))' expr3, ref3 = sign(2 * x + x**2) * x + x**2, 'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))' assert ccode(expr1) == ref1 assert ccode(expr1, 'z') == 'z = %s;' % ref1 assert ccode(expr2) == ref2 assert ccode(expr3) == ref3
def test_real(): x, y = symbols('x y') assert ask(x, Q.real) == None assert ask(x, Q.real, Assume(x, Q.real)) == True assert ask(x, Q.real, Assume(x, Q.nonzero)) == True assert ask(x, Q.real, Assume(x, Q.positive)) == True assert ask(x, Q.real, Assume(x, Q.negative)) == True assert ask(x, Q.real, Assume(x, Q.integer)) == True assert ask(x, Q.real, Assume(x, Q.even)) == True assert ask(x, Q.real, Assume(x, Q.prime)) == True assert ask(x/sqrt(2), Q.real, Assume(x, Q.real)) == True assert ask(x/sqrt(-2), Q.real, Assume(x, Q.real)) == False I = S.ImaginaryUnit assert ask(x+1, Q.real, Assume(x, Q.real)) == True assert ask(x+I, Q.real, Assume(x, Q.real)) == False assert ask(x+I, Q.real, Assume(x, Q.complex)) == None assert ask(2*x, Q.real, Assume(x, Q.real)) == True assert ask(I*x, Q.real, Assume(x, Q.real)) == False assert ask(I*x, Q.real, Assume(x, Q.imaginary)) == True assert ask(I*x, Q.real, Assume(x, Q.complex)) == None assert ask(x**2, Q.real, Assume(x, Q.real)) == True assert ask(sqrt(x), Q.real, Assume(x, Q.negative)) == False assert ask(x**y, Q.real, Assume(x, Q.real) & Assume(y, Q.integer)) == True assert ask(x**y, Q.real, Assume(x, Q.real) & Assume(y, Q.real)) == None assert ask(x**y, Q.real, Assume(x, Q.positive) & \ Assume(y, Q.real)) == True # trigonometric functions assert ask(sin(x), Q.real) == None assert ask(cos(x), Q.real) == None assert ask(sin(x), Q.real, Assume(x, Q.real)) == True assert ask(cos(x), Q.real, Assume(x, Q.real)) == True # exponential function assert ask(exp(x), Q.real) == None assert ask(exp(x), Q.real, Assume(x, Q.real)) == True assert ask(x + exp(x), Q.real, Assume(x, Q.real)) == True # Q.complexes assert ask(re(x), Q.real) == True assert ask(im(x), Q.real) == True
def test_rs_series(): x, a, b, c = symbols('x, a, b, c') assert rs_series(a, a, 5).as_expr() == a assert rs_series(sin(1/a), a, 5).as_expr() == sin(1/a) assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0, 5)).removeO() assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) + cos(a)).series(a, 0, 5)).removeO() assert rs_series(sin(a)*cos(a), a, 5).as_expr() == ((sin(a)* cos(a)).series(a, 0, 5)).removeO() p = (sin(a) - a)*(cos(a**2) + a**4/2) assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0, 10).removeO()) p = sin(a**2/2 + a/3) + cos(a/5)*sin(a/2)**3 assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = sin(x**2 + a)*(cos(x**3 - 1) - a - a**2) assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = sin(a**2 - a/3 + 2)**5*exp(a**3 - a/2) assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0, 10).removeO()) p = sin(a + b + c) assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = tan(sin(a**2 + 4) + b + c) assert expand(rs_series(p, a, 6).as_expr()) == expand(p.series(a, 0, 6).removeO())
def test_ccode_FunctionDef(): name = 'test' args = (InArgument('double', a), InArgument('int', b)) body = (Return(sin(a) + cos(b)),) results = (Result('double'),) f = FunctionDef(name, args, body, results) assert ccode(f) == ("double test(double a, int b) {\n" " return sin(a) + cos(b);\n" "}")
def test_real(): x, y = symbols('x,y') assert ask(Q.real(x)) == None assert ask(Q.real(x), Q.real(x)) == True assert ask(Q.real(x), Q.nonzero(x)) == True assert ask(Q.real(x), Q.positive(x)) == True assert ask(Q.real(x), Q.negative(x)) == True assert ask(Q.real(x), Q.integer(x)) == True assert ask(Q.real(x), Q.even(x)) == True assert ask(Q.real(x), Q.prime(x)) == True assert ask(Q.real(x/sqrt(2)), Q.real(x)) == True assert ask(Q.real(x/sqrt(-2)), Q.real(x)) == False I = S.ImaginaryUnit assert ask(Q.real(x+1), Q.real(x)) == True assert ask(Q.real(x+I), Q.real(x)) == False assert ask(Q.real(x+I), Q.complex(x)) == None assert ask(Q.real(2*x), Q.real(x)) == True assert ask(Q.real(I*x), Q.real(x)) == False assert ask(Q.real(I*x), Q.imaginary(x)) == True assert ask(Q.real(I*x), Q.complex(x)) == None assert ask(Q.real(x**2), Q.real(x)) == True assert ask(Q.real(sqrt(x)), Q.negative(x)) == False assert ask(Q.real(x**y), Q.real(x) & Q.integer(y)) == True assert ask(Q.real(x**y), Q.real(x) & Q.real(y)) == None assert ask(Q.real(x**y), Q.positive(x) & Q.real(y)) == True # trigonometric functions assert ask(Q.real(sin(x))) == None assert ask(Q.real(cos(x))) == None assert ask(Q.real(sin(x)), Q.real(x)) == True assert ask(Q.real(cos(x)), Q.real(x)) == True # exponential function assert ask(Q.real(exp(x))) == None assert ask(Q.real(exp(x)), Q.real(x)) == True assert ask(Q.real(x + exp(x)), Q.real(x)) == True # Q.complexes assert ask(Q.real(re(x))) == True assert ask(Q.real(im(x))) == True
def test_rs_series(): x, a, b, c = symbols('x, a, b, c') assert rs_series(a, a, 5).as_expr() == a assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0, 5)).removeO() assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) + cos(a)).series(a, 0, 5)).removeO() assert rs_series(sin(a)*cos(a), a, 5).as_expr() == ((sin(a)* cos(a)).series(a, 0, 5)).removeO() p = (sin(a) - a)*(cos(a**2) + a**4/2) assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0, 10).removeO()) p = sin(a**2/2 + a/3) + cos(a/5)*sin(a/2)**3 assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = sin(x**2 + a)*(cos(x**3 - 1) - a - a**2) assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = sin(a**2 - a/3 + 2)**5*exp(a**3 - a/2) assert expand(rs_series(p, a, 10).as_expr()) == expand(p.series(a, 0, 10).removeO()) p = sin(a + b + c) assert expand(rs_series(p, a, 5).as_expr()) == expand(p.series(a, 0, 5).removeO()) p = tan(sin(a**2 + 4) + b + c) assert expand(rs_series(p, a, 6).as_expr()) == expand(p.series(a, 0, 6).removeO()) p = a**QQ(2,5) + a**QQ(2,3) + a r = rs_series(tan(p), a, 2) assert r.as_expr() == a**QQ(9,5) + a**QQ(26,15) + a**QQ(22,15) + a**QQ(6,5)/3 + \ a + a**QQ(2,3) + a**QQ(2,5) r = rs_series(exp(p), a, 1) assert r.as_expr() == a**QQ(4,5)/2 + a**QQ(2,3) + a**QQ(2,5) + 1 r = rs_series(sin(p), a, 2) assert r.as_expr() == -a**QQ(9,5)/2 - a**QQ(26,15)/2 - a**QQ(22,15)/2 - \ a**QQ(6,5)/6 + a + a**QQ(2,3) + a**QQ(2,5) r = rs_series(cos(p), a, 2) assert r.as_expr() == a**QQ(28,15)/6 - a**QQ(5,3) + a**QQ(8,5)/24 - a**QQ(7,5) - \ a**QQ(4,3)/2 - a**QQ(16,15) - a**QQ(4,5)/2 + 1 assert rs_series(sin(a)/7, a, 5).as_expr() == (sin(a)/7).series(a, 0, 5).removeO()
def _sin_pow_integrate(n, x): if n > 0: if n == 1: #Recursion break return -cos(x) # n > 0 # / / # | | # | n -1 n-1 n - 1 | n-2 # | sin (x) dx = ______ cos (x) sin (x) + _______ | sin (x) dx # | | # | n n | #/ / # # return (Rational(-1, n) * cos(x) * sin(x)**(n - 1) + Rational(n - 1, n) * _sin_pow_integrate(n - 2, x)) if n < 0: if n == -1: ##Make sure this does not come back here again. ##Recursion breaks here or at n==0. return trigintegrate(1/sin(x), x) # n < 0 # / / # | | # | n 1 n+1 n + 2 | n+2 # | sin (x) dx = _______ cos (x) sin (x) + _______ | sin (x) dx # | | # | n + 1 n + 1 | #/ / # return (Rational(1, n + 1) * cos(x) * sin(x)**(n + 1) + Rational(n + 2, n + 1) * _sin_pow_integrate(n + 2, x)) else: #n == 0 #Recursion break. return x
def test_Function(): assert mcode(sin(x) ** cos(x)) == "sin(x).^cos(x)" assert mcode(abs(x)) == "abs(x)" assert mcode(ceiling(x)) == "ceil(x)" assert mcode(arg(x)) == "angle(x)" assert mcode(im(x)) == "imag(x)" assert mcode(re(x)) == "real(x)" assert mcode(Max(x, y) + Min(x, y)) == "max(x, y) + min(x, y)" assert mcode(Max(x, y, z)) == "max(x, max(y, z))" assert mcode(Min(x, y, z)) == "min(x, min(y, z))"
def test_sign(): expr = sign(x) * y assert rust_code(expr) == "y*x.signum()" assert rust_code(expr, assign_to='r') == "r = y*x.signum();" expr = sign(x + y) + 42 assert rust_code(expr) == "(x + y).signum() + 42" assert rust_code(expr, assign_to='r') == "r = (x + y).signum() + 42;" expr = sign(cos(x)) assert rust_code(expr) == "x.cos().signum()"
def test_rcode_Piecewise_deep(): p = rcode(2*Piecewise((x, x < 1), (x + 1, x < 2), (x**2, True))) assert p == "2*ifelse(x < 1,x,ifelse(x < 2,x + 1,x^2))" expr = x*y*z + x**2 + y**2 + Piecewise((0, x < 0.5), (1, True)) + cos(z) - 1 p = rcode(expr) ref="x^2 + x*y*z + y^2 + ifelse(x < 0.5,0,1) + cos(z) - 1" assert p == ref ref="c = x^2 + x*y*z + y^2 + ifelse(x < 0.5,0,1) + cos(z) - 1;" p = rcode(expr, assign_to='c') assert p == ref
def test_derivatives_in_spherical_coordinates(): with GA_Printer(): X = (r, th, phi) = symbols('r theta phi') curv = [[r*cos(phi)*sin(th), r*sin(phi)*sin(th), r*cos(th)], [1, r, r*sin(th)]] (er, eth, ephi, grad) = MV.setup('e_r e_theta e_phi', metric='[1,1,1]', coords=X, curv=curv) f = MV('f', 'scalar', fct=True) A = MV('A', 'vector', fct=True) B = MV('B', 'grade2', fct=True) assert str(f) == 'f' assert str(A) == 'A__r*e_r + A__theta*e_theta + A__phi*e_phi' assert str(B) == 'B__rtheta*e_r^e_theta + B__rphi*e_r^e_phi + B__thetaphi*e_theta^e_phi' assert str(grad*f) == 'D{r}f*e_r + D{theta}f/r*e_theta + D{phi}f/(r*sin(theta))*e_phi' assert str(grad | A) == 'D{r}A__r + 2*A__r/r + A__theta*cos(theta)/(r*sin(theta)) + D{theta}A__theta/r + D{phi}A__phi/(r*sin(theta))' assert str(-MV.I*(grad ^ A)) == '((A__phi*cos(theta)/sin(theta) + D{theta}A__phi - D{phi}A__theta/sin(theta))/r)*e_r + (-D{r}A__phi - A__phi/r + D{phi}A__r/(r*sin(theta)))*e_theta + (D{r}A__theta + A__theta/r - D{theta}A__r/r)*e_phi' assert str(grad ^ B) == '(D{r}B__thetaphi - B__rphi*cos(theta)/(r*sin(theta)) + 2*B__thetaphi/r - D{theta}B__rphi/r + D{phi}B__rtheta/(r*sin(theta)))*e_r^e_theta^e_phi' return
def test_ccode_sign(): expr = sign(x) * y assert ccode(expr) == 'y*(((x) > 0) - ((x) < 0))' assert ccode(expr, 'z') == 'z = y*(((x) > 0) - ((x) < 0));' assert ccode(sign(2 * x + x**2) * x + x**2) == \ 'pow(x, 2) + x*(((pow(x, 2) + 2*x) > 0) - ((pow(x, 2) + 2*x) < 0))' expr = sign(cos(x)) assert ccode(expr) == '(((cos(x)) > 0) - ((cos(x)) < 0))'
def _cos_pow_integrate(n,x): if n > 0 : if n==1: #Recursion break. return sin(x) # n > 0 # / / # | | # | n 1 n-1 n - 1 | n-2 # | sin (x) dx = ______ sin (x) cos (x) + _______ | cos (x) dx # | | # | n n | #/ / # # return Rational(1,n)*sin(x)*cos(x)**(n-1)+Rational(n-1,n)*_cos_pow_integrate(n-2,x) if n < 0: if n == -1: ##Recursion break return trigintegrate(1/cos(x),x) # # n < 0 # / / # | | # | n -1 n+1 n + 2 | n+2 # | cos (x) dx = _______ sin (x) cos (x) + _______ | cos (x) dx # | | # | n + 1 n + 1 | #/ / # # return Rational(-1,n+1)*sin(x)*cos(x)**(n+1) + Rational(n+2,n+1) * _cos_pow_integrate(n+2,x) else : # n == 0 #Recursion Break. return x
def test_rcode_sgn(): expr = sign(x) * y assert rcode(expr) == 'y*sign(x)' p = rcode(expr, 'z') assert p == 'z = y*sign(x);' p = rcode(sign(2 * x + x**2) * x + x**2) assert p == "x^2 + x*sign(x^2 + 2*x)" expr = sign(cos(x)) p = rcode(expr) assert p == 'sign(cos(x))'
def test_Function(): assert mcode(sin(x) ** cos(x)) == "sin(x).^cos(x)" assert mcode(sign(x)) == "sign(x)" assert mcode(exp(x)) == "exp(x)" assert mcode(log(x)) == "log(x)" assert mcode(factorial(x)) == "factorial(x)" assert mcode(floor(x)) == "floor(x)" assert mcode(atan2(y, x)) == "atan2(y, x)" assert mcode(beta(x, y)) == 'beta(x, y)' assert mcode(polylog(x, y)) == 'polylog(x, y)' assert mcode(harmonic(x)) == 'harmonic(x)' assert mcode(bernoulli(x)) == "bernoulli(x)" assert mcode(bernoulli(x, y)) == "bernoulli(x, y)"
def rs_cos(p, iv, prec): """ Cosine of a series Returns the series expansion of the cos of p, about 0. Examples ======== >>> from sympy.polys.domains import QQ >>> from sympy.polys.rings import ring >>> from sympy.polys.ring_series import rs_cos >>> R, x, y = ring('x, y', QQ) >>> rs_cos(x + x*y, x, 4) -1/2*x**2*y**2 - x**2*y - 1/2*x**2 + 1 See Also ======== cos """ ring = p.ring if _has_constant_term(p, iv): zm = ring.zero_monom c = S(p[zm]) if not c.is_real: raise NotImplementedError p1 = p - c # Makes use of sympy cos, sin fuctions to evaluate the values of the cos/sin # of the constant term. Should it be left unevaluated? from sympy.functions import cos, sin return cos(c)*rs_cos(p1, iv, prec) - sin(c)*rs_sin(p1, iv, prec) # Series is calculated in terms of tan as its evaluation is fast. if len(p) > 20 and ring.ngens == 1: t = rs_tan(p/2, iv, prec) t2 = rs_square(t, iv, prec) p1 = rs_series_inversion(1+t2, iv, prec) return rs_mul(p1 ,1 - t2, iv, prec) one = ring(1) n = 1 c = [] for k in range(2, prec + 2, 2): c.append(one/n) c.append(0) n *= -k*(k - 1) return rs_series_from_list(p, c, iv, prec)
def calc(k, prev): """recursively calculate \int(cos(x)**2k, x) formula used: ⌠ n-1 ⌠ ⎮ n C (x)*S(x) n-1 ⎮ n-2 ⎮C (x) = ──────────── + ─── * ⎮ C (x) ⌡ n n ⌡ see: http://en.wikipedia.org/wiki/List_of_integrals_of_trigonometric_functions n-1 XXX maybe combine C (x)*S(x) -> C(n*x) + ...? """ n = 2*k return (cos(_x))**(n-1) * sin(_x) / n + prev[k-1] * (n-1)/n
def test_C99CodePrinter_custom_type(): # We will look at __float128 (new in glibc 2.26) f128 = FloatType('_Float128', float128.nbits, float128.nmant, float128.nexp) p128 = C99CodePrinter( dict(type_aliases={real: f128}, type_literal_suffixes={f128: 'Q'}, type_func_suffixes={f128: 'f128'}, type_math_macro_suffixes={ real: 'f128', f128: 'f128' }, type_macros={f128: ('__STDC_WANT_IEC_60559_TYPES_EXT__', )})) assert p128.doprint(x) == 'x' assert not p128.headers assert not p128.libraries assert not p128.macros assert p128.doprint(2.0) == '2.0Q' assert not p128.headers assert not p128.libraries assert p128.macros == {'__STDC_WANT_IEC_60559_TYPES_EXT__'} assert p128.doprint(Rational(1, 2)) == '1.0Q/2.0Q' assert p128.doprint(sin(x)) == 'sinf128(x)' assert p128.doprint(cos(2., evaluate=False)) == 'cosf128(2.0Q)' var5 = Variable(x, f128, attrs={value_const}) dcl5a = Declaration(var5) assert ccode(dcl5a) == 'const _Float128 x' var5b = Variable(x, f128, pi, attrs={value_const}) dcl5b = Declaration(var5b) assert p128.doprint(dcl5b) == 'const _Float128 x = M_PIf128' var5b = Variable(x, f128, value=Catalan.evalf(38), attrs={value_const}) dcl5c = Declaration(var5b) assert p128.doprint(dcl5c) == 'const _Float128 x = %sQ' % Catalan.evalf( f128.decimal_dig)
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 jscode(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] = Math.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 jscode(expr) == ("((x > 0) ? (\n" " 2*A[2]\n" ")\n" ": (\n" " A[2]\n" ")) + Math.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 jscode(m, M) == ("M[0] = Math.sin(q[1]);\n" "M[1] = 0;\n" "M[2] = Math.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]/q[1];\n" "M[7] = Math.sqrt(q[0]) + 4;\n" "M[8] = 0;")
def _get_simplified_sol(sol, func, collectterms): r""" Helper function which collects the solution on collectterms. Ideally this should be handled by odesimp.It is used only when the simplify is set to True in dsolve. The parameter ``collectterms`` is a list of tuple (i, reroot, imroot) where `i` is the multiplicity of the root, reroot is real part and imroot being the imaginary part. """ f = func.func x = func.args[0] collectterms.sort(key=default_sort_key) collectterms.reverse() assert len(sol) == 1 and sol[0].lhs == f(x) sol = sol[0].rhs sol = expand_mul(sol) for i, reroot, imroot in collectterms: sol = collect(sol, x**i * exp(reroot * x) * sin(abs(imroot) * x)) sol = collect(sol, x**i * exp(reroot * x) * cos(imroot * x)) for i, reroot, imroot in collectterms: sol = collect(sol, x**i * exp(reroot * x)) sol = powsimp(sol) return Eq(f(x), sol)
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x) ** cos(x)) == "Sin[x]^Cos[x]" assert mcode(sec(x) * acsc(x)) == "ArcCsc[x]*Sec[x]" assert mcode(atan2(x, y)) == "ArcTan[x, y]" assert mcode(conjugate(x)) == "Conjugate[x]" assert mcode(Max(x, y, z)*Min(y, z)) == "Max[x, y, z]*Min[y, z]" assert mcode(fresnelc(x)) == "FresnelC[x]" assert mcode(fresnels(x)) == "FresnelS[x]" assert mcode(gamma(x)) == "Gamma[x]" assert mcode(uppergamma(x, y)) == "Gamma[x, y]" assert mcode(polygamma(x, y)) == "PolyGamma[x, y]" assert mcode(loggamma(x)) == "LogGamma[x]" assert mcode(erf(x)) == "Erf[x]" assert mcode(erfc(x)) == "Erfc[x]" assert mcode(erfi(x)) == "Erfi[x]" assert mcode(erf2(x, y)) == "Erf[x, y]" assert mcode(expint(x, y)) == "ExpIntegralE[x, y]" assert mcode(erfcinv(x)) == "InverseErfc[x]" assert mcode(erfinv(x)) == "InverseErf[x]" assert mcode(erf2inv(x, y)) == "InverseErf[x, y]" assert mcode(Ei(x)) == "ExpIntegralEi[x]" assert mcode(Ci(x)) == "CosIntegral[x]" assert mcode(li(x)) == "LogIntegral[x]" assert mcode(Si(x)) == "SinIntegral[x]" assert mcode(Shi(x)) == "SinhIntegral[x]" assert mcode(Chi(x)) == "CoshIntegral[x]" assert mcode(beta(x, y)) == "Beta[x, y]" assert mcode(factorial(x)) == "Factorial[x]" assert mcode(factorial2(x)) == "Factorial2[x]" assert mcode(subfactorial(x)) == "Subfactorial[x]" assert mcode(FallingFactorial(x, y)) == "FactorialPower[x, y]" assert mcode(RisingFactorial(x, y)) == "Pochhammer[x, y]" assert mcode(catalan(x)) == "CatalanNumber[x]" assert mcode(harmonic(x)) == "HarmonicNumber[x]" assert mcode(harmonic(x, y)) == "HarmonicNumber[x, y]"
def build_ideal(x, terms): """ Build generators for our ideal. Terms is an iterable with elements of the form (fn, coeff), indicating that we have a generator fn(coeff*x). If any of the terms is trigonometric, sin(x) and cos(x) are guaranteed to appear in terms. Similarly for hyperbolic functions. For tan(n*x), sin(n*x) and cos(n*x) are guaranteed. """ gens = [] I = [] y = Dummy('y') for fn, coeff in terms: for c, s, t, rel in ( [cos, sin, tan, cos(x)**2 + sin(x)**2 - 1], [cosh, sinh, tanh, cosh(x)**2 - sinh(x)**2 - 1]): if coeff == 1 and fn in [c, s]: I.append(rel) elif fn == t: I.append(t(coeff*x)*c(coeff*x) - s(coeff*x)) elif fn in [c, s]: cn = fn(coeff*y).expand(trig=True).subs(y, x) I.append(fn(coeff*x) - cn) return list(set(I))
def test_rs_series(): x, a, b, c = symbols('x, a, b, c') assert rs_series(a, a, 5).as_expr() == a assert rs_series(sin(1 / a), a, 5).as_expr() == sin(1 / a) assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0, 5)).removeO() assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) + cos(a)).series(a, 0, 5)).removeO() assert rs_series(sin(a) * cos(a), a, 5).as_expr() == ((sin(a) * cos(a)).series(a, 0, 5)).removeO() p = (sin(a) - a) * (cos(a**2) + a**4 / 2) assert expand(rs_series(p, a, 10).as_expr()) == expand( p.series(a, 0, 10).removeO()) p = sin(a**2 / 2 + a / 3) + cos(a / 5) * sin(a / 2)**3 assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = sin(x**2 + a) * (cos(x**3 - 1) - a - a**2) assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = sin(a**2 - a / 3 + 2)**5 * exp(a**3 - a / 2) assert expand(rs_series(p, a, 10).as_expr()) == expand( p.series(a, 0, 10).removeO()) p = sin(a + b + c) assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = tan(sin(a**2 + 4) + b + c) assert expand(rs_series(p, a, 6).as_expr()) == expand( p.series(a, 0, 6).removeO())
def test_matmul_simplify(): A = MatrixSymbol('A', 1, 1) assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \ MatMul(A, ImmutableMatrix([[1]]))
def test_trigintegrate_even(): assert trigintegrate(sin(x)**2, x) == x / 2 - cos(x) * sin(x) / 2 assert trigintegrate(cos(x)**2, x) == x / 2 + cos(x) * sin(x) / 2 assert trigintegrate(sin(3 * x)**2, x) == x / 2 - cos(3 * x) * sin(3 * x) / 6 assert trigintegrate(cos(3 * x)**2, x) == x / 2 + cos(3 * x) * sin(3 * x) / 6 assert (trigintegrate(sin(x)**2 * cos(x)**2, x) == x / 8 - sin(2 * x) * cos(2 * x) / 16) assert (trigintegrate(sin(x)**4 * cos(x)**2, x) == x / 16 - sin(x) * cos(x) / 16 - sin(x)**3 * cos(x) / 24 + sin(x)**5 * cos(x) / 6) assert (trigintegrate(sin(x)**2 * cos(x)**4, x) == x / 16 + cos(x) * sin(x) / 16 + cos(x)**3 * sin(x) / 24 - cos(x)**5 * sin(x) / 6) assert trigintegrate( sin(x)**(-4), x) == -2 * cos(x) / (3 * sin(x)) - cos(x) / (3 * sin(x)**3) assert trigintegrate(cos(x)**(-6), x) == sin(x) / (5 * cos(x)**5) + 4 * sin(x) / ( 15 * cos(x)**3) + 8 * sin(x) / (15 * cos(x))
def test_rcode_functions(): assert rcode(sin(x) ** cos(x)) == "sin(x)^cos(x)"
def _get_lambda_evaluator(self): fr = self.d_vars[0] t = self.u_interval.v h = self.v_interval.v fx, fy = fr * cos(t), fr * sin(t) return lambdify([t, h], [fx, fy, h])
def test_trigintegrate_symbolic(): n = Symbol("n", integer=True) assert trigintegrate(cos(x)**n, x) is None assert trigintegrate(sin(x)**n, x) is None assert trigintegrate(cot(x)**n, x) is None
def test_harmonic_rational(): ne = S(6) no = S(5) pe = S(8) po = S(9) qe = S(10) qo = S(13) Heee = harmonic(ne + pe / qe) Aeee = (-log(10) + 2 * (-1 / S(4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 - 1 / S(4)) * log(sqrt(sqrt(5) / 8 + 5 / S(8))) + pi * (1 / S(4) + sqrt(5) / 4) / (2 * sqrt(-sqrt(5) / 8 + 5 / S(8))) + 13944145 / S(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) + 2422020029 / S(702257080)) Heoe = harmonic(ne + po / qe) Aeoe = (-log(20) + 2 * (1 / S(4) + sqrt(5) / 4) * log(-1 / S(4) + sqrt(5) / 4) + 2 * (-1 / S(4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 - 1 / S(4)) * log(sqrt(sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 + 1 / S(4)) * log(1 / S(4) + sqrt(5) / 4) + 11818877030 / S(4286604231) + pi * (sqrt(5) / 8 + 5 / S(8)) / sqrt(-sqrt(5) / 8 + 5 / S(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) + 11669332571 / S(3628714320)) Hoee = harmonic(no + pe / qe) Aoee = (-log(10) + 2 * (-1 / S(4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 - 1 / S(4)) * log(sqrt(sqrt(5) / 8 + 5 / S(8))) + pi * (1 / S(4) + sqrt(5) / 4) / (2 * sqrt(-sqrt(5) / 8 + 5 / S(8))) + 779405 / S(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) + 53857323 / S(16331560)) Hooe = harmonic(no + po / qe) Aooe = (-log(20) + 2 * (1 / S(4) + sqrt(5) / 4) * log(-1 / S(4) + sqrt(5) / 4) + 2 * (-1 / S(4) + sqrt(5) / 4) * log(sqrt(-sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 - 1 / S(4)) * log(sqrt(sqrt(5) / 8 + 5 / S(8))) + 2 * (-sqrt(5) / 4 + 1 / S(4)) * log(1 / S(4) + sqrt(5) / 4) + 486853480 / S(186374097) + pi * (sqrt(5) / 8 + 5 / S(8)) / sqrt(-sqrt(5) / 8 + 5 / S(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) + 383693479 / S(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 abs(h.n() - a.n()) < 1e-12
def test_ccode_functions(): assert ccode(sin(x)**cos(x)) == "pow(sin(x), cos(x))"
def trigintegrate(f, x, conds='piecewise'): """Integrate f = Mul(trig) over x >>> from sympy import Symbol, sin, cos, tan, sec, csc, cot >>> from sympy.integrals.trigonometry import trigintegrate >>> from sympy.abc import x >>> trigintegrate(sin(x)*cos(x), x) sin(x)**2/2 >>> trigintegrate(sin(x)**2, x) x/2 - sin(x)*cos(x)/2 >>> trigintegrate(tan(x)*sec(x), x) 1/cos(x) >>> trigintegrate(sin(x)*tan(x), x) -log(sin(x) - 1)/2 + log(sin(x) + 1)/2 - sin(x) http://en.wikibooks.org/wiki/Calculus/Integration_techniques See Also ======== sympy.integrals.integrals.Integral.doit sympy.integrals.integrals.Integral """ from sympy.integrals.integrals import integrate pat, a, n, m = _pat_sincos(x) f = f.rewrite('sincos') M = f.match(pat) if M is None: return n, m = M[n], M[m] if n.is_zero and m.is_zero: return x zz = x if n.is_zero else S.Zero a = M[a] if n.is_odd or m.is_odd: u = _u n_, m_ = n.is_odd, m.is_odd # take smallest n or m -- to choose simplest substitution if n_ and m_: # Make sure to choose the positive one # otherwise an incorrect integral can occur. if n < 0 and m > 0: m_ = True n_ = False elif m < 0 and n > 0: n_ = True m_ = False # Both are negative so choose the smallest n or m # in absolute value for simplest substitution. elif (n < 0 and m < 0): n_ = n > m m_ = not (n > m) # Both n and m are odd and positive else: n_ = (n < m) # NB: careful here, one of the m_ = not (n < m) # conditions *must* be true # n m u=C (n-1)/2 m # S(x) * C(x) dx --> -(1-u^2) * u du if n_: ff = -(1 - u**2)**((n - 1) / 2) * u**m uu = cos(a * x) # n m u=S n (m-1)/2 # S(x) * C(x) dx --> u * (1-u^2) du elif m_: ff = u**n * (1 - u**2)**((m - 1) / 2) uu = sin(a * x) fi = integrate(ff, u) # XXX cyclic deps fx = fi.subs(u, uu) if conds == 'piecewise': return Piecewise((fx / a, Ne(a, 0)), (zz, True)) return fx / a # n & m are both even # # 2k 2m 2l 2l # we transform S (x) * C (x) into terms with only S (x) or C (x) # # example: # 100 4 100 2 2 100 4 2 # S (x) * C (x) = S (x) * (1-S (x)) = S (x) * (1 + S (x) - 2*S (x)) # # 104 102 100 # = S (x) - 2*S (x) + S (x) # 2k # then S is integrated with recursive formula # take largest n or m -- to choose simplest substitution n_ = (abs(n) > abs(m)) m_ = (abs(m) > abs(n)) res = S.Zero if n_: # 2k 2 k i 2i # C = (1 - S ) = sum(i, (-) * B(k, i) * S ) if m > 0: for i in range(0, m // 2 + 1): res += ((-1)**i * binomial(m // 2, i) * _sin_pow_integrate(n + 2 * i, x)) elif m == 0: res = _sin_pow_integrate(n, x) else: # m < 0 , |n| > |m| # / # | # | m n # | cos (x) sin (x) dx = # | # | #/ # / # | # -1 m+1 n-1 n - 1 | m+2 n-2 # ________ cos (x) sin (x) + _______ | cos (x) sin (x) dx # | # m + 1 m + 1 | # / res = (Rational(-1, m + 1) * cos(x)**(m + 1) * sin(x)**(n - 1) + Rational(n - 1, m + 1) * trigintegrate(cos(x)**(m + 2) * sin(x)**(n - 2), x)) elif m_: # 2k 2 k i 2i # S = (1 - C ) = sum(i, (-) * B(k, i) * C ) if n > 0: # / / # | | # | m n | -m n # | cos (x)*sin (x) dx or | cos (x) * sin (x) dx # | | # / / # # |m| > |n| ; m, n >0 ; m, n belong to Z - {0} # n 2 # sin (x) term is expanded here in terms of cos (x), # and then integrated. # for i in range(0, n // 2 + 1): res += ((-1)**i * binomial(n // 2, i) * _cos_pow_integrate(m + 2 * i, x)) elif n == 0: # / # | # | 1 # | _ _ _ # | m # | cos (x) # / # res = _cos_pow_integrate(m, x) else: # n < 0 , |m| > |n| # / # | # | m n # | cos (x) sin (x) dx = # | # | #/ # / # | # 1 m-1 n+1 m - 1 | m-2 n+2 # _______ cos (x) sin (x) + _______ | cos (x) sin (x) dx # | # n + 1 n + 1 | # / res = (Rational(1, n + 1) * cos(x)**(m - 1) * sin(x)**(n + 1) + Rational(m - 1, n + 1) * trigintegrate(cos(x)**(m - 2) * sin(x)**(n + 2), x)) else: if m == n: ##Substitute sin(2x)/2 for sin(x)cos(x) and then Integrate. res = integrate((sin(2 * x) * S.Half)**m, x) elif (m == -n): if n < 0: # Same as the scheme described above. # the function argument to integrate in the end will # be 1 , this cannot be integrated by trigintegrate. # Hence use sympy.integrals.integrate. res = (Rational(1, n + 1) * cos(x)**(m - 1) * sin(x)**(n + 1) + Rational(m - 1, n + 1) * integrate(cos(x)**(m - 2) * sin(x)**(n + 2), x)) else: res = ( Rational(-1, m + 1) * cos(x)**(m + 1) * sin(x)**(n - 1) + Rational(n - 1, m + 1) * integrate(cos(x)**(m + 2) * sin(x)**(n - 2), x)) if conds == 'piecewise': return Piecewise((res.subs(x, a * x) / a, Ne(a, 0)), (zz, True)) return res.subs(x, a * x) / a
def test_trigintegrate_odd(): assert trigintegrate(Rational(1), x) == x assert trigintegrate(x, x) is None assert trigintegrate(x**2, x) is None assert trigintegrate(sin(x), x) == -cos(x) assert trigintegrate(cos(x), x) == sin(x) assert trigintegrate(sin(3 * x), x) == -cos(3 * x) / 3 assert trigintegrate(cos(3 * x), x) == sin(3 * x) / 3 y = Symbol("y") assert trigintegrate(sin(y * x), x) == Piecewise( (-cos(y * x) / y, Ne(y, 0)), (0, True)) assert trigintegrate(cos(y * x), x) == Piecewise( (sin(y * x) / y, Ne(y, 0)), (x, True)) assert trigintegrate(sin(y * x)**2, x) == Piecewise( ((x * y / 2 - sin(x * y) * cos(x * y) / 2) / y, Ne(y, 0)), (0, True)) assert trigintegrate(sin(y * x) * cos(y * x), x) == Piecewise( (sin(x * y)**2 / (2 * y), Ne(y, 0)), (0, True)) assert trigintegrate(cos(y * x)**2, x) == Piecewise( ((x * y / 2 + sin(x * y) * cos(x * y) / 2) / y, Ne(y, 0)), (x, True)) y = Symbol("y", positive=True) # TODO: remove conds='none' below. For this to work we would have to rule # out (e.g. by trying solve) the condition y = 0, incompatible with # y.is_positive being True. assert trigintegrate(sin(y * x), x, conds="none") == -cos(y * x) / y assert trigintegrate(cos(y * x), x, conds="none") == sin(y * x) / y assert trigintegrate(sin(x) * cos(x), x) == sin(x)**2 / 2 assert trigintegrate(sin(x) * cos(x)**2, x) == -cos(x)**3 / 3 assert trigintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3 # check if it selects right function to substitute, # so the result is kept simple assert trigintegrate(sin(x)**7 * cos(x), x) == sin(x)**8 / 8 assert trigintegrate(sin(x) * cos(x)**7, x) == -cos(x)**8 / 8 assert (trigintegrate(sin(x)**7 * cos(x)**3, x) == -sin(x)**10 / 10 + sin(x)**8 / 8) assert (trigintegrate(sin(x)**3 * cos(x)**7, x) == cos(x)**10 / 10 - cos(x)**8 / 8) # both n, m are odd and -ve, and not necessarily equal assert trigintegrate(sin(x)**-1 * cos(x)**-1, x) == -log(sin(x)**2 - 1) / 2 + log(sin(x))
def test_harmonic_rational(): ne = S(6) no = S(5) pe = S(8) po = S(9) qe = S(10) qo = S(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 * sqrt(2 * sqrt(5) / 5 + 1) / 2 + Rational(13944145, 4720968)) Heeo = harmonic(ne + pe / qo) Aeeo = (-log(26) + 2 * log(sin(pi * Rational(3, 13))) * cos(pi * Rational(4, 13)) + 2 * log(sin(pi * Rational(2, 13))) * cos(pi * Rational(32, 13)) + 2 * log(sin(pi * Rational(5, 13))) * cos(pi * Rational(80, 13)) - 2 * log(sin(pi * Rational(6, 13))) * cos(pi * Rational(5, 13)) - 2 * log(sin(pi * Rational(4, 13))) * cos(pi / 13) + pi * cot(pi * Rational(5, 13)) / 2 - 2 * log(sin(pi / 13)) * cos(pi * Rational(3, 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(2 * sqrt(5) + 5) / 2) Heoo = harmonic(ne + po / qo) Aeoo = (-log(26) + 2 * log(sin(pi * Rational(3, 13))) * cos(pi * Rational(54, 13)) + 2 * log(sin(pi * Rational(4, 13))) * cos(pi * Rational(6, 13)) + 2 * log(sin(pi * Rational(6, 13))) * cos(pi * Rational(108, 13)) - 2 * log(sin(pi * Rational(5, 13))) * cos(pi / 13) - 2 * log(sin(pi / 13)) * cos(pi * Rational(5, 13)) + pi * cot(pi * Rational(4, 13)) / 2 - 2 * log(sin(pi * Rational(2, 13))) * cos(pi * Rational(3, 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 * sqrt(2 * sqrt(5) / 5 + 1) / 2 + Rational(779405, 277704)) Hoeo = harmonic(no + pe / qo) Aoeo = (-log(26) + 2 * log(sin(pi * Rational(3, 13))) * cos(pi * Rational(4, 13)) + 2 * log(sin(pi * Rational(2, 13))) * cos(pi * Rational(32, 13)) + 2 * log(sin(pi * Rational(5, 13))) * cos(pi * Rational(80, 13)) - 2 * log(sin(pi * Rational(6, 13))) * cos(pi * Rational(5, 13)) - 2 * log(sin(pi * Rational(4, 13))) * cos(pi / 13) + pi * cot(pi * Rational(5, 13)) / 2 - 2 * log(sin(pi / 13)) * cos(pi * Rational(3, 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(2 * sqrt(5) + 5) / 2) Hooo = harmonic(no + po / qo) Aooo = (-log(26) + 2 * log(sin(pi * Rational(3, 13))) * cos(pi * Rational(54, 13)) + 2 * log(sin(pi * Rational(4, 13))) * cos(pi * Rational(6, 13)) + 2 * log(sin(pi * Rational(6, 13))) * cos(pi * Rational(108, 13)) - 2 * log(sin(pi * Rational(5, 13))) * cos(pi / 13) - 2 * log(sin(pi / 13)) * cos(pi * Rational(5, 13)) + pi * cot(pi * Rational(4, 13)) / 2 - 2 * log(sin(pi * Rational(2, 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 abs(h.n() - a.n()) < 1e-12
def _pat_sincos(x): a = Wild('a', exclude=[x]) n, m = [Wild(s, exclude=[x], properties=[_integer_instance]) for s in 'nm'] pat = sin(a * x)**n * cos(a * x)**m return pat, a, n, m
def test_tensorflow_math(): if not tf: skip("TensorFlow not installed") expr = Abs(x) assert tensorflow_code(expr) == "tensorflow.math.abs(x)" _compare_tensorflow_scalar((x, ), expr) expr = sign(x) assert tensorflow_code(expr) == "tensorflow.math.sign(x)" _compare_tensorflow_scalar((x, ), expr) expr = ceiling(x) assert tensorflow_code(expr) == "tensorflow.math.ceil(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = floor(x) assert tensorflow_code(expr) == "tensorflow.math.floor(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = exp(x) assert tensorflow_code(expr) == "tensorflow.math.exp(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = sqrt(x) assert tensorflow_code(expr) == "tensorflow.math.sqrt(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = x**4 assert tensorflow_code(expr) == "tensorflow.math.pow(x, 4)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = cos(x) assert tensorflow_code(expr) == "tensorflow.math.cos(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = acos(x) assert tensorflow_code(expr) == "tensorflow.math.acos(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(0, 0.95)) expr = sin(x) assert tensorflow_code(expr) == "tensorflow.math.sin(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = asin(x) assert tensorflow_code(expr) == "tensorflow.math.asin(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = tan(x) assert tensorflow_code(expr) == "tensorflow.math.tan(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = atan(x) assert tensorflow_code(expr) == "tensorflow.math.atan(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = atan2(y, x) assert tensorflow_code(expr) == "tensorflow.math.atan2(y, x)" _compare_tensorflow_scalar((y, x), expr, rng=lambda: random.random()) expr = cosh(x) assert tensorflow_code(expr) == "tensorflow.math.cosh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = acosh(x) assert tensorflow_code(expr) == "tensorflow.math.acosh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(1, 2)) expr = sinh(x) assert tensorflow_code(expr) == "tensorflow.math.sinh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(1, 2)) expr = asinh(x) assert tensorflow_code(expr) == "tensorflow.math.asinh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(1, 2)) expr = tanh(x) assert tensorflow_code(expr) == "tensorflow.math.tanh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(1, 2)) expr = atanh(x) assert tensorflow_code(expr) == "tensorflow.math.atanh(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.uniform(-.5, .5)) expr = erf(x) assert tensorflow_code(expr) == "tensorflow.math.erf(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random()) expr = loggamma(x) assert tensorflow_code(expr) == "tensorflow.math.lgamma(x)" _compare_tensorflow_scalar((x, ), expr, rng=lambda: random.random())
def test_Functions(): assert rust_code(sin(x) ** cos(x)) == "x.sin().powf(x.cos())" assert rust_code(abs(x)) == "x.abs()" assert rust_code(ceiling(x)) == "x.ceil()"
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_small(cls, a, z): return cos(2 * a * asin(sqrt(z)))
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]" assert mcode(conjugate(x)) == "Conjugate[x]"
def _expr_small_minus(cls, a, z): return (1 + z)**a * cos(2 * a * atan(sqrt(z)))
def test_sin(): R, x, y = ring('x, y', QQ) assert rs_sin(x, x, 9)/x**5 == \ Rational(-1, 5040)*x**2 + Rational(1, 120) - Rational(1, 6)*x**(-2) + x**(-4) assert rs_sin(x*y + x**2*y**3, x, 9) == x**8*y**11/12 - \ x**8*y**9/720 + x**7*y**9/12 - x**7*y**7/5040 - x**6*y**9/6 + \ x**6*y**7/24 - x**5*y**7/2 + x**5*y**5/120 - x**4*y**5/2 - \ x**3*y**3/6 + x**2*y**3 + x*y # Constant term in series a = symbols('a') R, x, y = ring('x, y', QQ[sin(a), cos(a), a]) assert rs_sin(x + a, x, 5) == sin(a)*x**4/24 - cos(a)*x**3/6 - \ sin(a)*x**2/2 + cos(a)*x + sin(a) assert rs_sin(x + x**2*y + a, x, 5) == -sin(a)*x**4*y**2/2 - \ cos(a)*x**4*y/2 + sin(a)*x**4/24 - sin(a)*x**3*y - cos(a)*x**3/6 + \ cos(a)*x**2*y - sin(a)*x**2/2 + cos(a)*x + sin(a) R, x, y = ring('x, y', EX) assert rs_sin(x + a, x, 5) == EX(sin(a)/24)*x**4 - EX(cos(a)/6)*x**3 - \ EX(sin(a)/2)*x**2 + EX(cos(a))*x + EX(sin(a)) assert rs_sin(x + x**2*y + a, x, 5) == -EX(sin(a)/2)*x**4*y**2 - \ EX(cos(a)/2)*x**4*y + EX(sin(a)/24)*x**4 - EX(sin(a))*x**3*y - \ EX(cos(a)/6)*x**3 + EX(cos(a))*x**2*y - EX(sin(a)/2)*x**2 + \ EX(cos(a))*x + EX(sin(a))
def test_rcode_functions(): assert rcode(sin(x)**cos(x)) == "sin(x)^cos(x)" assert rcode(factorial(x) + gamma(y)) == "factorial(x) + gamma(y)" assert rcode(beta(Min(x, y), Max(x, y))) == "beta(min(x, y), max(x, y))"
def test_cos(): R, x, y = ring('x, y', QQ) assert rs_cos(x, x, 9)/x**5 == \ Rational(1, 40320)*x**3 - Rational(1, 720)*x + Rational(1, 24)*x**(-1) - S.Half*x**(-3) + x**(-5) assert rs_cos(x*y + x**2*y**3, x, 9) == x**8*y**12/24 - \ x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 - \ x**7*y**8/120 + x**6*y**8/4 - x**6*y**6/720 + x**5*y**6/6 - \ x**4*y**6/2 + x**4*y**4/24 - x**3*y**4 - x**2*y**2/2 + 1 # Constant term in series a = symbols('a') R, x, y = ring('x, y', QQ[sin(a), cos(a), a]) assert rs_cos(x + a, x, 5) == cos(a)*x**4/24 + sin(a)*x**3/6 - \ cos(a)*x**2/2 - sin(a)*x + cos(a) assert rs_cos(x + x**2*y + a, x, 5) == -cos(a)*x**4*y**2/2 + \ sin(a)*x**4*y/2 + cos(a)*x**4/24 - cos(a)*x**3*y + sin(a)*x**3/6 - \ sin(a)*x**2*y - cos(a)*x**2/2 - sin(a)*x + cos(a) R, x, y = ring('x, y', EX) assert rs_cos(x + a, x, 5) == EX(cos(a)/24)*x**4 + EX(sin(a)/6)*x**3 - \ EX(cos(a)/2)*x**2 - EX(sin(a))*x + EX(cos(a)) assert rs_cos(x + x**2*y + a, x, 5) == -EX(cos(a)/2)*x**4*y**2 + \ EX(sin(a)/2)*x**4*y + EX(cos(a)/24)*x**4 - EX(cos(a))*x**3*y + \ EX(sin(a)/6)*x**3 - EX(sin(a))*x**2*y - EX(cos(a)/2)*x**2 - \ EX(sin(a))*x + EX(cos(a))
def test_C99CodePrinter__precision(): n = symbols('n', integer=True) f32_printer = C99CodePrinter(dict(type_aliases={real: float32})) f64_printer = C99CodePrinter(dict(type_aliases={real: float64})) f80_printer = C99CodePrinter(dict(type_aliases={real: float80})) assert f32_printer.doprint(sin(x + 2.1)) == 'sinf(x + 2.1F)' assert f64_printer.doprint(sin(x + 2.1)) == 'sin(x + 2.1000000000000001)' assert f80_printer.doprint(sin(x + Float('2.0'))) == 'sinl(x + 2.0L)' for printer, suffix in zip([f32_printer, f64_printer, f80_printer], ['f', '', 'l']): def check(expr, ref): assert printer.doprint(expr) == ref.format(s=suffix, S=suffix.upper()) check(Abs(n), 'abs(n)') check(Abs(x + 2.0), 'fabs{s}(x + 2.0{S})') check( sin(x + 4.0)**cos(x - 2.0), 'pow{s}(sin{s}(x + 4.0{S}), cos{s}(x - 2.0{S}))') check(exp(x * 8.0), 'exp{s}(8.0{S}*x)') check(exp2(x), 'exp2{s}(x)') check(expm1(x * 4.0), 'expm1{s}(4.0{S}*x)') check(Mod(n, 2), '((n) % (2))') check(Mod(2 * n + 3, 3 * n + 5), '((2*n + 3) % (3*n + 5))') check(Mod(x + 2.0, 3.0), 'fmod{s}(1.0{S}*x + 2.0{S}, 3.0{S})') check(Mod(x, 2.0 * x + 3.0), 'fmod{s}(1.0{S}*x, 2.0{S}*x + 3.0{S})') check(log(x / 2), 'log{s}((1.0{S}/2.0{S})*x)') check(log10(3 * x / 2), 'log10{s}((3.0{S}/2.0{S})*x)') check(log2(x * 8.0), 'log2{s}(8.0{S}*x)') check(log1p(x), 'log1p{s}(x)') check(2**x, 'pow{s}(2, x)') check(2.0**x, 'pow{s}(2.0{S}, x)') check(x**3, 'pow{s}(x, 3)') check(x**4.0, 'pow{s}(x, 4.0{S})') check(sqrt(3 + x), 'sqrt{s}(x + 3)') check(Cbrt(x - 2.0), 'cbrt{s}(x - 2.0{S})') check(hypot(x, y), 'hypot{s}(x, y)') check(sin(3. * x + 2.), 'sin{s}(3.0{S}*x + 2.0{S})') check(cos(3. * x - 1.), 'cos{s}(3.0{S}*x - 1.0{S})') check(tan(4. * y + 2.), 'tan{s}(4.0{S}*y + 2.0{S})') check(asin(3. * x + 2.), 'asin{s}(3.0{S}*x + 2.0{S})') check(acos(3. * x + 2.), 'acos{s}(3.0{S}*x + 2.0{S})') check(atan(3. * x + 2.), 'atan{s}(3.0{S}*x + 2.0{S})') check(atan2(3. * x, 2. * y), 'atan2{s}(3.0{S}*x, 2.0{S}*y)') check(sinh(3. * x + 2.), 'sinh{s}(3.0{S}*x + 2.0{S})') check(cosh(3. * x - 1.), 'cosh{s}(3.0{S}*x - 1.0{S})') check(tanh(4.0 * y + 2.), 'tanh{s}(4.0{S}*y + 2.0{S})') check(asinh(3. * x + 2.), 'asinh{s}(3.0{S}*x + 2.0{S})') check(acosh(3. * x + 2.), 'acosh{s}(3.0{S}*x + 2.0{S})') check(atanh(3. * x + 2.), 'atanh{s}(3.0{S}*x + 2.0{S})') check(erf(42. * x), 'erf{s}(42.0{S}*x)') check(erfc(42. * x), 'erfc{s}(42.0{S}*x)') check(gamma(x), 'tgamma{s}(x)') check(loggamma(x), 'lgamma{s}(x)') check(ceiling(x + 2.), "ceil{s}(x + 2.0{S})") check(floor(x + 2.), "floor{s}(x + 2.0{S})") check(fma(x, y, -z), 'fma{s}(x, y, -z)') check(Max(x, 8.0, x**4.0), 'fmax{s}(8.0{S}, fmax{s}(x, pow{s}(x, 4.0{S})))') check(Min(x, 2.0), 'fmin{s}(2.0{S}, x)')
def test_rs_series(): x, a, b, c = symbols('x, a, b, c') assert rs_series(a, a, 5).as_expr() == a assert rs_series(sin(a), a, 5).as_expr() == (sin(a).series(a, 0, 5)).removeO() assert rs_series(sin(a) + cos(a), a, 5).as_expr() == ((sin(a) + cos(a)).series(a, 0, 5)).removeO() assert rs_series(sin(a) * cos(a), a, 5).as_expr() == ((sin(a) * cos(a)).series(a, 0, 5)).removeO() p = (sin(a) - a) * (cos(a**2) + a**4 / 2) assert expand(rs_series(p, a, 10).as_expr()) == expand( p.series(a, 0, 10).removeO()) p = sin(a**2 / 2 + a / 3) + cos(a / 5) * sin(a / 2)**3 assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = sin(x**2 + a) * (cos(x**3 - 1) - a - a**2) assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = sin(a**2 - a / 3 + 2)**5 * exp(a**3 - a / 2) assert expand(rs_series(p, a, 10).as_expr()) == expand( p.series(a, 0, 10).removeO()) p = sin(a + b + c) assert expand(rs_series(p, a, 5).as_expr()) == expand( p.series(a, 0, 5).removeO()) p = tan(sin(a**2 + 4) + b + c) assert expand(rs_series(p, a, 6).as_expr()) == expand( p.series(a, 0, 6).removeO()) p = a**QQ(2, 5) + a**QQ(2, 3) + a r = rs_series(tan(p), a, 2) assert r.as_expr() == a**QQ(9,5) + a**QQ(26,15) + a**QQ(22,15) + a**QQ(6,5)/3 + \ a + a**QQ(2,3) + a**QQ(2,5) r = rs_series(exp(p), a, 1) assert r.as_expr() == a**QQ(4, 5) / 2 + a**QQ(2, 3) + a**QQ(2, 5) + 1 r = rs_series(sin(p), a, 2) assert r.as_expr() == -a**QQ(9,5)/2 - a**QQ(26,15)/2 - a**QQ(22,15)/2 - \ a**QQ(6,5)/6 + a + a**QQ(2,3) + a**QQ(2,5) r = rs_series(cos(p), a, 2) assert r.as_expr() == a**QQ(28,15)/6 - a**QQ(5,3) + a**QQ(8,5)/24 - a**QQ(7,5) - \ a**QQ(4,3)/2 - a**QQ(16,15) - a**QQ(4,5)/2 + 1 assert rs_series(sin(a) / 7, a, 5).as_expr() == (sin(a) / 7).series(a, 0, 5).removeO() assert rs_series(log(1 + x), x, 5).as_expr() == -x**4/4 + x**3/3 - \ x**2/2 + x assert rs_series(log(1 + 4*x), x, 5).as_expr() == -64*x**4 + 64*x**3/3 - \ 8*x**2 + 4*x assert rs_series(log(1 + x + x**2), x, 10).as_expr() == -2*x**9/9 + \ x**8/8 + x**7/7 - x**6/3 + x**5/5 + x**4/4 - 2*x**3/3 + \ x**2/2 + x assert rs_series(log(1 + x*a**2), x, 7).as_expr() == -x**6*a**12/6 + \ x**5*a**10/5 - x**4*a**8/4 + x**3*a**6/3 - \ x**2*a**4/2 + x*a**2
def roots_cubic(f, trig=False): """Returns a list of roots of a cubic polynomial. References ========== [1] https://en.wikipedia.org/wiki/Cubic_function, General formula for roots, (accessed November 17, 2014). """ if trig: a, b, c, d = f.all_coeffs() p = (3*a*c - b**2)/3/a**2 q = (2*b**3 - 9*a*b*c + 27*a**2*d)/(27*a**3) D = 18*a*b*c*d - 4*b**3*d + b**2*c**2 - 4*a*c**3 - 27*a**2*d**2 if (D > 0) == True: rv = [] for k in range(3): rv.append(2*sqrt(-p/3)*cos(acos(3*q/2/p*sqrt(-3/p))/3 - k*2*pi/3)) return [i - b/3/a for i in rv] _, a, b, c = f.monic().all_coeffs() if c is S.Zero: x1, x2 = roots([1, a, b], multiple=True) return [x1, S.Zero, x2] p = b - a**2/3 q = c - a*b/3 + 2*a**3/27 pon3 = p/3 aon3 = a/3 u1 = None if p is S.Zero: if q is S.Zero: return [-aon3]*3 if q.is_real: if q.is_positive: u1 = -root(q, 3) elif q.is_negative: u1 = root(-q, 3) elif q is S.Zero: y1, y2 = roots([1, 0, p], multiple=True) return [tmp - aon3 for tmp in [y1, S.Zero, y2]] elif q.is_real and q.is_negative: u1 = -root(-q/2 + sqrt(q**2/4 + pon3**3), 3) coeff = I*sqrt(3)/2 if u1 is None: u1 = S(1) u2 = -S.Half + coeff u3 = -S.Half - coeff a, b, c, d = S(1), a, b, c D0 = b**2 - 3*a*c D1 = 2*b**3 - 9*a*b*c + 27*a**2*d C = root((D1 + sqrt(D1**2 - 4*D0**3))/2, 3) return [-(b + uk*C + D0/C/uk)/3/a for uk in [u1, u2, u3]] u2 = u1*(-S.Half + coeff) u3 = u1*(-S.Half - coeff) if p is S.Zero: return [u1 - aon3, u2 - aon3, u3 - aon3] soln = [ -u1 + pon3/u1 - aon3, -u2 + pon3/u2 - aon3, -u3 + pon3/u3 - aon3 ] return soln
def test_Function(): assert maple_code(sin(x)**cos(x)) == "sin(x)^cos(x)" assert maple_code(abs(x)) == "abs(x)" assert maple_code(ceiling(x)) == "ceil(x)"