Domanda

Suppose that I define some function, then make a change of variable and expand, as in the following lines:

declare(a,real); declare(k,real); declare(z,real);

myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;

myFun(a,k,z),simp,a=x0+x1*k;

expand(%);

What I would like to do now is to obtain a polynomial in k, i.e. collect the terms in each power of k with one command so that it shows something like:

(...)k^2 + (...)k + (...)
È stato utile?

Soluzione

declare(a,real); declare(k,real); declare(z,real);
myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;
myFun(a,k,z),simp,a=x0+x1*k;
P: expand(%);
rat(P, k);

gives

                2                     2              2
(%o7)/R/ - (((x1  - 2 x1 + 1) z - 9 x1  - 6 x1 - 1) k
                                                        2              2
        + ((2 x0 x1 - 2 x0) z - 18 x0 x1 - 6 x0) k + (x0  - 8) z - 9 x0  + 8)/8

coeff returns each of the coefficients

coeff(P, k^2);
                         2                    2
                       x1  z   x1 z   z   9 x1    3 x1   1
(%o8)                - ----- + ---- - - + ----- + ---- + -
                         8      4     8     8      4     8
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top