define a set of functions



Method I: Form the function names using concat:

   (%i5) (s0(x) := 1, s1(x) := x, s2(x) := x^2, z : 0)$

   (%i6) for k : 0 thru 2 do z : z + concat('s,k)(42)$

   (%i7) z;
   (%o7) 1807

Method II: construct a list of the function names and iterate over the list:

  (%i8) l : makelist(concat('s,k),k,0,2)$

  (%i9) z : 0$
  (%i10) for fk in l do z : z + fk(42);
  (%o10) done


Method III: Include the subscript as a function parameter

   (%i13) s(k,x) := if k=0 then 1 else if k=1 then x else if k=2 then x^2$

   (%i14) z : 0$
   (%i15) for k : 0 thru 2 do z : z + s(k,42);
   (%o15) done

   (%i16) z;
   (%o16) 1807


Method IV: Use memoizing functions:

  (%i21) (s[0](x) := 1, s[1](x) := x, s[2](x) := x^2, z : 0)$

  (%i22) for k : 0 thru 2 do z  : z + s[k](42);
  (%o22) done

  (%i23) z;
  (%o23) 1807

There are some tricky things to know about Maxima memoizing functions, so be careful...


--Barton
________________________________
From: maxima-bounces at math.utexas.edu [maxima-bounces at math.utexas.edu] on behalf of Jianrong Li [lijr07 at gmail.com]
Sent: Wednesday, November 21, 2012 21:29
To: maxima-request at math.utexas.edu; maxima at math.utexas.edu
Subject: define a set of functions

Dear all,


I want to define a set of functions as follows. But it is not convenient. How can I define s1(x, y, z), s2(x, y, z), ... as s[1](x, y, z), ...
such that I can use program flow for (for i from 0 to 48 do: s[i](x, y, z)...). Thank you very much.

Best wishes,
Jianrong.

s1(x, y, z):=ee(x, y, z);
s2(x,y,z):=apply(ee, (apply(a, apply(b, apply(a, apply(b, apply(c, apply(b, apply(a, b(x, y, z))))))))));
s3(x,y,z):=apply(ee, c(x, y, z));
s4(x,y,z):=apply(ee, apply(c, b(x, y, z)));
s5(x,y,z):=apply(ee, apply(a, apply(b, apply(a, b(x, y, z)))));
s6(x,y,z):=apply(ee, apply(b, apply(a, apply(b, apply(c, apply(b, apply(a, apply(b, c(x, y, z)))))))));
s7(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(b, apply(c, apply(b, apply(a, apply(b, c(x, y, z))))))))));
s8(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(c, apply(b, apply(a, apply(b, c(x, y, z)))))))));
s9(x,y,z):=apply(ee, apply(b, apply(a, apply(b, c(x, y, z)))));
s10(x,y,z):=apply(ee, apply(a, c(x, y, z)));
s11(x,y,z):=apply(ee, b(x, y, z));
s12(x,y,z):=apply(ee, apply(a, apply(c, apply(b, apply(a, b(x, y, z))))));
s13(x,y,z):=apply(ee, apply(b, apply(a, apply(b, apply(c, apply(b, apply(a, b(x, y, z))))))));
s14(x,y,z):=apply(ee, apply(b, apply(c(x, y, z))));
s15(x,y,z):=apply(ee, apply(a, apply(c, apply(b, a(x, y, z)))));
s16(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(b, apply(c, b(x, y, z)))))));
s17(x,y,z):=apply(ee, apply(a, apply(c, apply(b, apply(a, apply(b, c(x, y, z)))))));
s18(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(b, apply(c, apply(b, a(x, y, z))))))));
s19(x,y,z):=apply(ee, apply(c, apply(b, apply(a, apply(b, c(x, y, z))))));
s20(x,y,z):=apply(ee, a(x, y, z));
s21(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(c, apply(b, a(x, y, z)))))));
s22(x,y,z):=apply(ee, apply(b, apply(c, apply(b, apply(a, apply(b, c(x, y, z)))))));
s23(x,y,z):=apply(ee, apply(b, a(x, y, z)));
s24(x,y,z):=apply(ee, apply(c, apply(b, apply(a, b(x, y, z)))));
s25(x,y,z):=apply(ee, apply(b, apply(c, b(x, y, z))));
s26(x,y,z):=apply(ee, apply(a, apply(b, a(x, y, z))));
s27(x,y,z):=apply(ee, apply(a, apply(b, apply(c, apply(b, apply(a, apply(b, c(x, y, z))))))));
s28(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(b, c(x, y, z))))));
s29(x,y,z):=apply(ee, apply(b, apply(a, apply(c, apply(b, apply(a, b(x, y, z)))))));
s30(x,y,z):=apply(ee, apply(a, apply(b, apply(c, apply(b, apply(a, b(x, y, z)))))));
s31(x,y,z):=apply(ee, apply(b, apply(a, apply(b, apply(c, apply(b, a(x, y, z)))))));
s32(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(c, apply(b, apply(a, b(x, y, z))))))));
s33(x,y,z):=apply(ee, apply(b, apply(a, apply(b, apply(c, b(x, y, z))))));
s34(x,y,z):=apply(ee, apply(c, apply(b, a(x, y, z))));
s35(x,y,z):=apply(ee, apply(b, apply(a, b(x, y, z))));
s36(x,y,z):=apply(ee, apply(a, apply(b, apply(c, b(x, y, z)))));
s37(x,y,z):=apply(ee, apply(b, apply(c, apply(b, a(x, y, z)))));
s38(x,y,z):=apply(ee, apply(a, b(x, y, z)));
s39(x,y,z):=apply(ee, apply(b, apply(a, apply(c, apply(b, a(x, y, z))))));
s40(x,y,z):=apply(ee, apply(a, apply(b, apply(a, apply(c, b(x, y, z))))));
s41(x,y,z):=apply(ee, apply(b, apply(a, apply(c, apply(b, apply(a, apply(b, c(x, y, z))))))));
s42(x,y,z):=apply(ee, apply(a, apply(b, apply(a, c(x, y, z)))));
s43(x,y,z):=apply(ee, apply(a, apply(b, c(x, y, z))));
s44(x,y,z):=apply(ee, apply(b, apply(a, c(x, y, z))));
s45(x,y,z):=apply(ee, apply(a, apply(c, b(x, y, z))));
s46(x,y,z):=apply(ee, apply(b, apply(a, apply(c, b(x, y, z)))));
s47(x,y,z):=apply(ee, apply(a, apply(b, apply(c, apply(b, a(x, y, z))))));
 s48(x,y,z):=apply(ee, apply(b, apply(c, apply(b, apply(a, b(x, y, z))))));