Joachim De Beule | 15 May 17:04

apply(+,...), reduce, sum, ...

Hi All,

I have a number of related questions that keep popping up, probably because I 
am still new to maxima and am missing something still.

For example, how do convert a list to a sum, 
like from [a,b,c] to a+b+c?

My thoughts and troubles on that so far:

* apply(+,[a,b,c]) does not work because (unlike in lisp) + is an infix   
  operator requiring exactly two arguments.
* But I don't know of a reduce(+,[a,b,c]) that would do the trick as in lisp. 
  Is there something like that available?
* and I dont want to do something uggly like either

   lambda([l],sum(l[i],i,1,length(l)))([a,b,c])

   or

   block(res:1,
            for e in [a,b,c] do res: res+e,
            res)

   Mainly because sometimes maxima doesn't seem to expand such a sum 
   expression them. Can I force maxima to do that?

* I would be happy to do it in lisp but then without having to bother about 
type conversions to much, and I don't know how to do that. Or is this not a 
good idea?
(Continue reading)

Barton Willis | 15 May 17:23

Re: apply(+,...), reduce, sum, ...

-----maxima-bounces <at> math.utexas.edu wrote: -----

>For example, how do convert a list to a sum,  like from [a,b,c] to a+b+c?

(%i5) apply("+",[a,b,c]);
(%o5) c+b+a

Instead of apply, you can use xreduce, rreduce, lreduce, or tree_reduce
(see user documentation). The same quote trick works for apply("*",
[a,b,c])
and apply("^", [x,5]).

Barton
Joachim De Beule | 15 May 17:26

Re: apply(+,...), reduce, sum, ...

Great! Thanks. You may forget my previous mail...

On Thursday 15 May 2008 17:23:42 Barton Willis wrote:
> -----maxima-bounces <at> math.utexas.edu wrote: -----
>
> >For example, how do convert a list to a sum,  like from [a,b,c] to a+b+c?
>
> (%i5) apply("+",[a,b,c]);
> (%o5) c+b+a
>
> Instead of apply, you can use xreduce, rreduce, lreduce, or tree_reduce
> (see user documentation). The same quote trick works for apply("*",
> [a,b,c])
> and apply("^", [x,5]).
>
> Barton
Joachim De Beule | 15 May 17:24

Re: apply(+,...), reduce, sum, ...

On Thursday 15 May 2008 17:05:53 Joachim De Beule wrote:
> Hi All,
>
> I have a number of related questions that keep popping up, probably because
> I am still new to maxima and am missing something still.
>
> For example, how do convert a list to a sum,
> like from [a,b,c] to a+b+c?

Ok, I found it, with lsum. but still, I would like to have something like 
reduce or a way to `apply' an infix operator like + to a list. Is this 
possible?

j.

  
>
> My thoughts and troubles on that so far:
>
> * apply(+,[a,b,c]) does not work because (unlike in lisp) + is an infix
>   operator requiring exactly two arguments.
> * But I don't know of a reduce(+,[a,b,c]) that would do the trick as in
> lisp. Is there something like that available?
> * and I dont want to do something uggly like either
>
>    lambda([l],sum(l[i],i,1,length(l)))([a,b,c])
>
>    or
>
>    block(res:1,
(Continue reading)

Stavros Macrakis | 15 May 17:46

Re: apply(+,...), reduce, sum, ...

On Thu, May 15, 2008 at 11:24 AM, Joachim De Beule
<joachim <at> arti.vub.ac.be> wrote:
> ...a way to `apply' an infix operator like + to a list....

To name an operator with syntax, whether infix, prefix, or otherwise, quote it:

       "+"(2,3) => 5
       "if"(a,b) => if a then b
       "if"(a,b,true,d) => if a then b else c   (else is syntactic
sugar for <elseif true then xxx>)
       "and"(a,b) => a and b
       xreduce("and",[a,b,c]) => a and b and c

         -s

Gmane