Extending assignment Was: Functions with any number of arguments



Robert Dodier <robert.dodier <at> gmail.com> writes:

> 
> On 1/19/12, Alexander Klimov <alserkli <at> inbox.ru> wrote:
> 
> > On Sun, 15 Jan 2012, Aleksas Domarkas wrote:
> >> How  in Maxima define functions with any number of arguments ?
> >>
> >> For example, i need function MIN(arg1,arg2, ...)
> >> MIN(2) => 2
> >> MIN(3,4) =>3
> >> MIN(2/3,1/2,sqrt(2)/2) => 1/2
> >
> >  MIN([args]) := block([m:first(args)],
> >                       for i in rest(args) do m:min(m,i),
> >                       m);
> 
> >> Other example. How define G with following properties:
> >> G(n) => gamma(n)
> >> G(5) => 24
> >> G(a, x) => gamma_incomplete (a, x)
> >> G(a, z1, z2) =>  gamma_incomplete_generalized (a, z1, z2)
> >


For functions with arbitrary numbers of arguments, try sth like this:

how_many_args(X):=(
is_end:0, 
arr_if:make_array(any,1),
arr_if[0]:x[1]=1.1,

for i:1 while is_end=0 do (
    S:subst(listarray(arr_if),X),
    if numberp(S) then (is_end:1,last:i),
    arr_if:rearray(arr_if,i),
    arr_if[i]:x[i+1]=1.1
    ),
arr_xx :make_array(any,last),
for i:1 thru last do arr_xx[i-1]:x[i],
[last,listarray(arr_xx)]
);


It works for function of arguments x[1], x[2], ...

for example

how_many_args(x[1]*x[2]+x[3]+sin(x[4]))
=> [4,[x[1],x[2],x[3],x[4]]]

how_many_args(x[1]+x[21]/x[23]+x[3]+sin(x[4]))
=>
[23,[x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10],
x[11],x[12],x[13],x[14],x[15],x[16],x[17],x[18],x[19],x[20]
,x[21],x[22],x[23]]]

it is not nice but it works.

Be carefull if you put 0 insteed 1.1 in last example uou do 
[21,[x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9],x[10],x[11]
,x[12],x[13],x[14],x[15],x[16],x[17],x[18],x[19],x[20],x[21]]]

because 0*x[23]=0.

best 
Piotr