next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc :: sum(List)

sum(List) -- sum the elements of a list

Synopsis

Description

sum v yields the sum of the elements in the list v.

i1 : sum {1,2,3,4,5}

o1 = 15

The sum of an empty list is the integer 0.

i2 : sum {}

o2 = 0
i3 : class oo

o3 = ZZ

o3 : Ring

When summing a possibly empty list of elements from a ring, one may use promote to ensure the result is always in the same ring.

i4 : R = QQ[x_1 .. x_10];
i5 : f = n -> sum for i from 1 to n list x_i;
i6 : f 4

o6 = x  + x  + x  + x
      1    2    3    4

o6 : R
i7 : f 0

o7 = 0
i8 : class oo

o8 = ZZ

o8 : Ring
i9 : g = n -> promote(sum for i from 1 to n list x_i, R);
i10 : g 10

o10 = x  + x  + x  + x  + x  + x  + x  + x  + x  + x
       1    2    3    4    5    6    7    8    9    10

o10 : R
i11 : g 0

o11 = 0

o11 : R
i12 : class oo

o12 = R

o12 : PolynomialRing

See also