Ask Your Question

Revision history [back]

Unless you provide more 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)]

Unless you provide more 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)

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)