def test_hyper_as_trig(): from sympy.simplify.fu import _osborne as o, _osbornei as i, TR12 eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2*x) e, f = hyper_as_trig(tanh(x + y)) assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1) d = Dummy() assert o(sinh(x), d) == I*sin(x*d) assert o(tanh(x), d) == I*tan(x*d) assert o(coth(x), d) == cot(x*d)/I assert o(cosh(x), d) == cos(x*d) assert o(sech(x), d) == sec(x*d) assert o(csch(x), d) == csc(x*d)/I for func in (sinh, cosh, tanh, coth, sech, csch): h = func(pi) assert i(o(h, d), d) == h # /!\ the _osborne functions are not meant to work # in the o(i(trig, d), d) direction so we just check # that they work as they are supposed to work assert i(cos(x*y + z), y) == cosh(x + z*I) assert i(sin(x*y + z), y) == sinh(x + z*I)/I assert i(tan(x*y + z), y) == tanh(x + z*I)/I assert i(cot(x*y + z), y) == coth(x + z*I)*I assert i(sec(x*y + z), y) == sech(x + z*I) assert i(csc(x*y + z), y) == csch(x + z*I)*I
def trig_substitution_rule(integral): integrand, symbol = integral A = sympy.Wild('a', exclude=[0, symbol]) B = sympy.Wild('b', exclude=[0, symbol]) theta = sympy.Dummy("theta") target_pattern = A + B*symbol**2 matches = integrand.find(target_pattern) for expr in matches: match = expr.match(target_pattern) a = match.get(A, ZERO) b = match.get(B, ZERO) a_positive = ((a.is_number and a > 0) or a.is_positive) b_positive = ((b.is_number and b > 0) or b.is_positive) a_negative = ((a.is_number and a < 0) or a.is_negative) b_negative = ((b.is_number and b < 0) or b.is_negative) x_func = None if a_positive and b_positive: # a**2 + b*x**2. Assume sec(theta) > 0, -pi/2 < theta < pi/2 x_func = (sympy.sqrt(a)/sympy.sqrt(b)) * sympy.tan(theta) # Do not restrict the domain: tan(theta) takes on any real # value on the interval -pi/2 < theta < pi/2 so x takes on # any value restriction = True elif a_positive and b_negative: # a**2 - b*x**2. Assume cos(theta) > 0, -pi/2 < theta < pi/2 constant = sympy.sqrt(a)/sympy.sqrt(-b) x_func = constant * sympy.sin(theta) restriction = sympy.And(symbol > -constant, symbol < constant) elif a_negative and b_positive: # b*x**2 - a**2. Assume sin(theta) > 0, 0 < theta < pi constant = sympy.sqrt(-a)/sympy.sqrt(b) x_func = constant * sympy.sec(theta) restriction = sympy.And(symbol > -constant, symbol < constant) if x_func: # Manually simplify sqrt(trig(theta)**2) to trig(theta) # Valid due to assumed domain restriction substitutions = {} for f in [sympy.sin, sympy.cos, sympy.tan, sympy.sec, sympy.csc, sympy.cot]: substitutions[sympy.sqrt(f(theta)**2)] = f(theta) substitutions[sympy.sqrt(f(theta)**(-2))] = 1/f(theta) replaced = integrand.subs(symbol, x_func).trigsimp() replaced = replaced.subs(substitutions) if not replaced.has(symbol): replaced *= manual_diff(x_func, theta) replaced = replaced.trigsimp() secants = replaced.find(1/sympy.cos(theta)) if secants: replaced = replaced.xreplace({ 1/sympy.cos(theta): sympy.sec(theta) }) substep = integral_steps(replaced, theta) if not contains_dont_know(substep): return TrigSubstitutionRule( theta, x_func, replaced, substep, restriction, integrand, symbol)
def trig_rule(integral): integrand, symbol = integral if isinstance(integrand, sympy.sin) or isinstance(integrand, sympy.cos): arg = integrand.args[0] if not isinstance(arg, sympy.Symbol): return # perhaps a substitution can deal with it if isinstance(integrand, sympy.sin): func = "sin" else: func = "cos" return TrigRule(func, arg, integrand, symbol) if integrand == sympy.sec(symbol) ** 2: return TrigRule("sec**2", symbol, integrand, symbol) elif integrand == sympy.csc(symbol) ** 2: return TrigRule("csc**2", symbol, integrand, symbol) if isinstance(integrand, sympy.tan): rewritten = sympy.sin(*integrand.args) / sympy.cos(*integrand.args) elif isinstance(integrand, sympy.cot): rewritten = sympy.cos(*integrand.args) / sympy.sin(*integrand.args) elif isinstance(integrand, sympy.sec): arg = integrand.args[0] rewritten = (sympy.sec(arg) ** 2 + sympy.tan(arg) * sympy.sec(arg)) / (sympy.sec(arg) + sympy.tan(arg)) elif isinstance(integrand, sympy.csc): arg = integrand.args[0] rewritten = (sympy.csc(arg) ** 2 + sympy.cot(arg) * sympy.csc(arg)) / (sympy.csc(arg) + sympy.cot(arg)) else: return return RewriteRule(rewritten, integral_steps(rewritten, symbol), integrand, symbol)
def test_periodicity(): x = Symbol('x') y = Symbol('y') assert periodicity(sin(2*x), x) == pi assert periodicity((-2)*tan(4*x), x) == pi/4 assert periodicity(sin(x)**2, x) == 2*pi assert periodicity(3**tan(3*x), x) == pi/3 assert periodicity(tan(x)*cos(x), x) == 2*pi assert periodicity(sin(x)**(tan(x)), x) == 2*pi assert periodicity(tan(x)*sec(x), x) == 2*pi assert periodicity(sin(2*x)*cos(2*x) - y, x) == pi/2 assert periodicity(tan(x) + cot(x), x) == pi assert periodicity(sin(x) - cos(2*x), x) == 2*pi assert periodicity(sin(x) - 1, x) == 2*pi assert periodicity(sin(4*x) + sin(x)*cos(x), x) == pi assert periodicity(exp(sin(x)), x) == 2*pi assert periodicity(log(cot(2*x)) - sin(cos(2*x)), x) == pi assert periodicity(sin(2*x)*exp(tan(x) - csc(2*x)), x) == pi assert periodicity(cos(sec(x) - csc(2*x)), x) == 2*pi assert periodicity(tan(sin(2*x)), x) == pi assert periodicity(2*tan(x)**2, x) == pi assert periodicity(sin(x)**2 + cos(x)**2, x) == S.Zero assert periodicity(tan(x), y) == S.Zero assert periodicity(exp(x), x) is None assert periodicity(log(x), x) is None assert periodicity(exp(x)**sin(x), x) is None assert periodicity(sin(x)**y, y) is None
def test_periodicity_check(): x = Symbol('x') y = Symbol('y') assert periodicity(tan(x), x, check=True) == pi assert periodicity(sin(x) + cos(x), x, check=True) == 2*pi assert periodicity(sec(x), x) == 2*pi assert periodicity(sin(x*y), x) == 2*pi/abs(y) assert periodicity(Abs(sec(sec(x))), x) == pi
def test_manualintegrate_trigonometry(): assert manualintegrate(sin(x), x) == -cos(x) assert manualintegrate(tan(x), x) == -log(cos(x)) assert manualintegrate(sec(x), x) == log(sec(x) + tan(x)) assert manualintegrate(csc(x), x) == -log(csc(x) + cot(x)) assert manualintegrate(sin(x) * cos(x), x) in [sin(x) ** 2 / 2, -cos(x)**2 / 2] assert manualintegrate(-sec(x) * tan(x), x) == -sec(x) assert manualintegrate(csc(x) * cot(x), x) == -csc(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_sech(): x, y = symbols('x, y') k = Symbol('k', integer=True) n = Symbol('n', positive=True) assert sech(nan) == nan assert sech(zoo) == nan assert sech(oo) == 0 assert sech(-oo) == 0 assert sech(0) == 1 assert sech(-1) == sech(1) assert sech(-x) == sech(x) assert sech(pi*I) == sec(pi) assert sech(-pi*I) == sec(pi) assert sech(-2**1024 * E) == sech(2**1024 * E) assert sech(pi*I/2) == zoo assert sech(-pi*I/2) == zoo assert sech((-3*10**73 + 1)*pi*I/2) == zoo assert sech((7*10**103 + 1)*pi*I/2) == zoo assert sech(pi*I) == -1 assert sech(-pi*I) == -1 assert sech(5*pi*I) == -1 assert sech(8*pi*I) == 1 assert sech(pi*I/3) == 2 assert sech(-2*pi*I/3) == -2 assert sech(pi*I/4) == sqrt(2) assert sech(-pi*I/4) == sqrt(2) assert sech(5*pi*I/4) == -sqrt(2) assert sech(-5*pi*I/4) == -sqrt(2) assert sech(pi*I/6) == 2/sqrt(3) assert sech(-pi*I/6) == 2/sqrt(3) assert sech(7*pi*I/6) == -2/sqrt(3) assert sech(-5*pi*I/6) == -2/sqrt(3) assert sech(pi*I/105) == 1/cos(pi/105) assert sech(-pi*I/105) == 1/cos(pi/105) assert sech(x*I) == 1/cos(x) assert sech(k*pi*I) == 1/cos(k*pi) assert sech(17*k*pi*I) == 1/cos(17*k*pi) assert sech(n).is_real is True
def test_periodicity(): x = Symbol('x') y = Symbol('y') assert periodicity(sin(2*x), x) == pi assert periodicity((-2)*tan(4*x), x) == pi/4 assert periodicity(sin(x)**2, x) == 2*pi assert periodicity(3**tan(3*x), x) == pi/3 assert periodicity(tan(x)*cos(x), x) == 2*pi assert periodicity(sin(x)**(tan(x)), x) == 2*pi assert periodicity(tan(x)*sec(x), x) == 2*pi assert periodicity(sin(2*x)*cos(2*x) - y, x) == pi/2 assert periodicity(tan(x) + cot(x), x) == pi assert periodicity(sin(x) - cos(2*x), x) == 2*pi assert periodicity(sin(x) - 1, x) == 2*pi assert periodicity(sin(4*x) + sin(x)*cos(x), x) == pi assert periodicity(exp(sin(x)), x) == 2*pi assert periodicity(log(cot(2*x)) - sin(cos(2*x)), x) == pi assert periodicity(sin(2*x)*exp(tan(x) - csc(2*x)), x) == pi assert periodicity(cos(sec(x) - csc(2*x)), x) == 2*pi assert periodicity(tan(sin(2*x)), x) == pi assert periodicity(2*tan(x)**2, x) == pi assert periodicity(sin(x%4), x) == 4 assert periodicity(sin(x)%4, x) == 2*pi assert periodicity(tan((3*x-2)%4), x) == 4/3 assert periodicity((sqrt(2)*(x+1)+x) % 3, x) == 3 / (sqrt(2)+1) assert periodicity((x**2+1) % x, x) == None assert periodicity(sin(x)**2 + cos(x)**2, x) == S.Zero assert periodicity(tan(x), y) == S.Zero assert periodicity(exp(x), x) is None assert periodicity(log(x), x) is None assert periodicity(exp(x)**sin(x), x) is None assert periodicity(sin(x)**y, y) is None assert periodicity(Abs(sin(Abs(sin(x)))),x) == pi assert all(periodicity(Abs(f(x)),x) == pi for f in ( cos, sin, sec, csc, tan, cot)) assert periodicity(Abs(sin(tan(x))), x) == pi assert periodicity(Abs(sin(sin(x) + tan(x))), x) == 2*pi assert periodicity(sin(x) > S.Half, x) is 2*pi assert periodicity(x > 2, x) is None assert periodicity(x**3 - x**2 + 1, x) is None assert periodicity(Abs(x), x) is None assert periodicity(Abs(x**2 - 1), x) is None assert periodicity((x**2 + 4)%2, x) is None assert periodicity((E**x)%3, x) is None
def test_manualintegrate_trigpowers(): assert manualintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3 assert manualintegrate(sin(x)**2 * cos(x) **2, x) == \ x / 8 - sin(4*x) / 32 assert manualintegrate(sin(x) * cos(x)**3, x) == -cos(x)**4 / 4 assert manualintegrate(sin(x)**3 * cos(x)**2, x) == \ cos(x)**5 / 5 - cos(x)**3 / 3 assert manualintegrate(tan(x)**3 * sec(x), x) == sec(x)**3/3 - sec(x) assert manualintegrate(tan(x) * sec(x) **2, x) == sec(x)**2/2 assert manualintegrate(cot(x)**5 * csc(x), x) == \ -csc(x)**5/5 + 2*csc(x)**3/3 - csc(x) assert manualintegrate(cot(x)**2 * csc(x)**6, x) == \ -cot(x)**7/7 - 2*cot(x)**5/5 - cot(x)**3/3
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_stationary_points(): x, y = symbols('x y') assert stationary_points(sin(x), x, Interval(-pi/2, pi/2) ) == {-pi/2, pi/2} assert stationary_points(sin(x), x, Interval.Ropen(0, pi/4) ) == EmptySet() assert stationary_points(tan(x), x, ) == EmptySet() assert stationary_points(sin(x)*cos(x), x, Interval(0, pi) ) == {pi/4, 3*pi/4} assert stationary_points(sec(x), x, Interval(0, pi) ) == {0, pi} assert stationary_points((x+3)*(x-2), x ) == FiniteSet(-S.Half) assert stationary_points((x + 3)/(x - 2), x, Interval(-5, 5) ) == EmptySet() assert stationary_points((x**2+3)/(x-2), x ) == {2 - sqrt(7), 2 + sqrt(7)} assert stationary_points((x**2+3)/(x-2), x, Interval(0, 5) ) == {2 + sqrt(7)} assert stationary_points(x**4 + x**3 - 5*x**2, x, S.Reals ) == FiniteSet(-2, 0, S(5)/4) assert stationary_points(exp(x), x ) == EmptySet() assert stationary_points(log(x) - x, x, S.Reals ) == {1} assert stationary_points(cos(x), x, Union(Interval(0, 5), Interval(-6, -3)) ) == {0, -pi, pi} assert stationary_points(y, x, S.Reals ) == S.Reals
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_periodicity_check(): x = Symbol('x') y = Symbol('y') assert periodicity(tan(x), x, check=True) == pi assert periodicity(sin(x) + cos(x), x, check=True) == 2*pi raises(NotImplementedError, lambda: periodicity(sec(x), x, check=True)) raises(NotImplementedError, lambda: periodicity(sin(x*y), x, check=True))
def eval_trig(func, arg, integrand, symbol): if func == 'sin': return -sympy.cos(arg) elif func == 'cos': return sympy.sin(arg) elif func == 'sec*tan': return sympy.sec(arg) elif func == 'csc*cot': return sympy.csc(arg)
def test_inverses(): raises(AttributeError, lambda: sin(x).inverse()) raises(AttributeError, lambda: cos(x).inverse()) assert tan(x).inverse() == atan assert cot(x).inverse() == acot raises(AttributeError, lambda: csc(x).inverse()) raises(AttributeError, lambda: sec(x).inverse()) assert asin(x).inverse() == sin assert acos(x).inverse() == cos assert atan(x).inverse() == tan assert acot(x).inverse() == cot
def manual_diff(f, symbol): """Derivative of f in form expected by find_substitutions SymPy's derivatives for some trig functions (like cot) aren't in a form that works well with finding substitutions; this replaces the derivatives for those particular forms with something that works better. """ if f.args: arg = f.args[0] if isinstance(f, sympy.tan): return arg.diff(symbol) * sympy.sec(arg)**2 elif isinstance(f, sympy.cot): return -arg.diff(symbol) * sympy.csc(arg)**2 elif isinstance(f, sympy.sec): return arg.diff(symbol) * sympy.sec(arg) * sympy.tan(arg) elif isinstance(f, sympy.csc): return -arg.diff(symbol) * sympy.csc(arg) * sympy.cot(arg) elif isinstance(f, sympy.Add): return sum([manual_diff(arg, symbol) for arg in f.args]) return f.diff(symbol)
def trig_substitution_rule(integral): integrand, symbol = integral a = sympy.Wild('a', exclude=[0, symbol]) b = sympy.Wild('b', exclude=[0, symbol]) theta = sympy.Dummy("theta") matches = integrand.find(a + b*symbol**2) if matches: for expr in matches: match = expr.match(a + b*symbol**2) a = match[a] b = match[b] a_positive = ((a.is_number and a > 0) or a.is_positive) b_positive = ((b.is_number and b > 0) or b.is_positive) x_func = None if a_positive and b_positive: # a**2 + b*x**2 x_func = (sympy.sqrt(a)/sympy.sqrt(b)) * sympy.tan(theta) elif a_positive and not b_positive: # a**2 - b*x**2 x_func = (sympy.sqrt(a)/sympy.sqrt(-b)) * sympy.sin(theta) elif not a_positive and b_positive: # b*x**2 - a**2 x_func = (sympy.sqrt(-a)/sympy.sqrt(b)) * sympy.sec(theta) if x_func: replaced = integrand.subs(symbol, x_func).trigsimp() if not replaced.has(symbol): replaced *= manual_diff(x_func, theta) replaced = replaced.trigsimp() secants = replaced.find(1/sympy.cos(theta)) if secants: replaced = replaced.xreplace({ 1/sympy.cos(theta): sympy.sec(theta) }) substep = integral_steps(replaced, theta) if not contains_dont_know(substep): return TrigSubstitutionRule( theta, x_func, replaced, substep, integrand, symbol)
def test_manualintegrate_trigonometry(): assert manualintegrate(sin(x), x) == -cos(x) assert manualintegrate(tan(x), x) == -log(cos(x)) assert manualintegrate(sec(x), x) == log(sec(x) + tan(x)) assert manualintegrate(csc(x), x) == -log(csc(x) + cot(x)) assert manualintegrate(sin(x) * cos(x), x) in [sin(x) ** 2 / 2, -cos(x)**2 / 2] assert manualintegrate(-sec(x) * tan(x), x) == -sec(x) assert manualintegrate(csc(x) * cot(x), x) == -csc(x) assert manualintegrate(sec(x)**2, x) == tan(x) assert manualintegrate(csc(x)**2, x) == -cot(x) assert manualintegrate(x * sec(x**2), x) == log(tan(x**2) + sec(x**2))/2 assert manualintegrate(cos(x)*csc(sin(x)), x) == -log(cot(sin(x)) + csc(sin(x))) assert manualintegrate(cos(3*x)*sec(x), x) == -x + sin(2*x) assert manualintegrate(sin(3*x)*sec(x), x) == \ -3*log(cos(x)) + 2*log(cos(x)**2) - 2*cos(x)**2
def eval_trig(func, arg, integrand, symbol): if func == "sin": return -sympy.cos(arg) elif func == "cos": return sympy.sin(arg) elif func == "sec*tan": return sympy.sec(arg) elif func == "csc*cot": return sympy.csc(arg) elif func == "sec**2": return sympy.tan(arg) elif func == "csc**2": return -sympy.cot(arg)
def test_conv7b(): x = sympy.Symbol("x") y = sympy.Symbol("y") assert sympify(sympy.sin(x/3)) == sin(Symbol("x") / 3) assert sympify(sympy.sin(x/3)) != cos(Symbol("x") / 3) assert sympify(sympy.cos(x/3)) == cos(Symbol("x") / 3) assert sympify(sympy.tan(x/3)) == tan(Symbol("x") / 3) assert sympify(sympy.cot(x/3)) == cot(Symbol("x") / 3) assert sympify(sympy.csc(x/3)) == csc(Symbol("x") / 3) assert sympify(sympy.sec(x/3)) == sec(Symbol("x") / 3) assert sympify(sympy.asin(x/3)) == asin(Symbol("x") / 3) assert sympify(sympy.acos(x/3)) == acos(Symbol("x") / 3) assert sympify(sympy.atan(x/3)) == atan(Symbol("x") / 3) assert sympify(sympy.acot(x/3)) == acot(Symbol("x") / 3) assert sympify(sympy.acsc(x/3)) == acsc(Symbol("x") / 3) assert sympify(sympy.asec(x/3)) == asec(Symbol("x") / 3)
def test_hyper_as_trig(): from sympy.simplify.fu import _osborne, _osbornei eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2*x) assert _osborne(cosh(x)) == cos(x) assert _osborne(sinh(x)) == I*sin(x) assert _osborne(tanh(x)) == I*tan(x) assert _osborne(coth(x)) == cot(x)/I assert _osbornei(cos(x)) == cosh(x) assert _osbornei(sin(x)) == sinh(x)/I assert _osbornei(tan(x)) == tanh(x)/I assert _osbornei(cot(x)) == coth(x)*I assert _osbornei(sec(x)) == 1/cosh(x) assert _osbornei(csc(x)) == I/sinh(x)
def trig_powers_products_rule(integral): integrand, symbol = integral if any(integrand.has(f) for f in (sympy.sin, sympy.cos)): pattern, a, b, m, n = sincos_pattern(symbol) match = integrand.match(pattern) if match: a, b, m, n = match.get(a, 0),match.get(b, 0), match.get(m, 0), match.get(n, 0) return multiplexer({ sincos_botheven_condition: sincos_botheven, sincos_sinodd_condition: sincos_sinodd, sincos_cosodd_condition: sincos_cosodd })((a, b, m, n, integrand, symbol)) integrand = integrand.subs({ 1 / sympy.cos(symbol): sympy.sec(symbol) }) if any(integrand.has(f) for f in (sympy.tan, sympy.sec)): pattern, a, b, m, n = tansec_pattern(symbol) match = integrand.match(pattern) if match: a, b, m, n = match.get(a, 0),match.get(b, 0), match.get(m, 0), match.get(n, 0) return multiplexer({ tansec_tanodd_condition: tansec_tanodd, tansec_seceven_condition: tansec_seceven })((a, b, m, n, integrand, symbol)) integrand = integrand.subs({ 1 / sympy.sin(symbol): sympy.csc(symbol), 1 / sympy.tan(symbol): sympy.cot(symbol), sympy.cos(symbol) / sympy.tan(symbol): sympy.cot(symbol) }) if any(integrand.has(f) for f in (sympy.cot, sympy.csc)): pattern, a, b, m, n = cotcsc_pattern(symbol) match = integrand.match(pattern) if match: a, b, m, n = match.get(a, 0),match.get(b, 0), match.get(m, 0), match.get(n, 0) return multiplexer({ cotcsc_cotodd_condition: cotcsc_cotodd, cotcsc_csceven_condition: cotcsc_csceven })((a, b, m, n, integrand, symbol))
def trig_tansec_rule(integral): integrand, symbol = integral integrand = integrand.subs({ 1 / sympy.cos(symbol): sympy.sec(symbol) }) if any(integrand.has(f) for f in (sympy.tan, sympy.sec)): pattern, a, b, m, n = tansec_pattern(symbol) match = integrand.match(pattern) if match: a, b, m, n = match.get(a, 0),match.get(b, 0), match.get(m, 0), match.get(n, 0) return multiplexer({ tansec_tanodd_condition: tansec_tanodd, tansec_seceven_condition: tansec_seceven, tan_tansquared_condition: tan_tansquared })((a, b, m, n, integrand, symbol))
def trig_tansec_rule(integral): integrand, symbol = integral integrand = integrand.subs({ 1 / sympy.cos(symbol): sympy.sec(symbol) }) if any(integrand.has(f) for f in (sympy.tan, sympy.sec)): pattern, a, b, m, n = tansec_pattern(symbol) match = integrand.match(pattern) if not match: return return multiplexer({ tansec_tanodd_condition: tansec_tanodd, tansec_seceven_condition: tansec_seceven, tan_tansquared_condition: tan_tansquared })(tuple( [match.get(i, ZERO) for i in (a, b, m, n)] + [integrand, symbol]))
def trig_product_rule(integral): integrand, symbol = integral sectan = sympy.sec(symbol) * sympy.tan(symbol) q = integrand / sectan if symbol not in q.free_symbols: rule = TrigRule('sec*tan', symbol, sectan, symbol) if q != 1 and rule: rule = ConstantTimesRule(q, sectan, rule, integrand, symbol) return rule csccot = -sympy.csc(symbol) * sympy.cot(symbol) q = integrand / csccot if symbol not in q.free_symbols: rule = TrigRule('csc*cot', symbol, csccot, symbol) if q != 1 and rule: rule = ConstantTimesRule(q, csccot, rule, integrand, symbol) return rule
def test_cos_rewrite(): assert cos(x).rewrite(exp) == exp(I*x)/2 + exp(-I*x)/2 assert cos(x).rewrite(tan) == (1 - tan(x/2)**2)/(1 + tan(x/2)**2) assert cos(x).rewrite(cot) == -(1 - cot(x/2)**2)/(1 + cot(x/2)**2) assert cos(sinh(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, sinh(3)).n() assert cos(cosh(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cosh(3)).n() assert cos(tanh(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tanh(3)).n() assert cos(coth(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, coth(3)).n() assert cos(sin(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, sin(3)).n() assert cos(cos(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cos(3)).n() assert cos(tan(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tan(3)).n() assert cos(cot(x)).rewrite( exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cot(3)).n() assert cos(log(x)).rewrite(Pow) == x**I/2 + x**-I/2 assert cos(x).rewrite(sec) == 1/sec(x)
def trig_product_rule(integral): integrand, symbol = integral sectan = sympy.sec(symbol) * sympy.tan(symbol) q = integrand / sectan if symbol not in q.free_symbols: rule = TrigRule('sec*tan', symbol, sectan, symbol) if q != 1: rule = ConstantTimesRule(q, sectan, rule, integrand, symbol) return rule csccot = -sympy.csc(symbol) * sympy.cot(symbol) q = integrand / csccot if symbol not in q.free_symbols: rule = TrigRule('csc*cot', symbol, csccot, symbol) if q != 1: rule = ConstantTimesRule(q, csccot, rule, integrand, symbol) return rule
def test_stationary_points(): x, y = symbols("x y") assert stationary_points(sin(x), x, Interval(-pi / 2, pi / 2)) == {-pi / 2, pi / 2} assert stationary_points(sin(x), x, Interval.Ropen(0, pi / 4)) == EmptySet() assert stationary_points( tan(x), x, ) == EmptySet() assert stationary_points(sin(x) * cos(x), x, Interval(0, pi)) == { pi / 4, pi * Rational(3, 4), } assert stationary_points(sec(x), x, Interval(0, pi)) == {0, pi} assert stationary_points((x + 3) * (x - 2), x) == FiniteSet(Rational(-1, 2)) assert stationary_points((x + 3) / (x - 2), x, Interval(-5, 5)) == EmptySet() assert stationary_points((x**2 + 3) / (x - 2), x) == {2 - sqrt(7), 2 + sqrt(7)} assert stationary_points((x**2 + 3) / (x - 2), x, Interval(0, 5)) == {2 + sqrt(7)} assert stationary_points(x**4 + x**3 - 5 * x**2, x, S.Reals) == FiniteSet(-2, 0, Rational(5, 4)) assert stationary_points(exp(x), x) == EmptySet() assert stationary_points(log(x) - x, x, S.Reals) == {1} assert stationary_points(cos(x), x, Union(Interval(0, 5), Interval(-6, -3))) == { 0, -pi, pi, } assert stationary_points(y, x, S.Reals) == S.Reals assert stationary_points(y, x, S.EmptySet) == S.EmptySet
def sup(x): return -1 / (sec(x) + 1)
sincos_sinodd = trig_rewriter( lambda a, b, m, n, i, symbol: ( (1 - sympy.cos(a*symbol)**2)**((m - 1) / 2) * sympy.sin(a*symbol) * sympy.cos(b*symbol) ** n)) sincos_cosodd_condition = uncurry(lambda a, b, m, n, i, s: n.is_odd and n >= 3) sincos_cosodd = trig_rewriter( lambda a, b, m, n, i, symbol: ( (1 - sympy.sin(b*symbol)**2)**((n - 1) / 2) * sympy.cos(b*symbol) * sympy.sin(a*symbol) ** m)) tansec_seceven_condition = uncurry(lambda a, b, m, n, i, s: n.is_even and n >= 4) tansec_seceven = trig_rewriter( lambda a, b, m, n, i, symbol: ( (1 + sympy.tan(b*symbol)**2) ** (n/2 - 1) * sympy.sec(b*symbol)**2 * sympy.tan(a*symbol) ** m )) tansec_tanodd_condition = uncurry(lambda a, b, m, n, i, s: m.is_odd) tansec_tanodd = trig_rewriter( lambda a, b, m, n, i, symbol: ( (sympy.sec(a*symbol)**2 - 1) ** ((m - 1) / 2) * sympy.tan(a*symbol) * sympy.sec(b*symbol) ** n )) tan_tansquared_condition = uncurry(lambda a, b, m, n, i, s: m == 2 and n == 0) tan_tansquared = trig_rewriter( lambda a, b, m, n, i, symbol: ( sympy.sec(a*symbol)**2 - 1)) cotcsc_csceven_condition = uncurry(lambda a, b, m, n, i, s: n.is_even and n >= 4) cotcsc_csceven = trig_rewriter( lambda a, b, m, n, i, symbol: ( (1 + sympy.cot(b*symbol)**2) ** (n/2 - 1) *
def tansec_pattern(symbol): a, b, m, n = make_wilds(symbol) pattern = sympy.tan(a * symbol)**m * sympy.sec(b * symbol)**n return pattern, a, b, m, n
def test_issue_14411(): assert limit(3 * sec(4 * pi * x - x / 3), x, 3 * pi / (24 * pi - 2)) is -oo
def test_periodicity(): x = Symbol('x') y = Symbol('y') z = Symbol('z', real=True) assert periodicity(sin(2*x), x) == pi assert periodicity((-2)*tan(4*x), x) == pi/4 assert periodicity(sin(x)**2, x) == 2*pi assert periodicity(3**tan(3*x), x) == pi/3 assert periodicity(tan(x)*cos(x), x) == 2*pi assert periodicity(sin(x)**(tan(x)), x) == 2*pi assert periodicity(tan(x)*sec(x), x) == 2*pi assert periodicity(sin(2*x)*cos(2*x) - y, x) == pi/2 assert periodicity(tan(x) + cot(x), x) == pi assert periodicity(sin(x) - cos(2*x), x) == 2*pi assert periodicity(sin(x) - 1, x) == 2*pi assert periodicity(sin(4*x) + sin(x)*cos(x), x) == pi assert periodicity(exp(sin(x)), x) == 2*pi assert periodicity(log(cot(2*x)) - sin(cos(2*x)), x) == pi assert periodicity(sin(2*x)*exp(tan(x) - csc(2*x)), x) == pi assert periodicity(cos(sec(x) - csc(2*x)), x) == 2*pi assert periodicity(tan(sin(2*x)), x) == pi assert periodicity(2*tan(x)**2, x) == pi assert periodicity(sin(x%4), x) == 4 assert periodicity(sin(x)%4, x) == 2*pi assert periodicity(tan((3*x-2)%4), x) == Rational(4, 3) assert periodicity((sqrt(2)*(x+1)+x) % 3, x) == 3 / (sqrt(2)+1) assert periodicity((x**2+1) % x, x) is None assert periodicity(sin(re(x)), x) == 2*pi assert periodicity(sin(x)**2 + cos(x)**2, x) is S.Zero assert periodicity(tan(x), y) is S.Zero assert periodicity(sin(x) + I*cos(x), x) == 2*pi assert periodicity(x - sin(2*y), y) == pi assert periodicity(exp(x), x) is None assert periodicity(exp(I*x), x) == 2*pi assert periodicity(exp(I*z), z) == 2*pi assert periodicity(exp(z), z) is None assert periodicity(exp(log(sin(z) + I*cos(2*z)), evaluate=False), z) == 2*pi assert periodicity(exp(log(sin(2*z) + I*cos(z)), evaluate=False), z) == 2*pi assert periodicity(exp(sin(z)), z) == 2*pi assert periodicity(exp(2*I*z), z) == pi assert periodicity(exp(z + I*sin(z)), z) is None assert periodicity(exp(cos(z/2) + sin(z)), z) == 4*pi assert periodicity(log(x), x) is None assert periodicity(exp(x)**sin(x), x) is None assert periodicity(sin(x)**y, y) is None assert periodicity(Abs(sin(Abs(sin(x)))), x) == pi assert all(periodicity(Abs(f(x)), x) == pi for f in ( cos, sin, sec, csc, tan, cot)) assert periodicity(Abs(sin(tan(x))), x) == pi assert periodicity(Abs(sin(sin(x) + tan(x))), x) == 2*pi assert periodicity(sin(x) > S.Half, x) == 2*pi assert periodicity(x > 2, x) is None assert periodicity(x**3 - x**2 + 1, x) is None assert periodicity(Abs(x), x) is None assert periodicity(Abs(x**2 - 1), x) is None assert periodicity((x**2 + 4)%2, x) is None assert periodicity((E**x)%3, x) is None assert periodicity(sin(expint(1, x))/expint(1, x), x) is None
Keyword Arguments ----------------- trend : Returns ------- num_N : int ''' num_N = params.get('trend', int(12 + 2 * params['k'] * params['R'])) return num_N #%% secsq_alpha = sec(alpha)**2 secsq_beta = secsq_alpha.subs(alpha, beta) s_pt1 = (atan(tan(alpha) * tan(beta)) / (secsq_alpha + sqrt(secsq_alpha + tan(beta)**2))) s_pt2 = (atan(tan(alpha) * tan(beta)) / (secsq_beta + sqrt(secsq_beta + tan(alpha)**2))) S = (4 * R**2) * (s_pt1 + s_pt2) sval = lambdify([alpha, beta, R], S, 'mpmath') #%% # First implement the on-axis response. subsdict = {'n': 1, 'alpha': pi / 2.5, 'beta': pi / 2.5, 'k': 10, 'R': 0.1}
def func(x): return (1 - sec(x)) / (tan(x)**2)
def test_sec(): assert sec(x).diff(x) == tan(x) * sec(x)
def walk(width, theta): length = width * sympy.sec(theta) delta_y = width * sympy.tan(theta) return length, delta_y
def test_TR1(): assert TR1(2 * csc(x) + sec(x)) == 1 / cos(x) + 2 / sin(x)
(r"x \le y", Le(x, y)), (r"x \ge y", Ge(x, y)), (r"\lfloor x \rfloor", floor(x)), (r"\lceil x \rceil", ceiling(x)), (r"\langle x |", Bra('x')), (r"| x \rangle", Ket('x')), (r"\sin \theta", sin(theta)), (r"\sin(\theta)", sin(theta)), (r"\sin^{-1} a", asin(a)), (r"\sin a \cos b", _Mul(sin(a), cos(b))), (r"\sin \cos \theta", sin(cos(theta))), (r"\sin(\cos \theta)", sin(cos(theta))), (r"\frac{a}{b}", a / b), (r"\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), (r"\frac{7}{3}", _Mul(7, _Pow(3, -1))), (r"(\csc x)(\sec y)", csc(x) * sec(y)), (r"\lim_{x \to 3} a", Limit(a, x, 3)), (r"\lim_{x \rightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \Rightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \longrightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \Longrightarrow 3} a", Limit(a, x, 3)), (r"\lim_{x \to 3^{+}} a", Limit(a, x, 3, dir='+')), (r"\lim_{x \to 3^{-}} a", Limit(a, x, 3, dir='-')), (r"\infty", oo), (r"\lim_{x \to \infty} \frac{1}{x}", Limit(_Pow(x, -1), x, oo)), (r"\frac{d}{dx} x", Derivative(x, x)), (r"\frac{d}{dt} x", Derivative(x, t)), (r"f(x)", f(x)), (r"f(x, y)", f(x, y)), (r"f(x, y, z)", f(x, y, z)), (r"\frac{d f(x)}{dx}", Derivative(f(x), x)),
def test_periodicity(): x = Symbol('x') y = Symbol('y') z = Symbol('z', real=True) assert periodicity(sin(2 * x), x) == pi assert periodicity((-2) * tan(4 * x), x) == pi / 4 assert periodicity(sin(x)**2, x) == 2 * pi assert periodicity(3**tan(3 * x), x) == pi / 3 assert periodicity(tan(x) * cos(x), x) == 2 * pi assert periodicity(sin(x)**(tan(x)), x) == 2 * pi assert periodicity(tan(x) * sec(x), x) == 2 * pi assert periodicity(sin(2 * x) * cos(2 * x) - y, x) == pi / 2 assert periodicity(tan(x) + cot(x), x) == pi assert periodicity(sin(x) - cos(2 * x), x) == 2 * pi assert periodicity(sin(x) - 1, x) == 2 * pi assert periodicity(sin(4 * x) + sin(x) * cos(x), x) == pi assert periodicity(exp(sin(x)), x) == 2 * pi assert periodicity(log(cot(2 * x)) - sin(cos(2 * x)), x) == pi assert periodicity(sin(2 * x) * exp(tan(x) - csc(2 * x)), x) == pi assert periodicity(cos(sec(x) - csc(2 * x)), x) == 2 * pi assert periodicity(tan(sin(2 * x)), x) == pi assert periodicity(2 * tan(x)**2, x) == pi assert periodicity(sin(x % 4), x) == 4 assert periodicity(sin(x) % 4, x) == 2 * pi assert periodicity(tan((3 * x - 2) % 4), x) == Rational(4, 3) assert periodicity((sqrt(2) * (x + 1) + x) % 3, x) == 3 / (sqrt(2) + 1) assert periodicity((x**2 + 1) % x, x) is None assert periodicity(sin(re(x)), x) == 2 * pi assert periodicity(sin(x)**2 + cos(x)**2, x) is S.Zero assert periodicity(tan(x), y) is S.Zero assert periodicity(sin(x) + I * cos(x), x) == 2 * pi assert periodicity(x - sin(2 * y), y) == pi assert periodicity(exp(x), x) is None assert periodicity(exp(I * x), x) == 2 * pi assert periodicity(exp(I * z), z) == 2 * pi assert periodicity(exp(z), z) is None assert periodicity(exp(log(sin(z) + I * cos(2 * z)), evaluate=False), z) == 2 * pi assert periodicity(exp(log(sin(2 * z) + I * cos(z)), evaluate=False), z) == 2 * pi assert periodicity(exp(sin(z)), z) == 2 * pi assert periodicity(exp(2 * I * z), z) == pi assert periodicity(exp(z + I * sin(z)), z) is None assert periodicity(exp(cos(z / 2) + sin(z)), z) == 4 * pi assert periodicity(log(x), x) is None assert periodicity(exp(x)**sin(x), x) is None assert periodicity(sin(x)**y, y) is None assert periodicity(Abs(sin(Abs(sin(x)))), x) == pi assert all( periodicity(Abs(f(x)), x) == pi for f in (cos, sin, sec, csc, tan, cot)) assert periodicity(Abs(sin(tan(x))), x) == pi assert periodicity(Abs(sin(sin(x) + tan(x))), x) == 2 * pi assert periodicity(sin(x) > S.Half, x) == 2 * pi assert periodicity(x > 2, x) is None assert periodicity(x**3 - x**2 + 1, x) is None assert periodicity(Abs(x), x) is None assert periodicity(Abs(x**2 - 1), x) is None assert periodicity((x**2 + 4) % 2, x) is None assert periodicity((E**x) % 3, x) is None assert periodicity(sin(expint(1, x)) / expint(1, x), x) is None # returning `None` for any Piecewise p = Piecewise((0, x < -1), (x**2, x <= 1), (log(x), True)) assert periodicity(p, x) is None m = MatrixSymbol('m', 3, 3) raises(NotImplementedError, lambda: periodicity(sin(m), m)) raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m)) raises(NotImplementedError, lambda: periodicity(sin(m), m[0, 0])) raises(NotImplementedError, lambda: periodicity(sin(m[0, 0]), m[0, 0]))
# Exponentes y logaritmos sp.exp(num) # Calcular la exponencial sp.log(num) # Calcular el logaritmo natural sp.log(base, num) # Calcular el logaritmo en la base especificada # Conversión de angulos sp.deg(num) # Convertir ángulos de radianes a grados. sp.rad(num) # Convertir ángulos de grados a radianes. # Funciones para trigonometrÃa (Angulos en radianes) sp.sin(num) # Seno sp.cos(num) # Coseno sp.tan(num) # tangente sp.cot(num) # cotangente sp.sec(num) # secante sp.csc(num) # cosecante sp.asin(num) # Arcoseno sp.acos(num) # Arcocoseno sp.atan(num) # Arcotangente sp.atan2(catetoY, catetoX) # Arcotangente de un triangulo segun los catetos (Angulo) sp.acot(num) # Arcocotangente sp.asec(num) # Arcosecante sp.acsc(num) # Arcocosecante # Funciones hiperbólicas (Angulos en radianes) sp.sinh(num) # Seno sp.cosh(num) # Coseno sp.tanh(num) # tangente sp.coth(num) # cotangente
("5^3a^2", _Mul(sympy.Pow(5, 3, evaluate=False), sympy.Pow(a, 2, evaluate=False))), ("a^{32}b^2", _Mul(a**32, b**2)), ("5^{38}b^3", _Mul(sympy.Pow(5, 38, evaluate=False), b**3)), ("6a^3b^4", _Mul(6, _Mul(a**3, b**4))), ("4a^{b^7}", 4 * sympy.Pow(a, sympy.Pow(b, 7, evaluate=False), evaluate=False)), ("\\frac{d}{d\\theta} x", sympy.Derivative(x, theta)), ("\\sin \\theta", sympy.sin(theta)), ("\\sin(\\theta)", sympy.sin(theta)), ("\\sin^{-1} a", sympy.asin(a)), ("\\sin a \\cos b", _Mul(sympy.sin(a), sympy.cos(b))), ("\\sin \\cos \\theta", sympy.sin(sympy.cos(theta))), ("\\sin(\\cos \\theta)", sympy.sin(sympy.cos(theta))), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", _Mul(7, _Pow(3, -1))), ("(\\csc x)(\\sec y)", sympy.csc(x) * sympy.sec(y)), ("\\lim_{x \\to 3} a", sympy.Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", sympy.Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", sympy.Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", sympy.Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", sympy.Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", sympy.Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", sympy.Limit(a, x, 3, dir='-')), ("\\infty", sympy.oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", sympy.Limit(_Mul(1, _Pow(x, -1)), x, sympy.oo)), ("\\frac{d}{dx} x", sympy.Derivative(x, x)), ("\\frac{d}{dt} x", sympy.Derivative(x, t)), ("f(x)", f(x)), ("f(x, y)", f(x, y)), ("f(x, y, z)", f(x, y, z)), ("\\frac{d f(x)}{dx}", sympy.Derivative(f(x), x)), ("\\frac{d\\theta(x)}{dx}", sympy.Derivative(theta(x),
def test_sec(): x = symbols('x', real=True) z = symbols('z') assert sec.nargs == FiniteSet(1) assert sec(0) == 1 assert sec(pi) == -1 assert sec(pi / 2) == zoo assert sec(-pi / 2) == zoo assert sec(pi / 6) == 2 * sqrt(3) / 3 assert sec(pi / 3) == 2 assert sec(5 * pi / 2) == zoo assert sec(9 * pi / 7) == -sec(2 * pi / 7) assert sec(3 * pi / 4) == -sqrt(2) # issue 8421 assert sec(I) == 1 / cosh(1) assert sec(x * I) == 1 / cosh(x) assert sec(-x) == sec(x) assert sec(asec(x)) == x assert sec(x).rewrite(exp) == 1 / (exp(I * x) / 2 + exp(-I * x) / 2) assert sec(x).rewrite(sin) == sec(x) assert sec(x).rewrite(cos) == 1 / cos(x) assert sec(x).rewrite(tan) == (tan(x / 2)**2 + 1) / (-tan(x / 2)**2 + 1) assert sec(x).rewrite(pow) == sec(x) assert sec(x).rewrite(sqrt) == sec(x) assert sec(z).rewrite(cot) == (cot(z / 2)**2 + 1) / (cot(z / 2)**2 - 1) assert sec(z).conjugate() == sec(conjugate(z)) assert (sec(z).as_real_imag() == ( cos(re(z)) * cosh(im(z)) / (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2), sin(re(z)) * sinh(im(z)) / (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2))) assert sec(x).expand(trig=True) == 1 / cos(x) assert sec(2 * x).expand(trig=True) == 1 / (2 * cos(x)**2 - 1) assert sec(x).is_real == True assert sec(z).is_real == None assert sec(a).is_algebraic is None assert sec(na).is_algebraic is False assert sec(x).as_leading_term() == sec(x) assert sec(0).is_finite == True assert sec(x).is_finite == None assert sec(pi / 2).is_finite == False assert series(sec(x), x, x0=0, n=6) == 1 + x**2 / 2 + 5 * x**4 / 24 + O(x**6) # https://github.com/sympy/sympy/issues/7166 assert series(sqrt(sec(x))) == 1 + x**2 / 4 + 7 * x**4 / 96 + O(x**6) # https://github.com/sympy/sympy/issues/7167 assert (series(sqrt(sec(x)), x, x0=pi * 3 / 2, n=4) == 1 / sqrt(x - 3 * pi / 2) + (x - 3 * pi / 2)**(S(3) / 2) / 12 + (x - 3 * pi / 2)**(S(7) / 2) / 160 + O((x - 3 * pi / 2)**4, (x, 3 * pi / 2))) assert sec(x).diff(x) == tan(x) * sec(x) # Taylor Term checks assert sec(z).taylor_term(4, z) == 5 * z**4 / 24 assert sec(z).taylor_term(6, z) == 61 * z**6 / 720 assert sec(z).taylor_term(5, z) == 0
def convert(t): """Convert term t to SymPy term.""" if t.is_var(): if t.T == RealType: return sympy.Symbol(t.name) else: raise SymPyException("convert: unexpected variable type: %s" % str(t.T)) elif t == real.pi: return sympy.pi elif t.is_number(): val = t.dest_number() if isinstance(val, Fraction): return sympy.Number(val.numerator) / sympy.Number(val.denominator) else: return sympy.Number(val) elif t.is_plus(): return convert(t.arg1) + convert(t.arg) elif t.is_minus(): return convert(t.arg1) - convert(t.arg) elif t.is_uminus(): return -convert(t.arg) elif t.is_times(): return convert(t.arg1) * convert(t.arg) elif t.is_divides(): return convert(t.arg1) / convert(t.arg) elif t.is_nat_power() and t.arg.is_number(): return convert(t.arg1)**t.arg.dest_number() elif t.is_real_power(): return convert(t.arg1)**convert(t.arg) elif t.is_comb('real_closed_interval', 2): return sympy.Interval(convert(t.arg1), convert(t.arg)) elif t.is_comb('real_open_interval', 2): return sympy.Interval.open(convert(t.arg1), convert(t.arg)) elif t.is_comb('sqrt', 1): return sympy.sqrt(convert(t.arg)) elif t.is_comb('abs', 1): return sympy.Abs(convert(t.arg)) elif t.is_comb('exp', 1): return sympy.exp(convert(t.arg)) elif t.is_comb('log', 1): return sympy.log(convert(t.arg)) elif t.is_comb('sin', 1): return sympy.sin(convert(t.arg)) elif t.is_comb('cos', 1): return sympy.cos(convert(t.arg)) elif t.is_comb('tan', 1): return sympy.tan(convert(t.arg)) elif t.is_comb('cot', 1): return sympy.cot(convert(t.arg)) elif t.is_comb('sec', 1): return sympy.sec(convert(t.arg)) elif t.is_comb('csc', 1): return sympy.csc(convert(t.arg)) elif t.is_greater_eq(): return convert(t.arg1) >= convert(t.arg) elif t.is_greater(): return convert(t.arg1) > convert(t.arg) elif t.is_less_eq(): return convert(t.arg1) <= convert(t.arg) elif t.is_less(): return convert(t.arg1) < convert(t.arg) else: raise SymPyException("Unable to convert " + str(t))
GOOD_PAIRS = [("0", 0), ("1", 1), ("-3.14", _Mul(-1, 3.14)), ("(-7.13)(1.5)", _Mul(_Mul(-1, 7.13), 1.5)), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", _Add(a + b, -a)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin^{-1} a", asin(a)), ("\\sin a \\cos b", _Mul(sin(a), cos(b))), ("\\sin \\cos \\theta", sin(cos(theta))), ("\\sin(\\cos \\theta)", sin(cos(theta))), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", _Mul(7, _Pow(3, -1))), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), ("f(x)", f(x)), ("f(x, y)", f(x, y)), ("f(x, y, z)", f(x, y, z)), ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), ("\\frac{d\\theta(x)}{dx}", Derivative(Function('theta')(x), x)),
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_issue_16161(): i = integrate(x*sec(x)**2, x) assert not i.has(Integral)
sincos_sinodd = trig_rewriter( lambda a, b, m, n, i, symbol: ((1 - sympy.cos(a * symbol)**2)**( (m - 1) / 2) * sympy.sin(a * symbol) * sympy.cos(b * symbol)**n)) sincos_cosodd_condition = uncurry(lambda a, b, m, n, i, s: n.is_odd and n >= 3) sincos_cosodd = trig_rewriter( lambda a, b, m, n, i, symbol: ((1 - sympy.sin(b * symbol)**2)**( (n - 1) / 2) * sympy.cos(b * symbol) * sympy.sin(a * symbol)**m)) tansec_seceven_condition = uncurry( lambda a, b, m, n, i, s: n.is_even and n >= 4) tansec_seceven = trig_rewriter(lambda a, b, m, n, i, symbol: ( (1 + sympy.tan(b * symbol)**2)** (n / 2 - 1) * sympy.sec(b * symbol)**2 * sympy.tan(a * symbol)**m)) tansec_tanodd_condition = uncurry(lambda a, b, m, n, i, s: m.is_odd) tansec_tanodd = trig_rewriter( lambda a, b, m, n, i, symbol: ((sympy.sec(a * symbol)**2 - 1)**( (m - 1) / 2) * sympy.tan(a * symbol) * sympy.sec(b * symbol)**n)) tan_tansquared_condition = uncurry(lambda a, b, m, n, i, s: m == 2 and n == 0) tan_tansquared = trig_rewriter(lambda a, b, m, n, i, symbol: (sympy.sec(a * symbol)**2 - 1)) cotcsc_csceven_condition = uncurry( lambda a, b, m, n, i, s: n.is_even and n >= 4) cotcsc_csceven = trig_rewriter(lambda a, b, m, n, i, symbol: ( (1 + sympy.cot(b * symbol)**2)** (n / 2 - 1) * sympy.csc(b * symbol)**2 * sympy.cot(a * symbol)**m))
def test_sec(): x = symbols('x', real=True) z = symbols('z') assert sec.nargs == FiniteSet(1) assert sec(0) == 1 assert sec(pi) == -1 assert sec(pi / 2) == zoo assert sec(-pi / 2) == zoo assert sec(pi / 6) == 2 * sqrt(3) / 3 assert sec(pi / 3) == 2 assert sec(5 * pi / 2) == zoo assert sec(9 * pi / 7) == -sec(2 * pi / 7) assert sec(I) == 1 / cosh(1) assert sec(x * I) == 1 / cosh(x) assert sec(-x) == sec(x) assert sec(x).rewrite(exp) == 1 / (exp(I * x) / 2 + exp(-I * x) / 2) assert sec(x).rewrite(sin) == sec(x) assert sec(x).rewrite(cos) == 1 / cos(x) assert sec(x).rewrite(tan) == (tan(x / 2)**2 + 1) / (-tan(x / 2)**2 + 1) assert sec(x).rewrite(pow) == sec(x) assert sec(x).rewrite(sqrt) == sec(x) assert sec(z).conjugate() == sec(conjugate(z)) assert (sec(z).as_real_imag() == ( cos(re(z)) * cosh(im(z)) / (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2), sin(re(z)) * sinh(im(z)) / (sin(re(z))**2 * sinh(im(z))**2 + cos(re(z))**2 * cosh(im(z))**2))) assert sec(x).expand(trig=True) == 1 / cos(x) assert sec(2 * x).expand(trig=True) == 1 / (2 * cos(x)**2 - 1) assert sec(x).is_real == True assert sec(z).is_real == None assert sec(x).as_leading_term() == sec(x) assert sec(0).is_bounded == True assert sec(x).is_bounded == None assert sec(pi / 2).is_bounded == False assert series(sec(x), x, x0=0, n=6) == 1 + x**2 / 2 + 5 * x**4 / 24 + O(x**6) # https://code.google.com/p/sympy/issues/detail?id=4067 assert series(sqrt(sec(x))) == 1 + x**2 / 4 + 7 * x**4 / 96 + O(x**6) assert sec(x).diff(x) == tan(x) * sec(x)
def trig_substitution_rule(integral): integrand, symbol = integral A = sympy.Wild('a', exclude=[0, symbol]) B = sympy.Wild('b', exclude=[0, symbol]) theta = sympy.Dummy("theta") target_pattern = A + B * symbol**2 matches = integrand.find(target_pattern) for expr in matches: match = expr.match(target_pattern) a = match.get(A, ZERO) b = match.get(B, ZERO) a_positive = ((a.is_number and a > 0) or a.is_positive) b_positive = ((b.is_number and b > 0) or b.is_positive) a_negative = ((a.is_number and a < 0) or a.is_negative) b_negative = ((b.is_number and b < 0) or b.is_negative) x_func = None if a_positive and b_positive: # a**2 + b*x**2. Assume sec(theta) > 0, -pi/2 < theta < pi/2 x_func = (sympy.sqrt(a) / sympy.sqrt(b)) * sympy.tan(theta) # Do not restrict the domain: tan(theta) takes on any real # value on the interval -pi/2 < theta < pi/2 so x takes on # any value restriction = True elif a_positive and b_negative: # a**2 - b*x**2. Assume cos(theta) > 0, -pi/2 < theta < pi/2 constant = sympy.sqrt(a) / sympy.sqrt(-b) x_func = constant * sympy.sin(theta) restriction = sympy.And(symbol > -constant, symbol < constant) elif a_negative and b_positive: # b*x**2 - a**2. Assume sin(theta) > 0, 0 < theta < pi constant = sympy.sqrt(-a) / sympy.sqrt(b) x_func = constant * sympy.sec(theta) restriction = sympy.And(symbol > -constant, symbol < constant) if x_func: # Manually simplify sqrt(trig(theta)**2) to trig(theta) # Valid due to assumed domain restriction substitutions = {} for f in [ sympy.sin, sympy.cos, sympy.tan, sympy.sec, sympy.csc, sympy.cot ]: substitutions[sympy.sqrt(f(theta)**2)] = f(theta) substitutions[sympy.sqrt(f(theta)**(-2))] = 1 / f(theta) replaced = integrand.subs(symbol, x_func).trigsimp() replaced = replaced.subs(substitutions) if not replaced.has(symbol): replaced *= manual_diff(x_func, theta) replaced = replaced.trigsimp() secants = replaced.find(1 / sympy.cos(theta)) if secants: replaced = replaced.xreplace( {1 / sympy.cos(theta): sympy.sec(theta)}) substep = integral_steps(replaced, theta) if not contains_dont_know(substep): return TrigSubstitutionRule(theta, x_func, replaced, substep, restriction, integrand, symbol)
def test_find_substitutions(): assert find_substitutions((cot(x)**2 + 1)**2*csc(x)**2*cot(x)**2, x, u) == \ [(cot(x), 1, -u**6 - 2*u**4 - u**2)] assert find_substitutions((sec(x)**2 + tan(x) * sec(x)) / (sec(x) + tan(x)), x, u) == [(sec(x) + tan(x), 1, 1/u)] assert find_substitutions(x * exp(-x**2), x, u) == [(-x**2, -S.Half, exp(u))]
class TestAllGood(object): # These latex strings should parse to the corresponding SymPy expression GOOD_PAIRS = [ ("0", Rational(0)), ("1", Rational(1)), ("-3.14", Rational(-314, 100)), ("5-3", _Add(5, _Mul(-1, 3))), ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))), ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)), ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)), ("a\\mod b", Mod(a, b)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin\\left(\\theta\\right)", sin(theta)), ("\\sin^{-1} a", asin(a)), ("\\sin a \\cos b", _Mul(sin(a), cos(b))), ("\\sin \\cos \\theta", sin(cos(theta))), ("\\sin(\\cos \\theta)", sin(cos(theta))), ("\\arcsin(a)", asin(a)), ("\\arccos(a)", acos(a)), ("\\arctan(a)", atan(a)), ("\\sinh(a)", sinh(a)), ("\\cosh(a)", cosh(a)), ("\\tanh(a)", tanh(a)), ("\\sinh^{-1}(a)", asinh(a)), ("\\cosh^{-1}(a)", acosh(a)), ("\\tanh^{-1}(a)", atanh(a)), ("\\arcsinh(a)", asinh(a)), ("\\arccosh(a)", acosh(a)), ("\\arctanh(a)", atanh(a)), ("\\arsinh(a)", asinh(a)), ("\\arcosh(a)", acosh(a)), ("\\artanh(a)", atanh(a)), ("\\operatorname{arcsinh}(a)", asinh(a)), ("\\operatorname{arccosh}(a)", acosh(a)), ("\\operatorname{arctanh}(a)", atanh(a)), ("\\operatorname{arsinh}(a)", asinh(a)), ("\\operatorname{arcosh}(a)", acosh(a)), ("\\operatorname{artanh}(a)", atanh(a)), ("\\operatorname{gcd}(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{gcd}(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{floor}(a)", floor(a)), ("\\operatorname{ceil}(b)", ceiling(b)), ("\\cos^2(x)", cos(x)**2), ("\\cos(x)^2", cos(x)**2), ("\\gcd(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\gcd(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\floor(a)", floor(a)), ("\\ceil(b)", ceiling(b)), ("\\max(a, b)", Max(a, b)), ("\\min(a, b)", Min(a, b)), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", Rational(7, 3)), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\infty\\%", oo), ("\\$\\infty", oo), ("-\\infty", -oo), ("-\\infty\\%", -oo), ("-\\$\\infty", -oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), # ("f(x)", f(x)), # ("f(x, y)", f(x, y)), # ("f(x, y, z)", f(x, y, z)), # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("\\left|x\\right|", _Abs(x)), ("||x||", _Abs(_Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\lfloor x\\rfloor", floor(x)), ("\\lceil y\\rceil", ceiling(y)), ("\\pi^{|xy|}", pi**_Abs(x * y)), ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))), ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)), ("a+bI", a + I * b), ("e^{I\\pi}", Integer(-1)), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^b x dx", Integral(x, (x, a, b))), ("\\int^{b}_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))), ("\\int_{ }^{}x dx", Integral(x, x)), ("\\int^{ }_{ }x dx", Integral(x, x)), ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))), # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))), ("\\int (x+a)", Integral(_Add(x, a), x)), ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)), ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)), ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)), ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)), ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)), ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)), ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)), ("x_0", Symbol('x_0', real=True, positive=True)), ("x_{1}", Symbol('x_1', real=True, positive=True)), ("x_a", Symbol('x_a', real=True, positive=True)), ("x_{b}", Symbol('x_b', real=True, positive=True)), ("h_\\theta", Symbol('h_{\\theta}', real=True, positive=True)), ("h_\\theta ", Symbol('h_{\\theta}', real=True, positive=True)), ("h_{\\theta}", Symbol('h_{\\theta}', real=True, positive=True)), # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), # ("\\log_2 x", _log(x, 2)), ("\\log_{2} x", _log(x, 2)), # ("\\log_a x", _log(x, a)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)), ("2\\overline{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\overline{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_n', real=True, positive=True)), ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(x) / Symbol('xbar_n', real=True, positive=True)), ("2\\bar{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\bar{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4), ("\\ln\\left(\\theta\\right)", _log(theta, E)), ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)), ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)), ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)), ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)), ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)), ("\\frac{1}{2}xy(x+y)", Mul(Rational(1, 2), x, y, (x + y), evaluate=False)), ("\\frac{1}{2}\\theta(x+y)", Mul(Rational(1, 2), theta, (x + y), evaluate=False)), ("1-f(x)", 1 - f * x), ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])), ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])), ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])), # scientific notation ("2.5\\times 10^2", Rational(250)), ("1,500\\times 10^{-1}", Rational(150)), # e notation ("2.5E2", Rational(250)), ("1,500E-1", Rational(150)), # multiplication without cmd ("2x2y", Mul(2, x, 2, y, evaluate=False)), ("2x2", Mul(2, x, 2, evaluate=False)), ("x2", x * 2), # lin alg processing ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)), ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)), ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Rational(1, 9), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])), ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])), ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])), # us dollars ("\\$1,000.00", Rational(1000)), ("\\$543.21", Rational(54321, 100)), ("\\$0.009", Rational(9, 1000)), # percentages ("100\\%", Rational(1)), ("1.5\\%", Rational(15, 1000)), ("0.05\\%", Rational(5, 10000)), # empty set ("\\emptyset", S.EmptySet), # divide by zero ("\\frac{1}{0}", _Pow(0, -1)), ("1+\\frac{5}{0}", _Add(1, _Mul(5, _Pow(0, -1)))), # adjacent single char sub sup ("4^26^2", _Mul(_Pow(4, 2), _Pow(6, 2))), ("x_22^2", _Mul(Symbol('x_2', real=True, positive=True), _Pow(2, 2))) ] def test_good_pair(self, s, eq): assert_equal(s, eq)
def tansec_pattern(symbol): a, b, m, n = make_wilds(symbol) pattern = sympy.tan(a*symbol)**m * sympy.sec(b*symbol)**n return pattern, a, b, m, n
def rewrites_rule(integral): integrand, symbol = integral if integrand.match(1/sympy.cos(symbol)): rewritten = integrand.subs(1/sympy.cos(symbol), sympy.sec(symbol)) return RewriteRule(rewritten, integral_steps(rewritten, symbol), integrand, symbol)
def test_static_cases(self): test_cases = [ # Linear algebra 1 * x, Eq(8 * x, 0), Eq(10 * x, 5), Eq(x - 4 * x, -21), Eq(3 * x, 21), Eq(2 + 5, x), Eq(3 * (x - 4), x), Eq(12, 2 * x), Eq(3 * (x - 4) / x, 1), Eq(12 / x, 2), 0.5 * x + 0.1, Rational(1, 10) + Rational(1, 2) * x, Add(x, 2 * x, -3 * x, evaluate=False), # Trigonometry sin(x), cos(x), tan(x), csc(x), sec(x), cot(x), 1 - sin(x)**2, cos(x)**2, Eq(sin(x)**2, x), Eq(-x, 1 - cos(x)**2 - 2 * x), sin(2 * x), 2 * sin(x) * cos(x), Eq(1 - sin(2 * x), 0), Eq(sin(x)**2, 2 * sin(x) * cos(x) - cos(x)**2), cos(x * y), acos(x), Pow(cos(x), 1, evaluate=False), Pow(cos(x), 0, evaluate=False), # Hyperbolic functions sinh(x), cosh(x), tanh(x), #csch(x), #sech(x), #coth(x), # Exponential E, sqrt(E), E**x, exp(x), exp(ln(x), evaluate=False), # Imaginary/Complex I, 3 + 8 * I, re(x + I * y, evaluate=False), im(x + y * I, evaluate=False), re(1 - I, evaluate=False), im(1 - I, evaluate=False), E**(I * theta), # Infinity oo, Add(oo, oo, evaluate=False), Add(oo, Mul(3, oo, evaluate=False), evaluate=False), -oo, Add(-oo, -oo, evaluate=False), Mul(-2, oo, evaluate=False), Mul(-2, -oo, evaluate=False), Mul(-2, -oo, -5, evaluate=False), # Calculus Limit(x**2, x, theta), # Miscellaneous Abs(x), parse_expr('n!'), parse_expr('n!!'), factorial2(2 * x + y), pi, Pow(0, 0, evaluate=False), ] failed_tests = [] for expr in test_cases: try: s_l_expr = latex(expr) l_expr = process_sympy(s_l_expr) equiv = equivalent(expr, l_expr) e = None except Exception as e: # Parsing failed l_expr = None equiv = False if not equiv: print '%s %s' % (s_l_expr, 'PASSED' if equiv else 'FAILED') failed_tests.append((s_l_expr, l_expr)) print 'sympy: %s\nlatex: %s' % (expr, l_expr) if e is not None: print e print if failed_tests: print len(failed_tests), 'failed test cases' assert len(failed_tests) == 0