Search code examples
listfunctionargumentsmaxima

maxima: calling function with list as argument


I can do this :

(%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]

and this as well :

(%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)

but I can't do that :

(%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);

Why does that happen, and how could I make my way around ?

Thanks in advance !


Solution

  • I don't know where the error about declare is coming from -- I don't find any calls to declare in pdiff.lisp. When I copy your second example, instead of an error, I get: 'diff(q(dst),dst,[1,2,3]) which is to be expected, I think.

    I guess you want to make a list with the output of myFunc for each element of myList. My advice is to simply map myFunc over myList. Some functions map over lists automatically; that is a convenience feature, not all functions have it. Here's what I get in this case:

    (%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)]