Question: how to correctly find leading sign on expression?

8/24/19. Updated question the bottom

I need to find if expression has  a "-" in front of it or not.  This is for formatting purposes for something I am doing, that is all. 

I was using the command sign() for this, and it seems to work OK, but sometimes a Maple error is generated or not leading term sign is returned.

The sign function computes the sign of the leading coefficient of expr.

Is there a better and more robust way to do this other than using sign() or Am I using sign wrong?

restart;

expr:=-sin(x)/ln(y)+1;
op(expr);

-sin(x)/ln(y)+1

-sin(x)/ln(y), 1

#why this gives 1 as sign for the above?
sign(expr);

1

#workaround?
sign(op(1,expr))

-1

expr:=sin(x)/ln(y)+1;
sign(expr);

sin(x)/ln(y)+1

1

expr:=-x;
sign(expr);

-x

-1

expr:=-LambertW(exp(p))+1;
sign(expr)

-LambertW(exp(p))+1

-1

expr:=-LambertW(exp(p))/(1+LambertW(exp(p)))+1;
sign(expr)

-LambertW(exp(p))/(1+LambertW(exp(p)))+1

Error, invalid argument for sign, lcoeff or tcoeff

#it seems the following is a work around for the above case
#but why it is needed just here?
sign(op(1,expr))

-1

 

 

Download q1.mw

UPDATE

Thanks to comment below about using frontend. I never used this before except for now. It seems a very useful command and I think I will be using it a lot more from now on.

Just to clarify again what I want. I am just looking to see if there is a leading negative sign in front of an expression. I am not looking to find if the expression itself can be postive or negative when evaluated or anything more subtle or deep than that.

I simply want to check if there is literally a "-" in front of the maple expression. That is all.

Only issue left, is why Maple still gives Error, invalid argument for sign, lcoeff or tcoeff for the last example below? Here is the updated worksheet.

restart;

expr:=-sin(x)/ln(y)+1;
frontend(sign,[expr]);

-sin(x)/ln(y)+1

-1

expr:=sin(x)/ln(y)+1;
frontend(sign,[expr]);

sin(x)/ln(y)+1

1

expr:=-x;
frontend(sign,[expr]);

-x

-1

expr:=-LambertW(exp(p))+1;
frontend(print,[expr]);
frontend(sign,[expr]);

-LambertW(exp(p))+1

-O+1

-1

expr:=-LambertW(exp(p))/(1+LambertW(exp(p)))+1 ;
frontend(print,[expr]);
frontend(sign,[expr]);

-LambertW(exp(p))/(1+LambertW(exp(p)))+1

-O/(1+O)+1

Error, invalid argument for sign, lcoeff or tcoeff

 

 

Download q1_2.mw

Please Wait...