# Check if polynomial is subtraction free

I have several very long, factorised polynomials in several variables, e.g.

x1^4 x2^3 x3 x4^3 x5 x6 x7^2 x8 (1 + x2 + x2 x3 + x5 + x1 x5)


I want an easy way to check whether such a polynomial is 'subtraction free' in the sense of having no relative minus sign between terms anywhere. So

-x1(1+x2+x2*x3)


or

x1*x2+1+x3+x4*x5)


are both fine, but

x1*x2(1+x1*x3-x2)


is not.

Is there any easy way to implement this?

• What answer would you seek if the polynomial had the form -((-x - y) z)? I ask since you specified a "factorised polynomial", and I wasn't sure if that meant it shouldn't be multiplied out. Commented Sep 5, 2023 at 13:15
• Maybe "relative minus sign" means no nonzero monomials in a factor with opposite signs? Commented Sep 5, 2023 at 13:34
• Are the coefficients always integers (with head Integer)? Commented Sep 5, 2023 at 13:45
• -(-x-y)z would count as subtraction-free, yes. Rather than 'factorised polynomial' one could just expand out the whole thing and seek no relative minus signs between terms. The coefficients in my application would all be integer. Commented Sep 6, 2023 at 14:02

The expressions:

t1 = x1x2 (1 + x1x3 - x2);
t2 = -x1 (1 + x2 + x2x3);


should be bad/good. For this look at the FullForm of t1/t2:

t1//FullForm
t2//FullForm


Note the position of "-1":

Position[t1, -1]

Position[t2, -1]

{{2, 3, 1}}
{{1}}


If -1 is on level 1 it is good like in t2. In t1 -1 is deeper nested. We may exploit this lik3 e.g.:

check[ex_] := Length[Position[ex, -1][[1]]] <= 1


With this:

check[t1]
check[t2]

False
True


Perhaps something like:

f1 = FreeQ[Factor @ #, Plus[_?InternalSyntacticNegativeQ, __]] &;

f2 = FreeQ[Factor @ #, Times[_?InternalSyntacticNegativeQ, __], {1, ∞}] & ;


Examples:

expr1 = x1^4 x2^3 x3 x4^3 x5 x6 x7^2 x8 (1 + x2 + x2 x3 + x5 + x1 x5);

expr2 = - x1^4 x2^3 x3 x4^3 x5 x6 x7^2 x8 (1 + x2 + x2 x3 + x5 + x1 x5);

expr3 = x1^4 x2^3 x3 x4^3 x5 x6 x7^2 x8 (1 + x2 + x2 x3 - x5 +  x1 x5);

f1 /@ {expr1, expr2, expr3}

{True, True, False}

f2 /@ {expr1, expr2, expr3}

{True, True, False}

• expr2 seems to have a negative sign?
– Syed
Commented Sep 5, 2023 at 13:24
• @Syed, yes. "So -x1(1+x2+x2x3)  and x1*x2+1+x3+x4*x5) are both fine"
– kglr
Commented Sep 5, 2023 at 13:26
• I need to put glasses back on.
– Syed
Commented Sep 5, 2023 at 13:28
• I wonder if the output of f2[x1*x2 (1 + x1*x3 - 2 x2)] is correct? I'd expect coefficients other than -1, 0, +1 could occur, even though they are missing from the OP. Commented Sep 5, 2023 at 13:42
• Thank you @MichaelE2; updated with a fix.
– kglr
Commented Sep 5, 2023 at 13:45