(wx)Maxima:如何使用`args` 获得一致的表达式?

问题描述

我正在尝试编写一个小脚本,该脚本将查看表达式的第一项并确定它是正数还是负数,然后在该表达式前打印 +- , 因此;但是,我在编写它以可靠地提取表达式的第一项的方式时遇到了一些麻烦。

我一直在尝试使用 partargs。我一直倾向于 args,因为我还没有找到任何方法来确定任意表达式的 parts 的“深度”(ie我不确定如何可以决定是否使用,eg part(expr,1) or part(expr,1,1) etc.).

args 的问题在于,例如

declare(cos,posfun)$
args(-2*cos(x));
    > [2 cos(x)]

ie 否定被丢弃,大概是由于表达式的 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)) 之类的东西返回 +

这个让我难住了一段时间,所以任何建议将不胜感激。

解决方法

这是我的第一次尝试。除了 %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)                        [[-,- 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)                        [[+,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)                        [[-,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) 之类的东西在 -(2*cos(x)) 对其进行处理之前被重组为 args(尽管我认为 inpart 禁用了该行为或修改它)。

编辑:取 2。atom(-2) 返回 true,因此 -2 被前面定义中的第一种情况捕获。这是另一个尝试,将负数与其他原子区分开来。

f(e):= 
if atom(e)
 then (if numberp(e) and e < 0 then ["-",-e] else ["+",e])
 else if op(e) = "-" then ["-",e];

我没有试过这个代码,但也许你可以说它是否有效。

,

罗伯特回答的附录,以防对其他人有用。以下是用于提取值并重新组合它们(经过一些转换)的示例脚本。

NB 而不是 "+""-",为了重新组装,我重新定义了函数以返回 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] 形式的表达式并返回 +/-1

fExtSg(expr):=
  block([expr:expr],if listp(expr[1])
  then expr[1][1]
  else expr[1]);

提取词条

采用 [+/-1,r] 形式的表达式并返回 e

fExtTerm(expr):=
  block([expr:expr],if listp(expr[1])
  then expr[1][2]
  else expr[2]
  );

提取休息

采用 [+/-1,r] 形式的表达式并返回 r0

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]),length(Test));

/* collect the first terms */
First_Terms: makelist(fExtTerm(fTest[i]),length(Test));

/* collect the rest */
Rests: makelist(fExtRest(fTest[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],length(Test)); 

NB -1 + cc - 1 都将由 Maxima 处理,生成的“重新组装”版本将与 Maxima 返回的输出匹配...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...