-1

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 :)

Lightsong
  • 312
  • 2
  • 8

1 Answers1

0

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].

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thank you, that worked like a charm :), we used the 1st option and also did a little trick: When operating with this undeclared array, we gave different values to the variables based on iterations. For example: (p[k]*p[k+1]),x:i , with i being the value of a previous iteration and x being the variable inside the polynomial. – Lightsong Jan 18 '19 at 12:07