Menu

#4776 trigtools/trigfactor is not robust

None
open
nobody
trigtools (5)
5
2026-06-14
2026-06-14
No
load(trigtools)$
trigfactor(2) => 2
trigfactor(x) => x
trigfactor(x+2) => ERROR
trigfactor(sin(x)+cos(x)) => sqrt(2)*cos(x-%pi/4)   <<< Main use case
trigfactor(2*sin(x)+2*cos(x)) => 2*sin(x)+2*cos(x)  <<< not very helpful
trigfactor(2*(sin(x)+cos(x))) => 2*(sin(x) + cos(x))  <<< ditto
scanmap('trigfactor,2*(sin(x)+cos(x))) => ERROR

At the very least, it shouldn't throw errors, but just return the argument, something like this:

safetrigfactor(ex):=
      block([rr: errcatch(trigfactor(ex))],
               if rr=[] then ex else first(rr));
 scanmap('safetrigfactor,2*(sin(x)+cos(x))) => 2

This still doesn't help with trigfactor(sin(x)+cos(x)+1).

I just looked at the source of trigfactor. It follows the very poor programming practice of defining a global function st -- easily fixed with local(st).

Of course, it is in contrib, so in theory there are no guarantees about how good it is, but....

Discussion


Log in to post a comment.

Auth0 Logo