def test_normal_denom(): DE = DifferentialExtension(extension={'D': [Poly(1, x)]}) raises(NonElementaryIntegralException, lambda: normal_denom(Poly(1, x), Poly(1, x), Poly(1, x), Poly(x, x), DE)) fa, fd = Poly(t**2 + 1, t), Poly(1, t) ga, gd = Poly(1, t), Poly(t**2, t) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]}) assert normal_denom(fa, fd, ga, gd, DE) == \ (Poly(t, t), (Poly(t**3 - t**2 + t - 1, t), Poly(1, t)), (Poly(1, t), Poly(1, t)), Poly(t, t))
def test_DifferentialExtension_misc(): # Odd ends assert DifferentialExtension(sin(y)*exp(x), x)._important_attrs == \ (Poly(sin(y)*t0, t0, domain='ZZ[sin(y)]'), Poly(1, t0, domain='ZZ'), [Poly(1, x, domain='ZZ'), Poly(t0, t0, domain='ZZ')], [x, t0], [Lambda(i, exp(i))], [], [None, 'exp'], [None, x]) raises(NotImplementedError, lambda: DifferentialExtension(sin(x), x)) assert DifferentialExtension(10**x, x)._important_attrs == \ (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(log(10)*t0, t0)], [x, t0], [Lambda(i, exp(i*log(10)))], [(exp(x*log(10)), 10**x)], [None, 'exp'], [None, x*log(10)]) assert DifferentialExtension(log(x) + log(x**2), x)._important_attrs in [ (Poly(3 * t0, t0), Poly(2, t0), [Poly(1, x), Poly(2 / x, t0)], [x, t0], [Lambda(i, log(i**2))], [], [ None, ], [], [1], [x**2]), (Poly(3 * t0, t0), Poly(1, t0), [Poly(1, x), Poly(1 / x, t0)], [x, t0], [Lambda(i, log(i))], [], [None, 'log'], [None, x]) ] assert DifferentialExtension(S.Zero, x)._important_attrs == \ (Poly(0, x), Poly(1, x), [Poly(1, x)], [x], [], [], [None], [None]) assert DifferentialExtension(tan(atan(x).rewrite(log)), x)._important_attrs == \ (Poly(x, x), Poly(1, x), [Poly(1, x)], [x], [], [], [None], [None])
def random_poly(length, d, neg_ones_diff=0): return Poly( np.random.permutation( np.concatenate((np.zeros(length - 2 * d - neg_ones_diff), np.ones(d), -np.ones(d + neg_ones_diff)))), x).set_domain(ZZ)
def test_DifferentialExtension_exp(): assert DifferentialExtension(exp(x) + exp(x**2), x)._important_attrs == \ (Poly(t1 + t0, t1), Poly(1, t1), [Poly(1, x,), Poly(t0, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2]) assert DifferentialExtension(exp(x) + exp(2*x), x)._important_attrs == \ (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0, t0)], [x, t0], [Lambda(i, exp(i))], [], [None, 'exp'], [None, x]) assert DifferentialExtension(exp(x) + exp(x/2), x)._important_attrs == \ (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0], [Lambda(i, exp(i/2))], [], [None, 'exp'], [None, x/2]) assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2), x)._important_attrs == \ (Poly((1 + t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2]) assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2 + 1), x)._important_attrs == \ (Poly((1 + S.Exp1*t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2]) assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2), x)._important_attrs == \ (Poly((t0 + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x), Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i/2)), Lambda(i, exp(i**2))], [(exp(x/2), sqrt(exp(x)))], [None, 'exp', 'exp'], [None, x/2, x**2]) assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2 + 3), x)._important_attrs == \ (Poly((t0*exp(3) + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x), Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i/2)), Lambda(i, exp(i**2))], [(exp(x/2), sqrt(exp(x)))], [None, 'exp', 'exp'], [None, x/2, x**2]) assert DifferentialExtension(sqrt(exp(x)), x)._important_attrs == \ (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0], [Lambda(i, exp(i/2))], [(exp(x/2), sqrt(exp(x)))], [None, 'exp'], [None, x/2]) assert DifferentialExtension(exp(x/2), x)._important_attrs == \ (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0], [Lambda(i, exp(i/2))], [], [None, 'exp'], [None, x/2])
def test_DifferentialExtension_symlog(): # See comment on test_risch_integrate below assert DifferentialExtension(log(x**x), x)._important_attrs == \ (Poly(t0*x, t1), Poly(1, t1), [Poly(1, x), Poly(1/x, t0), Poly((t0 + 1)*t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(i*t0))], [(exp(x*log(x)), x**x)], [None, 'log', 'exp'], [None, x, t0*x]) assert DifferentialExtension(log(x**y), x)._important_attrs == \ (Poly(y*t0, t0), Poly(1, t0), [Poly(1, x), Poly(1/x, t0)], [x, t0], [Lambda(i, log(i))], [(y*log(x), log(x**y))], [None, 'log'], [None, x]) assert DifferentialExtension(log(sqrt(x)), x)._important_attrs == \ (Poly(t0, t0), Poly(2, t0), [Poly(1, x), Poly(1/x, t0)], [x, t0], [Lambda(i, log(i))], [(log(x)/2, log(sqrt(x)))], [None, 'log'], [None, x])
def test_frac_in(): assert frac_in(Poly((x + 1)/x*t, t), x) == \ (Poly(t*x + t, x), Poly(x, x)) assert frac_in((x + 1)/x*t, x) == \ (Poly(t*x + t, x), Poly(x, x)) assert frac_in((Poly((x + 1)/x*t, t), Poly(t + 1, t)), x) == \ (Poly(t*x + t, x), Poly((1 + t)*x, x)) raises(ValueError, lambda: frac_in((x + 1) / log(x) * t, x)) assert frac_in(Poly((2 + 2*x + x*(1 + x))/(1 + x)**2, t), x, cancel=True) == \ (Poly(x + 2, x), Poly(x + 1, x))
def test_integrate_hyperexponential_polynomial(): # Without proper cancellation within integrate_hyperexponential_polynomial(), # this will take a long time to complete, and will return a complicated # expression p = Poly( (-28 * x**11 * t0 - 6 * x**8 * t0 + 6 * x**9 * t0 - 15 * x**8 * t0**2 + 15 * x**7 * t0**2 + 84 * x**10 * t0**2 - 140 * x**9 * t0**3 - 20 * x**6 * t0**3 + 20 * x**7 * t0**3 - 15 * x**6 * t0**4 + 15 * x**5 * t0**4 + 140 * x**8 * t0**4 - 84 * x**7 * t0**5 - 6 * x**4 * t0**5 + 6 * x**5 * t0**5 + x**3 * t0**6 - x**4 * t0**6 + 28 * x**6 * t0**6 - 4 * x**5 * t0**7 + x**9 - x**10 + 4 * x**12) / (-8 * x**11 * t0 + 28 * x**10 * t0**2 - 56 * x**9 * t0**3 + 70 * x**8 * t0**4 - 56 * x**7 * t0**5 + 28 * x**6 * t0**6 - 8 * x**5 * t0**7 + x**4 * t0**8 + x**12) * t1**2 + (-28 * x**11 * t0 - 12 * x**8 * t0 + 12 * x**9 * t0 - 30 * x**8 * t0**2 + 30 * x**7 * t0**2 + 84 * x**10 * t0**2 - 140 * x**9 * t0**3 - 40 * x**6 * t0**3 + 40 * x**7 * t0**3 - 30 * x**6 * t0**4 + 30 * x**5 * t0**4 + 140 * x**8 * t0**4 - 84 * x**7 * t0**5 - 12 * x**4 * t0**5 + 12 * x**5 * t0**5 - 2 * x**4 * t0**6 + 2 * x**3 * t0**6 + 28 * x**6 * t0**6 - 4 * x**5 * t0**7 + 2 * x**9 - 2 * x**10 + 4 * x**12) / (-8 * x**11 * t0 + 28 * x**10 * t0**2 - 56 * x**9 * t0**3 + 70 * x**8 * t0**4 - 56 * x**7 * t0**5 + 28 * x**6 * t0**6 - 8 * x**5 * t0**7 + x**4 * t0**8 + x**12) * t1 + (-2 * x**2 * t0 + 2 * x**3 * t0 + x * t0**2 - x**2 * t0**2 + x**3 - x**4) / (-4 * x**5 * t0 + 6 * x**4 * t0**2 - 4 * x**3 * t0**3 + x**2 * t0**4 + x**6), t1, z, expand=False) DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(1 / x, t0), Poly(2 * x * t1, t1)]}) assert integrate_hyperexponential_polynomial(p, DE, z) == (Poly( (x - t0) * t1**2 + (-2 * t0 + 2 * x) * t1, t1), Poly(-2 * x * t0 + x**2 + t0**2, t1), True) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t0, t0)]}) assert integrate_hyperexponential_polynomial(Poly(0, t0), DE, z) == (Poly(0, t0), Poly(1, t0), True)
def test_latex_Poly(): assert latex(Poly(x**2 + 2 * x, x)) == r"x^{2} + 2 x"
def test_sympify_poly(): p = Poly(x**2 + x + 1, x) assert _sympify(p) is p assert sympify(p) is p
""" # Importa função Poly, para definir polinômio. # Indica que x é a variável independente de sympy.abc. # Importa função solve_rational_inequalities de sympy.solvers.inequalities # para resolver inequações racionais. from sympy import Poly from sympy.abc import x from sympy.solvers.inequalities import solve_rational_inequalities # Limpa a área de console para facilitar a visualização do resultado. print('\n' * 100) # Resolve inequação [(x-1)/(x+4)] <= 0 resultado = solve_rational_inequalities([[ ((Poly(x - 1), Poly(x + 4)), '<='), ]]) print(u'Solução da inequação [(x-1)/(x+4)] <= 0') print(resultado) print(u'Interpretando resultado:') print(u'Solução -4 < x <= 1.') print('\n') # Resolve inequação [(x-1)/(x+4)] > 0 resultado = solve_rational_inequalities([[ ((Poly(x - 1), Poly(x + 4)), '>'), ]]) print(u'Solução da inequação [(x-1)/(x+4)] > 0') print(resultado) print(u'Interpretando resultado:') print(u'Solução x < -4 ou x > 1.')
# See equations 4.4.3 and 4.11.4 of Kane & Levinson Fr_c = Fr_u[:3, :].col_join(Fr_u[6:, :]) + A_rs.T * Fr_u[3:6, :] Fr_star_c = Fr_star_u[:3, :].col_join(Fr_star_u[6:, :])\ + A_rs.T * Fr_star_u[3:6, :] Fr_star_steady = Fr_star_c.subs(ud_zero).subs(u_dep_dict)\ .subs(steady_conditions).subs({q[3]: -r*cos(q[1])}).expand() mprint(Fr_c) mprint(Fr_star_steady) # First dynamic equation, under steady conditions is 2nd order polynomial in # dq0/dt. steady_turning_dynamic_equation = Fr_c[0] + Fr_star_steady[0] # Equilibrium is posible when the solution to this quadratic is real, i.e., # when the discriminant in the quadratic is non-negative p = Poly(steady_turning_dynamic_equation, qd[0]) a, b, c = p.coeffs() discriminant = b*b - 4*a*c # Must be non-negative for equilibrium # in case of thin disc inertia assumptions #mprint((discriminant / (r**3 * m**2)).expand()) # ADD ALL CODE DIRECTLY BELOW HERE, do not change above! # Think there should be at 12 assertion tests: # 1) Fr[i] == fr from KanesMethod i = 0, ..., 5 # 2) Fr_star[i] == frstar from KanesMethod i = 0, ..., 5 # if 2) is slow, try comparing this instead: # 2a) Fr_star_steady[i] == frstar from KanesMethod, evaluated at steady turning # conditions. # This should be something like frstar.subs(ud_zero).subs(steady_conditions)
def test_has_polys(): poly = Poly(x**2 + x * y * sin(z), x, y, t) assert poly.has(x) assert poly.has(x, y, z) assert poly.has(x, y, z, t)
for monos in monomios: list_grados = [] for var in list_variables: grado = degree(monos, gen=var) list_grados.append(grado) if np.sum(list_grados) <= order: f_4 = f_4 + monos * coef1_2[k] k = k + 1 return f_4 #%% order = 4 K_x, coef = K_var_no_central([y, a], order) eq1 = Poly(xpunto, x, y).subs(x, K_x) eq1_filtrada = filtro_f(eq1, [x, y, a], order) eq2 = Poly(diff(K_x, y) * ypunto, x, y, a) eq2 = eq2.subs(x, K_x) eq2_filtrada = filtro_f(eq2, [x, y, a], order) eq_final = Poly(eq1_filtrada - eq2_filtrada, x, y, a) eq_final_info = eq_final.as_expr().as_coefficients_dict() monomios = eq_final_info.keys() monomios2 = [] for monos in monomios: monomios2.append(monos) list_res = {} for j in range(len(monomios2)):
def test_special_denom(): # TODO: add more tests here DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) assert special_denom(Poly(1, t), Poly(t**2, t), Poly(1, t), Poly(t**2 - 1, t), Poly(t, t), DE) == \ (Poly(1, t), Poly(t**2 - 1, t), Poly(t**2 - 1, t), Poly(t, t)) # assert special_denom(Poly(1, t), Poly(2*x, t), Poly((1 + 2*x)*t, t), DE) == 1 # Issue 841 # Note, this isn't a very good test, because the denominator is just 1, # but at least it tests the exp cancellation case DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-2*x*t0, t0), Poly(I*k*t1, t1)]}) DE.decrement_level() assert special_denom(Poly(1, t0), Poly(I*k, t0), Poly(1, t0), Poly(t0, t0), Poly(1, t0), DE) == \ (Poly(1, t0), Poly(I*k, t0), Poly(t0, t0), Poly(1, t0))
def test_recognize_log_derivative(): a = Poly(2 * x**2 + 4 * x * t - 2 * t - x**2 * t, t) d = Poly((2 * x + t) * (t + x**2), t) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) assert recognize_log_derivative(a, d, DE, z) == True DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 / x, t)]}) assert recognize_log_derivative(Poly(t + 1, t), Poly(t + x, t), DE) == True assert recognize_log_derivative(Poly(2, t), Poly(t**2 - 1, t), DE) == True DE = DifferentialExtension(extension={'D': [Poly(1, x)]}) assert recognize_log_derivative(Poly(1, x), Poly(x**2 - 2, x), DE) == False assert recognize_log_derivative(Poly(1, x), Poly(x**2 + x, x), DE) == True DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]}) assert recognize_log_derivative(Poly(1, t), Poly(t**2 - 2, t), DE) == False assert recognize_log_derivative(Poly(1, t), Poly(t**2 + t, t), DE) == False
def __validate_assumptions(self, field, params): # Validate assumptions and compute formula parameters. # TODO: Should this also validate coordinate assumptions and compute their parameters? is_symbolic = any(isinstance(x, SymbolicMod) for x in params.values()) for assumption in self.assumptions: assumption_string = unparse(assumption)[1:-2] lhs, rhs = assumption_string.split(" == ") if lhs in params: # Handle an assumption check on value of input points. alocals: Dict[str, Union[Mod, int]] = {**params} compiled = compile(assumption, "", mode="eval") holds = eval(compiled, None, alocals) if not holds: # The assumption doesn't hold, see what is the current configured action and do it. raise_unsatisified_assumption( getconfig().ec.unsatisfied_formula_assumption_action, f"Unsatisfied assumption in the formula ({assumption_string}).", ) elif lhs in self.parameters and is_symbolic: # Handle a symbolic assignment to a new parameter. k = FF(field) expr = sympify(rhs, evaluate=False) for curve_param, value in params.items(): if isinstance(value, SymbolicMod): expr = expr.subs(curve_param, value.x) else: expr = expr.subs(curve_param, k(value)) params[lhs] = SymbolicMod(expr, field) else: k = FF(field) expr = sympify(f"{rhs} - {lhs}", evaluate=False) for curve_param, value in params.items(): if isinstance(value, SymbolicMod): expr = expr.subs(curve_param, value.x) else: expr = expr.subs(curve_param, k(value)) if (len(expr.free_symbols) > 1 or (param := str( expr.free_symbols.pop())) not in self.parameters): raise ValueError( f"This formula couldn't be executed due to an unsupported assumption ({assumption_string})." ) def resolve(expression): if not expression.args: return expression args = [] for arg in expression.args: if isinstance(arg, Rational): a = arg.p b = arg.q arg = k(a) / k(b) else: arg = resolve(arg) args.append(arg) return expression.func(*args) expr = resolve(expr) poly = Poly(expr, symbols(param), domain=k) roots = poly.ground_roots() for root in roots: params[param] = Mod(int(root), field) break else: raise UnsatisfiedAssumptionError( f"Unsatisfied assumption in the formula ({assumption_string}).\n" f"'{expr}' has no roots in the base field {k}.")
def test_residue_reduce(): a = Poly(2 * t**2 - t - x**2, t) d = Poly(t**3 - x**2 * t, t) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(1 / x, t)], 'Tfuncs': [log] }) assert residue_reduce(a, d, DE, z, invert=False) == \ ([(Poly(z**2 - S(1)/4, z), Poly((1 + 3*x*z - 6*z**2 - 2*x**2 + 4*x**2*z**2)*t - x*z + x**2 + 2*x**2*z**2 - 2*z*x**3, t))], False) assert residue_reduce(a, d, DE, z, invert=True) == \ ([(Poly(z**2 - S(1)/4, z), Poly(t + 2*x*z, t))], False) assert residue_reduce(Poly(-2/x, t), Poly(t**2 - 1, t,), DE, z, invert=False) == \ ([(Poly(z**2 - 1, z), Poly(-2*z*t/x - 2/x, t))], True) ans = residue_reduce(Poly(-2 / x, t), Poly(t**2 - 1, t), DE, z, invert=True) assert ans == ([(Poly(z**2 - 1, z), Poly(t + z, t))], True) assert residue_reduce_to_basic(ans[0], DE, z) == -log(-1 + log(x)) + log(1 + log(x)) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(-t**2 - t / x - (1 - nu**2 / x**2), t)] }) # TODO: Skip or make faster assert residue_reduce(Poly((-2*nu**2 - x**4)/(2*x**2)*t - (1 + x**2)/x, t), Poly(t**2 + 1 + x**2/2, t), DE, z) == \ ([(Poly(z + S(1)/2, z, domain='QQ'), Poly(t**2 + 1 + x**2/2, t, domain='EX'))], True) DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(1 + t**2, t)]}) assert residue_reduce(Poly(-2*x*t + 1 - x**2, t), Poly(t**2 + 2*x*t + 1 + x**2, t), DE, z) == \ ([(Poly(z**2 + S(1)/4, z), Poly(t + x + 2*z, t))], True) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) assert residue_reduce(Poly(t, t), Poly(t + sqrt(2), t), DE, z) == \ ([(Poly(z - 1, z), Poly(t + sqrt(2), t))], True)
def asPoly(arr): """ Convert a NumPy array to a polynomial """ return Poly(arr[::-1],x).set_domain(ZZ)
def test_integrate_hyperexponential(): # TODO: Add tests for integrate_hyperexponential() from the book a = Poly( (1 + 2 * t1 + t1**2 + 2 * t1**3) * t**2 + (1 + t1**2) * t + 1 + t1**2, t) d = Poly(1, t) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(1 + t1**2, t1), Poly(t * (1 + t1**2), t)], 'Tfuncs': [tan, Lambda(i, exp(tan(i)))] }) assert integrate_hyperexponential(a, d, DE) == \ (exp(2*tan(x))*tan(x) + exp(tan(x)), 1 + t1**2, True) a = Poly((t1**3 + (x + 1) * t1**2 + t1 + x + 2) * t, t) assert integrate_hyperexponential(a, d, DE) == \ ((x + tan(x))*exp(tan(x)), 0, True) a = Poly(t, t) d = Poly(1, t) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(2 * x * t, t)], 'Tfuncs': [Lambda(i, exp(x**2))] }) assert integrate_hyperexponential(a, d, DE) == \ (0, NonElementaryIntegral(exp(x**2), x), False) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(t, t)], 'Tfuncs': [exp] }) assert integrate_hyperexponential(a, d, DE) == (exp(x), 0, True) a = Poly( 25 * t**6 - 10 * t**5 + 7 * t**4 - 8 * t**3 + 13 * t**2 + 2 * t - 1, t) d = Poly(25 * t**6 + 35 * t**4 + 11 * t**2 + 1, t) assert integrate_hyperexponential(a, d, DE) == \ (-(11 - 10*exp(x))/(5 + 25*exp(2*x)) + log(1 + exp(2*x)), -1, True) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(t0, t0), Poly(t0 * t, t)], 'Tfuncs': [exp, Lambda(i, exp(exp(i)))] }) assert integrate_hyperexponential(Poly(2 * t0 * t**2, t), Poly(1, t), DE) == (exp(2 * exp(x)), 0, True) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(t0, t0), Poly(-t0 * t, t)], 'Tfuncs': [exp, Lambda(i, exp(-exp(i)))] }) assert integrate_hyperexponential(Poly(-27*exp(9) - 162*t0*exp(9) + 27*x*t0*exp(9), t), Poly((36*exp(18) + x**2*exp(18) - 12*x*exp(18))*t, t), DE) == \ (27*exp(exp(x))/(-6*exp(9) + x*exp(9)), 0, True) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(t, t)], 'Tfuncs': [exp] }) assert integrate_hyperexponential(Poly(x**2/2*t, t), Poly(1, t), DE) == \ ((2 - 2*x + x**2)*exp(x)/2, 0, True) assert integrate_hyperexponential(Poly(1 + t, t), Poly(t, t), DE) == \ (-exp(-x), 1, True) # x - exp(-x) assert integrate_hyperexponential(Poly(x, t), Poly(t + 1, t), DE) == \ (0, NonElementaryIntegral(x/(1 + exp(x)), x), False) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(1 / x, t0), Poly(2 * x * t1, t1)], 'Tfuncs': [log, Lambda(i, exp(i**2))] }) elem, nonelem, b = integrate_hyperexponential( Poly( (8 * x**7 - 12 * x**5 + 6 * x**3 - x) * t1**4 + (8 * t0 * x**7 - 8 * t0 * x**6 - 4 * t0 * x**5 + 2 * t0 * x**3 + 2 * t0 * x**2 - t0 * x + 24 * x**8 - 36 * x**6 - 4 * x**5 + 22 * x**4 + 4 * x**3 - 7 * x**2 - x + 1) * t1**3 + (8 * t0 * x**8 - 4 * t0 * x**6 - 16 * t0 * x**5 - 2 * t0 * x**4 + 12 * t0 * x**3 + t0 * x**2 - 2 * t0 * x + 24 * x**9 - 36 * x**7 - 8 * x**6 + 22 * x**5 + 12 * x**4 - 7 * x**3 - 6 * x**2 + x + 1) * t1**2 + (8 * t0 * x**8 - 8 * t0 * x**6 - 16 * t0 * x**5 + 6 * t0 * x**4 + 10 * t0 * x**3 - 2 * t0 * x**2 - t0 * x + 8 * x**10 - 12 * x**8 - 4 * x**7 + 2 * x**6 + 12 * x**5 + 3 * x**4 - 9 * x**3 - x**2 + 2 * x) * t1 + 8 * t0 * x**7 - 12 * t0 * x**6 - 4 * t0 * x**5 + 8 * t0 * x**4 - t0 * x**2 - 4 * x**7 + 4 * x**6 + 4 * x**5 - 4 * x**4 - x**3 + x**2, t1), Poly((8 * x**7 - 12 * x**5 + 6 * x**3 - x) * t1**4 + (24 * x**8 + 8 * x**7 - 36 * x**6 - 12 * x**5 + 18 * x**4 + 6 * x**3 - 3 * x**2 - x) * t1**3 + (24 * x**9 + 24 * x**8 - 36 * x**7 - 36 * x**6 + 18 * x**5 + 18 * x**4 - 3 * x**3 - 3 * x**2) * t1**2 + (8 * x**10 + 24 * x**9 - 12 * x**8 - 36 * x**7 + 6 * x**6 + 18 * x**5 - x**4 - 3 * x**3) * t1 + 8 * x**10 - 12 * x**8 + 6 * x**6 - x**4, t1), DE) assert factor(elem) == -((x - 1) * log(x) / ((x + exp(x**2)) * (2 * x**2 - 1))) assert (nonelem, b) == (NonElementaryIntegral(exp(x**2) / (exp(x**2) + 1), x), False)
def __init__(self, N, p, q): self.N = N self.p = p self.q = q self.R = Poly(x**N - 1, x).set_domain(ZZ)
def test_integrate_primitive(): DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(1 / x, t)], 'Tfuncs': [log] }) assert integrate_primitive(Poly(t, t), Poly(1, t), DE) == (x * log(x), -1, True) assert integrate_primitive(Poly(x, t), Poly(t, t), DE) == (0, NonElementaryIntegral(x / log(x), x), False) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(1 / x, t1), Poly(1 / (x + 1), t2)], 'Tfuncs': [log, Lambda(i, log(i + 1))] }) assert integrate_primitive(Poly(t1, t2), Poly(t2, t2), DE) == \ (0, NonElementaryIntegral(log(x)/log(1 + x), x), False) DE = DifferentialExtension( extension={ 'D': [Poly(1, x), Poly(1 / x, t1), Poly(1 / (x * t1), t2)], 'Tfuncs': [log, Lambda(i, log(log(i)))] }) assert integrate_primitive(Poly(t2, t2), Poly(t1, t2), DE) == \ (0, NonElementaryIntegral(log(log(x))/log(x), x), False) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(1 / x, t0)], 'Tfuncs': [log] }) assert integrate_primitive(Poly(x**2*t0**3 + (3*x**2 + x)*t0**2 + (3*x**2 + 2*x)*t0 + x**2 + x, t0), Poly(x**2*t0**4 + 4*x**2*t0**3 + 6*x**2*t0**2 + 4*x**2*t0 + x**2, t0), DE) == \ (-1/(log(x) + 1), NonElementaryIntegral(1/(log(x) + 1), x), False)
def test_canonical_representation(): DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(1 + t**2, t)]}) assert canonical_representation(Poly(x - t, t), Poly(t**2, t), DE) == \ (Poly(0, t), (Poly(0, t), Poly(1, t)), (Poly(-t + x, t), Poly(t**2, t))) DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]}) assert canonical_representation(Poly(t**5 + t**3 + x**2*t + 1, t), Poly((t**2 + 1)**3, t), DE) == \ (Poly(0, t), (Poly(t**5 + t**3 + x**2*t + 1, t), Poly(t**6 + 3*t**4 + 3*t**2 + 1, t)), (Poly(0, t), Poly(1, t)))
def test_derivation(): p = Poly( 4 * x**4 * t**5 + (-4 * x**3 - 4 * x**4) * t**4 + (-3 * x**2 + 2 * x**3) * t**3 + (2 * x + 7 * x**2 + 2 * x**3) * t**2 + (1 - 4 * x - 4 * x**2) * t - 1 + 2 * x, t) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(-t**2 - 3 / (2 * x) * t + 1 / (2 * x), t)] }) assert derivation(p, DE) == Poly( -20 * x**4 * t**6 + (2 * x**3 + 16 * x**4) * t**5 + (21 * x**2 + 12 * x**3) * t**4 + (7 * x / 2 - 25 * x**2 - 12 * x**3) * t**3 + (-5 - 15 * x / 2 + 7 * x**2) * t**2 - (3 - 8 * x - 10 * x**2 - 4 * x**3) / (2 * x) * t + (1 - 4 * x**2) / (2 * x), t) assert derivation(Poly(1, t), DE) == Poly(0, t) assert derivation(Poly(t, t), DE) == DE.d assert derivation(Poly(t**2 + 1/x*t + (1 - 2*x)/(4*x**2), t), DE) == \ Poly(-2*t**3 - 4/x*t**2 - (5 - 2*x)/(2*x**2)*t - (1 - 2*x)/(2*x**3), t, domain='ZZ(x)') DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(1 / x, t1), Poly(t, t)]}) assert derivation(Poly(x * t * t1, t), DE) == Poly(t * t1 + x * t * t1 + t, t) assert derivation(Poly(x*t*t1, t), DE, coefficientD=True) == \ Poly((1 + t1)*t, t) DE = DifferentialExtension(extension={'D': [Poly(1, x)]}) assert derivation(Poly(x, x), DE) == Poly(1, x) # Test basic option assert derivation((x + 1) / (x - 1), DE, basic=True) == -2 / (1 - 2 * x + x**2) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) assert derivation((t + 1) / (t - 1), DE, basic=True) == -2 * t / (1 - 2 * t + t**2) assert derivation(t + 1, DE, basic=True) == t
def test_hermite_reduce(): DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]}) assert hermite_reduce(Poly(x - t, t), Poly(t**2, t), DE) == \ ((Poly(-x, t), Poly(t, t)), (Poly(0, t), Poly(1, t)), (Poly(-x, t), Poly(1, t))) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(-t**2 - t / x - (1 - nu**2 / x**2), t)] }) assert hermite_reduce( Poly(x**2*t**5 + x*t**4 - nu**2*t**3 - x*(x**2 + 1)*t**2 - (x**2 - nu**2)*t - x**5/4, t), Poly(x**2*t**4 + x**2*(x**2 + 2)*t**2 + x**2 + x**4 + x**6/4, t), DE) == \ ((Poly(-x**2 - 4, t), Poly(4*t**2 + 2*x**2 + 4, t)), (Poly((-2*nu**2 - x**4)*t - (2*x**3 + 2*x), t), Poly(2*x**2*t**2 + x**4 + 2*x**2, t)), (Poly(x*t + 1, t), Poly(x, t))) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 / x, t)]}) a = Poly( (-2 + 3 * x) * t**3 + (-1 + x) * t**2 + (-4 * x + 2 * x**2) * t + x**2, t) d = Poly( x * t**6 - 4 * x**2 * t**5 + 6 * x**3 * t**4 - 4 * x**4 * t**3 + x**5 * t**2, t) assert hermite_reduce(a, d, DE) == \ ((Poly(3*t**2 + t + 3*x, t), Poly(3*t**4 - 9*x*t**3 + 9*x**2*t**2 - 3*x**3*t, t)), (Poly(0, t), Poly(1, t)), (Poly(0, t), Poly(1, t))) assert hermite_reduce( Poly(-t**2 + 2*t + 2, t), Poly(-x*t**2 + 2*x*t - x, t), DE) == \ ((Poly(3, t), Poly(t - 1, t)), (Poly(0, t), Poly(1, t)), (Poly(1, t), Poly(x, t))) assert hermite_reduce( Poly(-x**2*t**6 + (-1 - 2*x**3 + x**4)*t**3 + (-3 - 3*x**4)*t**2 - 2*x*t - x - 3*x**2, t), Poly(x**4*t**6 - 2*x**2*t**3 + 1, t), DE) == \ ((Poly(x**3*t + x**4 + 1, t), Poly(x**3*t**3 - x, t)), (Poly(0, t), Poly(1, t)), (Poly(-1, t), Poly(x**2, t))) assert hermite_reduce( Poly((-2 + 3*x)*t**3 + (-1 + x)*t**2 + (-4*x + 2*x**2)*t + x**2, t), Poly(x*t**6 - 4*x**2*t**5 + 6*x**3*t**4 - 4*x**4*t**3 + x**5*t**2, t), DE) == \ ((Poly(3*t**2 + t + 3*x, t), Poly(3*t**4 - 9*x*t**3 + 9*x**2*t**2 - 3*x**3*t, t)), (Poly(0, t), Poly(1, t)), (Poly(0, t), Poly(1, t)))
def test_DifferentialExtension_handle_first(): assert DifferentialExtension(exp(x)*log(x), x, handle_first='log')._important_attrs == \ (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(1/x, t0), Poly(t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(i))], [], [None, 'log', 'exp'], [None, x, x]) assert DifferentialExtension(exp(x)*log(x), x, handle_first='exp')._important_attrs == \ (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0), Poly(1/x, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, log(i))], [], [None, 'exp', 'log'], [None, x, x]) # This one must have the log first, regardless of what we set it to # (because the log is inside of the exponential: x**x == exp(x*log(x))) assert DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x, handle_first='exp')._important_attrs == \ DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x, handle_first='log')._important_attrs == \ (Poly((-1 + x - x*t0**2)*t1, t1), Poly(x, t1), [Poly(1, x), Poly(1/x, t0), Poly((1 + t0)*t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(t0*i))], [(exp(x*log(x)), x**x)], [None, 'log', 'exp'], [None, x, t0*x])
def test_gcdex_diophantine(): assert gcdex_diophantine(Poly(x**4 - 2*x**3 - 6*x**2 + 12*x + 15), Poly(x**3 + x**2 - 4*x - 4), Poly(x**2 - 1)) == \ (Poly((-x**2 + 4*x - 3)/5), Poly((x**3 - 7*x**2 + 16*x - 10)/5))
def test_splitfactor(): p = Poly(4 * x**4 * t**5 + (-4 * x**3 - 4 * x**4) * t**4 + (-3 * x**2 + 2 * x**3) * t**3 + (2 * x + 7 * x**2 + 2 * x**3) * t**2 + (1 - 4 * x - 4 * x**2) * t - 1 + 2 * x, t, field=True) DE = DifferentialExtension(extension={ 'D': [Poly(1, x), Poly(-t**2 - 3 / (2 * x) * t + 1 / (2 * x), t)] }) assert splitfactor(p, DE) == (Poly( 4 * x**4 * t**3 + (-8 * x**3 - 4 * x**4) * t**2 + (4 * x**2 + 8 * x**3) * t - 4 * x**2, t), Poly(t**2 + 1 / x * t + (1 - 2 * x) / (4 * x**2), t, domain='ZZ(x)')) assert splitfactor(Poly(x, t), DE) == (Poly(x, t), Poly(1, t)) r = Poly( -4 * x**4 * z**2 + 4 * x**6 * z**2 - z * x**3 - 4 * x**5 * z**3 + 4 * x**3 * z**3 + x**4 + z * x**5 - x**6, t) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 / x, t)]}) assert splitfactor(r, DE, coefficientD=True) == \ (Poly(x*z - x**2 - z*x**3 + x**4, t), Poly(-x**2 + 4*x**2*z**2, t)) assert splitfactor_sqf(r, DE, coefficientD=True) == \ (((Poly(x*z - x**2 - z*x**3 + x**4, t), 1),), ((Poly(-x**2 + 4*x**2*z**2, t), 1),)) assert splitfactor(Poly(0, t), DE) == (Poly(0, t), Poly(1, t)) assert splitfactor_sqf(Poly(0, t), DE) == (((Poly(0, t), 1), ), ())
def test_recognize_derivative(): DE = DifferentialExtension(extension={'D': [Poly(1, t)]}) a = Poly(36, t) d = Poly((t - 2) * (t**2 - 1)**2, t) assert recognize_derivative(a, d, DE) == False DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 / x, t)]}) a = Poly(2, t) d = Poly(t**2 - 1, t) assert recognize_derivative(a, d, DE) == False assert recognize_derivative(Poly(x * t, t), Poly(1, t), DE) == True DE = DifferentialExtension( extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]}) assert recognize_derivative(Poly(t, t), Poly(1, t), DE) == True
def test_solve_poly_inequality(): assert psolve(Poly(0, x), '==') == [S.Reals] assert psolve(Poly(1, x), '==') == [S.EmptySet] assert psolve(PurePoly(x + 1, x), ">") == [Interval(-1, oo, True, False)]
def test_weak_normalizer(): a = Poly((1 + x)*t**5 + 4*t**4 + (-1 - 3*x)*t**3 - 4*t**2 + (-2 + 2*x)*t, t) d = Poly(t**4 - 3*t**2 + 2, t) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]}) r = weak_normalizer(a, d, DE, z) assert r == (Poly(t**5 - t**4 - 4*t**3 + 4*t**2 + 4*t - 4, t), (Poly((1 + x)*t**2 + x*t, t), Poly(t + 1, t))) assert weak_normalizer(r[1][0], r[1][1], DE) == (Poly(1, t), r[1]) r = weak_normalizer(Poly(1 + t**2), Poly(t**2 - 1, t), DE, z) assert r == (Poly(t**4 - 2*t**2 + 1, t), (Poly(-3*t**2 + 1, t), Poly(t**2 - 1, t))) assert weak_normalizer(r[1][0], r[1][1], DE, z) == (Poly(1, t), r[1]) DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1 + t**2)]}) r = weak_normalizer(Poly(1 + t**2), Poly(t, t), DE, z) assert r == (Poly(t, t), (Poly(0, t), Poly(1, t))) assert weak_normalizer(r[1][0], r[1][1], DE, z) == (Poly(1, t), r[1])