Creating a function?



maxima-bounces at math.utexas.edu wrote on 03/30/2011 09:03:22 AM:

> In Michel's example, the boundary conditions are expressed as the
> vector function
> 
> g(z) := [z[2] - 2.0d0*z[1]*(v-z[3]-z[4]), z[2] - 2.0d0*z[1]*z[3],
>                 z[1] - 1.0d0, z[2] - 2.0d0*z[1]*z[4]]; 
> 

It's tempting to use z[1], z[2], ... as subscripted variables, but doing
this will cause bugs. Example: What if a user does the perfectly 
reasonable:

(%i1) z[1,2] : 42;
(%o1) 42

(%i2) g(z) := [z[2] - 2.0d0*z[1]*(v-z[3]-z[4]), z[2] - 2.0d0*z[1]*z[3], 
z[1] - 1.0d0, z[2] - 2.0d0*z[1]*z[4]]$
(%i3) g(z);
evaluation: array z must have 2 indices; found: z[2]

Or suppose (%i1) is z[1] : 42? I'd guess this would cause an error that 
the user
might not notice. Also, xxx[i] is a bug waiting to happen:

(%i1) x[1] : %pi;
(%o1) %pi

(%i2) x : [a,b,c];
(%o2) [a,b,c]

(%i3) x[1];
(%o3) %pi

It's much much better to use inpart.

> The function we want is then
> 
> dg:jacobian(g(z), [z[1],z[2],z[3],z[4]]);
> dgsub(i, z) := row(dg, i)[1];

The free variable (z) in dgsub looks like a bug waiting to happen. Should 
dgsub have
another argument? But I'm not sure what dgsub is suppose to do. 

Sorry to be so negative, nonhelpful, and grumpy.

--Barton