> why do you need an alternative to factoring I want to separate variables in an expression. It is not important the factors be factored since each factor will be solved for some variable (and, if polynomial, Poly will prefer the unfactored form). So I want the quickest method of finding those unfactored-but-separated-factors of an expression. My tests have shown that if an expression *can* be factored then the separation routine I am using (below) is, on average, about the same as factoring. When the expression cannot be separated the routine is about 3X faster. Times vary a lot, though, ranging from 2X to 20X faster.
def ift(e): """return a separation of variables without resorting to use of `factor`. Examples ======== >>> from sympy.solvers.solvers import ift >>> from sympy.abc import a,b,c,d,e,f,g,x,y,z >>> ift(a*b*e + b**2*c*d*f/(-c*d + c*g) + b**2*c*f*g/(c*d - c*g)) b*(a*e - b*f) >>> ift((z*(x + y)**2*(2*y + 4)**3).expand()) 8*z*(x**2 + 2*x*y + y**2)*(y**3 + 6*y**2 + 12*y + 8) """ try:from sympy.simplify import bottom_up except:from sympy.core.traversal import bottom_up from sympy.simplify.simplify import signsimp def do(e): free = e.free_symbols if not free or not e.args: return e e = collect(e, free.pop()) if e.is_Mul: return e.func(*[do(i) for i in e.args]) if not e.is_Add: return e i, d = e.as_independent(Add) if not d: return e return factor_terms(i) + d n, d = [factor_terms(bottom_up(i, do)) for i in e.as_numer_denom()] return factor_terms(signsimp(n/d)) On Wednesday, March 16, 2022 at 5:35:16 PM UTC-5 Oscar wrote: > On Fri, 11 Mar 2022 at 23:13, Chris Smith <smi...@gmail.com> wrote: > > > > Given two expressions, `p` and `q` > > > > p = x*y + x + y*z > > q = p + z > > > > it is easy to show that `q = (x + z)*(y + 1)`. But I'm wanting to avoid > factoring but would like to know whether a solution for `x` from `p = 0` or > `q = 0` is a factor or not. For `q`, the root `x = -z` represent a simple > factor whereas the root of x = `-yz/(y + 1)` is different. > > > > I'm lacking the vocabulary to differentiate `-z` from `-yz/(y + 1)`. Is > it that the latter has a denominator while the former does not? (And since > the original expression had no denominator then the `-z` represents a > "whole" factor while the other root does not?) > > The vocabulary I would use is to say that x + z is a polynomial factor > within the polynomial ring Z[x,y,z] (or Q[x,y,z]) whereas x-yz/(y+1) > is a polynomial factor in the polynomial ring Q(y, z)[x] which is the > ring of polynomials in x whose coefficients are rational functions of > y and z. > > > As an alternative to factoring, > > Why do you need an alternative to factoring? > > It is possible to check if one polynomial is a factor of another > without computing a full factorisation. You can just polynomial > division: > > In [18]: p = x*y + x + y*z > > In [19]: q = p + z > > In [20]: sx = solve(q, x)[0] > > In [21]: sx > Out[21]: -z > > In [22]: div(q, x - sx) > Out[22]: (y + 1, 0) > > In [24]: rem(q, x - sx) > Out[24]: 0 > > Note the remainder 0. > > -- > Oscar > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b7fd568f-c342-46c1-bb75-b212e68e1bbcn%40googlegroups.com.