I have a huge dynamic system in wxmaxima, and I need to do some vector substitution but it ends up just with some crazy results. This is what I need to do:
forces:[
F1=[x1,y1,z1],
F2=[x2,y2,z2]
];
equations:[F3=-F2];
subst(forces,subst(equations,F1+F3));
the result which I'm seeking is just a simple [x1+x2,y1+y2,z1+z2]
, but I got instead: [[x1-x2,x1-y2,x1-z2],[y1-x2,y1-y2,y1-z2],[z1-x2,z1-y2,z1-z2]]
any suggestions?
OK, that is pretty puzzling, although I see now what's going on.
subst
is serial (one by one) substitution, so subst([F1 = ..., F2 = ...], ...)
is equivalent to subst(F2 = ..., subst(F1 = ..., ...))
. That is, substitute for F1
first and then substitute F2
into the result of that.
However the result of subst(F1 = [x1, y1, z1], F1 - F2)
is [x1 - F2, y1 - F2, z1 - F2]
. You can see now what's going to happen if you substitute F2
into that -- you'll get the messy nested list result.
I think if you try psubst
(parallel substitution) you'll get the expected result.
(%i2) forces:[
F1=[x1,y1,z1],
F2=[x2,y2,z2]
];
(%o2) [F1 = [x1, y1, z1], F2 = [x2, y2, z2]]
(%i3) equations:[F3=-F2];
(%o3) [F3 = - F2]
(%i4) subst(equations, F1 + F3);
(%o4) F1 - F2
(%i5) psubst (forces, %o4);
(%o5) [x1 - x2, y1 - y2, z1 - z2]
(%i6) psubst(forces, subst(equations, F1 + F3));
(%o6) [x1 - x2, y1 - y2, z1 - z2]