combinat::cartesianProduct – cartesian products
The library combinat::cartesianProduct provides functions for counting and generating elements of cartesian products of sets.
Superdomain
Categories
Axioms
Details:
Let be sets. The cartesian product of is the set of all -tuples with in for each , .
Each set can be represented either by a set (DOM_SET), a list (DOM_LIST), or an integer (which represents the set ).
Entries
"domtype" |
The MuPAD domain used to represent cartesian products: DOM_LIST |
Methods
isA – test if an object is a cartesian product
combinat::cartesianProduct::isA(any type cp, <set or list or nonnegative integer S1, ...,set or list or nonnegative integer Sk>)
Returns whether the object cp is an element of a cartesian product.
If optional arguments are present, returns whether cp is a an element of the cartesian product .
count – number of elements of a cartesian product
combinat::cartesianProduct::count(set S1, set S2, ...,set Sk)
Returns the number of elements of the cartesian product .
generator – generator for the elements of a cartesian product
combinat::cartesianProduct::generator(set S1, set S2, ...,set Sk)
Returns a generator for the elements of the cartesian product .
list – list of the elements of a cartesian product
combinat::cartesianProduct::list(set S1, set S2, ...,set Sk)
Returns the list of the elements of the cartesian product .
In this example we show how to check whether we have an element of a cartesian product of some sets. The sets can be represented either by a list, a set or a non negative integer.
combinat::cartesianProduct::isA([2, 3, 4], {1, 2}, [a, 3], 7);
combinat::cartesianProduct::isA([2, 3, 4, d], 2, 3, 4, {a, d});
combinat::cartesianProduct::isA([2, 3, 4, d], 2, 1, 4, {a, d});
combinat::cartesianProduct::isA([2, 3, 4, d], 2, 3);
If no optional argument is provided (i.e. the sets), the function tests whether an object makes sense as an element of a cartesian product.
combinat::cartesianProduct::isA([2, 3, 4, d]);
combinat::cartesianProduct::isA([ ]);
combinat::cartesianProduct::isA(a);
There are elements in the cartesian product of the sets {1,2,3} and {a,b,c,d}:
combinat::cartesianProduct::count({1,2,3},{a,b,c,d});
Here is the list:
combinat::cartesianProduct::list({1,2,3},{a,b,c,d});
When you want to run through the elements of a cartesian product you can generate them one by one to save memory:
g := combinat::cartesianProduct::generator({1,2},{a,b}):
g(); g(); g(); g(); g()
Typically, this would be used as follows:
g := combinat::cartesianProduct::generator({1,2},{a,b}):
while (p := g()) <> FAIL do print(p): end:
[1, a]
[1, b]
[2, a]
[2, b]
Each input set can be represented by a set, a list, or a number:
combinat::cartesianProduct::list({a,b},[x,y],2);
The empty cartesian product has one element:
combinat::cartesianProduct::list();
When any one of the sets is empty, the cartesian product is also empty:
combinat::cartesianProduct::list(2,0);
The cartesian product is not commutative:
combinat::cartesianProduct::list({Diamondsuit},2);
combinat::cartesianProduct::list(2,{Diamondsuit})
Which cards exist, if you have the following suits and numbers available?
combinat::cartesianProduct::list(
{Diamondsuit,Heartsuit,Spadesuit,Clubsuit},
{7,8,9,10})