Dirk Laurie on Wed, 07 Aug 2013 16:26:09 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Variable checker


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.