Creating a function?



On 3/30/11, Raymond Toy <toy.raymond at gmail.com> wrote:

> dg:jacobian(g(z), [z[1],z[2],z[3],z[4]]);
> dgsub(i, z) := row(dg, i)[1];
>
> However dgsub doesn't work as we need:
>
> dgsub(1, w) -> [-2.0*(-z[4]-z[3]+0.1),1,2.0*z[1],2.0*z[1]]

I don't see anything wrong here, for the record.

> What is the correct way to define the function dgsub?

I would suggest dgsub(i, z) := subst('z = z, row(dg, i)[1]).

> I did find that the following definition does what we need:
>
> dgsub(i,z) := ev(row(dg, i)[1]);

ev has the right effect in this case, but it is easy for unintended
side effects to creep in. subst('z = ...) affects only z,
while ev can affect all variables and function calls.

HTH

Robert Dodier