problems in storing values into an array made with make_array



On 3/20/07, Robert Gloeckner <Robert.Gloeckner at web.de> wrote:

> try1D(func1D, x) := block([result],
>   result : ev( errcatch( func1D(x)), numer),
>   if( length(result) < 1) then( "?") else (first(result)));
>
> testfunc1d(func, xmin, xmax, xstep) := block([
>   n : ev((xmax - xmin)/xstep, numer), x, result_array],
>   print( "n = ", n),
>   result_array : make_array( any, ?truncate( 2 * (n + 1))),
>   (for i: 0 thru n do (
>     x : xmin + xstep * i,
>     result_array[ 2 * i ] : x,
>     result_array[ 2 * i + 1] : ev(try1D(func, x)))),
> result_array);

> (%i72) testfunc1d(sin, 2.0, 9.0, 2.0);
>
> n =  3.5
> (%o72) {Array:  #(2.0 (($try1D SIMP) %SIN 2.0) 4.0 (($try1D SIMP) %SIN
> 4.0) 6.#
> 0
>   (($try1D SIMP) %SIN 6.0) 8.0 (($try1D SIMP) %SIN 8.0) NIL)}

Robert, I don't understand why you're getting this output.
When I try it, I get
(%o3) {Array:  #(2.0 0.9092974268256817 4.0 -0.7568024953079282 6.0 -0.2794154\
9819892586 8.0 0.9893582466233818 NIL)}
which is probably closer to what you want.
I tried it with Maxima 5.11.0 + Clisp + Linux and 5.11.0 + GCL + Windows.
What is your Maxima version?

Backing up a step, I think there are some changes which can make
this easier (although these changes do not address the evaluation
problem you reported). (1) Use lists and matrices instead of arrays.
Maxima doesn't know much about arrays. (2) Use map and makelist
instead of for-loops. (3) If the goal is to write some numbers to a file,
you can use write_data in the numericalio package for that.

So I would rework testfunc1d as follows.

  testfunc1d (func, xmin, xmax, xstep) := block
    ([n, xx, yy], n : floor((xmax - xmin)/xstep),
      xx : makelist (i, i, 0, n) * xstep + xmin,
      yy : map (lambda ([x], try1D (func, x)), xx),
      transpose (matrix (xx, yy)));

Then I find

M : testfunc1d(sin, 2.0, 9.0, 2.0);
  => matrix([2.0,.9092974268256817],[4.0,-.7568024953079282],
       [6.0,-.2794154981989259],[8.0,.9893582466233818])

load (numericalio);
write_data (M, "/tmp/tmp.data");
printfile ("/tmp/tmp.data");
  =>
2.0 .9092974268256817
4.0 -.7568024953079282
6.0 -.2794154981989259
8.0 .9893582466233818

Hope this helps
Robert Dodier