def test_hyperexpand_bases(): assert ( hyperexpand(hyper([2], [a], z)) == a + z ** (-a + 1) * (-a ** 2 + 3 * a + z * (a - 1) - 2) * exp(z) * lowergamma(a - 1, z) - 1 ) # TODO [a+1, a-S.Half], [2*a] assert hyperexpand(hyper([1, 2], [3], z)) == -2 / z - 2 * log(exp_polar(-I * pi) * z + 1) / z ** 2 assert hyperexpand(hyper([S.Half, 2], [S(3) / 2], z)) == -1 / (2 * z - 2) + log((sqrt(z) + 1) / (-sqrt(z) + 1)) / ( 4 * sqrt(z) ) assert hyperexpand(hyper([S(1) / 2, S(1) / 2], [S(5) / 2], z)) == (-3 * z + 3) / 4 / (z * sqrt(-z + 1)) + ( 6 * z - 3 ) * asin(sqrt(z)) / (4 * z ** (S(3) / 2)) assert hyperexpand(hyper([1, 2], [S(3) / 2], z)) == -1 / (2 * z - 2) - asin(sqrt(z)) / ( sqrt(z) * (2 * z - 2) * sqrt(-z + 1) ) assert hyperexpand(hyper([-S.Half - 1, 1, 2], [S.Half, 3], z)) == sqrt(z) * (6 * z / 7 - S(6) / 5) * atanh( sqrt(z) ) + (-30 * z ** 2 + 32 * z - 6) / 35 / z - 6 * log(-z + 1) / (35 * z ** 2) assert hyperexpand(hyper([1 + S.Half, 1, 1], [2, 2], z)) == -4 * log(sqrt(-z + 1) / 2 + S(1) / 2) / z # TODO hyperexpand(hyper([a], [2*a + 1], z)) # TODO [S.Half, a], [S(3)/2, a+1] assert hyperexpand(hyper([2], [b, 1], z)) == z ** (-b / 2 + S(1) / 2) * besseli(b - 1, 2 * sqrt(z)) * gamma( b ) + z ** (-b / 2 + 1) * besseli(b, 2 * sqrt(z)) * gamma(b)
def test_issue405(): a = Symbol("a") e = asin(a*x)/x assert e.series(x, 4, n=2).removeO().subs(x, x - 4) == ( asin(4*a)/4 - (x - 4)*asin(4*a)/16 + a*(x - 4)/(4*sqrt(1 - 16*a**2)))
def test_latex_functions(): assert latex(exp(x)) == "$e^{x}$" assert latex(exp(1) + exp(2)) == "$e + e^{2}$" f = Function("f") assert latex(f(x)) == "$\\operatorname{f}\\left(x\\right)$" beta = Function("beta") assert latex(beta(x)) == r"$\operatorname{beta}\left(x\right)$" assert latex(sin(x)) == r"$\operatorname{sin}\left(x\right)$" assert latex(sin(x), fold_func_brackets=True) == r"$\operatorname{sin}x$" assert latex(sin(2 * x ** 2), fold_func_brackets=True) == r"$\operatorname{sin}2 x^{2}$" assert latex(sin(x ** 2), fold_func_brackets=True) == r"$\operatorname{sin}x^{2}$" assert latex(asin(x) ** 2) == r"$\operatorname{asin}^{2}\left(x\right)$" assert latex(asin(x) ** 2, inv_trig_style="full") == r"$\operatorname{arcsin}^{2}\left(x\right)$" assert latex(asin(x) ** 2, inv_trig_style="power") == r"$\operatorname{sin}^{-1}\left(x\right)^{2}$" assert latex(asin(x ** 2), inv_trig_style="power", fold_func_brackets=True) == r"$\operatorname{sin}^{-1}x^{2}$" assert latex(factorial(k)) == r"$k!$" assert latex(factorial(-k)) == r"$\left(- k\right)!$" assert latex(floor(x)) == r"$\lfloor{x}\rfloor$" assert latex(ceiling(x)) == r"$\lceil{x}\rceil$" assert latex(abs(x)) == r"$\lvert{x}\rvert$" assert latex(re(x)) == r"$\Re{x}$" assert latex(im(x)) == r"$\Im{x}$" assert latex(conjugate(x)) == r"$\overline{x}$" assert latex(gamma(x)) == r"$\operatorname{\Gamma}\left(x\right)$" assert latex(Order(x)) == r"$\operatorname{\mathcal{O}}\left(x\right)$"
def test_issue_2850(): assert manualintegrate(asin(x)*log(x), x) == -x*asin(x) - sqrt(-x**2 + 1) \ + (x*asin(x) + sqrt(-x**2 + 1))*log(x) - Integral(sqrt(-x**2 + 1)/x, x) assert manualintegrate(acos(x)*log(x), x) == -x*acos(x) + sqrt(-x**2 + 1) + \ (x*acos(x) - sqrt(-x**2 + 1))*log(x) + Integral(sqrt(-x**2 + 1)/x, x) assert manualintegrate(atan(x)*log(x), x) == -x*atan(x) + (x*atan(x) - \ log(x**2 + 1)/2)*log(x) + log(x**2 + 1)/2 + Integral(log(x**2 + 1)/x, x)/2
def test_heurisch_trigonometric(): assert heurisch(sin(x), x) == -cos(x) assert heurisch(pi*sin(x) + 1, x) == x - pi*cos(x) assert heurisch(cos(x), x) == sin(x) assert heurisch(tan(x), x) in [ log(1 + tan(x)**2)/2, log(tan(x) + I) + I*x, log(tan(x) - I) - I*x, ] assert heurisch(sin(x)*sin(y), x) == -cos(x)*sin(y) assert heurisch(sin(x)*sin(y), y) == -cos(y)*sin(x) # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test assert heurisch(sin(x)*cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2] assert heurisch(cos(x)/sin(x), x) == log(sin(x)) assert heurisch(x*sin(7*x), x) == sin(7*x) / 49 - x*cos(7*x) / 7 assert heurisch(1/pi/4 * x**2*cos(x), x) == 1/pi/4*(x**2*sin(x) - 2*sin(x) + 2*x*cos(x)) assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \ + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4) assert heurisch(sin(x)/(cos(x)**2+1), x) == -atan(cos(x)) #fixes issue 13723 assert heurisch(1/(cos(x)+2), x) == 2*sqrt(3)*atan(sqrt(3)*tan(x/2)/3)/3 assert heurisch(2*sin(x)*cos(x)/(sin(x)**4 + 1), x) == atan(sqrt(2)*sin(x) - 1) - atan(sqrt(2)*sin(x) + 1) assert heurisch(1/cosh(x), x) == 2*atan(tanh(x/2))
def _expr_big(cls, z, n): from sympy import log, I, pi, asin, sqrt if n.is_even: return (n - S(1) / 2) * pi * I + log(sqrt(z) / 2) + I * asin(1 / sqrt(z)) else: return (n - S(1) / 2) * pi * I + log(sqrt(z) / 2) - I * asin(1 / sqrt(z))
def test_special_is_rational(): i = Symbol('i', integer=True) r = Symbol('r', rational=True) x = Symbol('x') assert sqrt(3).is_rational is False assert (3 + sqrt(3)).is_rational is False assert (3*sqrt(3)).is_rational is False assert exp(3).is_rational is False assert exp(i).is_rational is False assert exp(r).is_rational is False assert exp(x).is_rational is None assert exp(log(3), evaluate=False).is_rational is True assert log(exp(3), evaluate=False).is_rational is True assert log(3).is_rational is False assert log(i).is_rational is False assert log(r).is_rational is False assert log(x).is_rational is None assert (sqrt(3) + sqrt(5)).is_rational is None assert (sqrt(3) + S.Pi).is_rational is None assert (x**i).is_rational is None assert (i**i).is_rational is True assert (r**i).is_rational is True assert (r**r).is_rational is None assert (r**x).is_rational is None assert sin(1).is_rational is False assert sin(i).is_rational is False assert sin(r).is_rational is False assert sin(x).is_rational is None assert asin(r).is_rational is False assert sin(asin(3), evaluate=False).is_rational is True
def test_special_is_rational(): i = Symbol('i', integer=True) ni = Symbol('ni', integer=True, nonzero=True) r = Symbol('r', rational=True) rn = Symbol('r', rational=True, nonzero=True) nr = Symbol('nr', irrational=True) x = Symbol('x') assert sqrt(3).is_rational is False assert (3 + sqrt(3)).is_rational is False assert (3*sqrt(3)).is_rational is False assert exp(3).is_rational is False assert exp(ni).is_rational is False assert exp(rn).is_rational is False assert exp(x).is_rational is None assert exp(log(3), evaluate=False).is_rational is True assert log(exp(3), evaluate=False).is_rational is True assert log(3).is_rational is False assert log(ni + 1).is_rational is False assert log(rn + 1).is_rational is False assert log(x).is_rational is None assert (sqrt(3) + sqrt(5)).is_rational is None assert (sqrt(3) + S.Pi).is_rational is False assert (x**i).is_rational is None assert (i**i).is_rational is True assert (r**i).is_rational is True assert (r**r).is_rational is None assert (r**x).is_rational is None assert (nr**i).is_rational is None # issue 8598 assert (nr**Symbol('z', zero=True)).is_rational assert sin(1).is_rational is False assert sin(ni).is_rational is False assert sin(rn).is_rational is False assert sin(x).is_rational is None assert asin(r).is_rational is False assert sin(asin(3), evaluate=False).is_rational is True
def test_asin_series(): x = Symbol('x') assert asin(x).series(x, 0, 9) == \ x + x**3/6 + 3*x**5/40 + 5*x**7/112 + O(x**9) t5 = asin(x).taylor_term(5, x) assert t5 == 3*x**5/40 assert asin(x).taylor_term(7, x, t5, 0) == 5*x**7/112
def angle_euler(A,euler_axis=1): A11,A12,A13,A21,A22,A23,A31,A32,A33 = A_comp(A) if euler_axis == 1: angle = sp.asin((A32-A23)/2) elif euler_axis == 2: angle = sp.asin((A13-A31)/2) elif euler_axis == 3: angle = sp.asin((A21-A12)/2) return angle
def test_acsch(): x = Symbol('x') assert acsch(-x) == acsch(-x) assert acsch(x) == -acsch(-x) # values at fixed points assert acsch(1) == log(1 + sqrt(2)) assert acsch(-1) == - log(1 + sqrt(2)) assert acsch(0) == zoo assert acsch(2) == log((1+sqrt(5))/2) assert acsch(-2) == - log((1+sqrt(5))/2) assert acsch(I) == - I*pi/2 assert acsch(-I) == I*pi/2 assert acsch(-I*(sqrt(6) + sqrt(2))) == I*pi / 12 assert acsch(I*(sqrt(2) + sqrt(6))) == -I*pi / 12 assert acsch(-I*(1 + sqrt(5))) == I*pi / 10 assert acsch(I*(1 + sqrt(5))) == -I*pi / 10 assert acsch(-I*2 / sqrt(2 - sqrt(2))) == I*pi / 8 assert acsch(I*2 / sqrt(2 - sqrt(2))) == -I*pi / 8 assert acsch(-I*2) == I*pi / 6 assert acsch(I*2) == -I*pi / 6 assert acsch(-I*sqrt(2 + 2/sqrt(5))) == I*pi / 5 assert acsch(I*sqrt(2 + 2/sqrt(5))) == -I*pi / 5 assert acsch(-I*sqrt(2)) == I*pi / 4 assert acsch(I*sqrt(2)) == -I*pi / 4 assert acsch(-I*(sqrt(5)-1)) == 3*I*pi / 10 assert acsch(I*(sqrt(5)-1)) == -3*I*pi / 10 assert acsch(-I*2 / sqrt(3)) == I*pi / 3 assert acsch(I*2 / sqrt(3)) == -I*pi / 3 assert acsch(-I*2 / sqrt(2 + sqrt(2))) == 3*I*pi / 8 assert acsch(I*2 / sqrt(2 + sqrt(2))) == -3*I*pi / 8 assert acsch(-I*sqrt(2 - 2/sqrt(5))) == 2*I*pi / 5 assert acsch(I*sqrt(2 - 2/sqrt(5))) == -2*I*pi / 5 assert acsch(-I*(sqrt(6) - sqrt(2))) == 5*I*pi / 12 assert acsch(I*(sqrt(6) - sqrt(2))) == -5*I*pi / 12 # properties # acsch(x) == asinh(1/x) assert acsch(-I*sqrt(2)) == asinh(I/sqrt(2)) assert acsch(-I*2 / sqrt(3)) == asinh(I*sqrt(3) / 2) # acsch(x) == -I*asin(I/x) assert acsch(-I*sqrt(2)) == -I*asin(-1/sqrt(2)) assert acsch(-I*2 / sqrt(3)) == -I*asin(-sqrt(3)/2) # csch(acsch(x)) / x == 1 assert expand_mul(csch(acsch(-I*(sqrt(6) + sqrt(2)))) / (-I*(sqrt(6) + sqrt(2)))) == 1 assert expand_mul(csch(acsch(I*(1 + sqrt(5)))) / ((I*(1 + sqrt(5))))) == 1 assert (csch(acsch(I*sqrt(2 - 2/sqrt(5)))) / (I*sqrt(2 - 2/sqrt(5)))).simplify() == 1 assert (csch(acsch(-I*sqrt(2 - 2/sqrt(5)))) / (-I*sqrt(2 - 2/sqrt(5)))).simplify() == 1 # numerical evaluation assert str(acsch(5*I+1).n(6)) == '0.0391819 - 0.193363*I' assert str(acsch(-5*I+1).n(6)) == '0.0391819 + 0.193363*I'
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1)+exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == '\\operatorname{f}{\\left (x \\right )}' beta = Function('beta') assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2,inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2,inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2),inv_trig_style="power",fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2,k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3,k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3,k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Abs(x)) == r"\lvert{x}\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x+y)) == r"\Re {\left (x + y \right )}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x,y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta{\left (x \right )}'
def test_main_trig_functions_numeric(self): print "\n\n\n" + " Test if sin, cos and tan and inverses Work Numerically ".center(75, "#") from sympy import symbols, sin, cos, tan, asin, acos, atan x, y = symbols('x,y') test_expr = sin(x) + cos(x) + tan(x) + asin(y) + acos(y) + atan(y) target_expr = sin(x) + cos(x) + tan(x) + asin(y) + acos(y) + atan(y) print "Target expression: '%s'" % target_expr print "Test expression: '%s'" % test_expr equal = api.numeric_equality(test_expr, target_expr) self.assertTrue(equal, "Expected expressions to be found numerically equal!") print " PASS ".center(75, "#")
def test_trigo(): # Vérifie que arcsin est correctement implémenté (appel à math, numpy ou # sympy suivant le type d'objet). assertAlmostEqual(asin(.2), math.asin(.2)) a, b, c = asin([.2, .3, .4]) d, e, f = numpy.arcsin([.2, .3, .4]) assertAlmostEqual(a, d) assertAlmostEqual(b, e) assertAlmostEqual(c, f) assertEqual(asin(x + 1), sympy.asin(x + 1)) # Vérifie que arccos est correctement implémenté (appel à math, numpy ou # sympy suivant le type d'objet). assertAlmostEqual(acos(.2), math.acos(.2)) a, b, c = acos([.2, .3, .4]) d, e, f = numpy.arccos([.2, .3, .4]) assertAlmostEqual(a, d) assertAlmostEqual(b, e) assertAlmostEqual(c, f) assertEqual(acos(x + 1), sympy.acos(x + 1)) # Vérifie que arctan est correctement implémenté (appel à math, numpy ou # sympy suivant le type d'objet). assertAlmostEqual(atan(.2), math.atan(.2)) a, b, c = atan([.2, .3, .4]) d, e, f = numpy.arctan([.2, .3, .4]) assertAlmostEqual(a, d) assertAlmostEqual(b, e) assertAlmostEqual(c, f) assertEqual(atan(x + 1), sympy.atan(x + 1))
def toeuler(quat): """Convert quaternion rotation to roll-pitch-yaw Euler angles.""" q0, q1, q2, q3 = quat roll = sympy.atan2(2*(q2*q3 + q0*q1), q0**2 - q1**2 - q2**2 + q3**2) pitch = -sympy.asin(2*(q1*q3 - q0*q2)) yaw = sympy.atan2(2*(q1*q2 + q0*q3), q0**2 + q1**2 - q2**2 - q3**2) return np.array([roll, pitch, yaw])
def test_acot_rewrite(): assert acot(x).rewrite(log) == I*log((x - I)/(x + I))/2 assert acot(x).rewrite(asin) == x*(-asin(sqrt(-x**2)/sqrt(-x**2 - 1)) + pi/2)*sqrt(x**(-2)) assert acot(x).rewrite(acos) == x*sqrt(x**(-2))*acos(sqrt(-x**2)/sqrt(-x**2 - 1)) assert acot(x).rewrite(atan) == atan(1/x) assert acot(x).rewrite(asec) == x*sqrt(x**(-2))*asec(sqrt((x**2 + 1)/x**2)) assert acot(x).rewrite(acsc) == x*(-acsc(sqrt((x**2 + 1)/x**2)) + pi/2)*sqrt(x**(-2))
def eval(cls, arg): from sympy import asin arg = sympify(arg) if arg.is_Number: if arg is S.NaN: return S.NaN elif arg is S.Infinity: return S.Infinity elif arg is S.NegativeInfinity: return S.NegativeInfinity elif arg is S.Zero: return S.Zero elif arg is S.One: return log(sqrt(2) + 1) elif arg is S.NegativeOne: return log(sqrt(2) - 1) elif arg.is_negative: return -cls(-arg) else: if arg is S.ComplexInfinity: return S.ComplexInfinity i_coeff = arg.as_coefficient(S.ImaginaryUnit) if i_coeff is not None: return S.ImaginaryUnit * asin(i_coeff) else: if _coeff_isneg(arg): return -cls(-arg)
def critical_angle(medium1, medium2): """ This function calculates the critical angle of incidence (marking the onset of total internal) to Medium 2 from Medium 1 in radians. Parameters ========== medium 1 : Medium or sympifiable Refractive index of Medium 1 medium 2 : Medium or sympifiable Refractive index of Medium 1 Examples ======== >>> from sympy.physics.optics import critical_angle >>> critical_angle(1.33, 1) 0.850908514477849 """ n1 = refractive_index_of_medium(medium1) n2 = refractive_index_of_medium(medium2) if n2 > n1: raise ValueError('Total internal reflection impossible for n1 < n2') else: return asin(n2/n1)
def jsify_expr(expr): clamp = Function("clamp") bottom_clamp = Function("bottom") x = Wild("x") # Prevent NaNs on inverse trig functions expr = expr.replace(asin, lambda x: asin(clamp(x, -1, 1))) expr = expr.replace(acos, lambda x: acos(clamp(x, -1, 1))) # Prevent NaNs on sqrts expr = expr.replace(sqrt(x + 1), sqrt(bottom_clamp(x + 1, 0))) js_expr = jscode(expr, user_functions = { "clamp": "THREE.Math.clamp", "bottom": "THREE.Math.clampBottom" }) # Convert all matrix references for compatibility with # three.js atoms = expr.atoms(Symbol) matrices = set([]) for atom in atoms: matrix_name = re.findall("([a-zA-Z]+)_\d,\d", atom.name) if len(matrix_name) > 0: matrices.add(matrix_name[0]) for matrix in matrices: js_expr = subs_matrix_elements(js_expr, matrix) return js_expr
def test_messy(): from sympy import (laplace_transform, Si, Shi, Chi, atan, Piecewise, acoth, E1, besselj, acosh, asin, And, re, fourier_transform, sqrt) assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True) assert laplace_transform(Shi(x), x, s) == (acoth(s)/s, 1, True) # where should the logs be simplified? assert laplace_transform(Chi(x), x, s) == \ ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True) # TODO maybe simplify the inequalities? assert laplace_transform(besselj(a, x), x, s)[1:] == \ (0, And(S(0) < re(a/2) + S(1)/2, S(0) < re(a/2) + 1)) # NOTE s < 0 can be done, but argument reduction is not good enough yet assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \ (Piecewise((0, 4*abs(pi**2*s**2) > 1), (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0) # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons) # - folding could be better assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \ log(1 + sqrt(2)) assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \ log(S(1)/2 + sqrt(2)/2) assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \ Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
def eval_trigsubstitution(theta, func, rewritten, substep, restriction, integrand, symbol): func = func.subs(sympy.sec(theta), 1/sympy.cos(theta)) trig_function = list(func.find(TrigonometricFunction)) assert len(trig_function) == 1 trig_function = trig_function[0] relation = sympy.solve(symbol - func, trig_function) assert len(relation) == 1 numer, denom = sympy.fraction(relation[0]) if isinstance(trig_function, sympy.sin): opposite = numer hypotenuse = denom adjacent = sympy.sqrt(denom**2 - numer**2) inverse = sympy.asin(relation[0]) elif isinstance(trig_function, sympy.cos): adjacent = numer hypotenuse = denom opposite = sympy.sqrt(denom**2 - numer**2) inverse = sympy.acos(relation[0]) elif isinstance(trig_function, sympy.tan): opposite = numer adjacent = denom hypotenuse = sympy.sqrt(denom**2 + numer**2) inverse = sympy.atan(relation[0]) substitution = [ (sympy.sin(theta), opposite/hypotenuse), (sympy.cos(theta), adjacent/hypotenuse), (sympy.tan(theta), opposite/adjacent), (theta, inverse) ] return sympy.Piecewise( (_manualintegrate(substep).subs(substitution).trigsimp(), restriction) )
def test_ansi_math1_codegen(): # not included: log10 from sympy import acos, asin, atan, ceiling, cos, cosh, floor, log, ln, \ sin, sinh, sqrt, tan, tanh, N x = symbols('x') name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_ceil", ceiling(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_floor", floor(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval)) numerical_tests.append((name, (xval,), expected, 1e-14)) run_cc_test("ansi_math1", name_expr, numerical_tests)
def test_conv7(): x = Symbol("x") y = Symbol("y") assert sin(x/3) == sin(sympy.Symbol("x") / 3) assert cos(x/3) == cos(sympy.Symbol("x") / 3) assert tan(x/3) == tan(sympy.Symbol("x") / 3) assert cot(x/3) == cot(sympy.Symbol("x") / 3) assert csc(x/3) == csc(sympy.Symbol("x") / 3) assert sec(x/3) == sec(sympy.Symbol("x") / 3) assert asin(x/3) == asin(sympy.Symbol("x") / 3) assert acos(x/3) == acos(sympy.Symbol("x") / 3) assert atan(x/3) == atan(sympy.Symbol("x") / 3) assert acot(x/3) == acot(sympy.Symbol("x") / 3) assert acsc(x/3) == acsc(sympy.Symbol("x") / 3) assert asec(x/3) == asec(sympy.Symbol("x") / 3) assert sin(x/3)._sympy_() == sympy.sin(sympy.Symbol("x") / 3) assert sin(x/3)._sympy_() != sympy.cos(sympy.Symbol("x") / 3) assert cos(x/3)._sympy_() == sympy.cos(sympy.Symbol("x") / 3) assert tan(x/3)._sympy_() == sympy.tan(sympy.Symbol("x") / 3) assert cot(x/3)._sympy_() == sympy.cot(sympy.Symbol("x") / 3) assert csc(x/3)._sympy_() == sympy.csc(sympy.Symbol("x") / 3) assert sec(x/3)._sympy_() == sympy.sec(sympy.Symbol("x") / 3) assert asin(x/3)._sympy_() == sympy.asin(sympy.Symbol("x") / 3) assert acos(x/3)._sympy_() == sympy.acos(sympy.Symbol("x") / 3) assert atan(x/3)._sympy_() == sympy.atan(sympy.Symbol("x") / 3) assert acot(x/3)._sympy_() == sympy.acot(sympy.Symbol("x") / 3) assert acsc(x/3)._sympy_() == sympy.acsc(sympy.Symbol("x") / 3) assert asec(x/3)._sympy_() == sympy.asec(sympy.Symbol("x") / 3)
def test_acos_series(): assert acos(x).series(x, 0, 8) == \ pi/2 - x - x**3/6 - 3*x**5/40 - 5*x**7/112 + O(x**8) assert acos(x).series(x, 0, 8) == pi/2 - asin(x).series(x, 0, 8) t5 = acos(x).taylor_term(5, x) assert t5 == -3*x**5/40 assert acos(x).taylor_term(7, x, t5, 0) == -5*x**7/112
def test_acos_rewrite(): assert acos(x).rewrite(log) == pi/2 + I*log(I*x + sqrt(1 - x**2)) assert acos(x).rewrite(atan) == \ atan(sqrt(1 - x**2)/x) + (pi/2)*(1 - x*sqrt(1/x**2)) assert acos(0).rewrite(atan) == S.Pi/2 assert acos(0.5).rewrite(atan) == acos(0.5).rewrite(log) assert acos(x).rewrite(asin) == S.Pi/2 - asin(x)
def test_intrinsic_math1_codegen(): # not included: log10 from sympy import acos, asin, atan, ceiling, cos, cosh, floor, log, ln, \ sin, sinh, sqrt, tan, tanh, N x = symbols('x') name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval)) numerical_tests.append((name, (xval,), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_atan_rewrite(): assert atan(x).rewrite(log) == I*log((1 - I*x)/(1 + I*x))/2 assert atan(x).rewrite(asin) == (-asin(1/sqrt(x**2 + 1)) + pi/2)*sqrt(x**2)/x assert atan(x).rewrite(acos) == sqrt(x**2)*acos(1/sqrt(x**2 + 1))/x assert atan(x).rewrite(acot) == acot(1/x) assert atan(x).rewrite(asec) == sqrt(x**2)*asec(sqrt(x**2 + 1))/x assert atan(x).rewrite(acsc) == (-acsc(sqrt(x**2 + 1)) + pi/2)*sqrt(x**2)/x
def test_tsolve_1(): a, b = symbols('ab') x, y, z = symbols('xyz') assert solve(exp(x)-3, x) == [log(3)] assert solve((a*x+b)*(exp(x)-3), x) == [-b/a, log(3)] assert solve(cos(x)-y, x) == [acos(y)] assert solve(2*cos(x)-y,x)== [acos(y/2)] raises(NotImplementedError, "solve(Eq(cos(x), sin(x)), x)") # XXX in the following test, log(2*y + 2*...) should -> log(2) + log(y +...) assert solve(exp(x)+exp(-x)-y,x) == [-log(4) + log(2*y + 2*(-4 + y**2)**Rational(1,2)), -log(4) + log(2*y - 2*(-4 + y**2)**Rational(1,2))] assert solve(exp(x)-3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x)-3, x) == [exp(3)] assert solve(sqrt(3*x)-4, x) == [Rational(16,3)] assert solve(3**(x+2), x) == [-oo] assert solve(3**(2-x), x) == [oo] assert solve(4*3**(5*x+2)-7, x) == [(-log(4) - 2*log(3) + log(7))/(5*log(3))] assert solve(x+2**x, x) == [-LambertW(log(2))/log(2)] assert solve(3*x+5+2**(-5*x+3), x) in \ [[-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))],\ [(-25*log(2) + 3*LambertW(-10240*2**(Rational(1, 3))*log(2)/3))/(15*log(2))]] assert solve(5*x-1+3*exp(2-7*x), x) == \ [Rational(1,5) + LambertW(-21*exp(Rational(3,5))/5)/7] assert solve(2*x+5+log(3*x-2), x) == \ [Rational(2,3) + LambertW(2*exp(-Rational(19,3))/3)/2] assert solve(3*x+log(4*x), x) == [LambertW(Rational(3,4))/3] assert solve((2*x+8)*(8+exp(x)), x) == [-4, log(8) + pi*I] assert solve(2*exp(3*x+4)-3, x) in [ [-Rational(4,3)+log(Rational(3,2))/3],\ [Rational(-4, 3) - log(2)/3 + log(3)/3]] assert solve(2*log(3*x+4)-3, x) == [(exp(Rational(3,2))-4)/3] assert solve(exp(x)+1, x) == [pi*I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3/log(3)*LambertW(-log(3)/3)] assert solve(2*(3*x+4)**5 - 6*7**(3*x+9), x) in \ [[Rational(-4,3) - 5/log(7)/3*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10)],\ [(-5*LambertW(-7*2**(Rational(4, 5))*6**(Rational(1, 5))*log(7)/10) - 4*log(7))/(3*log(7))], \ [-((4*log(7) + 5*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10))/(3*log(7)))]] assert solve(z*cos(x)-y, x) == [acos(y/z)] assert solve(z*cos(2*x)-y, x) == [acos(y/z)/2] assert solve(z*cos(sin(x))-y, x) == [asin(acos(y/z))] assert solve(z*cos(x), x) == [acos(0)] assert solve(exp(x)+exp(-x)-y, x)== [-log(4) + log(2*y + 2*(-4 + y**2)**(Rational(1, 2))), -log(4) + log(2*y - 2*(-4 + y**2)**(Rational(1, 2)))] # issue #1409 assert solve(y - b*x/(a+x), x) in [[-a*y/(y - b)], [a*y/(b - y)]] assert solve(y - b*exp(a/x), x) == [a/(-log(b) + log(y))] # issue #1408 assert solve(y-b/(1+a*x),x) == [(b - y)/(a*y)] # issue #1407 assert solve(y-a*x**b , x) == [y**(1/b)*(1/a)**(1/b)] # issue #1406 assert solve(z**x - y, x) == [log(y)/log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10)/log(2)]
def test_cot(): assert cot(nan) == nan assert cot.nargs == FiniteSet(1) assert cot(oo*I) == -I assert cot(-oo*I) == I assert cot(0) == zoo assert cot(2*pi) == zoo assert cot(acot(x)) == x assert cot(atan(x)) == 1 / x assert cot(asin(x)) == sqrt(1 - x**2) / x assert cot(acos(x)) == x / sqrt(1 - x**2) assert cot(atan2(y, x)) == x/y assert cot(pi*I) == -coth(pi)*I assert cot(-pi*I) == coth(pi)*I assert cot(-2*I) == coth(2)*I assert cot(pi) == cot(2*pi) == cot(3*pi) assert cot(-pi) == cot(-2*pi) == cot(-3*pi) assert cot(pi/2) == 0 assert cot(-pi/2) == 0 assert cot(5*pi/2) == 0 assert cot(7*pi/2) == 0 assert cot(pi/3) == 1/sqrt(3) assert cot(-2*pi/3) == 1/sqrt(3) assert cot(pi/4) == S.One assert cot(-pi/4) == -S.One assert cot(17*pi/4) == S.One assert cot(-3*pi/4) == S.One assert cot(pi/6) == sqrt(3) assert cot(-pi/6) == -sqrt(3) assert cot(7*pi/6) == sqrt(3) assert cot(-5*pi/6) == sqrt(3) assert cot(x*I) == -coth(x)*I assert cot(k*pi*I) == -coth(k*pi)*I assert cot(r).is_real is True assert cot(a).is_algebraic is None assert cot(na).is_algebraic is False assert cot(10*pi/7) == cot(3*pi/7) assert cot(11*pi/7) == -cot(3*pi/7) assert cot(-11*pi/7) == cot(3*pi/7) assert cot(x).is_finite is None assert cot(r).is_finite is None i = Symbol('i', imaginary=True) assert cot(i).is_finite is True assert cot(x).subs(x, 3*pi) == zoo
def calc_Bragg_angle(self, Energy, Miller=None): if Miller == None: Miller = self.Miller wavelength=12398./Energy thissubs = dict(zip(self.structure.miller, Miller)) Braggq= 2*sp.pi * self.structure.qfunc.dictcall(thissubs) thetaBragg=sp.asin(Braggq*wavelength/(4*sp.pi)) return thetaBragg
def test_acsc(): assert acsc(nan) == nan assert acsc(1) == pi / 2 assert acsc(-1) == -pi / 2 assert acsc(oo) == 0 assert acsc(-oo) == 0 assert acsc(zoo) == 0 assert acsc(x).diff(x) == -1 / (x**2 * sqrt(1 - 1 / x**2)) assert acsc(x).as_leading_term(x) == log(x) assert acsc(x).rewrite(log) == -I * log(sqrt(1 - 1 / x**2) + I / x) assert acsc(x).rewrite(asin) == asin(1 / x) assert acsc(x).rewrite(acos) == -acos(1 / x) + pi / 2 assert acsc(x).rewrite(atan) == (-atan(sqrt(x**2 - 1)) + pi / 2) * sqrt( x**2) / x assert acsc(x).rewrite(acot) == (-acot(1 / sqrt(x**2 - 1)) + pi / 2) * sqrt(x**2) / x assert acsc(x).rewrite(asec) == -asec(x) + pi / 2
def go_to_state(n,m): """ For equal superpositions of states in a n-dimensional system, this function will create an operator that sends all the amplitude to state |m> """ # preprocessing overlap = 1/sp.sqrt(n) # compute angles theta_val = ( sp.asin(overlap) ) phi_val = ( sp.acos(overlap)/2 ) # flip state & operator f = sp.cos( theta_val + phi_val ), sp.sin( phi_val + theta_val ) flip_ = nans.dot(f[0]) + ans.dot(f[1]) diffuse_op = 2 * outer(flip_, flip_) - identity(n, object) return diffuse_op
def compute_internal_angles(self, theta_0, theta_1): """ Return the internal angles of the robot leg from the current motor angles """ alpha_0, alpha_1, A, B, C = symbols('alpha_0 alpha_1 A B C', real=True) d = sqrt(l_base * l_base + l1 * l1 - 2 * l_base * l1 * cos(theta_0)) beta = -asin(l1 / d * sin(theta_0)) A = sympy.simplify(2 * l1 * l2 * cos(theta_1) + 2 * d * l2 * cos(beta)) B = sympy.simplify(2 * l1 * l2 * sin(theta_1) + 2 * d * l2 * sin(beta)) C = sympy.simplify(-l1 * l1 - d * d - 2 * d * l1 * cos(theta_1 - beta)) alpha_1 = sympy.simplify(atan(B / A) + acos(C / sqrt(A * A + B * B))) alpha_0 = beta + acos( (l1 * cos(theta_1 - beta) + l2 * cos(alpha_1 - beta) + d) / l2) return (alpha_0, alpha_1)
def test_tsolve(): x = Symbol('x') y = Symbol('y') z = Symbol('z') assert solve(exp(x)-3, x) == [log(3)] assert solve(cos(x)-y, x) == [acos(y)] assert solve(2*cos(x)-y,x)== [acos(y/2)] # XXX in the following test, log(2*y + 2*...) should -> log(2) + log(y +...) assert solve(exp(x)+exp(-x)-y,x) == [-log(4) + log(2*y + 2*(-4 + y**2)**Rational(1,2)), -log(4) + log(2*y - 2*(-4 + y**2)**Rational(1,2))] assert tsolve(exp(x)-3, x) == [log(3)] assert tsolve(Eq(exp(x), 3), x) == [log(3)] assert tsolve(log(x)-3, x) == [exp(3)] assert tsolve(sqrt(3*x)-4, x) == [Rational(16,3)] assert tsolve(3**(x+2), x) == [-oo] assert tsolve(3**(2-x), x) == [oo] assert tsolve(4*3**(5*x+2)-7, x) == [(log(Rational(7,4))-2*log(3))/(5*log(3))] assert tsolve(x+2**x, x) == [-LambertW(log(2))/log(2)] assert tsolve(3*x+5+2**(-5*x+3), x) == \ [-Rational(5,3) + LambertW(-10240*2**Rational(1,3)*log(2)/3)/(5*log(2))] assert tsolve(5*x-1+3*exp(2-7*x), x) == \ [Rational(1,5) + LambertW(-21*exp(Rational(3,5))/5)/7] assert tsolve(2*x+5+log(3*x-2), x) == \ [Rational(2,3) + LambertW(2*exp(-Rational(19,3))/3)/2] assert tsolve(3*x+log(4*x), x) == [LambertW(Rational(3,4))/3] assert tsolve((2*x+8)*(8+exp(x)), x) == [-4] assert tsolve(2*exp(3*x+4)-3, x) == [-Rational(4,3)+log(Rational(3,2))/3] assert tsolve(2*log(3*x+4)-3, x) == [(exp(Rational(3,2))-4)/3] assert tsolve(exp(x)+1, x) == [pi*I] assert tsolve(x**2 - 2**x, x) == [2] assert tsolve(x**3 - 3**x, x) == [-3/log(3)*LambertW(-log(3)/3)] assert tsolve(2*(3*x+4)**5 - 6*7**(3*x+9), x) == \ [Rational(-4,3) - 5/log(7)/3*LambertW(-7*2**Rational(4,5)*6**Rational(1,5)*log(7)/10)] assert tsolve(z*cos(x)-y, x) == [acos(y/z)] assert tsolve(z*cos(2*x)-y, x) == [acos(y/z)/2] assert tsolve(z*cos(sin(x))-y, x) == [asin(acos(y/z))] assert tsolve(z*cos(x), x) == [acos(0)] assert tsolve(exp(x)+exp(-x)-y, x)== [log(y/2 + Rational(1,2)*(-4 + y**2)**Rational(1,2)), log(y/2 - Rational(1,2)*(-4 + y**2)**Rational(1,2))]
def _modula_funcion_fm(self): signo_moduladora = utils.signo_en_funcion(self.fun_moduladora) funcion_moduladora = utils.funcion_en_string(self.fun_moduladora, self.wm, self.t) if 'saw' in self.fun_moduladora: self.moduladora = self.Vm_sierra * (signo_moduladora * funcion_moduladora) elif 'tri' in self.fun_moduladora: self.moduladora = self.Vm_sierra * (signo_moduladora * funcion_moduladora) else: self.moduladora = self.Vm * (signo_moduladora * funcion_moduladora) integral = self._integra_kl_vmt(self.kl, self.moduladora) signo_portadora = utils.signo_en_funcion(self.fun_portadora) funcion_portadora = utils.funcion_en_string(self.fun_portadora, self.wc, self.t) if 'saw' in self.fun_portadora: self.portadora = self.Vm_sierra * (signo_portadora * funcion_portadora) elif 'tri' in self.fun_portadora: self.portadora = self.Vm_sierra * (signo_portadora * funcion_portadora) else: self.portadora = self.Vc * (signo_portadora * funcion_portadora) if 'cos' in self.fun_portadora: self.modulada = self.Vc * (signo_portadora * sp.cos((self.wc * self.t) + integral)) + self.noise elif 'sen' in self.fun_portadora or 'sin' in self.fun_portadora: self.modulada = self.Vc * (signo_portadora * sp.sin(self.wc * self.t + integral)) + self.noise elif 'saw' in self.fun_portadora: self.modulada = (self.Vc_sierra) * (signo_portadora * sp.atan(sp.cot((self.wc/2) * self.t + integral))) \ + self.noise elif 'tri' in self.fun_portadora: self.modulada = (self.Vc_sierra) * (signo_portadora * sp.asin(sp.sin(self.wc * self.t + integral))) \ + self.noise return self.modulada
def test_simplify_function_inverse(): # "inverse" attribute does not guarantee that f(g(x)) is x # so this simplification should not happen automatically. # See issue #12140 x, y = symbols('x, y') g = Function('g') class f(Function): def inverse(self, argindex=1): return g assert simplify(f(g(x))) == f(g(x)) assert inversecombine(f(g(x))) == x assert simplify(f(g(x)), inverse=True) == x assert simplify(f(g(sin(x)**2 + cos(x)**2)), inverse=True) == 1 assert simplify(f(g(x, y)), inverse=True) == f(g(x, y)) assert simplify(2 * asin(sin(3 * x)), inverse=True) == 6 * x assert simplify(log(exp(x))) == log(exp(x)) assert simplify(log(exp(x)), inverse=True) == x assert simplify(log(exp(x), 2), inverse=True) == x / log(2) assert simplify(log(exp(x), 2, evaluate=False), inverse=True) == x / log(2)
def gainFromDampingRatio(transferFunction, dampingRatio): """ Returns gain found from a given control.matlab.tf transferFunction and dampingRatio By sampling along matlab.rlocus plot. Converges on an area of the plot by taking smaller sized samples (defined by variable 'spacing') in each iteration until spacing = 0.1 , thus the gain will only ever be within 0.1 """ minGainSample = 0.0 spacing = 1000.0 closerToMGS = False gainFound = False slopeOfDampingRatioLine = abs(sympy.tan(sympy.asin(dampingRatio))) while not gainFound : data = matlab.rlocus(transferFunction, np.array([minGainSample,minGainSample + (1.0*spacing),minGainSample + (2.0*spacing), minGainSample + (3.0*spacing),minGainSample + (4.0*spacing),minGainSample + (5.0*spacing), minGainSample + (6.0*spacing),minGainSample + (7.0*spacing),minGainSample + (8.0*spacing), minGainSample + (9.0*spacing),minGainSample + (10.0*spacing)])) for j in range(0, len(data[0][0])): for i in range(1, len(data[0])) : data_point = data[0][i][j] if data_point.imag > 0 : if (data[0][i-1][j].imag > 0 and abs(data_point.real / data_point.imag) <= slopeOfDampingRatioLine and abs(data[0][i-1][j].real / data[0][i-1][j].imag) >= slopeOfDampingRatioLine) : minGainSample = data[1][i-1] if(data[0][i-1][j].imag > 0 and abs(abs(data_point.real / data_point.imag) - abs(slopeOfDampingRatioLine)) >= abs(abs(data[0][i-1][j].real / data[0][i-1][j].imag) - abs(slopeOfDampingRatioLine))) : closerToMGS = True else : closerToMGS = False if spacing == 0.1: gainFound = True else : spacing = spacing / 10 ## End While Loop if closerToMGS : return minGainSample else : return minGainSample + 0.1
def tan_simplify(eqn, atom, implicit=True, positive_branch=False): s = sp.Dummy('s') c = sp.Dummy('c') eq_temp = eqn.subs([(sp.sin(atom), s), (sp.cos(atom), c)]).collect(c).collect(s) # A sin + B cos + C = 0 # R = sign(A)/(A^2 +B^2)^{1/2} # A / R sin(x) + B/R cos(x) + C/R = 0 # sin(x + phi) + C / R= 0 # B / R = sin(phi) # A / R = cos(phi) # tan = sin/cos = (B/A) # phi = arctan(B / A) # sin(x + phi) + C / R = 0 A = eq_temp.coeff(s, 1).coeff(c, 0) B = eq_temp.coeff(c, 1).coeff(s, 0) remainder = eq_temp.coeff(c, 0).coeff(s, 0) difference = sp.expand(A * s + B * c + remainder - eq_temp) if difference != 0 or not A or not B: return eqn #A = _simplify_all(A) #B = _simplify_all(B) magnitude = sp.simplify(A**2 + B**2) R = sp.sign(A) * sp.sqrt(magnitude) C = remainder / R ratio = sp.atan2(B / R, A / R) phi = sp.atan(ratio) if implicit: return sp.simplify(R * sp.sin(atom + phi) + remainder) else: return -ratio - sp.asin(sp.simplify(C))
def __init__(self, control_idx, lower_expr: sympy.Expr, upper_expr: sympy.Expr, ind_var_sym, state_syms, parameter_syms, constant_syms, local_compiler: LocalCompiler = None): self.control_idx = control_idx self.lower_expr = lower_expr self.upper_expr = upper_expr self.args = [ind_var_sym, state_syms, parameter_syms, constant_syms] if local_compiler is None: self.local_compiler = LocalCompiler() else: self.local_compiler = local_compiler u = sympy.Symbol('_u') u_trig = sympy.Symbol('_u_trig') u_min = self.lower_expr u_max = self.upper_expr u_range = (u_max - u_min) u_offset = (u_max + u_min) / 2 u_trig_expr = sympy.asin((u - u_offset) / u_range) u_expr = u_range / 2 * sympy.sin(u_trig) + u_offset self.map_func = self.local_compiler.lambdify([u] + self.args, u_trig_expr) self.inv_map_func = self.local_compiler.lambdify([u_trig] + self.args, u_expr)
def test_bng_printer(): # Constants assert _bng_print(sympy.pi) == '_pi' assert _bng_print(sympy.E) == '_e' x, y = sympy.symbols('x y') # Binary functions assert _bng_print(sympy.sympify('x & y')) == 'x && y' assert _bng_print(sympy.sympify('x | y')) == 'x || y' # Trig functions assert _bng_print(sympy.sin(x)) == 'sin(x)' assert _bng_print(sympy.cos(x)) == 'cos(x)' assert _bng_print(sympy.tan(x)) == 'tan(x)' assert _bng_print(sympy.asin(x)) == 'asin(x)' assert _bng_print(sympy.acos(x)) == 'acos(x)' assert _bng_print(sympy.atan(x)) == 'atan(x)' assert _bng_print(sympy.sinh(x)) == 'sinh(x)' assert _bng_print(sympy.cosh(x)) == 'cosh(x)' assert _bng_print(sympy.tanh(x)) == 'tanh(x)' assert _bng_print(sympy.asinh(x)) == 'asinh(x)' assert _bng_print(sympy.acosh(x)) == 'acosh(x)' assert _bng_print(sympy.atanh(x)) == 'atanh(x)' # Logs and powers assert _bng_print(sympy.log(x)) == 'ln(x)' assert _bng_print(sympy.exp(x)) == 'exp(x)' assert _bng_print(sympy.sqrt(x)) == 'sqrt(x)' # Rounding assert _bng_print(sympy.Abs(x)) == 'abs(x)' assert _bng_print(sympy.floor(x)) == 'rint(x - 0.5)' assert _bng_print(sympy.ceiling(x)) == '(rint(x + 1) - 1)' # Min/max assert _bng_print(sympy.Min(x, y)) == 'min(x, y)' assert _bng_print(sympy.Max(x, y)) == 'max(x, y)'
def test_matrix_to_array(self): x, y = sp.symbols('x, y') ones = np.ones(10) M = sp.Matrix([ 0.5 * (x + y), sp.asin(sp.sin(0.5 * (x + y))), sp.sin(x + y)**2 + sp.cos(x + y)**2 ]) f = pytrajectory.auxiliary.cse_lambdify(args=(x, y), expr=M, modules=[{ 'ImmutableMatrix': np.array }, 'numpy']) F = f(1., 1.) assert type(F == np.ndarray) assert not isinstance(F, np.matrix) assert F.shape == (3, 1) assert np.allclose(F, np.ones((3, 1)))
def critical_angle(medium1, medium2): """ This function calculates the critical angle of incidence (marking the onset of total internal) to Medium 2 from Medium 1 in radians. Parameters ========== medium 1 : Medium or sympifiable Refractive index of Medium 1 medium 2 : Medium or sympifiable Refractive index of Medium 1 Examples ======== >>> from sympy.physics.optics import critical_angle >>> critical_angle(1.33, 1) 0.850908514477849 """ n1, n2 = None, None if isinstance(medium1, Medium): n1 = medium1.refractive_index else: n1 = sympify(medium1) if isinstance(medium2, Medium): n2 = medium2.refractive_index else: n2 = sympify(medium2) if n2 > n1: raise ValueError('Total internal reflection impossible for n1 < n2') else: return asin(n2/n1)
def test_presentation_mathml_trig(): mml = mpp._print(sin(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'sin' mml = mpp._print(cos(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'cos' mml = mpp._print(tan(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'tan' mml = mpp._print(asin(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arcsin' mml = mpp._print(acos(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arccos' mml = mpp._print(atan(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arctan' mml = mpp._print(sinh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'sinh' mml = mpp._print(cosh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'cosh' mml = mpp._print(tanh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'tanh' mml = mpp._print(asinh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arcsinh' mml = mpp._print(atanh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arctanh' mml = mpp._print(acosh(x)) assert mml.childNodes[0].childNodes[0].nodeValue == 'arccosh'
def test_simpleDE(): # Tests just the first valid DE for DE in simpleDE(exp(x), x, f): assert DE == (-f(x) + Derivative(f(x), x), 1) break for DE in simpleDE(sin(x), x, f): assert DE == (f(x) + Derivative(f(x), x, x), 2) break for DE in simpleDE(log(1 + x), x, f): assert DE == ((x + 1)*Derivative(f(x), x, 2) + Derivative(f(x), x), 2) break for DE in simpleDE(asin(x), x, f): assert DE == (x*Derivative(f(x), x) + (x**2 - 1)*Derivative(f(x), x, x), 2) break for DE in simpleDE(exp(x)*sin(x), x, f): assert DE == (2*f(x) - 2*Derivative(f(x)) + Derivative(f(x), x, x), 2) break for DE in simpleDE(((1 + x)/(1 - x))**n, x, f): assert DE == (2*n*f(x) + (x**2 - 1)*Derivative(f(x), x), 1) break for DE in simpleDE(airyai(x), x, f): assert DE == (-x*f(x) + Derivative(f(x), x, x), 2) break
def test_mathml_trig(): mml = mp._print(sin(x)) assert mml.childNodes[0].nodeName == 'sin' mml = mp._print(cos(x)) assert mml.childNodes[0].nodeName == 'cos' mml = mp._print(tan(x)) assert mml.childNodes[0].nodeName == 'tan' mml = mp._print(asin(x)) assert mml.childNodes[0].nodeName == 'arcsin' mml = mp._print(acos(x)) assert mml.childNodes[0].nodeName == 'arccos' mml = mp._print(atan(x)) assert mml.childNodes[0].nodeName == 'arctan' mml = mp._print(sinh(x)) assert mml.childNodes[0].nodeName == 'sinh' mml = mp._print(cosh(x)) assert mml.childNodes[0].nodeName == 'cosh' mml = mp._print(tanh(x)) assert mml.childNodes[0].nodeName == 'tanh' mml = mp._print(asinh(x)) assert mml.childNodes[0].nodeName == 'arcsinh' mml = mp._print(atanh(x)) assert mml.childNodes[0].nodeName == 'arctanh' mml = mp._print(acosh(x)) assert mml.childNodes[0].nodeName == 'arccosh'
def test_issue_3504(): a = Symbol("a") e = asin(a * x) / x assert e.series(x, 4, n=2).removeO() == \ (x - 4)*(a/(4*sqrt(-16*a**2 + 1)) - asin(4*a)/16) + asin(4*a)/4
def eval_arcsin(integrand, symbol): return sympy.asin(symbol)
def test_issue_4052(): f = S(1) / 2 * asin(x) + x * sqrt(1 - x**2) / 2 assert integrate(cos(asin(x)), x) == f assert integrate(sin(acos(x)), x) == f
import sympy.physics.mechanics as _me import sympy as _sm import math as m import numpy as _np x, y = _me.dynamicsymbols('x y') x_d, y_d = _me.dynamicsymbols('x_ y_', 1) e = _sm.cos(x)+_sm.sin(x)+_sm.tan(x)+_sm.cosh(x)+_sm.sinh(x)+_sm.tanh(x)+_sm.acos(x)+_sm.asin(x)+_sm.atan(x)+_sm.log(x)+_sm.exp(x)+_sm.sqrt(x)+_sm.factorial(x)+_sm.ceiling(x)+_sm.floor(x)+_sm.sign(x) e = (x)**2+_sm.log(x, 10) a = _sm.Abs(-1*1)+int(1.5)+round(1.9) e1 = 2*x+3*y e2 = x+y am = _sm.Matrix([e1.expand().coeff(x), e1.expand().coeff(y), e2.expand().coeff(x), e2.expand().coeff(y)]).reshape(2, 2) b = (e1).expand().coeff(x) c = (e2).expand().coeff(y) d1 = (e1).collect(x).coeff(x,0) d2 = (e1).collect(x).coeff(x,1) fm = _sm.Matrix([i.collect(x)for i in _sm.Matrix([e1,e2]).reshape(1, 2)]).reshape((_sm.Matrix([e1,e2]).reshape(1, 2)).shape[0], (_sm.Matrix([e1,e2]).reshape(1, 2)).shape[1]) f = (e1).collect(y) g = (e1).subs({x:2*x}) gm = _sm.Matrix([i.subs({x:3}) for i in _sm.Matrix([e1,e2]).reshape(2, 1)]).reshape((_sm.Matrix([e1,e2]).reshape(2, 1)).shape[0], (_sm.Matrix([e1,e2]).reshape(2, 1)).shape[1]) frame_a = _me.ReferenceFrame('a') frame_b = _me.ReferenceFrame('b') theta = _me.dynamicsymbols('theta') frame_b.orient(frame_a, 'Axis', [theta, frame_a.z]) v1 = 2*frame_a.x-3*frame_a.y+frame_a.z v2 = frame_b.x+frame_b.y+frame_b.z a = _me.dot(v1, v2) bm = _sm.Matrix([_me.dot(v1, v2),_me.dot(v1, 2*v2)]).reshape(2, 1) c = _me.cross(v1, v2) d = 2*v1.magnitude()+3*v1.magnitude()
ax0.yaxis.set_label_text('soltice_yf') ax0.xaxis.set_label_text('sin(alpha)') ax0.legend() plt.show() plot_model1_rms(raw_training_data, m1_alpha, m1_sf) import scipy.optimize as opt import sympy as sy from sympy.utilities.lambdify import lambdify y_n, phi_n, psi_n = sy.symbols('y_n phi_n psi_n') ## data u = sy.symbols('u') ## params m1b_p_n = sy.tan(phi_n) * sy.tan(sy.asin(u * sy.cos(psi_n))) ## the prediction m1b_e_n = (y_n - m1b_p_n)**2 ## squared error def model1b_feature_matrix(raw_data): rows = [] for m in raw_data: lat, lng = m['pos'] dl_s = parse_daylight_s(m['daylight']) year_f = parse_year_fraction(m['date'], lng) psi = (year_f - june21_yf) * (2 * np.pi) phi = lat * np.pi / 180 night_fraction = (1 - dl_s / day_duration_s) y = np.cos(np.pi * night_fraction) rows.append([phi, psi, y]) return np.array(rows)
def test_tsolve(): assert solve(exp(x) - 3, x) == [log(3)] assert set(solve((a * x + b) * (exp(x) - 3), x)) == set([-b / a, log(3)]) assert solve(cos(x) - y, x) == [acos(y)] assert solve(2 * cos(x) - y, x) == [acos(y / 2)] assert set(solve(Eq(cos(x), sin(x)), x)) == set([-3 * pi / 4, pi / 4]) assert set(solve(exp(x) + exp(-x) - y, x)) in [ set([ log(y / 2 - sqrt(y**2 - 4) / 2), log(y / 2 + sqrt(y**2 - 4) / 2), ]), set([ log(y - sqrt(y**2 - 4)) - log(2), log(y + sqrt(y**2 - 4)) - log(2) ]), set([ log(y / 2 - sqrt((y - 2) * (y + 2)) / 2), log(y / 2 + sqrt((y - 2) * (y + 2)) / 2) ]) ] assert solve(exp(x) - 3, x) == [log(3)] assert solve(Eq(exp(x), 3), x) == [log(3)] assert solve(log(x) - 3, x) == [exp(3)] assert solve(sqrt(3 * x) - 4, x) == [Rational(16, 3)] assert solve(3**(x + 2), x) == [] assert solve(3**(2 - x), x) == [] assert solve(x + 2**x, x) == [-LambertW(log(2)) / log(2)] ans = solve(3 * x + 5 + 2**(-5 * x + 3), x) assert len(ans) == 1 and ans[0].expand() == \ -Rational(5, 3) + LambertW(-10240*2**(S(1)/3)*log(2)/3)/(5*log(2)) assert solve(5*x - 1 + 3*exp(2 - 7*x), x) == \ [Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7] assert solve(2*x + 5 + log(3*x - 2), x) == \ [Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2] assert solve(3 * x + log(4 * x), x) == [LambertW(Rational(3, 4)) / 3] assert set(solve((2 * x + 8) * (8 + exp(x)), x)) == set([S(-4), log(8) + pi * I]) eq = 2 * exp(3 * x + 4) - 3 ans = solve(eq, x) # this generated a failure in flatten assert len(ans) == 3 and all(eq.subs(x, a).n(chop=True) == 0 for a in ans) assert solve(2 * log(3 * x + 4) - 3, x) == [(exp(Rational(3, 2)) - 4) / 3] assert solve(exp(x) + 1, x) == [pi * I] assert solve(x**2 - 2**x, x) == [2] assert solve(x**3 - 3**x, x) == [-3 * LambertW(-log(3) / 3) / log(3)] A = -7 * 2**Rational(4, 5) * 6**Rational(1, 5) * log(7) / 10 B = -7 * 3**Rational(1, 5) * log(7) / 5 result = solve(2 * (3 * x + 4)**5 - 6 * 7**(3 * x + 9), x) assert len(result) == 1 and expand(result[0]) in [ Rational(-4, 3) - 5 / log(7) / 3 * LambertW(A), Rational(-4, 3) - 5 / log(7) / 3 * LambertW(B), ] assert solve(z * cos(x) - y, x) == [acos(y / z)] assert solve(z * cos(2 * x) - y, x) == [acos(y / z) / 2] assert solve(z * cos(sin(x)) - y, x) == [asin(acos(y / z))] assert solve(z * cos(x), x) == [acos(0)] # issue #1409 assert solve(y - b * x / (a + x), x) in [[-a * y / (y - b)], [a * y / (b - y)]] assert solve(y - b * exp(a / x), x) == [a / log(y / b)] # issue #1408 assert solve(y - b / (1 + a * x), x) in [[(b - y) / (a * y)], [-((y - b) / (a * y))]] # issue #1407 assert solve(y - a * x**b, x) == [(y / a)**(1 / b)] # issue #1406 assert solve(z**x - y, x) == [log(y) / log(z)] # issue #1405 assert solve(2**x - 10, x) == [log(10) / log(2)] # issue #3645 assert solve(x * y) == [{x: 0}, {y: 0}] assert solve([x * y]) == [{x: 0}, {y: 0}] assert solve(x**y - 1) == [{x: 1}, {y: 0}] assert solve([x**y - 1]) == [{x: 1}, {y: 0}] assert solve(x * y * (x**2 - y**2)) == [{x: 0}, {x: -y}, {x: y}, {y: 0}] assert solve([x * y * (x**2 - y**2)]) == [{x: 0}, {x: -y}, {x: y}, {y: 0}] #issue #1640 assert solve(exp(log(5) * x) - 2**x, x) == [0]
def test_issue_4492(): assert simplify(integrate(x**2 * sqrt(5 - x**2), x)) == Piecewise( (I*(2*x**5 - 15*x**3 + 25*x - 25*sqrt(x**2 - 5)*acosh(sqrt(5)*x/5)) / (8*sqrt(x**2 - 5)), 1 < Abs(x**2)/5), ((-2*x**5 + 15*x**3 - 25*x + 25*sqrt(-x**2 + 5)*asin(sqrt(5)*x/5)) / (8*sqrt(-x**2 + 5)), True))
def test_plane(): x, y, z, u, v = symbols('x y z u v', real=True) p1 = Point3D(0, 0, 0) p2 = Point3D(1, 1, 1) p3 = Point3D(1, 2, 3) pl3 = Plane(p1, p2, p3) pl4 = Plane(p1, normal_vector=(1, 1, 1)) pl4b = Plane(p1, p2) pl5 = Plane(p3, normal_vector=(1, 2, 3)) pl6 = Plane(Point3D(2, 3, 7), normal_vector=(2, 2, 2)) pl7 = Plane(Point3D(1, -5, -6), normal_vector=(1, -2, 1)) pl8 = Plane(p1, normal_vector=(0, 0, 1)) pl9 = Plane(p1, normal_vector=(0, 12, 0)) pl10 = Plane(p1, normal_vector=(-2, 0, 0)) pl11 = Plane(p2, normal_vector=(0, 0, 1)) l1 = Line3D(Point3D(5, 0, 0), Point3D(1, -1, 1)) l2 = Line3D(Point3D(0, -2, 0), Point3D(3, 1, 1)) l3 = Line3D(Point3D(0, -1, 0), Point3D(5, -1, 9)) assert Plane(p1, p2, p3) != Plane(p1, p3, p2) assert Plane(p1, p2, p3).is_coplanar(Plane(p1, p3, p2)) assert pl3 == Plane(Point3D(0, 0, 0), normal_vector=(1, -2, 1)) assert pl3 != pl4 assert pl4 == pl4b assert pl5 == Plane(Point3D(1, 2, 3), normal_vector=(1, 2, 3)) assert pl5.equation(x, y, z) == x + 2*y + 3*z - 14 assert pl3.equation(x, y, z) == x - 2*y + z assert pl3.p1 == p1 assert pl4.p1 == p1 assert pl5.p1 == p3 assert pl4.normal_vector == (1, 1, 1) assert pl5.normal_vector == (1, 2, 3) assert p1 in pl3 assert p1 in pl4 assert p3 in pl5 assert pl3.projection(Point(0, 0)) == p1 p = pl3.projection(Point3D(1, 1, 0)) assert p == Point3D(7/6, 2/3, 1/6) assert p in pl3 l = pl3.projection_line(Line(Point(0, 0), Point(1, 1))) assert l == Line3D(Point3D(0, 0, 0), Point3D(7/6, 2/3, 1/6)) assert l in pl3 # get a segment that does not intersect the plane which is also # parallel to pl3's normal veector t = Dummy() r = pl3.random_point() a = pl3.perpendicular_line(r).arbitrary_point(t) s = Segment3D(a.subs(t, 1), a.subs(t, 2)) assert s.p1 not in pl3 and s.p2 not in pl3 assert pl3.projection_line(s).equals(r) assert pl3.projection_line(Segment(Point(1, 0), Point(1, 1))) == \ Segment3D(Point3D(5/6, 1/3, -1/6), Point3D(7/6, 2/3, 1/6)) assert pl6.projection_line(Ray(Point(1, 0), Point(1, 1))) == \ Ray3D(Point3D(14/3, 11/3, 11/3), Point3D(13/3, 13/3, 10/3)) assert pl3.perpendicular_line(r.args) == pl3.perpendicular_line(r) assert pl3.is_parallel(pl6) is False assert pl4.is_parallel(pl6) assert pl6.is_parallel(l1) is False assert pl3.is_perpendicular(pl6) assert pl4.is_perpendicular(pl7) assert pl6.is_perpendicular(pl7) assert pl6.is_perpendicular(l1) is False assert pl6.distance(pl6.arbitrary_point(u, v)) == 0 assert pl7.distance(pl7.arbitrary_point(u, v)) == 0 assert pl6.distance(pl6.arbitrary_point(t)) == 0 assert pl7.distance(pl7.arbitrary_point(t)) == 0 assert pl6.p1.distance(pl6.arbitrary_point(t)).simplify() == 1 assert pl7.p1.distance(pl7.arbitrary_point(t)).simplify() == 1 assert pl3.arbitrary_point(t) == Point3D(-sqrt(30)*sin(t)/30 + \ 2*sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/15 + sqrt(5)*cos(t)/5, sqrt(30)*sin(t)/6) assert pl3.arbitrary_point(u, v) == Point3D(2*u - v, u + 2*v, 5*v) assert pl7.distance(Point3D(1, 3, 5)) == 5*sqrt(6)/6 assert pl6.distance(Point3D(0, 0, 0)) == 4*sqrt(3) assert pl6.distance(pl6.p1) == 0 assert pl7.distance(pl6) == 0 assert pl7.distance(l1) == 0 assert pl6.distance(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == 0 pl6.distance(Plane(Point3D(5, 5, 5), normal_vector=(8, 8, 8))) == sqrt(3) assert pl6.angle_between(pl3) == pi/2 assert pl6.angle_between(pl6) == 0 assert pl6.angle_between(pl4) == 0 assert pl7.angle_between(Line3D(Point3D(2, 3, 5), Point3D(2, 4, 6))) == \ -asin(sqrt(3)/6) assert pl6.angle_between(Ray3D(Point3D(2, 4, 1), Point3D(6, 5, 3))) == \ asin(sqrt(7)/3) assert pl7.angle_between(Segment3D(Point3D(5, 6, 1), Point3D(1, 2, 4))) == \ asin(7*sqrt(246)/246) assert are_coplanar(l1, l2, l3) is False assert are_coplanar(l1) is False assert are_coplanar(Point3D(2, 7, 2), Point3D(0, 0, 2), Point3D(1, 1, 2), Point3D(1, 2, 2)) assert are_coplanar(Plane(p1, p2, p3), Plane(p1, p3, p2)) assert Plane.are_concurrent(pl3, pl4, pl5) is False assert Plane.are_concurrent(pl6) is False raises(ValueError, lambda: Plane.are_concurrent(Point3D(0, 0, 0))) raises(ValueError, lambda: Plane((1, 2, 3), normal_vector=(0, 0, 0))) assert pl3.parallel_plane(Point3D(1, 2, 5)) == Plane(Point3D(1, 2, 5), \ normal_vector=(1, -2, 1)) # perpendicular_plane p = Plane((0, 0, 0), (1, 0, 0)) # default assert p.perpendicular_plane() == Plane(Point3D(0, 0, 0), (0, 1, 0)) # 1 pt assert p.perpendicular_plane(Point3D(1, 0, 1)) == \ Plane(Point3D(1, 0, 1), (0, 1, 0)) # pts as tuples assert p.perpendicular_plane((1, 0, 1), (1, 1, 1)) == \ Plane(Point3D(1, 0, 1), (0, 0, -1)) a, b = Point3D(0, 0, 0), Point3D(0, 1, 0) Z = (0, 0, 1) p = Plane(a, normal_vector=Z) # case 4 assert p.perpendicular_plane(a, b) == Plane(a, (1, 0, 0)) n = Point3D(*Z) # case 1 assert p.perpendicular_plane(a, n) == Plane(a, (-1, 0, 0)) # case 2 assert Plane(a, normal_vector=b.args).perpendicular_plane(a, a + b) == \ Plane(Point3D(0, 0, 0), (1, 0, 0)) # case 1&3 assert Plane(b, normal_vector=Z).perpendicular_plane(b, b + n) == \ Plane(Point3D(0, 1, 0), (-1, 0, 0)) # case 2&3 assert Plane(b, normal_vector=b.args).perpendicular_plane(n, n + b) == \ Plane(Point3D(0, 0, 1), (1, 0, 0)) assert pl6.intersection(pl6) == [pl6] assert pl4.intersection(pl4.p1) == [pl4.p1] assert pl3.intersection(pl6) == [ Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6))] assert pl3.intersection(Line3D(Point3D(1,2,4), Point3D(4,4,2))) == [ Point3D(2, 8/3, 10/3)] assert pl3.intersection(Plane(Point3D(6, 0, 0), normal_vector=(2, -5, 3)) ) == [Line3D(Point3D(-24, -12, 0), Point3D(-25, -13, -1))] assert pl6.intersection(Ray3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [ Point3D(-1, 3, 10)] assert pl6.intersection(Segment3D(Point3D(2, 3, 1), Point3D(1, 3, 4))) == [ Point3D(-1, 3, 10)] assert pl7.intersection(Line(Point(2, 3), Point(4, 2))) == [ Point3D(13/2, 3/4, 0)] r = Ray(Point(2, 3), Point(4, 2)) assert Plane((1,2,0), normal_vector=(0,0,1)).intersection(r) == [ Ray3D(Point(2, 3), Point(4, 2))] assert pl9.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, 0))] assert pl10.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(0, 2, 1))] assert pl4.intersection(pl8) == [Line3D(Point3D(0, 0, 0), Point3D(1, -1, 0))] assert pl11.intersection(pl8) == [] assert pl9.intersection(pl11) == [Line3D(Point3D(0, 0, 1), Point3D(12, 0, 1))] assert pl9.intersection(pl4) == [Line3D(Point3D(0, 0, 0), Point3D(12, 0, -12))] assert pl3.random_point() in pl3 # test geometrical entity using equals assert pl4.intersection(pl4.p1)[0].equals(pl4.p1) assert pl3.intersection(pl6)[0].equals(Line3D(Point3D(8, 4, 0), Point3D(2, 4, 6))) pl8 = Plane((1, 2, 0), normal_vector=(0, 0, 1)) assert pl8.intersection(Line3D(p1, (1, 12, 0)))[0].equals(Line((0, 0, 0), (0.1, 1.2, 0))) assert pl8.intersection(Ray3D(p1, (1, 12, 0)))[0].equals(Ray((0, 0, 0), (1, 12, 0))) assert pl8.intersection(Segment3D(p1, (21, 1, 0)))[0].equals(Segment3D(p1, (21, 1, 0))) assert pl8.intersection(Plane(p1, normal_vector=(0, 0, 112)))[0].equals(pl8) assert pl8.intersection(Plane(p1, normal_vector=(0, 12, 0)))[0].equals( Line3D(p1, direction_ratio=(112 * pi, 0, 0))) assert pl8.intersection(Plane(p1, normal_vector=(11, 0, 1)))[0].equals( Line3D(p1, direction_ratio=(0, -11, 0))) assert pl8.intersection(Plane(p1, normal_vector=(1, 0, 11)))[0].equals( Line3D(p1, direction_ratio=(0, 11, 0))) assert pl8.intersection(Plane(p1, normal_vector=(-1, -1, -11)))[0].equals( Line3D(p1, direction_ratio=(1, -1, 0))) assert pl3.random_point() in pl3 assert len(pl8.intersection(Ray3D(Point3D(0, 2, 3), Point3D(1, 0, 3)))) is 0 # check if two plane are equals assert pl6.intersection(pl6)[0].equals(pl6) assert pl8.equals(Plane(p1, normal_vector=(0, 12, 0))) is False assert pl8.equals(pl8) assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12))) assert pl8.equals(Plane(p1, normal_vector=(0, 0, -12*sqrt(3)))) # issue 8570 l2 = Line3D(Point3D(S(50000004459633)/5000000000000, -S(891926590718643)/1000000000000000, S(231800966893633)/100000000000000), Point3D(S(50000004459633)/50000000000000, -S(222981647679771)/250000000000000, S(231800966893633)/100000000000000)) p2 = Plane(Point3D(S(402775636372767)/100000000000000, -S(97224357654973)/100000000000000, S(216793600814789)/100000000000000), (-S('9.00000087501922'), -S('4.81170658872543e-13'), S('0.0'))) assert str([i.n(2) for i in p2.intersection(l2)]) == \ '[Point3D(4.0, -0.89, 2.3)]'
def test_latex_functions(): assert latex(exp(x)) == "e^{x}" assert latex(exp(1) + exp(2)) == "e + e^{2}" f = Function('f') assert latex(f(x)) == '\\operatorname{f}{\\left (x \\right )}' beta = Function('beta') assert latex(beta(x)) == r"\beta{\left (x \right )}" assert latex(sin(x)) == r"\sin{\left (x \right )}" assert latex(sin(x), fold_func_brackets=True) == r"\sin {x}" assert latex(sin(2*x**2), fold_func_brackets=True) == \ r"\sin {2 x^{2}}" assert latex(sin(x**2), fold_func_brackets=True) == \ r"\sin {x^{2}}" assert latex(asin(x)**2) == r"\operatorname{asin}^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="full") == \ r"\arcsin^{2}{\left (x \right )}" assert latex(asin(x)**2, inv_trig_style="power") == \ r"\sin^{-1}{\left (x \right )}^{2}" assert latex(asin(x**2), inv_trig_style="power", fold_func_brackets=True) == \ r"\sin^{-1} {x^{2}}" assert latex(factorial(k)) == r"k!" assert latex(factorial(-k)) == r"\left(- k\right)!" assert latex(subfactorial(k)) == r"!k" assert latex(subfactorial(-k)) == r"!\left(- k\right)" assert latex(factorial2(k)) == r"k!!" assert latex(factorial2(-k)) == r"\left(- k\right)!!" assert latex(binomial(2, k)) == r"{\binom{2}{k}}" assert latex(FallingFactorial(3, k)) == r"{\left(3\right)}_{\left(k\right)}" assert latex(RisingFactorial(3, k)) == r"{\left(3\right)}^{\left(k\right)}" assert latex(floor(x)) == r"\lfloor{x}\rfloor" assert latex(ceiling(x)) == r"\lceil{x}\rceil" assert latex(Min(x, 2, x**3)) == r"\min\left(2, x, x^{3}\right)" assert latex(Min(x, y)**2) == r"\min\left(x, y\right)^{2}" assert latex(Max(x, 2, x**3)) == r"\max\left(2, x, x^{3}\right)" assert latex(Max(x, y)**2) == r"\max\left(x, y\right)^{2}" assert latex(Abs(x)) == r"\lvert{x}\rvert" assert latex(re(x)) == r"\Re{x}" assert latex(re(x + y)) == r"\Re{x} + \Re{y}" assert latex(im(x)) == r"\Im{x}" assert latex(conjugate(x)) == r"\overline{x}" assert latex(gamma(x)) == r"\Gamma\left(x\right)" assert latex(Order(x)) == r"\mathcal{O}\left(x\right)" assert latex(lowergamma(x, y)) == r'\gamma\left(x, y\right)' assert latex(uppergamma(x, y)) == r'\Gamma\left(x, y\right)' assert latex(cot(x)) == r'\cot{\left (x \right )}' assert latex(coth(x)) == r'\coth{\left (x \right )}' assert latex(re(x)) == r'\Re{x}' assert latex(im(x)) == r'\Im{x}' assert latex(root(x, y)) == r'x^{\frac{1}{y}}' assert latex(arg(x)) == r'\arg{\left (x \right )}' assert latex(zeta(x)) == r'\zeta\left(x\right)' assert latex(zeta(x)) == r"\zeta\left(x\right)" assert latex(zeta(x)**2) == r"\zeta^{2}\left(x\right)" assert latex(zeta(x, y)) == r"\zeta\left(x, y\right)" assert latex(zeta(x, y)**2) == r"\zeta^{2}\left(x, y\right)" assert latex(dirichlet_eta(x)) == r"\eta\left(x\right)" assert latex(dirichlet_eta(x)**2) == r"\eta^{2}\left(x\right)" assert latex(polylog(x, y)) == r"\operatorname{Li}_{x}\left(y\right)" assert latex(polylog(x, y)**2) == r"\operatorname{Li}_{x}^{2}\left(y\right)" assert latex(lerchphi(x, y, n)) == r"\Phi\left(x, y, n\right)" assert latex(lerchphi(x, y, n)**2) == r"\Phi^{2}\left(x, y, n\right)" assert latex(Ei(x)) == r'\operatorname{Ei}{\left (x \right )}' assert latex(Ei(x)**2) == r'\operatorname{Ei}^{2}{\left (x \right )}' assert latex(expint(x, y)**2) == r'\operatorname{E}_{x}^{2}\left(y\right)' assert latex(Shi(x)**2) == r'\operatorname{Shi}^{2}{\left (x \right )}' assert latex(Si(x)**2) == r'\operatorname{Si}^{2}{\left (x \right )}' assert latex(Ci(x)**2) == r'\operatorname{Ci}^{2}{\left (x \right )}' assert latex(Chi(x)**2) == r'\operatorname{Chi}^{2}{\left (x \right )}' assert latex(jacobi(n, a, b, x)) == r'P_{n}^{\left(a,b\right)}\left(x\right)' assert latex(jacobi( n, a, b, x)**2) == r'\left(P_{n}^{\left(a,b\right)}\left(x\right)\right)^{2}' assert latex(gegenbauer(n, a, x)) == r'C_{n}^{\left(a\right)}\left(x\right)' assert latex(gegenbauer( n, a, x)**2) == r'\left(C_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(chebyshevt(n, x)) == r'T_{n}\left(x\right)' assert latex(chebyshevt(n, x)**2) == r'\left(T_{n}\left(x\right)\right)^{2}' assert latex(chebyshevu(n, x)) == r'U_{n}\left(x\right)' assert latex(chebyshevu(n, x)**2) == r'\left(U_{n}\left(x\right)\right)^{2}' assert latex(legendre(n, x)) == r'P_{n}\left(x\right)' assert latex(legendre(n, x)**2) == r'\left(P_{n}\left(x\right)\right)^{2}' assert latex(assoc_legendre(n, a, x)) == r'P_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_legendre( n, a, x)**2) == r'\left(P_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(laguerre(n, x)) == r'L_{n}\left(x\right)' assert latex(laguerre(n, x)**2) == r'\left(L_{n}\left(x\right)\right)^{2}' assert latex(assoc_laguerre(n, a, x)) == r'L_{n}^{\left(a\right)}\left(x\right)' assert latex(assoc_laguerre( n, a, x)**2) == r'\left(L_{n}^{\left(a\right)}\left(x\right)\right)^{2}' assert latex(hermite(n, x)) == r'H_{n}\left(x\right)' assert latex(hermite(n, x)**2) == r'\left(H_{n}\left(x\right)\right)^{2}' # Test latex printing of function names with "_" assert latex( polar_lift(0)) == r"\operatorname{polar\_lift}{\left (0 \right )}" assert latex(polar_lift(0)** 3) == r"\operatorname{polar\_lift}^{3}{\left (0 \right )}"
#For Paper thisIsOne = sp.simplify( ((eqnX / eqnr)**2 + (eqnY / eqnr)**2 + (eqnZ / eqnr)**2)) thetaEquation = sp.simplify(eqnY / eqnX) FORPAPER_eqnS_eqnr = sp.simplify(sp.sqrt((eqnX / eqnr)**2. + (eqnY / eqnr)**2.)) #s eqnS = sp.sqrt(eqnX**2. + eqnY**2.) eqnSAlpha = a * sp.sin(alpha * np.pi / 180.) #alpha in deg eqnAlpha1 = sp.acos( eqnZ / sp.sqrt(eqnX**2. + eqnY**2. + eqnZ**2.)) * 180. / np.pi eqnAlpha2 = sp.asin( eqnS / sp.sqrt(eqnX**2. + eqnY**2. + eqnZ**2.)) * 180. / np.pi print('Alpha') print(eqnAlpha1) #dmag eqnDmagInside = p * (R / a)**2. * Phi eqnDmag = -2.5 * sp.log(eqnDmagInside, 10) #### PHASE FUNCTIONS ############## #LAMBERT eqnLAMBERT = ( sp.sin(alpha * np.pi / 180.) + (np.pi - alpha * np.pi / 180.) * sp.cos(alpha * np.pi / 180.)) / np.pi #START, STOP eqnSTART = 0.5 + 0.5 * sp.tanh((alpha - A) / B)
def test_invert_real(): x = Symbol('x', real=True) y = Symbol('y') n = Symbol('n') def ireal(x, s=S.Reals): return Intersection(s, x) minus_n = Intersection(Interval(-oo, 0), FiniteSet(-n)) plus_n = Intersection(Interval(0, oo), FiniteSet(n)) assert solveset(abs(x) - n, x, S.Reals) == Union(minus_n, plus_n) assert invert_real(exp(x), y, x) == (x, ireal(FiniteSet(log(y)))) y = Symbol('y', positive=True) n = Symbol('n', real=True) assert invert_real(x + 3, y, x) == (x, FiniteSet(y - 3)) assert invert_real(x * 3, y, x) == (x, FiniteSet(y / 3)) assert invert_real(exp(x), y, x) == (x, FiniteSet(log(y))) assert invert_real(exp(3 * x), y, x) == (x, FiniteSet(log(y) / 3)) assert invert_real(exp(x + 3), y, x) == (x, FiniteSet(log(y) - 3)) assert invert_real(exp(x) + 3, y, x) == (x, ireal(FiniteSet(log(y - 3)))) assert invert_real(exp(x) * 3, y, x) == (x, FiniteSet(log(y / 3))) assert invert_real(log(x), y, x) == (x, FiniteSet(exp(y))) assert invert_real(log(3 * x), y, x) == (x, FiniteSet(exp(y) / 3)) assert invert_real(log(x + 3), y, x) == (x, FiniteSet(exp(y) - 3)) minus_y = Intersection(Interval(-oo, 0), FiniteSet(-y)) plus_y = Intersection(Interval(0, oo), FiniteSet(y)) assert invert_real(Abs(x), y, x) == (x, Union(minus_y, plus_y)) assert invert_real(2**x, y, x) == (x, FiniteSet(log(y) / log(2))) assert invert_real(2**exp(x), y, x) == (x, ireal(FiniteSet(log(log(y) / log(2))))) assert invert_real(x**2, y, x) == (x, FiniteSet(sqrt(y), -sqrt(y))) assert invert_real(x**Rational(1, 2), y, x) == (x, FiniteSet(y**2)) raises(ValueError, lambda: invert_real(x, x, x)) raises(ValueError, lambda: invert_real(x**pi, y, x)) raises(ValueError, lambda: invert_real(S.One, y, x)) assert invert_real(x**31 + x, y, x) == (x**31 + x, FiniteSet(y)) y_1 = Intersection(Interval(-1, oo), FiniteSet(y - 1)) y_2 = Intersection(Interval(-oo, -1), FiniteSet(-y - 1)) assert invert_real(Abs(x**31 + x + 1), y, x) == (x**31 + x, Union(y_1, y_2)) assert invert_real(sin(x), y, x) == \ (x, imageset(Lambda(n, n*pi + (-1)**n*asin(y)), S.Integers)) assert invert_real(sin(exp(x)), y, x) == \ (x, imageset(Lambda(n, log((-1)**n*asin(y) + n*pi)), S.Integers)) assert invert_real(csc(x), y, x) == \ (x, imageset(Lambda(n, n*pi + (-1)**n*acsc(y)), S.Integers)) assert invert_real(csc(exp(x)), y, x) == \ (x, imageset(Lambda(n, log((-1)**n*acsc(y) + n*pi)), S.Integers)) assert invert_real(cos(x), y, x) == \ (x, Union(imageset(Lambda(n, 2*n*pi + acos(y)), S.Integers), \ imageset(Lambda(n, 2*n*pi - acos(y)), S.Integers))) assert invert_real(cos(exp(x)), y, x) == \ (x, Union(imageset(Lambda(n, log(2*n*pi + acos(y))), S.Integers), \ imageset(Lambda(n, log(2*n*pi - acos(y))), S.Integers))) assert invert_real(sec(x), y, x) == \ (x, Union(imageset(Lambda(n, 2*n*pi + asec(y)), S.Integers), \ imageset(Lambda(n, 2*n*pi - asec(y)), S.Integers))) assert invert_real(sec(exp(x)), y, x) == \ (x, Union(imageset(Lambda(n, log(2*n*pi + asec(y))), S.Integers), \ imageset(Lambda(n, log(2*n*pi - asec(y))), S.Integers))) assert invert_real(tan(x), y, x) == \ (x, imageset(Lambda(n, n*pi + atan(y)), S.Integers)) assert invert_real(tan(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + atan(y))), S.Integers)) assert invert_real(cot(x), y, x) == \ (x, imageset(Lambda(n, n*pi + acot(y)), S.Integers)) assert invert_real(cot(exp(x)), y, x) == \ (x, imageset(Lambda(n, log(n*pi + acot(y))), S.Integers)) assert invert_real(tan(tan(x)), y, x) == \ (tan(x), imageset(Lambda(n, n*pi + atan(y)), S.Integers)) x = Symbol('x', positive=True) assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1 / pi))) # Test for ``set_h`` containing information about the domain n = Dummy('n') x = Symbol('x') h1 = Intersection(Interval(-oo, -3), FiniteSet(-a + b - 3), imageset(Lambda(n, n - a - 3), Interval(0, oo))) h2 = Intersection(Interval(-3, oo), FiniteSet(a - b - 3), imageset(Lambda(n, -n + a - 3), Interval(0, oo))) assert invert_real(Abs(Abs(x + 3) - a) - b, 0, x) == (x, Union(h1, h2))
def test_sin(): x, y = symbols('x,y') r = Symbol('r', real=True) k = Symbol('k', integer=True) assert sin(nan) == nan assert sin(oo * I) == oo * I assert sin(-oo * I) == -oo * I assert sin(oo).args[0] == oo assert sin(0) == 0 assert sin(asin(x)) == x assert sin(atan(x)) == x / sqrt(1 + x**2) assert sin(acos(x)) == sqrt(1 - x**2) assert sin(acot(x)) == 1 / (sqrt(1 + 1 / x**2) * x) assert sin(pi * I) == sinh(pi) * I assert sin(-pi * I) == -sinh(pi) * I assert sin(-2 * I) == -sinh(2) * I assert sin(pi) == 0 assert sin(-pi) == 0 assert sin(2 * pi) == 0 assert sin(-2 * pi) == 0 assert sin(-3 * 10**73 * pi) == 0 assert sin(7 * 10**103 * pi) == 0 assert sin(pi / 2) == 1 assert sin(-pi / 2) == -1 assert sin(5 * pi / 2) == 1 assert sin(7 * pi / 2) == -1 assert sin(pi / 3) == S.Half * sqrt(3) assert sin(-2 * pi / 3) == -S.Half * sqrt(3) assert sin(pi / 4) == S.Half * sqrt(2) assert sin(-pi / 4) == -S.Half * sqrt(2) assert sin(17 * pi / 4) == S.Half * sqrt(2) assert sin(-3 * pi / 4) == -S.Half * sqrt(2) assert sin(pi / 6) == S.Half assert sin(-pi / 6) == -S.Half assert sin(7 * pi / 6) == -S.Half assert sin(-5 * pi / 6) == -S.Half assert sin(1 * pi / 5) == sqrt((5 - sqrt(5)) / 8) assert sin(2 * pi / 5) == sqrt((5 + sqrt(5)) / 8) assert sin(3 * pi / 5) == sin(2 * pi / 5) assert sin(4 * pi / 5) == sin(1 * pi / 5) assert sin(6 * pi / 5) == -sin(1 * pi / 5) assert sin(8 * pi / 5) == -sin(2 * pi / 5) assert sin(-1273 * pi / 5) == -sin(2 * pi / 5) assert sin(104 * pi / 105) == sin(pi / 105) assert sin(106 * pi / 105) == -sin(pi / 105) assert sin(-104 * pi / 105) == -sin(pi / 105) assert sin(-106 * pi / 105) == sin(pi / 105) assert sin(x * I) == sinh(x) * I assert sin(k * pi) == 0 assert sin(17 * k * pi) == 0 assert sin(k * pi * I) == sinh(k * pi) * I assert sin(r).is_real == True assert isinstance(sin(re(x) - im(y)), sin) == True assert isinstance(sin(-re(x) + im(y)), sin) == False
def test_intrinsic_math_codegen(): # not included: log10 from sympy import (acos, asin, atan, ceiling, cos, cosh, floor, log, ln, sin, sinh, sqrt, tan, tanh, Abs) x = symbols('x') name_expr = [ ("test_abs", Abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), # ("test_ceil", ceiling(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), # ("test_floor", floor(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] result = codegen(name_expr, "F95", "file", header=False, empty=False) assert result[0][0] == "file.f90" expected = ('REAL*8 function test_abs(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_abs = Abs(x)\n' 'end function\n' 'REAL*8 function test_acos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_acos = acos(x)\n' 'end function\n' 'REAL*8 function test_asin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_asin = asin(x)\n' 'end function\n' 'REAL*8 function test_atan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_atan = atan(x)\n' 'end function\n' 'REAL*8 function test_cos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_cos = cos(x)\n' 'end function\n' 'REAL*8 function test_cosh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_cosh = cosh(x)\n' 'end function\n' 'REAL*8 function test_log(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_log = log(x)\n' 'end function\n' 'REAL*8 function test_ln(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_ln = log(x)\n' 'end function\n' 'REAL*8 function test_sin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sin = sin(x)\n' 'end function\n' 'REAL*8 function test_sinh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sinh = sinh(x)\n' 'end function\n' 'REAL*8 function test_sqrt(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sqrt = sqrt(x)\n' 'end function\n' 'REAL*8 function test_tan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_tan = tan(x)\n' 'end function\n' 'REAL*8 function test_tanh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_tanh = tanh(x)\n' 'end function\n') assert result[0][1] == expected assert result[1][0] == "file.h" expected = ('interface\n' 'REAL*8 function test_abs(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_acos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_asin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_atan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_cos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_cosh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_log(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_ln(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sinh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sqrt(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_tan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_tanh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n') assert result[1][1] == expected
def test_f3(): a = Symbol('a') #issue 405 assert limit(asin(a * x) / x, x, 0) == a
ddc_dzdz = dcdz.jacobian(z) # make functions mass_f = sp.lambdify(((z),(par),), mass) coriolisAndPot_f = sp.lambdify(((z),(zt),(par),), coriolisAndPot) dcdz_f = sp.lambdify(((z),(par),), dcdz) ddc_dzdz_f = sp.lambdify(((z),(par),), ddc_dzdz) Etot_f = sp.lambdify(((z),(zt),(par),), Etot) Ekm3_f = sp.lambdify(((z),(zt),(par),), Ekm3) massFree_f = sp.lambdify(((z[:2,:]),(par),), massFree) coriolisAndPotFree_f = sp.lambdify(((z[:2,:]),(zt[:2,:]),(par),), coriolisAndPotFree) # initial solution; th2 as a function of th1 such that m3 is on the floor th2Init = sp.asin(-(h + la * sp.sin(th1))/lb) th2Init_f = sp.lambdify(((th1),(par),), th2Init) c_f = sp.lambdify(((z),(par),), c) def solve_lam(z, zt, par): m_N = mass_f(z, par) mInv_N = np.linalg.inv(m_N) dcdz_N = dcdz_f(z, par) ddc_dzdz_N = ddc_dzdz_f(z, par) coriolisAndPot_N = coriolisAndPot_f(z, zt, par) den = np.matmul(np.matmul(dcdz_N.transpose(), mInv_N), dcdz_N) num1 = np.matmul(np.matmul(zt, ddc_dzdz_N), zt) num2 = np.matmul(np.matmul(dcdz_N.transpose(), mInv_N), coriolisAndPot_N) lam_N = (num1 - num2) / den return lam_N, m_N, coriolisAndPot_N, dcdz_N