I have some monster expressions, but for simplicity, consider
m = -(-3 + a+3 b-5 c)/(-d -5);
How can I check if "undisplayable" expressions contain minuses? For m
, I can use the eye to see it does, but what can I do for expressions that are too long to display?
This is in fact discussed in the question: In a long sum ... In a long sum, how can we find how many terms are preceded by the plus (or minus) sign
However, the accepted answers there
Count[m, _?Internal`SyntacticNegativeQ]
Count[m, _?Negative*Optional[__]]
yield both wrongly 0 in this case. Maybe the answers in that question do not apply to rational expressions? My favorite answer was first, for simplicity, that of @ydd
`Length[Cases[m, -_, Infinity]]`
It catches 2 of the five minuses (probably because three minuses simplified). It's nice that the AI caught that, but I didn't ask for that, so I'm a bit puzzled. Unfortunately @Michael E2 found a counterexample
Length[Cases[3b-5c, -_ , Infinity]]
where this fails. I also found an example where the solution of @Roland F (the second for simplicity) does weird things
m = -(-3 + a + 3 b - 5 c)/(-2 - d - 5);
countMSp[m_] :=
Cases[HoldForm[m], #, \[Infinity]] & /@ {(x_?Negative + ___ :>
x), (_?Negative*___)}
countMSp[m]
({{-7}, {-a, -3 b, -d}})
Length[Cases[m, -_ , Infinity]] > 0
? $\endgroup$k[3]-2
in the expression. $\endgroup$Denominator
andNumerator
for the presence of minuses. $\endgroup$FullForm
. $\endgroup$