Search code examples
mathsymbolic-mathmaxima

Maxima: How to factor out a finite sum?


This question is about humane representation of Maxima output.

In short, how do I make

b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)

look like

b*sum_{i=1}^{4} d_i + a*sum_{j=1}^{4}sin(c_i + \alpha)

where sum_{*}^{*}* is a summation sign and an expression with subscripts ?

Or deeper, how to properly model a finite set of items here ?

Consider a finite set of entities $x_i$ (trying to speak tex here) that are numbered from 1 to n where n is known. Let a function $F$ depend on several characteristics of those entities $c_ji = c_j(x_i), j = 1..k$ (k - also known) so that $F = F(c_11,...,c_kn)$.

Now when I try to implement that in Maxima and do things with it, it would yield sums and products of all kinds, where the numbered items are represented something like $c_1*a + c_2*a + c_3*a + c_4*a + d_1*b + d_2*b + d_3*b + d_4*b$ which you would write down on paper as $a*\sum_{i=1}^{4}c_i + b*sum_{i=1}^{4}d_i$.

So how can I make Maxima do that sort of expression contraction ?

To be more specific, here is an actual code example: (Maxima output marked as ">>>")

/* let's have 4 entities: */
n: 4 $

/* F is a sum of similar components corresponding to each entity F = F_1 + F_2 + F_3 + F_4 */
F_i: a*sin(alpha + c_i) + b*d_i;
>>> b*d_i + a*sin(c_i + alpha)

/* defining the characteristics */
c(i) := concat(c, i) $
d(i) := concat(d, i) $

/* now let's see what F looks like */

/* first, we should model the fact that we have 4 entities somehow: */
F_i(i) := subst(c(i), c_i, subst(d(i), d_i, F_i)) $

/* now we can evaluate F: */
F: sum(F_i(i), i, 1, 4);
>>> b*d4 + b*d3 + b*d2 + b*d1 + a*sin(c4 + alpha) + a*sin(c3 + alpha) + a*sin(c2 + alpha) + a*sin(c1 + alpha)

/* at this point it would be nice to do something like:                */
/*     pretty(F);                                                      */
/* and get an output of:                                               */
/*     $b*\sum_{i=1}^{4}d_i + a*\sum_{j=1}^4 sin(c_j + \alpha)$        */
/* not to mention having Maxima write things in the same order as I do */

So, to sum up, there are three quetions here:

  1. How do I factor out a sum from an expression like the one on top of this post ?
  2. How do I properly let Maxima know what I'm speaking about here ?
  3. How to make Maxima preserve my order of things in output ?

Thanks in advance.


Solution

  • Here's a way to go about what I think you want.

    (%i1) n: 4 $
    (%i2) F(i) := a*sin(alpha + c[i]) + b*d[i];
    (%o2)                  F(i) := a sin(alpha + c ) + b d
                                                  i       i
    (%i3) 'sum(F(i), i,1,4);
                            4
                           ====
                           \
    (%o3)                   >    (a sin(c  + alpha) + b d )
                           /             i               i
                           ====
                           i = 1
    (%i4) declare (nounify(sum), linear);
    (%o4)                                done
    (%i5) 'sum(F(i), i,1,4);
                            4                         4
                           ====                      ====
                           \                         \
    (%o5)                a  >    sin(c  + alpha) + b  >    d
                           /          i              /      i
                           ====                      ====
                           i = 1                     i = 1
    (%i6)
    

    The most important thing here is that I've written what we would call "c sub i" and "d sub i" as c[i] and d[i] respectively. These are indexed variables named c and d, and i is the index. It's not necessary for there to actually exist arrays or lists named c or d, and i might or might not have a specific value.

    I have written F as an ordinary function. I've avoided the construction of variable names via concat and avoided the substitution of those names into an expression. I would like to emphasize that such operations are almost certainly not the best way to go about it.

    In %i3 note that I wrote the summation as 'sum(...) which makes it a so-called noun expression, which means it is maintained in a symbolic form and not evaluated.

    By default, summations are not treated as linear, so in %i4 I declared summations as linear so that the result in %o5 is as expected.

    Maxima doesn't have a way to collect expressions such as a1 + a2 + a3 back into a symbolic summation, but perhaps you don't need such an operation.