Search code examples
maximawxmaxima

How to dynamically define multiple polynomials inside a loop in Maxima


So...I want to create five different polynomials inside a loop in order to make a Sturm sequence, but I don't seem to be able to dynamically name a set of polynomials with different names.

For example:

In the first iteration it would define p1(x):whatever

Then, in the second iteration it would define p2(x):whatever

Lastly, in the Nth iteration it would define pn(x):whatever

So far, I have managed to simply store them in a list and call them one by one by its position. But surely there is a more professional way to accomplish this?

Sorry for the non-technical language :)


Solution

  • I think a subscripted variable is appropriate here. Something like:

    for k:1 thru 5 do
        p[k] : make_my_polynomial(k);
    

    Then p[1], ..., p[5] are your polynomials.

    When you assign to a subscripted variable e.g. something like foo[bar]: baz, where foo hasn't been defined as a list or array already, Maxima creates what it calls an "undeclared array", which is just a lookup table.

    EDIT: You can refer to subscripted variables without assigning them any values. E.g. instead of x^2 - 3*x + 1 you could write u[i]^2 - 3*u[i] + 1 where u[i] is not yet assigned any value. Many (most?) functions treat subscripted variables the same as non-subscripted ones, e.g. diff(..., u[i]) to differentiate w.r.t. u[i].