Extending assignment Was: Functions with any number of arguments



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)
>
> Use "length(args)" and "if <cond_1> then <expr_1> elseif ..."

Another approach is to use the simplification system.
A rule is defined for each case of interest. Creating new
rules doesn't require touching existing ones.

e.g.

matchdeclare ([aa, xx, yy], all);
tellsimp (G (xx), gamma (xx));
tellsimp (G (aa, xx), gamma_incomplete (aa, xx));
tellsimp (G (aa, xx, yy), gamma_incomplete_generalized (aa, xx, yy));
G (5);
 => 24
G (u, 5);
 => gamma_incomplete(u, 5)
G (u, %e, %pi + 1);
 => gamma_incomplete_generalized(u, %e, %pi + 1)

For better or worse that approach doesn't really work for functions with
arbitrary numbers of arguments. User-defined rules can only be applied
to functions with a stated number of arguments (except for "+" and "*").
So you would have to make up rules for MIN(xx), MIN(xx, yy), MIN(xx, yy, zz),
etc etc. I have often thought about extending the rule system to handle
general n-ary functions but never got around to it. One of these days!

best

Robert Dodier