This example shows how to use the Permute, multifor and State classes.
Output
V = (x1, x2, x0)
X = {x0, x1, x2}
Note that the ordering of the variables in X is the canonical ordering
(ascendingly according to their labels) but the ordering in V is different.
The permutation between both variable orderings is sigma = (2, 0, 1), or more verbosely:
sigma[0] = 2
sigma[1] = 0
sigma[2] = 1
This means that variable V[sigma[n]] should correspond with the n'th variable in X (for n=0,...,2)...OK.
The states of the variables x0,x1,x2 are, according to the ordering in V:
SV: x0: x1: x2:
0 0 0 0
1 0 1 0
2 0 2 0
3 0 0 1
4 0 1 1
5 0 2 1
6 1 0 0
7 1 1 0
8 1 2 0
9 1 0 1
10 1 1 1
11 1 2 1
The states of the variables x0,x1,x2 are, according to the canonical ordering in X:
SX: x0: x1: x2:
0 0 0 0
1 1 0 0
2 0 1 0
3 1 1 0
4 0 2 0
5 1 2 0
6 0 0 1
7 1 0 1
8 0 1 1
9 1 1 1
10 0 2 1
11 1 2 1
The permutation sigma induces the following permutation of linear indices of joint states:
SV: SX:
0 0
1 2
2 4
3 6
4 8
5 10
6 1
7 3
8 5
9 7
10 9
11 11
Source
#include <iostream>
#include <iomanip>
int main() {
vector<Var> V;
V.push_back( x1 );
V.push_back( x2 );
V.push_back( x0 );
cout << "V = " << V << endl;
X |= x2;
X |= x0;
X |= x1;
cout << "X = " << X << endl;
cout << "Note that the ordering of the variables in X is the canonical ordering" << endl;
cout << "(ascendingly according to their labels) but the ordering in V is different." << endl << endl;
size_t N = V.size();
cout <<
"The permutation between both variable orderings is sigma = " << sigma.
sigma() <<
", or more verbosely:" << endl;
for( size_t n = 0; n < N; n++ )
cout << " sigma[" << n << "] = " << sigma[n] << endl;
cout << "This means that variable V[sigma[n]] should correspond with the n'th variable in X (for n=0,...," << (N-1) << ")...";
VarSet::const_iterator X_n = X.
begin();
for( size_t n = 0; n < N; n++, X_n++ )
cout << "OK. " << endl << endl;
cout << "The states of the variables x0,x1,x2 are, according to the ordering in V:" << endl;
cout << "SV: x0: x1: x2:" << endl;
std::vector<size_t> ranges;
for( size_t i = 0; i < V.size(); i++ )
ranges.push_back( V[i].states() );
cout << setw(2) << (size_t)SV << " " << SV[sigma[0]] << " " << SV[sigma[1]] << " " << SV[sigma[2]] << endl;
cout << endl;
cout << "The states of the variables x0,x1,x2 are, according to the canonical ordering in X:" << endl;
cout << "SX: x0: x1: x2:" << endl;
cout << setw(2) << SX << " " << SX(x0) << " " << SX(x1) << " " << SX(x2) << endl;
cout << endl;
cout << "The permutation sigma induces the following permutation of linear indices of joint states:" << endl;
cout << "SV: SX:" << endl;
for(
size_t li = 0; li < X.
nrStates(); li++ )
return 0;
}