# Finite sums and multiplication

Am I right in assuming that Sage has no meaningful way to translate mathematical notation as " for i in I, take the product of c_i" , " for i in I, take the sum of a_i", " for i in I, take the cartesian product of the sets B_i " ?

If this is true isn't this a very big problem of a language that's supposed to be about mathematics?

edit retag close merge delete

Sort by ยป oldest newest most voted

Unless you provide more precise details about what you want to achieve, i guess you are not right in assuming that, see for example:

sage: C = [1, 2, 4, 6]
sage: prod(C)
48
sage: sum(C)
13


If you want to select some indicies with some for notation, you can do:

sage: prod(C[i] for i in [0,2,3])
24


As for cartesian product, you can do:

sage: B = [[1,2,3], [4,5,6], [7,8]]
sage: from itertools import product
sage: list(product(*B))
[(1, 4, 7),
(1, 4, 8),
(1, 5, 7),
(1, 5, 8),
(1, 6, 7),
(1, 6, 8),
(2, 4, 7),
(2, 4, 8),
(2, 5, 7),
(2, 5, 8),
(2, 6, 7),
(2, 6, 8),
(3, 4, 7),
(3, 4, 8),
(3, 5, 7),
(3, 5, 8),
(3, 6, 7),
(3, 6, 8)]


If you do not like Python tools:

sage: cartesian_product(B)
The Cartesian product of ({1, 2, 3}, {4, 5, 6}, {7, 8})

sage: for i in cartesian_product(B):
....:     print(i)
(1, 4, 7)
(1, 4, 8)
(1, 5, 7)
(1, 5, 8)
(1, 6, 7)
(1, 6, 8)
(2, 4, 7)
(2, 4, 8)
(2, 5, 7)
(2, 5, 8)
(2, 6, 7)
(2, 6, 8)
(3, 4, 7)
(3, 4, 8)
(3, 5, 7)
(3, 5, 8)
(3, 6, 7)
(3, 6, 8)


Again, if you want to select some indices:

sage: for i in cartesian_product([B[i] for i in [0,2]]):
....:     print(i)
(1, 7)
(1, 8)
(2, 7)
(2, 8)
(3, 7)
(3, 8)

more

Ok, nice. I'm happy I was wrong

( 2021-02-26 14:34:27 +0100 )edit

A symbolic form of sums and products can be obtained by keeping L in their expressions ; for example :

sum(c[L[i]], i, 0, len(L)-1)


Is mathematically equivalent to $\displaystyle\sum_{i\in L} c_i$, although the notation is $\displaystyle\sum_{i=1}^{\mathrm{len}(L)} c_{L_i}$ which is different...

(And differs also by the fact that Python list indexing is zero-based, wheras mathematical notation indexing is one-based, but this is another story...).

A similar trick may be applied for other cases ; the general principle is to add an indirection through the list of elements of nteres (or possibly through a dictionary or a set).

more