Ask Your Question
0

Finite sums and multiplication

asked 2021-02-25 14:38:56 +0200

Alain Ngalani gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-02-25 16:48:23 +0200

tmonteil gravatar image

updated 2021-02-25 16:52:03 +0200

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)
edit flag offensive delete link more

Comments

Ok, nice. I'm happy I was wrong

Alain Ngalani gravatar imageAlain Ngalani ( 2021-02-26 14:34:27 +0200 )edit
0

answered 2021-02-25 22:27:21 +0200

Emmanuel Charpentier gravatar image

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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-02-25 14:38:56 +0200

Seen: 71 times

Last updated: Feb 25