GECODE Pairwise Distinct Columns In A Matrix - c++

I'm working on a GECODE solver to implement a Matrix Generation problem. I have figured out all the constraints I require except for one:
Given a Matrix[M, N], all column vectors must be pairwise distinct.
This is the code I would like to write:
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( i != j )
{
notsame(*this, m.col(i), m.col(j));
}
}
}
But I can't figure out how to express that with the provided primitive constraints. I know distinct() exists, however I can't figure out how to operate over columns in a matrix, instead of elements in the column matrix itself. What would be the best way to express this constraint over matricies?

I have come up with an implementation that seems to work.
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( i != j )
{
// Every column should not be equal to any other column
// Check each column pairwise element
BoolVarArgs equalities;
for(int r = 0; r < M; r++)
{
equalities << expr(*this, m(i, r) == m(j, r));
}
rel(*this, BOT_AND, equalities, 0);
}
}
}

Related

Iterate over vectors with n and n*n elements

I have this Google test assertions
ASSERT_FLOAT_EQ(longerVector[0], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[1], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[2], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[3], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[4], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[5], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[6], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[7], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[8], shorterVector[2]);
as you can see, there are two vectors, shorter with n elements and longer with n*n elements (in this case 3 and 9 respectively).
How could I create two nested for loops, which would make my code shorter and simpler?
I tried for example this but it failed. I understand why but I was not able to come up with anything better.
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector(i*j), shorterVector(j);
}
}
Thank you very much for any help and I apologize for maybe a dumb question.
Use this instead
for(size_t i = 0; i < longerVector.size(); i++)
{
ASSERT_FLOAT_EQ(longerVector[i], shorterVector[i%3]);
}
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[i*shorterVector.size() + j], shorterVector[j]);
}
}
You should assert before that shorterVector.size() * shorterVector.size() == longerVector.size().
If you do not want to have the multiplication in the inner loop body you can do:
for(size_t i = 0, k = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[k++], shorterVector[j]);
}
}

what is the Time complexity of this function

How to calculate time complexity of this function step by step ?
This function converts an adjacency list to a matrix, manipulate the matrix and then convert the matrix back to a list
Graphe *Graphe::grapheInverse( void ){
Graphe *r = new Graphe (_adjacences.size() );
std::vector<vector<int> > matrix(_adjacences.size(), vector<int>( _adjacences.size() ) );
std::vector<vector<int> > liste(matrix.size());
for (unsigned i = 0; i < _adjacences.size(); i++)
for (auto j : *_adjacences[i])
matrix[i][j] = 1;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
if (matrix[i][j] == 1)
matrix[i][j] = 0;
else
matrix[i][j] = 1;
if (i == j)
matrix[i][j] = 0;
}
}
for (int i = 0; i < matrix.size(); i++){
for (int j = 0; j < matrix[i].size(); j++){
if (matrix[i][j] == 1){
liste[i].push_back(j);
}
}
}
for (int i = 0; i < liste.size(); i++) {
for (int j = 0; j < liste[i].size(); j++) {
r->ajouterArcs( i, liste[i][j] );
}
}
return r;
}
Note that the following all applies to big-O time complexity:
Calculating the time complexity involves looking at how many times you iterate over the data. One for loop is N, as you touch each point once. A nested for loop (for i in data, for j in data) is N^2 because you touch each point once for each point there is.
A for loop next to a for loop (For i in data do X, for i in data do Y) touches the data N + N times. This is still considered N time complexity because as N approaches a very large number, 2N doesn't make much difference. The same goes for nested loops, N^2+N^2 = 2N^2 -> Essentially, you would ignore any multipliers, and go based on the times you touch N. That means 2N^2 changes to N^2
To reiterate, this is specifically for big-O time complexity

Partial Pivoting/Gaussian elimination- swapping columns instead of rows producing wrong output

I'm trying to implement a quick program to solve a system of linear equations. The program reads the input from a file and then writes the upper-triangular system and solutions to a file. It is working with no pivoting, but when I try to implement the pivoting it produces incorrect results.
As example input, here is the following system of equations:
w+2x-3y+4z=12
2w+2x-2y+3z=10
x+y=-1
w-x+y-2z=-4
I expect the results to be w=1, x=0, y=-1 and z=2. When I don't pivot, I get this answer (with some rounding error on x). When I add in the pivoting, I get the same numbers but in the wrong order: w=2,x=1,y=-1 and z=0.
What do I need to do to get these in the correct order? Am I missing a step somewhere? I need to do column swapping instead of rows because I need to adapt this to a parallel algorithm later that requires that. Here is the code that does the elimination and back substitution:
void gaussian_elimination(double** A, double* b, double* x, int n)
{
int maxIndex;
double temp;
int i;
for (int k = 0; k < n; k++)
{
i = k;
for (int j = k+1; j < n; j++)
{
if (abs(A[k][j]) > abs(A[k][i]))
{
i = j;
}
}
if (i != k)
{
for (int j = 0; j < n; j++)
{
temp = A[j][k];
A[j][k] = A[j][i];
A[j][i] = temp;
}
}
for (int j = k + 1; j < n; j++)
{
A[k][j] = A[k][j] / A[k][k];
}
b[k] = b[k] / A[k][k];
A[k][k] = 1;
for (i = k + 1; i < n; i++)
{
for (int j = k + 1; j < n; j++)
{
A[i][j] = A[i][j] - A[i][k] * A[k][j];
}
b[i] = b[i] - A[i][k] * b[k];
A[i][k] = 0;
}
}
}
void back_substitution(double**U, double*x, double*y, int n)
{
for (int k = n - 1; k >= 0; k--)
{
x[k] = y[k];
for (int i = k - 1; i >= 0; i--)
{
y[i] = y[i] - x[k]*U[i][k];
}
}
}
I believe what you implemented is actually complete pivoting.
With complete pivoting, you must keep track of the permutation of columns, and apply the same permutation to your answer.
You can do this with an array {0, 1, ..., n}, where you swap the i'th and k'th values in the second loop. Then, rearange the solution using this array.
If what you were trying to do is partial pivoting, you need to look for the maximum in the respective row, and swap the rows and the values of 'b' accordingly.

Adding pivoting functionality to a Doolittle algorithm

So far I have this code for an LU decomposition. It takes in an input array and it returns the lower and upper triangular matrix.
void LUFactorization ( int d, const double*S, double*L, double*U )
{
for(int k = 0; k < d; ++k){
if (
for(int j = k; j < d; ++j){
double sum = 0.;
for(int p = 0; p < k; ++p) {
sum+=L[k*d+p]*L[p*d+j];
cout << L[k*d+p] << endl;
}
sum = S[k*d+j] - sum;
L[k*d+j]=sum;
U[k*d+j]=sum;
}
for(int i = k + 1; i < d; ++i){
double sum=0.;
for(int p = 0; p < k; ++p) sum+=L[i*d+p]*L[p*d+k];
L[i*d+k]=(S[i*d+k]-sum)/L[k*d+k];
}
}
for(int k = 0; k < d; ++k){
for(int j = k; j < d; ++j){
if (k < j) L[k*d+j]=0;
else if (k == j) L[k*d+j]=1;
}
}
}
Is there some way I can adapt this to perform row exchanges? If not, is there some other algorithm I could be directed towards?
Thanks
The usual approach for LU decompositions with pivoting is to store a permutation array P which is initialized as the identity permutation (P={0,1,2,...,d - 1}) and then swapping entries in P instead of swapping rows in S
If you have this permutation array, every access to S must use P[i] instead of i for the row number.
Note that P itself is part of the output, since it represents the permutation matrix such that
P*A = L*U, so if you want to use it to solve systems of linear equations, you'll have to apply P on the right-hand side before applying backward and forward substitution

Trying to multiply two dynamically created matrices(2d vector's) together in c++

So what I am trying to do is multiply one 2d vector by another 2d vector.
I come from Java,Python and C# so I am pretty much learning C++ as I go along.
I have the code down to generate the vector and display the vector but I can't seem to finish the multiplication part.
v1 is another matrix that is already generated.
vector<vector<int> > v2 = getVector();
int n1 = v1[0].size();
int n2 = v2.size();
vector<int> a1(n2, 0);
vector<vector<int> > ans(n1, a1);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < 10; k++) {
// same as z[i][j] = z[i][j] + x[i][k] * y[k][j];
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
displayVector(ans);
My guess for where I am going wrong is in the inner-most loop. I can't figure out what to actually put in place of that 10 I have there now.
When you multiply matrices, the number of columns of the matrix on the left side must equal the number of rows of the matrix on the right side. You need to check that that is true, and use that common number for your size of the k variable:
int nCommon = v1.size();
assert(v2[0].size() == nCommon);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon ; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
For you inner loop, you should do something like this
ans[i][j] = 0;
for (int k = 0; k < n2; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
I don't know where the 10 comes from.