Search code examples
maximawxmaxima

Automatically assigning name to a list created by iteration


I am currently trying to create a command for maxima that allows for iteration of a function and places the result in a list. I managed to create the iteration through a loop, and it successfully places the results in a list.

However, I would like that list's name to be assigned when the user does the input. I tried using makelist command or direct naming built into the function but it doesn't work.

FunctionIterationList(f,a,j,listname):= block(
b:a,
s: [],
for iteraciones: 0 thru j do
(
b: f(b),
s: cons([iteraciones,bfloat(b)],s)
),    
listname: reverse(s));

This would be an example of the result with the current code

FunctionIterationList(f(x):=x+1,0,10,asdf);

[[0,1.0b0],[1,2.0b0],[2,3.0b0],[3,4.0b0],[4,5.0b0],[5,6.0b0],[6,7.0b0],[7,8.0b0],[8,9.0b0],[9,1.0b1],[10,1.1b1]]


Solution

  • I see a couple of ways to get to a solution.

    (1) You can fix up the existing function definition by using the :: assignment operator instead of :, since :: means to assign the value of the right-hand side to the value of the left-hand side. E.g. after x: 'asdf, then x :: 123 assigns 123 to asdf, not to x.

    So in your function definition you could say listname :: reverse(s) at the end.

    (2) Another way to go about it is by using the built-in function makelist, e.g.

    asdf: makelist (x + 1, x, 0, 10);
    

    or

    asdf: makelist (f(x), x, 0, 10);
    

    when f is the function you want.