rat and bothcoef error



On 2/21/2012 1:02 AM, nijso beishuizen wrote:
> Hello Richard and others,
>
> The bothcoef description doesn't say that expr needs to be in 
> polynomial form.
True. Perhaps it should.
The point is, the command is probably meaningless unless expr is 
explicitly or implicitly a polynomial.

> Bothcoef calls ratcoef or coeff if I understand the manual correctly? 
> So there is the need for expr to be in polynomial form because it 
> calls the other routines.
that's right, but those are probably meaningless in the same circumstances.
>
> I need something like the example in bothcoef, a routine that checks 
> if expr is a linear function of the form a*x+b (I don't actually need 
> to know a and b).

There are many ways of detecting linearity.  f(x) is linear in x if   
its second derivative is zero, for example.

> The problem is that the expression can contain anything except 
> differential operators (so also sin(x), 1/x, integrals and unknown 
> functions like f(x)).
in which case bothcoef is hardly appropriate.  What is the coefficient 
of x in sin(x) ?

>
> I have browsed the manual and maybe a better method would be to use 
> the example from defmatch then:
> (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b, freeof(x));
> (%o1)                         done
> (%i2) defmatch (linearp, a*x + b, x);

This will generally work only in the same circumstances, since defmatch 
generates calls to ratcoef.

Another way of finding a and b in a*x+b  is to find b:   
subst(0,x,expr).  then to find a, subst(1,x,expr)-b.

Then you can test if a*x+b = expr.

There are probably another few plausible tricks.
RJF