def test_algebraic(): x, y = symbols('x,y') assert ask(x, 'algebraic') == None assert ask(I, 'algebraic') == True assert ask(2*I, 'algebraic') == True assert ask(I/3, 'algebraic') == True assert ask(sqrt(7), 'algebraic') == True assert ask(2*sqrt(7), 'algebraic') == True assert ask(sqrt(7)/3, 'algebraic') == True assert ask(I*sqrt(3), 'algebraic') == True assert ask(sqrt(1+I*sqrt(3)), 'algebraic') == True assert ask((1+I*sqrt(3)**(S(17)/31)), 'algebraic') == True assert ask((1+I*sqrt(3)**(S(17)/pi)), 'algebraic') == False assert ask(sin(7), 'algebraic') == None assert ask(sqrt(sin(7)), 'algebraic') == None assert ask(sqrt(y+I*sqrt(7)), 'algebraic') == None assert ask(oo, 'algebraic') == False assert ask(-oo, 'algebraic') == False assert ask(2.47, 'algebraic') == False
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_algebraic(): x, y = symbols('x,y') assert ask(Q.algebraic(x)) == None assert ask(Q.algebraic(I)) == True assert ask(Q.algebraic(2*I)) == True assert ask(Q.algebraic(I/3)) == True assert ask(Q.algebraic(sqrt(7))) == True assert ask(Q.algebraic(2*sqrt(7))) == True assert ask(Q.algebraic(sqrt(7)/3)) == True assert ask(Q.algebraic(I*sqrt(3))) == True assert ask(Q.algebraic(sqrt(1+I*sqrt(3)))) == True assert ask(Q.algebraic((1+I*sqrt(3)**(S(17)/31)))) == True assert ask(Q.algebraic((1+I*sqrt(3)**(S(17)/pi)))) == False assert ask(Q.algebraic(sin(7))) == None assert ask(Q.algebraic(sqrt(sin(7)))) == None assert ask(Q.algebraic(sqrt(y+I*sqrt(7)))) == None assert ask(Q.algebraic(oo)) == False assert ask(Q.algebraic(-oo)) == False assert ask(Q.algebraic(2.47)) == False
def test_Integral(): assert mcode(Integral(sin(sin(x)), x)) == "Hold[Integrate[Sin[Sin[x]], x]]" assert mcode(Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))) == \ "Hold[Integrate[Exp[-x^2 - y^2], {x, -Infinity, Infinity}, " \ "{y, -Infinity, Infinity}]]"
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 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 test_algebraic(): x, y = symbols("x,y") assert ask(x, "algebraic") == None assert ask(I, "algebraic") == True assert ask(2 * I, "algebraic") == True assert ask(I / 3, "algebraic") == True assert ask(sqrt(7), "algebraic") == True assert ask(2 * sqrt(7), "algebraic") == True assert ask(sqrt(7) / 3, "algebraic") == True assert ask(I * sqrt(3), "algebraic") == True assert ask(sqrt(1 + I * sqrt(3)), "algebraic") == True assert ask((1 + I * sqrt(3) ** (S(17) / 31)), "algebraic") == True assert ask((1 + I * sqrt(3) ** (S(17) / pi)), "algebraic") == False assert ask(sin(7), "algebraic") == None assert ask(sqrt(sin(7)), "algebraic") == None assert ask(sqrt(y + I * sqrt(7)), "algebraic") == None assert ask(oo, "algebraic") == False assert ask(-oo, "algebraic") == False assert ask(2.47, "algebraic") == False
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(sin(x)**2 - 1)/2 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(cos(x)**2 - 1)/2 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)
def test_functional_exponent(): t = standard_transformations + (convert_xor, function_exponentiation) x = Symbol('x') y = Symbol('y') a = Symbol('a') yfcn = Function('y') assert parse_expr("sin^2(x)", transformations=t) == (sin(x))**2 assert parse_expr("sin^y(x)", transformations=t) == (sin(x))**y assert parse_expr("exp^y(x)", transformations=t) == (exp(x))**y assert parse_expr("E^y(x)", transformations=t) == exp(yfcn(x)) assert parse_expr("a^y(x)", transformations=t) == a**(yfcn(x))
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_Sum(): assert mcode(Sum(sin(x), (x, 0, 10))) == "Hold[Sum[Sin[x], {x, 0, 10}]]" assert mcode(Sum(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))) == \ "Hold[Sum[Exp[-x^2 - y^2], {x, -Infinity, Infinity}, " \ "{y, -Infinity, Infinity}]]"
def test_differential_operators(): xyz_coords = (x, y, z) = symbols('x y z', real=True) (o3d, ex, ey, ez) = Ga.build('e', g=[1, 1, 1], coords=xyz_coords) f = o3d.mv('f', 'scalar', f=True) lap = o3d.grad*o3d.grad assert str(lap) == 'D{x}^2 + D{y}^2 + D{z}^2' assert str(lap * f) == 'D{x}^2f + D{y}^2f + D{z}^2f' sph_coords = (r, th, phi) = symbols('r theta phi', real=True) (sp3d, er, eth, ephi) = Ga.build('e', g=[1, r**2, r**2 * sin(th)**2], coords=sph_coords, norm=True) f = sp3d.mv('f', 'scalar', f=True) lap = sp3d.grad*sp3d.grad assert str(lap) == '2/r*D{r} + cos(theta)/(r**2*sin(theta))*D{theta} + D{r}^2 + r**(-2)*D{theta}^2 + 1/(r**2*sin(theta)**2)*D{phi}^2' assert str(lap * f) == 'D{r}^2f + 2*D{r}f/r + D{theta}^2f/r**2 + cos(theta)*D{theta}f/(r**2*sin(theta)) + D{phi}^2f/(r**2*sin(theta)**2)' A = o3d.mv('A','vector') xs = o3d.mv(x) assert o3d.grad*A == 0 assert str(A*o3d.grad) == 'A__x*D{x} + A__y*D{y} + A__z*D{z} + e_x^e_y*(-A__y*D{x} + A__x*D{y}) + e_x^e_z*(-A__z*D{x} + A__x*D{z}) + e_y^e_z*(-A__z*D{y} + A__y*D{z})' assert o3d.grad*xs == ex assert str(xs*o3d.grad) == 'e_x*x*D{x} + e_y*x*D{y} + e_z*x*D{z}' assert str(o3d.grad*(o3d.grad+xs)) == 'D{x}^2 + D{y}^2 + D{z}^2 + e_x*D{}' assert str((o3d.grad+xs)*o3d.grad) == 'D{x}^2 + D{y}^2 + D{z}^2 + e_x*x*D{x} + e_y*x*D{y} + e_z*x*D{z}' return
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_sympy_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3*I, 'exp(x)': exp(x), 'x!': factorial(x), 'x!!': factorial2(x), '(x + 1)! - 1': factorial(x + 1) - 1, '3.[3]': Rational(10, 3), '.0[3]': Rational(1, 30), '3.2[3]': Rational(97, 30), '1.3[12]': Rational(433, 330), '1 + 3.[3]': Rational(13, 3), '1 + .0[3]': Rational(31, 30), '1 + 3.2[3]': Rational(127, 30), '.[0011]': Rational(1, 909), '0.1[00102] + 1': Rational(366697, 333330), '1.[0191]': Rational(10190, 9999), '10!': 3628800, '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, "S('S(3).n(n=3)')": 3.00, 'factorint(12, visual=True)': Mul( Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir="-")': Limit(sin(x), x, 0, dir='-'), } for text, result in inputs.items(): assert parse_expr(text) == result
def test_octave_piecewise(): expr = Piecewise((x, x < 1), (x**2, True)) assert mcode(expr) == "((x < 1).*(x) + (~(x < 1)).*(x.^2))" assert mcode(expr, assign_to="r") == ( "r = ((x < 1).*(x) + (~(x < 1)).*(x.^2));") assert mcode(expr, assign_to="r", inline=False) == ( "if (x < 1)\n" " r = x;\n" "else\n" " r = x.^2;\n" "end") expr = Piecewise((x**2, x < 1), (x**3, x < 2), (x**4, x < 3), (x**5, True)) expected = ("((x < 1).*(x.^2) + (~(x < 1)).*( ...\n" "(x < 2).*(x.^3) + (~(x < 2)).*( ...\n" "(x < 3).*(x.^4) + (~(x < 3)).*(x.^5))))") assert mcode(expr) == expected assert mcode(expr, assign_to="r") == "r = " + expected + ";" assert mcode(expr, assign_to="r", inline=False) == ( "if (x < 1)\n" " r = x.^2;\n" "elseif (x < 2)\n" " r = x.^3;\n" "elseif (x < 3)\n" " r = x.^4;\n" "else\n" " r = x.^5;\n" "end") # Check that Piecewise without a True (default) condition error expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0)) raises(ValueError, lambda: mcode(expr))
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_julia_piecewise(): expr = Piecewise((x, x < 1), (x**2, True)) assert julia_code(expr) == "((x < 1) ? (x) : (x.^2))" assert julia_code(expr, assign_to="r") == ( "r = ((x < 1) ? (x) : (x.^2))") assert julia_code(expr, assign_to="r", inline=False) == ( "if (x < 1)\n" " r = x\n" "else\n" " r = x.^2\n" "end") expr = Piecewise((x**2, x < 1), (x**3, x < 2), (x**4, x < 3), (x**5, True)) expected = ("((x < 1) ? (x.^2) :\n" "(x < 2) ? (x.^3) :\n" "(x < 3) ? (x.^4) : (x.^5))") assert julia_code(expr) == expected assert julia_code(expr, assign_to="r") == "r = " + expected assert julia_code(expr, assign_to="r", inline=False) == ( "if (x < 1)\n" " r = x.^2\n" "elseif (x < 2)\n" " r = x.^3\n" "elseif (x < 3)\n" " r = x.^4\n" "else\n" " r = x.^5\n" "end") # Check that Piecewise without a True (default) condition error expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0)) raises(ValueError, lambda: julia_code(expr))
def test_julia_matrix_elements(): A = Matrix([[x, 2, x*y]]) assert julia_code(A[0, 0]**2 + A[0, 1] + A[0, 2]) == "x.^2 + x.*y + 2" A = MatrixSymbol('AA', 1, 3) assert julia_code(A) == "AA" assert julia_code(A[0, 0]**2 + sin(A[0,1]) + A[0,2]) == \ "sin(AA[1,2]) + AA[1,1].^2 + AA[1,3]" assert julia_code(sum(A)) == "AA[1,1] + AA[1,2] + AA[1,3]"
def test_octave_matrix_elements(): A = Matrix([[x, 2, x*y]]) assert mcode(A[0, 0]**2 + A[0, 1] + A[0, 2]) == "x.^2 + x.*y + 2" A = MatrixSymbol('AA', 1, 3) assert mcode(A) == "AA" assert mcode(A[0,0]**2 + sin(A[0,1]) + A[0,2]) == \ "sin(AA(1, 2)) + AA(1, 1).^2 + AA(1, 3)" assert mcode(sum(A)) == "AA(1, 1) + AA(1, 2) + AA(1, 3)"
def test_Matrices_entries_not_hadamard(): # For Matrix with col >= 2, row >= 2, they need to be scalars # FIXME: is it worth worrying about this? Its not wrong, just # leave it user's responsibility to put scalar data for x. A = Matrix([[1, sin(2/x), 3*pi/x/5], [1, 2, x*y]]) expected = ("[1 sin(2/x) 3*pi/(5*x);\n" "1 2 x*y]") # <- we give x.*y assert julia_code(A) == expected
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_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_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 _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_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 glsl_code(mat, assign_to=A) == ( '''A[0][0] = x*y; if (y > 0) { A[1][0] = x + 2; } else { A[1][0] = y; } A[2][0] = sin(z);''' ) assert glsl_code(Matrix([A[0],A[1]])) # 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 glsl_code(expr) == ( '''((x > 0) ? ( 2*A[2][0] ) : ( A[2][0] )) + sin(A[1][0]) + A[0][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 glsl_code(m,M) == ( '''M[0][0] = sin(q[1]); M[0][1] = 0; M[0][2] = cos(q[2]); M[1][0] = q[1] + q[2]; M[1][1] = q[3]; M[1][2] = 5; M[2][0] = 2*q[4]/q[1]; M[2][1] = sqrt(q[0]) + 4; M[2][2] = 0;''' )
def _expr_small_minus(cls, a, z): return sqrt(z) * (1 + z)**a * sin(2 * a * atan(sqrt(z)))
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
# This program will give you the taylor expansion series # and it's corresponding value to the N-th terms # Dhava Gautama # Thanks to Allah, My Parents, Bu Fitri, StackOverflow, GeeksforGeeks, SciPy Docs, JournalDev, Programiz # https://github.com/dhava-stmkg/taylor_series # Versi 0.1.4 import numpy as np import sympy as sy from sympy.functions import sin, exp, ln from math import factorial # Define x as symbol using sympy lib x = sy.Symbol('x') # Input the function f = sin(x) # Taylor approximation at x0 of the function f(x) def taylor(fungsi, x0, n, nilai_x): i = 0 p = 0 tailor = [] while i <= n: # fungsi.diff(x,i) => Calculate i-th order derivative of the function on x # subs(x,x0) => Subs the value of x with x0 p = p + (fungsi.diff(x, i).subs(x, x0)) / (factorial(i)) * (x - x0)**i tailor.append(p.subs(x, nilai_x)) i += 1 hasil_pendekatan = np.float(p.subs(x, nilai_x)) true_value = np.float(fungsi.subs(x, nilai_x))
def test_matmul_simplify(): A = MatrixSymbol('A', 1, 1) assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \ MatMul(A, Matrix([[1]]))
def _get_lambda_evaluator(self): fr = self.d_vars[0] t = self.t_interval.v fx, fy = fr * cos(t), fr * sin(t) return lambdify([t], [fx, fy, 0.0])
def trigintegrate(f, x): """Integrate f = Mul(trig) over x >>> from sympy import Symbol, sin, cos >>> 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 - cos(x)*sin(x)/2 http://en.wikibooks.org/wiki/Calculus/Further_integration_techniques """ pat, a, n, m = _pat_sincos(x) ##m - cos ##n - sin M = f.match(pat) if M is None: return n, m = M[n], M[m] # should always be there if n is S.Zero and m is S.Zero: return x a = M[a] if n.is_integer and m.is_integer: 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_: n_ = n_ and (n < m) # NB: careful here, one of the m_ = m_ and 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 = sympy.integrals.integrate(ff, u) # XXX cyclic deps fx = fi.subs(u, uu) return fx / a # n & m are even else: # 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 -1 m+1 n-1 n - 1 | m+2 n-2 # | cos (x) sin (x) dx = ________ 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 interms 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 1 m-1 n+1 m - 1 | m-2 n+2 # | cos (x) sin (x) dx = _______ 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 = sympy.integrals.integrate( (Rational(1, 2) * sin(2 * x))**m, x) elif (m == -n): if n < 0: ##Same as the scheme described above. res = Rational(1, (n + 1)) * cos(x)**(m - 1) * sin(x)**( n + 1 ) + Rational(m - 1, n + 1) * sympy.integrals.integrate( cos(x) **(m - 2) * sin(x)** (n + 2), x ) ##the function argument to integrate in the end will be 1 , this cannot be integrated by trigintegrate. Hence use sympy.integrals.integrate. else: res = Rational(-1, m + 1) * cos(x)**(m + 1) * sin(x)**( n - 1) + Rational( n - 1, m + 1) * sympy.integrals.integrate( cos(x)**(m + 2) * sin(x)**(n - 2), x) return res.subs(x, a * x) / a
def _pat_sincos(x): a, n, m = [Wild(s, exclude=[x]) for s in 'anm'] pat = sin(a * x)**n * cos(a * x)**m return pat, a, n, m
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 P_l_m(m, l, theta): """Legendre polynomial""" eq = diff(P_l(l, theta), cos(theta), Abs(m)) result = sin(theta)**Abs(m) * eq #note 1-cos^2(theta) = sin^2(theta) return result
def test_rcode_settings(): raises(TypeError, lambda: rcode(sin(x), method="garbage"))
Created on Thu Sep 10 19:13:59 2020 @author: Julian """ import sympy as sy import numpy as np import math from sympy.functions import sin,cos import matplotlib.pyplot as plt plt.style.use("ggplot") # Define the variable and the function to approximate x = sy.Symbol('x') f = math.e**(sin(x)-cos(x**2)) # Factorial function def factorial(n): if n <= 0: return 1 else: return n*factorial(n-1) # Taylor approximation at x0 of the function 'function' def taylor(function,x0,n): i = 0 p = 0 while i <= n: p = p + (function.diff(x,i).subs(x,x0))/(factorial(i))*(x-x0)**i i += 1
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]" assert mcode(Max(x, y, z) * Min(y, z)) == "Max[x, y, z]*Min[y, z]"
def test_jscode_functions(): assert jscode(sin(x)**cos(x)) == "Math.pow(Math.sin(x), Math.cos(x))" assert jscode(sinh(x) * cosh(x)) == "Math.sinh(x)*Math.cosh(x)" assert jscode(Max(x, y) + Min(x, y)) == "Math.max(x, y) + Math.min(x, y)" assert jscode(tanh(x) * acosh(y)) == "Math.tanh(x)*Math.acosh(y)" assert jscode(asin(x) - acos(y)) == "-Math.acos(y) + Math.asin(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])
from pylab import rc from sympy.plotting import plot3d from sympy.abc import x, y from sympy.functions import sin, sqrt rc('font', size=16) rc('text', usetex=False) plot3d(sin(sqrt(x**2 + y**2)), (x, -10, 10), (y, -10, 10), xlabel='$x$', ylabel='$y$')
def test_commutator(): assert Commutator(R2.e_x, R2.e_y) == 0 assert Commutator(R2.x * R2.e_x, R2.x * R2.e_x) == 0 assert Commutator(R2.x * R2.e_x, R2.x * R2.e_y) == R2.x * R2.e_y c = Commutator(R2.e_x, R2.e_r) assert c(R2.x) == R2.y * (R2.x**2 + R2.y**2)**(-1) * sin(R2.theta)
def test_sympy_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3 * I, 'exp(x)': exp(x), 'x!': factorial(x), 'x!!': factorial2(x), '(x + 1)! - 1': factorial(x + 1) - 1, '3.[3]': Rational(10, 3), '.0[3]': Rational(1, 30), '3.2[3]': Rational(97, 30), '1.3[12]': Rational(433, 330), '1 + 3.[3]': Rational(13, 3), '1 + .0[3]': Rational(31, 30), '1 + 3.2[3]': Rational(127, 30), '.[0011]': Rational(1, 909), '0.1[00102] + 1': Rational(366697, 333330), '1.[0191]': Rational(10190, 9999), '10!': 3628800, '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, "S('S(3).n(n=3)')": 3.00, 'factorint(12, visual=True)': Mul(Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir="-")': Limit(sin(x), x, 0, dir='-'), 'Q.even(x)': Q.even(x), } for text, result in inputs.items(): assert parse_expr(text) == result raises(TypeError, lambda: parse_expr('x', standard_transformations)) raises(TypeError, lambda: parse_expr('x', transformations=lambda x, y: 1)) raises(TypeError, lambda: parse_expr('x', transformations=(lambda x, y: 1, ))) raises(TypeError, lambda: parse_expr('x', transformations=((), ))) raises(TypeError, lambda: parse_expr('x', {}, [], [])) raises(TypeError, lambda: parse_expr('x', [], [], {})) raises(TypeError, lambda: parse_expr('x', [], [], {}))
def test_Derivative(): assert mcode(Derivative(sin(x), x)) == "Hold[D[Sin[x], x]]" assert mcode(Derivative(x, x)) == "Hold[D[x, x]]" assert mcode(Derivative(sin(x)*y**4, x, 2)) == "Hold[D[y^4*Sin[x], {x, 2}]]" assert mcode(Derivative(sin(x)*y**4, x, y, x)) == "Hold[D[y^4*Sin[x], x, y, x]]" assert mcode(Derivative(sin(x)*y**4, x, y, 3, x)) == "Hold[D[y^4*Sin[x], x, {y, 3}, x]]"
def test_C99CodePrinter__precision_f80(): f80_printer = C99CodePrinter(dict(type_aliases={real: float80})) assert f80_printer.doprint(sin(x+Float('2.1'))) == 'sinl(x + 2.1L)'
def test_ccode_functions(): assert ccode(sin(x)**cos(x)) == "pow(sin(x), cos(x))"
def test_sin(): R, x, y = ring('x, y', QQ) assert rs_sin(x, x, 9)/x**5 == \ -1/5040*x**2 + 1/120 - 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_cos(): R, x, y = ring('x, y', QQ) assert rs_cos(x, x, 9)/x**5 == \ 1/40320*x**3 - 1/720*x + 1/24*x**(-1) - 1/2*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_vector_entries_hadamard(): # For a row or column, user might to use the other dimension A = Matrix([[1, sin(2 / x), 3 * pi / x / 5]]) assert mcode(A) == "[1 sin(2./x) 3*pi./(5*x)]" assert mcode(A.T) == "[1; sin(2./x); 3*pi./(5*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 * (-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 * sqrt(2 * sqrt(5) / 5 + 1) / 2 + 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(2 * sqrt(5) + 5) / 2) 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 * sqrt(2 * sqrt(5) / 5 + 1) / 2 + 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(2 * sqrt(5) + 5) / 2) 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_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 _expr_small(cls, a, z): return sqrt(z) / sqrt(1 - z) * sin(2 * a * asin(sqrt(z)))
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_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 test_dereference_printing(): expr = x + y + sin(z) + z assert rcode(expr, dereference=[z]) == "x + y + (*z) + sin((*z))"
def __init__(self): self.s, self.t, self.x, self.y, self.z = symbols('s,t,x,y,z') self.stack = [] self.defs = {} self.mode = 0 self.hist = [('', [])] # Innehåller en lista med (kommandorad, stack) self.lastx = '' self.clear = True self.op0 = { 's': lambda: self.s, 't': lambda: self.t, 'x': lambda: self.x, 'y': lambda: self.y, 'z': lambda: self.z, 'oo': lambda: S('oo'), 'inf': lambda: S('oo'), 'infinity': lambda: S('oo'), '?': lambda: self.help(), 'help': lambda: self.help(), 'hist': lambda: self.history(), 'history': lambda: self.history(), 'sketch': lambda: self.sketch(), } self.op1 = { 'radians': lambda x: pi / 180 * x, 'sin': lambda x: sin(x), 'cos': lambda x: cos(x), 'tan': lambda x: tan(x), 'sq': lambda x: x**2, 'sqrt': lambda x: sqrt(x), 'ln': lambda x: ln(x), 'exp': lambda x: exp(x), 'log': lambda x: log(x), 'simplify': lambda x: simplify(x), 'polynom': lambda x: self.polynom(x), 'inv': lambda x: 1 / x, 'chs': lambda x: -x, 'center': lambda x: x.center, 'radius': lambda x: x.radius, 'expand': lambda x: x.expand(), 'factor': lambda x: x.factor(), 'incircle': lambda x: x.incircle, 'circumcircle': lambda x: x.circumcircle, 'xdiff': lambda x: x.diff(self.x), 'ydiff': lambda x: x.diff(self.y), 'xint': lambda x: x.integrate(self.x), 'xsolve': lambda x: solve(x, self.x), 'xapart': lambda x: apart(x, self.x), 'xtogether': lambda x: together(x, self.x), 'N': lambda x: N(x), 'info': lambda x: [x.__class__.__name__, [m for m in dir(x) if m[0] != '_']], } self.op2 = { '+': lambda x, y: y + x, '-': lambda x, y: y - x, '*': lambda x, y: y * x, '/': lambda x, y: y / x, '**': lambda x, y: y**x, 'item': lambda x, y: y[x], 'point': lambda x, y: Point(y, x), 'line': lambda x, y: Line(y, x), 'circle': lambda x, y: Circle(y, x), 'tangent_lines': lambda x, y: y.tangent_lines(x), 'intersection': lambda x, y: intersection(x, y), 'perpendicular_line': lambda x, y: y.perpendicular_line(x), 'diff': lambda x, y: y.diff(x), 'int': lambda x, y: y.integrate(x), 'solve': lambda x, y: solve(y, x), 'apart': lambda x, y: apart(y, x), 'together': lambda x, y: together(y, x), 'xeval': lambda x, y: y.subs(self.x, x), } self.op3 = { 'triangle': lambda x, y, z: Triangle(x, y, z), 'limit': lambda x, y, z: limit( z, y, x), # limit(sin(x)/x,x,0) <=> x sin x / x 0 limit 'eval': lambda x, y, z: z.subs(y, x), } self.op4 = { 'sum': lambda x, y, z, t: Sum(t, (z, y, x)).doit( ) # Sum(1/x**2,(x,1,oo)).doit() <=> 1 x x * / x 1 oo sum } self.lastx = ''