maxima:以list作为参数调用函数

时间:2015-04-24 09:53:21

标签: list function arguments maxima

我可以这样做:

(%i1) myFunc(x) := x^2 + 1;
      myList : [1,2,3];
      myFunc(myList);

(%o1) myFunc(x):=x^2+1
(%o2) [1,2,3]
(%o3) [2,5,10]

这也是:

(%i1) kill(all);
      load("pdiff");
      myFunc(x) := ratdisrep(taylor(diff(q('dst),'dst,x),'dst,0,5));
      myFunc(1);
      myFunc(2);
      myFunc(3);
(%o1) myFunc(x):=ratdisrep(taylor(diff(q('dst),'dst,x),'dst,0,5))
(%o2) [1,2,3]
(%o3) (q[(6)](0)*dst^5)/120+(q[(5)](0)*dst^4)/24+(q[(4)](0)*dst^3)/6+(q[(3)](0)*dst^2)/2+q[(2)](0)*dst+q[(1)](0)
(%o4) (q[(7)](0)*dst^5)/120+(q[(6)](0)*dst^4)/24+(q[(5)](0)*dst^3)/6+(q[(4)](0)*dst^2)/2+q[(3)](0)*dst+q[(2)](0)
(%o5) (q[(8)](0)*dst^5)/120+(q[(7)](0)*dst^4)/24+(q[(6)](0)*dst^3)/6+(q[(5)](0)*dst^2)/2+q[(4)](0)*dst+q[(3)](0)

但我无法做到:

(%i1) kill(all);
      load("pdiff");
      myFunc(x) := ratdisrep(taylor(diff(q('dst),'dst,x),'dst,0,5));
      myList : [1,2,3];
      myFunc(myList);

(%o1) myFunc(x):=ratdisrep(taylor(diff(q('dst),'dst,x),'dst,0,5))
(%o2) [1,2,3]
declare: argument must be a symbol; found "d"
 -- an error. To debug this try: debugmode(true);

为什么会这样,我怎么能绕道而行?

提前致谢!

1 个答案:

答案 0 :(得分:2)

我不知道declare的错误来自何处 - 我在declare找不到pdiff.lisp的任何电话。当我复制你的第二个例子而不是错误时,我得到:'diff(q(dst),dst,[1,2,3])我认为这是预期的。

我想你想为myFunc的每个元素制作一个包含myList输出的列表。我的建议是简单地将myFunc映射到myList。有些函数会自动映射列表;这是一个方便的功能,并非所有功能都有它。这是我在这种情况下得到的:

(%i1) display2d : false $
(%i2) load ("pdiff") $
(%i3) myFunc(x) := ratdisrep(taylor(diff(q('dst),'dst,x),'dst,0,5)) $
(%i4) myList : [1,2,3] $
(%i5) map (myFunc, myList);
(%o5) [((pderivop(q,6))(0)*dst^5)/120+((pderivop(q,5))(0)*dst^4)/24
                                     +((pderivop(q,4))(0)*dst^3)/6
                                     +((pderivop(q,3))(0)*dst^2)/2
                                     +(pderivop(q,2))(0)*dst
                                     +(pderivop(q,1))(0),
       ((pderivop(q,7))(0)*dst^5)/120+((pderivop(q,6))(0)*dst^4)/24
                                     +((pderivop(q,5))(0)*dst^3)/6
                                     +((pderivop(q,4))(0)*dst^2)/2
                                     +(pderivop(q,3))(0)*dst
                                     +(pderivop(q,2))(0),
       ((pderivop(q,8))(0)*dst^5)/120+((pderivop(q,7))(0)*dst^4)/24
                                     +((pderivop(q,6))(0)*dst^3)/6
                                     +((pderivop(q,5))(0)*dst^2)/2
                                     +(pderivop(q,4))(0)*dst
                                     +(pderivop(q,3))(0)]