千里马:如何用符号代替普通子表达式 [英] Maxima: how to replace common subexpressions with symbols
问题描述
假设我有一个表达式(实际上我的要复杂得多,数千个字符)
Suppose I have an expression like (actually mine is much more complex, thousands of characters)
expr:a+b*c+b*c*d;
并且我想用符号替换内部子表达式(用于避免重新计算常见子表达式),例如用k
代替b*c
:
and I want to replace an internal sub-expression with a symbol (useful to avoid recomputation of common subexpressions), say k
in place of b*c
:
subst(b*c=k,expr);
返回
k+b*c*d+a
我如何使Maxima计算正确的"替代值以便返回(除简化之外,在这里)
How I can make Maxima calculate the "right" substitution so to return (apart from obviuos simplification, here)
k+k*d+a
?
推荐答案
看看let
和letsimp
.例如:
(%i2) expr : a + b*c + b*c*d;
(%o2) b*c*d+b*c+a
(%i3) let (b*c, k);
(%o3) b*c --> k
(%i4) letsimp (expr);
(%o4) d*k+k+a
letsimp
与subst
和tellsimp
或defrule
的不同之处在于,这些其他函数仅进行形式上的替换,即替换与某种模式完全相同的子表达式.
letsimp
differs from subst
and tellsimp
or defrule
in that those other functions make only formal substitutions, i.e., replacing subexpressions which are exactly the same as some pattern.
这篇关于千里马:如何用符号代替普通子表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!