我想计算一个表达式,其中包含一些符号(在矩阵s
中给出),其值在矩阵v
中给出:
s: matrix([a,b,c]);
v: matrix([1,2,3]);
expr: a*b+c;
ev(expr,s=v); /* not working but gives the idea of the purpose */
如何生成传递给ev
的正确赋值列表[a=1,b=2,c=3]
?
经过很长时间,我发布了一个更简单的解决方案。我建议用新的方案代替下面的方案。
在Maxima邮件列表上,我找到了一种方法来生成分配列表[a=1,b=2,c=3]
,并将其传递给ev
,使用一种更通用的方法(允许求解两边都未知的矩阵方程A=B
——从未用火焰喷射器煮过热狗?)。假定矩阵可以用
m2l(M):= xreduce('append,args(M)) $
传递给ev
的赋值列表[a=1,b=2,c=3]
可通过
assign_list(s,v):= algsys(xreduce('append, args(s-v)), m2l(s)) $
因此,给定矩阵s
, v
和表达式expr
, expr
可以简单地用
求值ev(expr,assign_list(s,v));
您可以使用列表代替矩阵,并为subst创建适配器
(%i1) msubst(a, b, c):=block([L: map(lambda([a0, b0], a0=b0), a, b)], subst(L, c)) $
(%i2) s : [a,b,c] $
(%i3) v : [1,2,3] $
(%i4) msubst(s, v, a*b+c);
(%o4) 5
如果需要将矩阵转换为列表
(%i1) m2l(M):=block([L: []], matrixmap(lambda([e], push(e, L)), M), L) $
(%i2) s: matrix([a,b,c]) $
(%i3) m2l(s);
(%o3) [c, b, a]
有一个更简单的解决方案:
m2l(M):= xreduce('append,args(M)) $
massl(A,B):= map("=",m2l(A),m2l(B)) $ /*massl stands for "matrix assignment list"*/
在本例中,massl(s,v)
返回赋值列表[a=1,b=2,c=3]
。