| Bill Allombert on Wed, 07 Aug 2013 22:49:17 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: Variable checker |
On Wed, Aug 07, 2013 at 04:25:57PM +0200, Dirk Laurie wrote:
> I have written a routine that for a given expression checks whether
> it contains any variables that are not in a given list. It works.
>
> Only problem is, I don't understand why.
>
> { foreign(expr,vars) =
> if (type(expr)=="t_RFRAC",
> return(foreign(numerator(expr)) || foreign(denominator(expr))));
> my(var=trap(,,variable(expr)));
> print(expr", "var);
> if(!var, return(0)); \\ expr is scalar
> if(subst(vars,var,0)==vars, return(var)); \\ var is not present in vars
> foreign(polcoeff(expr,0),vars) }
> addhelp(foreign,"foreign(expr,vars): 0 if expr can be written in terms
> of vars, otherwise an offending variable")
>
> ? foreign(x^2+a*x+b,[x,b])
> x^2 + a*x + b, x
> b, a
> %2 = a
>
> The value of 'expr' on the recursive call is 'b'. There's no 'a' in
> it. Yet variable(expr) returns 'a'.
>
> I'm grateful it does. But it baffles me.
subst(x^2 + a*x + b,x,0) is equal to a^0*b.
This is due to variable priority: 0+a*0+b evaluate to a polynomial in "a" because
"a" has higher priority than "b" (it was introduced earlier).
Cheers,
Bill.