(wx)Maxima:如何使用 `args` 获得一致的表达式?
(wx)Maxima: how to get consistent expressions using `args`?
我正在尝试编写一个小脚本,它将查看表达式的第一项并确定它是正数还是负数,然后在前面打印 +
或 -
因此,那个表达;但是,我在以可靠地提取表达式的第一项的方式编写它时遇到了一些麻烦。
我一直在试验 part
和 args
。我一直倾向于 args
因为我还没有找到任何方法来确定任意表达式的 parts
的“深度”( 即 我不是确定如何确定是否使用,例如 part(expr,1)
or part(expr,1,1)
or part(expr, 1,1,1)
etc.) .
args
的问题在于,例如
declare(cos, posfun)$
args(-2*cos(x));
> [2 cos(x)]
即 负数被丢弃,大概是由于表达式的 lisp 表示(我们从 part(-2*cos(x),1)
得到相同的结果;此外,part(-2*cos(x),2)
“掉到最后”——似乎 part
根本看不到 -
).
相比之下,
args(-2*cos(x)+x);
> [x, -2cos(x) ]
符合预期。
不管这些函数是否需要这样的行为,我都希望找到一些方法来绕过它,这样我就可以拥有一个具有以下行为的函数:
addOp(x) > ["+", x]
addOp(-x) > ["-", x]
addOp(1+2*x+x^2) > ["+", 1+2*x+x^2]
addOp(-2+2*x+x^2) > ["-", 2+2*x+x^2] /* NB: only the first term is scaled by -1, not the entire expression */
addOp(cos(...)) > ["+", cos(...)]
addOp(-2x*cos(...)) > ["-", 2x*cos(x) ]
我还尝试使用 op
函数和一个已知数字;然而,负数的内部表示意味着 op(1-3*cos(x))
returns +
.
这个问题让我困惑了一段时间,所以如果有任何建议,我们将不胜感激。
这是我的第一次尝试。它似乎主要按照您描述的方式工作,除了 %o11
因为 -2
从头到尾移动。
(%i1) f(e):= if atom(e) then ["+", e]
else if op(e) = "-" then ["-", -e]
elseif op(e) = "+" then [f(first(e)), rest(e)]
else ["+", e];
(%o1) f(e) := if atom(e) then ["+", e] else (if op(e) = "-" then ["-", - e]
elseif op(e) = "+" then [f(first(e)), rest(e)] else ["+", e])
(%i2) f(x);
(%o2) [+, x]
(%i3) f(-x);
(%o3) [-, x]
(%i4) f(-2*x);
(%o4) [-, 2 x]
(%i5) f(-2*cos(x));
(%o5) [-, 2 cos(x)]
(%i6) f(1-2*cos(x));
(%o6) [[+, 1], - 2 cos(x)]
(%i7) f(-1+2*cos(x));
(%o7) [[+, 2 cos(x)], - 1]
(%i8) f(-1-2*cos(x));
(%o8) [[-, 2 cos(x)], - 1]
(%i9) f(a*b+c*d-e*f*g);
(%o9) [[-, e f g], c d + a b]
(%i10) f(1+2*x+x^2);
2
(%o10) [[+, x ], 2 x + 1]
(%i11) f(-2+2*x+x^2);
2
(%o11) [[+, x ], 2 x - 2]
(%i12) f(cos(a*b-c));
(%o12) [+, cos(c - a b)]
(%i13) f(-2*cos(x-y*z));
(%o13) [-, 2 cos(y z - x)]
(%i14) f(-2*x*cos(b-c));
(%o14) [-, 2 cos(c - b) x]
(%i15) -2+2*x+x^2;
2
(%o15) x + 2 x - 2
(%i16) f(-2 + 2*x - x^2);
2
(%o16) [[-, x ], 2 x - 2]
(%i17) -2 + 2*x - x^2;
2
(%o17) (- x ) + 2 x - 2
(%i18) f(a-b);
(%o18) [[+, a], - b]
(%i19) f(b-a);
(%o19) [[+, b], - a]
关于 op(e) = "-"
的事情是像 -2*cos(x)
这样的东西在 args
工作之前被重组为 -(2*cos(x))
(尽管我认为 inpart
禁用了它行为或对其进行修改)。
编辑:取 2. atom(-2)
returns true
,所以 -2 被前面定义中的第一种情况捕获。这是另一种尝试,其中负数与其他原子区分开来。
f(e):=
if atom(e)
then (if numberp(e) and e < 0 then ["-", -e] else ["+", e])
else if op(e) = "-" then ["-", -e]
elseif op(e) = "+" then [f(first(e)), rest(e)]
else ["+", e];
我没有试过这段代码,但也许你可以说它是否有效。
罗伯特回答的附录,以防对其他人有用。以下是用于提取值并重新组合它们(经过一些转换)的示例脚本。
NB 而不是 "+"
和 "-"
,为了重新组装,我将函数重新定义为 return 1
或 -1
,而不是。
罗伯特函数(已修改):
f(e):=
if numberp(e)
then if e >= 0
then [1, e]
else [-1, -e]
else if atom(e)
then [1, e]
else if op(e) = "-"
then [-1, -e]
else if op(e) = "+"
then [f(first(e)), rest(e)]
else [1, e];
提取符号
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 return 形式的表达式 +/-1
。
fExtSg(expr):=
block([expr:expr],
if listp(expr[1])
then expr[1][1]
else expr[1]);
提取词条
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 returns e
.
形式的表达式
fExtTerm(expr):=
block([expr:expr],
if listp(expr[1])
then expr[1][2]
else expr[2]
);
提取休息
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 returns r
或 0
.
形式的表达式
fExtRest(expr):=
block([expr:expr],
if listp(expr[1])
then expr[2]
else 0
);
拆分表达式并重新组合的示例:
Test:[-1+c,c-1,-b,3,-7*sin(x)-10*cos(x), -7*sin(x)+10*cos(x), 7*sin(x)-10*cos(x)];
/* apply `f` to all terms of the test list */
fTest: makelist(f(Test[i]), i, length(Test));
/* collect the signs into a list */
First_Term_Signs: makelist(fExtSg(fTest[i]), i, length(Test));
/* collect the first terms */
First_Terms: makelist(fExtTerm(fTest[i]), i, length(Test));
/* collect the rest */
Rests: makelist(fExtRest(fTest[i]), i, length(Test));
/* recombine and compare to original list, to ensure proper functioning */
reassembled_test: makelist(First_Term_Signs[i]*First_Terms[i] + Rests[i], i, length(Test));
注意 -1 + c
和 c - 1
都将被 Maxima 处理相同,由此产生的“重新组装”版本将匹配由 Maxima 编辑的输出 return...
我正在尝试编写一个小脚本,它将查看表达式的第一项并确定它是正数还是负数,然后在前面打印 +
或 -
因此,那个表达;但是,我在以可靠地提取表达式的第一项的方式编写它时遇到了一些麻烦。
我一直在试验 part
和 args
。我一直倾向于 args
因为我还没有找到任何方法来确定任意表达式的 parts
的“深度”( 即 我不是确定如何确定是否使用,例如 part(expr,1)
or part(expr,1,1)
or part(expr, 1,1,1)
etc.) .
args
的问题在于,例如
declare(cos, posfun)$
args(-2*cos(x));
> [2 cos(x)]
即 负数被丢弃,大概是由于表达式的 lisp 表示(我们从 part(-2*cos(x),1)
得到相同的结果;此外,part(-2*cos(x),2)
“掉到最后”——似乎 part
根本看不到 -
).
相比之下,
args(-2*cos(x)+x);
> [x, -2cos(x) ]
符合预期。
不管这些函数是否需要这样的行为,我都希望找到一些方法来绕过它,这样我就可以拥有一个具有以下行为的函数:
addOp(x) > ["+", x]
addOp(-x) > ["-", x]
addOp(1+2*x+x^2) > ["+", 1+2*x+x^2]
addOp(-2+2*x+x^2) > ["-", 2+2*x+x^2] /* NB: only the first term is scaled by -1, not the entire expression */
addOp(cos(...)) > ["+", cos(...)]
addOp(-2x*cos(...)) > ["-", 2x*cos(x) ]
我还尝试使用 op
函数和一个已知数字;然而,负数的内部表示意味着 op(1-3*cos(x))
returns +
.
这个问题让我困惑了一段时间,所以如果有任何建议,我们将不胜感激。
这是我的第一次尝试。它似乎主要按照您描述的方式工作,除了 %o11
因为 -2
从头到尾移动。
(%i1) f(e):= if atom(e) then ["+", e]
else if op(e) = "-" then ["-", -e]
elseif op(e) = "+" then [f(first(e)), rest(e)]
else ["+", e];
(%o1) f(e) := if atom(e) then ["+", e] else (if op(e) = "-" then ["-", - e]
elseif op(e) = "+" then [f(first(e)), rest(e)] else ["+", e])
(%i2) f(x);
(%o2) [+, x]
(%i3) f(-x);
(%o3) [-, x]
(%i4) f(-2*x);
(%o4) [-, 2 x]
(%i5) f(-2*cos(x));
(%o5) [-, 2 cos(x)]
(%i6) f(1-2*cos(x));
(%o6) [[+, 1], - 2 cos(x)]
(%i7) f(-1+2*cos(x));
(%o7) [[+, 2 cos(x)], - 1]
(%i8) f(-1-2*cos(x));
(%o8) [[-, 2 cos(x)], - 1]
(%i9) f(a*b+c*d-e*f*g);
(%o9) [[-, e f g], c d + a b]
(%i10) f(1+2*x+x^2);
2
(%o10) [[+, x ], 2 x + 1]
(%i11) f(-2+2*x+x^2);
2
(%o11) [[+, x ], 2 x - 2]
(%i12) f(cos(a*b-c));
(%o12) [+, cos(c - a b)]
(%i13) f(-2*cos(x-y*z));
(%o13) [-, 2 cos(y z - x)]
(%i14) f(-2*x*cos(b-c));
(%o14) [-, 2 cos(c - b) x]
(%i15) -2+2*x+x^2;
2
(%o15) x + 2 x - 2
(%i16) f(-2 + 2*x - x^2);
2
(%o16) [[-, x ], 2 x - 2]
(%i17) -2 + 2*x - x^2;
2
(%o17) (- x ) + 2 x - 2
(%i18) f(a-b);
(%o18) [[+, a], - b]
(%i19) f(b-a);
(%o19) [[+, b], - a]
关于 op(e) = "-"
的事情是像 -2*cos(x)
这样的东西在 args
工作之前被重组为 -(2*cos(x))
(尽管我认为 inpart
禁用了它行为或对其进行修改)。
编辑:取 2. atom(-2)
returns true
,所以 -2 被前面定义中的第一种情况捕获。这是另一种尝试,其中负数与其他原子区分开来。
f(e):=
if atom(e)
then (if numberp(e) and e < 0 then ["-", -e] else ["+", e])
else if op(e) = "-" then ["-", -e]
elseif op(e) = "+" then [f(first(e)), rest(e)]
else ["+", e];
我没有试过这段代码,但也许你可以说它是否有效。
罗伯特回答的附录,以防对其他人有用。以下是用于提取值并重新组合它们(经过一些转换)的示例脚本。
NB 而不是 "+"
和 "-"
,为了重新组装,我将函数重新定义为 return 1
或 -1
,而不是。
罗伯特函数(已修改):
f(e):=
if numberp(e)
then if e >= 0
then [1, e]
else [-1, -e]
else if atom(e)
then [1, e]
else if op(e) = "-"
then [-1, -e]
else if op(e) = "+"
then [f(first(e)), rest(e)]
else [1, e];
提取符号
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 return 形式的表达式 +/-1
。
fExtSg(expr):=
block([expr:expr],
if listp(expr[1])
then expr[1][1]
else expr[1]);
提取词条
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 returns e
.
fExtTerm(expr):=
block([expr:expr],
if listp(expr[1])
then expr[1][2]
else expr[2]
);
提取休息
采用 [+/-1, e]
或 [ [+/-1, e], r]
和 returns r
或 0
.
fExtRest(expr):=
block([expr:expr],
if listp(expr[1])
then expr[2]
else 0
);
拆分表达式并重新组合的示例:
Test:[-1+c,c-1,-b,3,-7*sin(x)-10*cos(x), -7*sin(x)+10*cos(x), 7*sin(x)-10*cos(x)];
/* apply `f` to all terms of the test list */
fTest: makelist(f(Test[i]), i, length(Test));
/* collect the signs into a list */
First_Term_Signs: makelist(fExtSg(fTest[i]), i, length(Test));
/* collect the first terms */
First_Terms: makelist(fExtTerm(fTest[i]), i, length(Test));
/* collect the rest */
Rests: makelist(fExtRest(fTest[i]), i, length(Test));
/* recombine and compare to original list, to ensure proper functioning */
reassembled_test: makelist(First_Term_Signs[i]*First_Terms[i] + Rests[i], i, length(Test));
注意 -1 + c
和 c - 1
都将被 Maxima 处理相同,由此产生的“重新组装”版本将匹配由 Maxima 编辑的输出 return...