def square_root_of_expr(expr): """ If expression is product of even powers then every power is divided by two and the product is returned. If some terms in product are not even powers the sqrt of the absolute value of the expression is returned. If the expression is a number the sqrt of the absolute value of the number is returned. """ if expr.is_number: if expr > 0: return(sqrt(expr)) else: return(sqrt(-expr)) else: expr = trigsimp(expr) (coef, pow_lst) = sqf_list(expr) if coef != S(1): if coef.is_number: coef = square_root_of_expr(coef) else: coef = sqrt(abs(coef)) # Product coefficient not a number for p in pow_lst: (f, n) = p if n % 2 != 0: return(sqrt(abs(expr))) # Product not all even powers else: coef *= f ** (n / 2) # Positive sqrt of the square of an expression return coef
def test3(): x, a, b, c = map(Symbol, ['x', 'a', 'b', 'c']) #f = x**3 - 8.99085235595703*x**2 + 14.963430343676*x + 24.954282699633 f = x**3 - 8.990852355957031 * x**2 + 14.96343034367601 * x + 24.95428269963304 #f = (x + 1) * (x - 4.995426177978516)**2 f = expand(f) print('f : ', f) f_ = diff(f) print('f_ : ', f_) a0 = gcd(f, f_) print('a0 : ', a0) b1 = quo(f, a0) print('b1 : ', b1) c1 = quo(f_, a0) print('c1 : ', c1) d1 = c1 - diff(b1) print('d1 : ', d1) a1 = gcd(b1, d1) print('a1 : ', a1) b2 = quo(b1, a1) print('b2 : ', b2) L = sqf_list(f) #L = sqf(f) print(L)
def _square_free(Phi,var): """ Given a polynomial `\Phi \in \mathbb{L}[Z]` returns a collection of pairs `\{(\Psi,r)\}` with `\Psi \in \mathbb{L}[Z]` and `r` a positive integer such that each `\Psi` is square-free and `\Phi = \prod \Psi^r` and the `\Psi`'s are pairwise coprime. ALGORITHM: Such a decomposition can be obtained with derivations and gcd computations without any factorization algorithm. It's implemented in sympy as ``sympy.sqf`` and ``sympy.sqf_list`` """ return sympy.sqf_list(Phi,var)[1]
def square_root_of_expr(expr): if expr.is_number: if expr > 0: return(sqrt(expr)) else: return(sqrt(-expr)) else: expr = trigsimp(expr) (coef, pow_lst) = sqf_list(expr) if coef != S(1): if coef.is_number: coef = square_root_of_expr(coef) else: coef = sqrt(abs(coef)) for p in pow_lst: (f, n) = p if n % 2 != 0: return(sqrt(abs(expr))) else: coef *= f ** (n / 2) return coef
def test2(): x, a, b, c = map(Symbol, ['x', 'a', 'b', 'c']) #f = 2*x**2 + 5*x**3 + 4*x**4 + x**5 f = (x + 2) * x**2 * (x + 1)**2 f = expand(f) #print('ex : ', expand((x + 2) * (x**2 + x)**2)) print('f : ', f) f_ = diff(f) print('f_ : ', f_) a0 = gcd(f, f_) print('a0 : ', a0) b1 = quo(f, a0) print('b1 : ', b1) c1 = quo(f_, a0) print('c1 : ', c1) d1 = c1 - diff(b1) print('d1 : ', d1) a1 = gcd(b1, d1) print('a1 : ', a1) b2 = quo(b1, a1) print('b2 : ', b2) c2 = quo(d1, a1) print('c2 : ', c2) d2 = c2 - diff(b2) print('d2 : ', d2) a2 = gcd(b2, d2) print('a2 : ', a2) b3 = quo(b2, a2) print('b3 : ', b3) L = sqf_list(f) #sqf(f) print(L)
import sympy as s from sympy.abc import x, y f = (x+1)**2 * (x+3)*(x+4) f = s.expand(f) # I expect: [(x**2+7x + 12), 1] , [(x+1), 2] # but it factors (x+3) , (x+4) print(s.sqf_list(f))