block() and local variables?



Hello Neilen,

> (%i3) symvert:matrix(   [x1, y1, z1],
>                         [x2, y2, z2],
>                         [x3, y3, z3],
>                         [x4, y4, z3])$

Does the last row want to be [x4, y4, z4] ?

> As can be seen from the display(el_nodes) output,
> the correct values seem to be passed to the function,
> but the wrong matrix gets generated...

As pointed out elsewhere, the problem is that a function
defined as foo[a, b] remembers the results that were
returned by previous calls (a so-called memoizing function).

If I'm not mistaken, the following version of tetmat
seems to do what you want:

tetmat (el_nodes, verts) := block (local (coord_mat),
  coord_mat [i,j] := verts [el_nodes [i]][j],
  addcol (columnvector ([1,1,1,1]), genmatrix (coord_mat, 4, 3)));

Notes: (1) local (a, b, c) causes the property list of symbols a, b, c
to be saved and restored. The property of interest here is the
function definition, so memoized values are thrown away at
the end of the block. (2) The return value of a function is the last
expression evaluated. An explicit return(foo) or functionname : foo
isn't necessary. (3) The disfun(coord_mat) in your function
definition presumably wants to be dispfun(coord_mat) .

Hope this helps,
Robert Dodier