Issue with code for NxN determinant function (C++) - c++

I'm trying to write a matrix inverse calculator (been doing stuff to do with matrices for my maths module in uni so I figured it would be a good way to get practice with recursive functions).
At the moment I'm working on functions for working out the determinant of functions, one for 2x2, one for 3x3 which calls the 2x2 one (recursive formula for determinants I'm sure you know the drill).
Then a third function takes a matrix as input initially checks if it's 2x2 or 3x3, if so sends it to the appropriate prior mentioned function. Next we eliminate rows and columns recursively following the determinant formula until we end up with a value for the determinant.
This code works up to 4x4 matrices, however any matrix larger than this results in the wrong answer.
I'm on my first year at uni and reletively new to programming, this being my first attempt with recursive functions, any advice would be appreciated. My lecturer for maths suggested maybe using cramers rule instead, but it would be interesting to see if I can get this method working.
Appologies if my formatting isn't the best, stuck on old laptop at the moment.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
double MatrixDet2By2(vector<vector<double>> matrix);
double MatrixDet3By3(vector<vector<double>> matrix);
double MatrixDet(vector<vector<double>> matrix);
//vector<vector<double>> CalcMinorMatrix(vector<vector<double>> matrix);
//vector<vector<double>> CalcCofactorMatrix(vector<vector<double>> matrix);
int main(int argc, char** argv)
{
vector<vector<double>> testMatrix = {{1,4},{7,9}};
vector<vector<double>> testMatrix2 = { {5,3,7},{6,-1,0},{4,-11,-2} };
vector<vector<double>> testMatrix3 =
{
{5,3,7,6},
{6,-1,0,4},
{4,-11,-2,3},
{1,3,7,9},
};
vector<vector<double>> testMatrix4 =
{
{1,2,-1,6,1},
{6,-1,0,4,3},
{4,0,-2,3,2},
{1,3,7,2,3},
{-2,7,0,2,5},
};
//cout << MatrixDet2By2(testMatrix) << endl;
cout << MatrixDet(testMatrix4) << endl;
cout << endl;
return 0;
}
double MatrixDet2By2(vector<vector<double>> matrix)
{
return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
}
double MatrixDet3By3(vector<vector<double>> matrix)
{
vector<vector<double>> subMatrix1 = {
{matrix[1][1], matrix[1][2]},
{matrix[2][1], matrix[2][2]}
};
vector<vector<double>> subMatrix2 = {
{matrix[1][0], matrix[1][2]},
{matrix[2][0], matrix[2][2]}
};
vector<vector<double>> subMatrix3 = {
{matrix[1][0], matrix[1][1]},
{matrix[2][0], matrix[2][1]}
};
return ((matrix[0][0] * MatrixDet2By2(subMatrix1)) - (matrix[0][1] * MatrixDet2By2(subMatrix2)) + (matrix[0][2] * MatrixDet2By2(subMatrix3)));
}
/*
vector<vector<double>> CalcMinorMatrix(vector<vector<double>> matrix)
{
vector<vector<double>> subMatrix1 = {
{matrix[1][1], matrix[1][2]},
{matrix[2][1], matrix[2][2]}
};
vector<vector<double>> subMatrix2 = {
{matrix[1][0], matrix[1][2]},
{matrix[2][0], matrix[2][2]}
};
vector<vector<double>> subMatrix3 = {
{matrix[1][0], matrix[1][1]},
{matrix[2][0], matrix[2][1]}
};
vector<vector<double>> subMatrix4 = {
{matrix[0][1], matrix[0][2]},
{matrix[2][1], matrix[2][2]}
};
vector<vector<double>> subMatrix5 = {
{matrix[0][0], matrix[0][2]},
{matrix[2][0], matrix[2][2]}
};
vector<vector<double>> subMatrix6 = {
{matrix[0][0], matrix[0][1]},
{matrix[2][0], matrix[2][1]}
};
vector<vector<double>> subMatrix7 = {
{matrix[0][1], matrix[0][2]},
{matrix[1][1], matrix[1][2]}
};
vector<vector<double>> subMatrix8 = {
{matrix[0][0], matrix[0][2]},
{matrix[1][0], matrix[1][2]}
};
vector<vector<double>> subMatrix9 = {
{matrix[0][0], matrix[0][1]},
{matrix[1][0], matrix[1][1]}
};
vector<vector<double>> matrixOfMinors = {
{MatrixDet2By2(subMatrix1), MatrixDet2By2(subMatrix2), MatrixDet2By2(subMatrix3)},
{MatrixDet2By2(subMatrix4), MatrixDet2By2(subMatrix5), MatrixDet2By2(subMatrix6)},
{MatrixDet2By2(subMatrix7), MatrixDet2By2(subMatrix8), MatrixDet2By2(subMatrix9)},
};
return matrixOfMinors;
}
vector<vector<double>> CalcCofactorMatrix(vector<vector<double>> matrix)
{
return matrix;
}
*/
double MatrixDet(vector<vector<double>> matrix)
{
vector<vector<double>> tempMatrix{};
static double totalDeterminant = 0;
if (matrix.size() != matrix[0].size())
{
cout << "\r\nPlease enter a valid square matrix" << endl;
}
else if (matrix.size() == 2)
{
return MatrixDet2By2(matrix);
}
else if (matrix.size() == 3)
{
return MatrixDet3By3(matrix);
}
else
{
size_t pos = 0;
for (auto value : matrix[0])
{
tempMatrix = matrix;
tempMatrix.erase(tempMatrix.begin());
for (size_t i = 0; i < tempMatrix.size(); i++)
{
if (tempMatrix[i].size() > pos)
{
tempMatrix[i].erase(tempMatrix[i].begin() + pos);
}
}
cout << "\r\n---------" << endl;
for (auto vec : tempMatrix)
{
for (auto val : vec)
{
cout << val << " ";
}
cout << endl;
}
cout << "\r\n---------" << endl;
//totalDeterminant += MatrixDet(tempMatrix);
if ((pos + 1) % 2 == 0)
{
totalDeterminant += (-value * MatrixDet(tempMatrix));
}
else
{
totalDeterminant += (value * MatrixDet(tempMatrix));
}
pos++;
}
}
return totalDeterminant;
}

Since you define variable totalDeterminant in MatrixDet using the keyword static, there is only one totalDeterminant variable in your program, ever. And the = 0 initializer only applies the very first time the program gets there. So when computing the determinant of the very first 4x4 minor matrix, that goes fine. Then that result is multiplied by matrix[0][0] and added to totalDeterminant. The computation for the second 4x4 minor matrix starts with that strange value (1+matrix[0][0])*detMinor1 and begins adding to it.
In fact, if you just called MatrixDet on two 4x4 matrices in the same program, the second call would return the sum of the two determinants.
You need a separate sum for each main matrix and submatrix computation (since the result of a submatrix determinant needs to be multiplied by an element before being added to anything else). So totalDeterminant needs to NOT be static. When I remove the static from your program, it gives the correct final result of MatrixDet(testMatrix4) == -856.
Note that once the general case is correct, you could remove the code for the 3x3 and possibly even 2x2 case. Don't forget to support a 1x1 matrix: det [[x]] = x.

One error is in the following lines
for (size_t i = 0; i < tempMatrix.size(); i++)
{
if (tempMatrix[i].size() > pos)
{
tempMatrix[i].erase(tempMatrix[i].begin() + pos);
}
}
The check if (tempMatrix[i].size() > pos) is not necessary.
All you need to get the sub-matrix is to just exclude the pos-th column. You need to use:
// Remove the "pos" column of tempMatrix.
for (size_t i = 0; i < tempMatrix.size(); i++)
{
tempMatrix[i].erase(tempMatrix[i].begin() + pos);
}
The second error is the use of a static variable for totalDeterminant, as was pointed out by #aschepler. The line
static double totalDeterminant = 0;
needs to be simply
double totalDeterminant = 0;

Related

Efficient matrix implementation

I have the following problem:
I've a precomputed 2d matrix of values which i need to lookup very often and compute only once
The size of the matrix is about 4000x4000 at most
The matrix won't be sparse, i typically need almost all values.
The values in the matrix can be boolean, integer or double. At least they are always small objects
Currently i am storing the precomputed values in a std::vector<<std::vector<T>>, and i've noticed the lookups into this datastructure takes quite some time in heavy computations. I've googled around and so far the suggested implementation seems to be to try a solution in which all the memory is stored contigious using an 1D array where the location in this array is computed based on i and j.
Does anybody have a good example implementation of this or has an even better suggestion? I couldn't find a modern C++ example, while it seems to be a very common problem to me. I'd prefer to use someone elses code instead of reinventing the wheel here. Of course i will measure the differences to see whether it actually improves performance.
Examples i've found:
https://medium.com/#patdhlk/c-2d-array-a-different-better-solution-6d371363ebf8
https://secure.eld.leidenuniv.nl/~moene/Home/tips/matrix2d/
Here is a very simple and efficient 2-d matrix. The 'main' creates a 10000x10000 double array 'mat', then filled it with random number. The array 'mat' is copied into another array 'mat2'. your may input two integers 'n' and 'm' between 0 and 9999 to fetch the double data at mat2(n,m).
Feel free to use or test it. Let me know if you encounter problems or need some more functions to be implemented. Good luck!
#ifndef ytlu_simple_matrix_class_
#define ytlu_simple_matrix_class_
#include <iostream>
#include <iomanip>
#include <complex>
template <typename T> class tMatrix
{
public:
T *ptr;
int col, row, size;
inline T* begin() const {return ptr;}
inline T* end() const {return this->ptr + this->size;}
inline T operator()(const int i, const int j) const { return ptr[i*col+j];
} // r-value
inline T&operator()(const int i, const int j) { return ptr[i*col+j]; } //l-value
inline tMatrix(): col{0}, row{0}, size{0}, ptr{0} {;}
tMatrix(const int i, const int j): col(j), row(i), size(i*j)
{
ptr = new T [this->size] ;
}
tMatrix(const tMatrix<T>&a) : tMatrix<T>(a.row, a.col)
{
std::copy(a.begin(), a.end(), this->ptr);
}
tMatrix<T>& operator=(tMatrix<T>&&a)
{
this->col = a.col;
this->row = a.row;
delete [] this->ptr;
this->ptr = a.ptr;
a.ptr = nullptr;
return *this;
}
tMatrix<T>& operator=(const tMatrix<T>&a)
{
if (col==a.cpl && row==a.row) std::copy(a.begin(), a.end(), this->ptr);
else { tMatrix<T>&&v(a); *this = std::move(v);}
return *this;
}
~tMatrix() {delete [] this->ptr;}
}; //end of class tMatrix
template <typename X> std::ostream& operator<<(std::ostream&p, const tMatrix<X>&a)
{
p << std::fixed;
for (int i=0; i<a.row; i++) {
for (int j=0; j <a.col; j++) p << std::setw(12) << a(i, j);
p << std::endl;
}
return p;
}
using iMatrix = tMatrix<int>;
using rMatrix = tMatrix<double>;
using cMatrix = tMatrix<std::complex<double> >;
#endif
//
//
#include <ctime>
#include <cstdlib>
#define N1 10000
int main()
{
int n, m;
std:srand(time(NULL)); // randomize
rMatrix mat(N1, N1); // declare a 10000 x 10000 double matrix
//
// fill the whole matrix with double random number 0.0 - 1.0
//
for (int i = 0; i<mat.row; i++)
{ for (int j=0; j<mat.col; j++) mat(i, j) = (double)std::rand() / (double)RAND_MAX; }
//
// copy mat to mat 2 just for test
//
rMatrix mat2 = mat;
//
// fetch data test input 0 <= n m < 10000 to print mat2(n, m)
//
while(1)
{
std::cout << "Fetch 2d array at (n m) = ";
std::cin >> n >> m;
if ((n < 0) || (m < 0) || (n > mat2.row) || (m > mat2.col) )break;
std::cout << "mat(" << n << ", " << m << ") = " << mat2(n, m) << std::endl << std::endl;
}
return 0;
}
The compile parameter I used and the test run. It takes a couple seconds to fill the random numbers, and I felt no lapse at all in fetch a data running in my aged PC.
ytlu#ytlu-PC MINGW32 /d/ytlu/working/cpptest
$ g++ -O3 -s mtx_class.cpp -o a.exe
ytlu#ytlu-PC MINGW32 /d/ytlu/working/cpptest
$ ./a.exe
Fetch 2d array at (n m) = 7000 9950
mat(7000, 9950) = 0.638447
Fetch 2d array at (n m) = 2904 5678
mat(2904, 5678) = 0.655934
Fetch 2d array at (n m) = -3 4

Getting variables values from a .txt file in C++

I am writing a CFD solver in C++ but I am in the very beginning. Now I am coding a solver for the linear convection. The math is ok, working well, but I need to write also a code for reading variables from a .txt file.
My code is:
//Code for solving the convection linear equation from Navier-Stokes
#include <iostream>
using namespace std;
int main()
{
float endtime=0.3; //simulation time
float dx=0.025; //element size
float xmax=2; //domain size
float c=1; //wave velocity
float C=0.3; //Courant-Friedrich-Lewy number
float dt=C*dx/c; //defining timestep by the Courant equantion
int nx=xmax/dx + 1; //defining number of elements
int nt=endtime/dt; //defining number of time points
float u[nx] = { }; //defining an initial velocity array.
float un[nx] = { };
for(int i = 4; i <= 9; i++) //defining contour conditions
{
u[i] = 2;
}
for(int n=1; n<=nt; n++)
{
std::copy(u, u + nx, un);
for(int i=1; i<=nx; i++)
{
u[i] = un[i] - c*(dt/dx)*(un[i]-un[i-1]);
}
}
for (int i = 0; i <= nx; i++)
{
cout << u[i] << endl;
}
return 0;
}
I need to take these variables values from a .txt, like the end time, element size, etc. Then, I have a .txt called "parameters", which is exactly written like that:
endtime=0.3
dx=0.025
xmax=2
c=1
C=0.3
What's the most effiecient way to get these variables values from this .txt and use it in the code?
Using only standard features:
#include <iostream>
#include <tuple>
using namespace std;
tuple<bool, string, string> read_one_value(istream& in)
{
string name;
if(getline(in, name, '='))
{
string value;
if(getline(in, value))
{
return make_tuple(true, name, value);
}
}
return make_tuple(false, "", "");
}
int main()
{
for(auto val = read_one_value(cin); get<0>(val); val = read_one_value(cin))
{
std::cout << get<1>(val) << " -> " << get<2>(val) << '\n';
}
return 0;
}
This leaves converting from the value string objects to the needed type as an exercise for the reader, and assumes your format of name=value is consistent.

why my function doesn't change my object's attribute

I'm trying to do a Matrix class using C++ Vector, but i don't know why the inside of "Matrix result" inside my function isn't passed to my object but it remain enclosed inside the function.
for semplicity so far I've tryed only to do an "addition function" among two Matrices.
I have tryied to work with pointer but in this way (according to my knowledgs) i cant call my funtion to an object in this wise:
foo.function1(bar1).function2(bar2);
but working with pointer i have to call function in this manner:
foo.function1(bar1);
foo.function2(bar2);
//and so on..
this is my header file:
#include <iostream>
#include <vector>
using namespace std;
class Matrix
{
public:
Matrix (int height, int width);
Matrix add(Matrix m);
Matrix applyFunction(double (*function)(double));
void print();
private:
vector<vector<double> > matrix;
int height;
int width;
};
this is the .cpp file:
Matrix::Matrix(int height, int width)
{
this->height = height;
this->width = width;
this->matrix = vector<vector<double> >(this->height, vector<double>(this->width));
}
Matrix Matrix::add(Matrix m)
{
Matrix result(this->height, this->width);
if (m.height== this->height&& m.width== this->width)
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
result.matrix[i][j] = this->matrix[i][j] + m.matrix[i][j];
}
return result;
}
}
else
{
cout << "Impossible to do addition, matrices doesn't have the same dimension" << endl;
return result;
}
}
Matrix Matrix::applyFunction(double(*function)(double))
{
Matrix result(this->height, this->width);
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
result.matrix[i][j] = (*function)(this->matrix[i][j]);
}
}
return result;
}
void Matrix::print()
{
for (int i = 0; i < this->height; i++)
{
for (int j = 0; j < this->width; j++)
{
cout << this->matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
the output should be the addition beetwen A B 2x2:
x1 x2
x3 x4
but computer show only zeros.
Your member functions all return a new object (they return "by value").
From your usage of chaining, it seems like you actually want to modify the object and return *this by reference.
Otherwise you'll need something like:
auto bar2 = foo.function1(bar1);
auto bar3 = foo.function2(bar2);
// etc
There are no pointers here at present.
There are two variants how you can implement your add
Matrix add(Matrix m)
{
// optimisation: you don't need separate result, m already IS a copy!
// so you can just calculate:
...
{
m.matrix[i][j] += this->matrix[i][j];
}
return m;
}
or:
Matrix& add(Matrix const& m)
// ^ accept const reference to avoid unnecessary copy
// ^ returning reference(!)
{
...
{
// modifies itself!
this->matrix[i][j] += m.matrix[i][j];
}
return *this; // <- (!)
}
This allows now to do:
Matrix m0, m1, m2;
m0.add(m1).add(m2);
// m0 now contains the result, original value is lost (!)
So you don't need the final assignment as in first variant:
m0 = m0.add(m1).add(m2);
// or assign to a new variable, if you want to retain m0's original values
which is what you lacked in your question (thus you did not get the desired result).
Maybe you want to have both variants, and you might rename one of. But there's a nice feature in C++ that you might like even better: Operator overloading. Consider ordinary int:
int n0, n1;
n0 += n1;
int n2 = n0 + n1;
Well, suppose you know what's going on. And if you could do exactly the same with your matrices? Actually, you can! You need to do is overloading the operators:
Matrix& operator+=(Matrix const& m)
{
// identical to second variant of add above!
}
Matrix operator+(Matrix m) // again: the copy!
{
// now implement in terms of operator+=:
return m += *this;
}
Yes, now you can do:
Matrix m0, m1, m2;
m0 += m1 += m2;
m2 = m1 + m0;
Alternatively (and I'd prefer it) you can implement the second operator (operator+) as free standing function as well:
// defined OUTSIDE Matrix class!
Matrix operator+(Matrix first, Matrix const& second)
{
return first += second;
}
Finally: If dimensions don't match, better than returning some dummy matrix would be throwing some exception; std::domain_error might be a candidate for, or you define your own exception, something like SizeMismatch. And please don't output anything to console or elsewhere in such operators, this is not what anybody would expect from them, additionally, you impose console output to others who might consider it inappropriate (perhaps they want output in another language?).

How to add a column to a multi-dimesional vector (C++)?

This might be easy but I just cant find a solution. I created a 2D vector like this:
vector<vector<int> > matrix;
vector<int> row;
for (int i = 0; i < x;i++) {
row.push_back(i);
}
for (int i = 0; i < x; i++) {
matrix.push_back(row);
}
The problem I have is that I now wanna add rows and columns as I need them because I might run out of matrix space over time but I don't know how to do it. The rows are easy I can just .push_back another row to the bottom end of the matrix...but I have no Idea how to add another column.
I'm sorry if this question is super stupid but I'm not that experienced in programming yet and I couldn't find anything on the particular problem of adding columns to a 2D vector.
Adding to your code, the idea is to loop over each row in the matrix and push a new element onto each vector. Together, all of these elements are a new column.
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Initialize a 3x3 matrix
vector<vector<int>> matrix;
vector<int> row;
int x = 3;
for (int i = 0; i < x; i++) {
row.push_back(i);
}
for (int i = 0; i < x; i++) {
matrix.push_back(row);
}
// Add a column; the matrix is now 3x4
int newElement = 3;
for (auto &row : matrix) {
row.push_back(newElement);
}
// Print the matrix
for (auto &row : matrix) {
for (auto &cell : row) {
cout << cell << ' ';
}
cout << endl;
}
return 0;
}
You'll likely want to separate this code into functions.
vector<vector<T>> is a BAD way to implement a matrix. Well - it may not be a bad backend implementation, but its a bad public API.
I'd suggest using some library designed to support a Matrix. A few come to mind:
https://github.com/SophistSolutions/Stroika/blob/V2.1-Release/Library/Sources/Stroika/Foundation/Math/LinearAlgebra/Matrix.h
https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html
http://arma.sourceforge.net/docs.html#part_classes
or write your own and implement the basic functions directly.
Here is a simple function that will add a column to your matrix at a specified position:
void add_column(std::vector<std::vector<int>>& matrix,
const std::vector<int>& column, std::size_t position)
{
if(matrix.size() < position || position < 0) return; // check for position correctness
std::size_t index = 0;
for(auto& row : matrix){
row.insert(row.begin() + position, column[index++]);
}
}
For example, given a matrix:
std::vector<std::vector<int>> matrix;
and initializing it like so:
std::vector<int> column;
for (int i = 0; i < 3; i++) {
column.push_back(3);
}
for (int i = 0; i < 3; i++) {
matrix.push_back(column);
}
you end with with 3x3 matrix of 3s. Now, adding a column in between the first and the second one:
add_column(matrix, std::vector<int>({9, 9, 9}), 1);
and printing it:
for (auto& row : matrix) {
for (const auto x : row) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
You end up with:
3 9 3 3
3 9 3 3
3 9 3 3

Program stuck in infinite lop during single for loop calculation

I am new to c++ programming and am taking a computational physics class where we are analyzing the problem of percolation on a square lattice using a single-cluster algorithm. My professor has given us some base code, and asked us to modify it as well as write some additional code and scripts within and without this specific program. I have written the majority of the code and scripts necessary to solve and plot this problem, but I am having an issue with my main data output program, specifically that of an infinite loop when I set an input parameter to any value other than 0.
Three main function comprise this program, namely LATTICE::LATTICE, CLUSTER::grow, and CUSTER::print, and also uses a standard Mersenne Twister header file. The heavily modified, commented, and toyed with c++ program is as follows:
#include <fstream>
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include "MersenneTwister.h"
using namespace std;
class PARAMS
{
public:
int Nlin; // linear size of lattice
double pr; // probability for a site
double Nclust; // number of clusters in a bin
double Nbin; // number of bins of data to output
int SEED; // seed for mersenne twister
string latt_; // which lattice
PARAMS();//constructor
};
class LATTICE
{
public:
LATTICE(const PARAMS&);//constructor
int Nsite;// number of lattice sites
int Lx,Ly;
vector<vector<int> > nrnbrs;
void print ();
};
class CLUSTER
{
public:
CLUSTER(const PARAMS&, const LATTICE&);//constructor
void grow(const PARAMS&, const LATTICE&, MTRand&);
void meas_clear(const LATTICE&);
void meas(const LATTICE&);
void binwrite(const PARAMS&, const LATTICE&);
//void print(const LATTICE& latt, int index);
void print(const PARAMS& p, const LATTICE& latt);
~CLUSTER();// destructor
//private:
int size;
vector <int> conf;
vector <int> stack;
double pr;
//int stck_pnt,stck_end;
double avg_size;
ofstream dfout;
vector <int> stck_pnt;
vector <int> stck_end;
int z, pnt, prob, val, row, column;
vector< vector< vector <int> > > imax;
};
int main(void)
{
PARAMS p;
LATTICE latt(p);
CLUSTER cluster(p,latt);
MTRand ran(p.SEED);
latt.print();
/*for (int bin=0;bin<p.Nbin;bin++)
{
cluster.meas_clear(latt);
for(int clust=0;clust<p.Nclust;clust++)
{
cluster.grow(p,latt,ran);
cluster.meas(latt);
}
cluster.binwrite(p,latt);
}
*/
cluster.grow(p, latt, ran);
cluster.print(p,latt);
}
PARAMS::PARAMS(){
//initializes commonly used parameters from a file
ifstream pfin;
pfin.open("param.dat");
if (pfin.is_open()) {
pfin >> Nlin;
pfin >> pr;
pfin >> Nclust;
pfin >> Nbin;
pfin >> SEED;
pfin >> latt_;
}
else
{cout << "No input file to read ... exiting!"<<endl;exit(1);}
pfin.close();
// print out all parameters for record
cout << "--- Parameters at input for percolation problem ---"<<endl;
cout <<"Nlin = "<<Nlin<<"; prob. of site = "<<pr<<endl;
cout <<"Number of clusters in a bin = "<<Nclust<<"; Number of bins = "<<Nbin<<endl;
cout <<"RNG will be given SEED of = "<<SEED<<endl;
cout <<"Percolation problem on lattice --> "<<latt_<<endl;
};//constructor
LATTICE::LATTICE (const PARAMS& p)
{
string latt_=p.latt_;
if(p.latt_=="sqlatt_PBC")
{
Lx=p.Nlin;Ly=p.Nlin;
Nsite=Lx*Ly;
int i;
nrnbrs = vector<vector<int> >(Nsite, vector<int>(4));
for (i=0; i<Nsite; i++){
if((i+1) % p.Nlin != 0) nrnbrs[i][0] = i+1;
else nrnbrs[i][0] = i - p.Nlin + 1 ;
if(i + p.Nlin < Nsite ) nrnbrs[i][1] = i+p.Nlin;
else nrnbrs[i][1] = i - (Nsite-p.Nlin);
if(i % p.Nlin > 0) nrnbrs[i][2] = i-1;
else nrnbrs[i][2] = i-1+p.Nlin;
if(i - p.Nlin >= 0) nrnbrs[i][3] = i-p.Nlin;
else nrnbrs[i][3] = i + (Nsite-p.Nlin);
}
}
else if(p.latt_=="sqlatt_OBC")
{
Lx=p.Nlin;Ly=p.Nlin;
Nsite=Lx*Ly;
nrnbrs = vector<vector<int> >(Nsite, vector<int>(0));
for (int i=0; i<Nsite; i++){
if((i+1) % p.Nlin != 0){
nrnbrs[i].push_back(i+1);
}
if(i + p.Nlin < Nsite ){
nrnbrs[i].push_back(i+p.Nlin);
}
if(i % p.Nlin > 0){
nrnbrs[i].push_back(i-1);
}
if(i - p.Nlin >= 0){
nrnbrs[i].push_back(i-p.Nlin);
}
}
}
else
{cout <<"Dont know your option for lattice in param.dat .. exiting"<<endl;exit(1);}
}
void LATTICE::print()
{
//THIS FUNCTIONS MAY BE CALLED DURING DEBUGGING TO MAKE SURE LATTICE HAS BEEN DEFINED CORRECTLY
cout <<"---printing out properties of lattice ---"<<endl;
cout<<"size is "<<Lx<<"x"<<Ly<<endl;
cout <<"neighbors are"<<endl;
for (int site=0;site<Nsite;site++)
{
cout <<site<<" : ";
for (size_t nn=0;nn<nrnbrs.at(site).size();nn++)
cout<<nrnbrs.at(site).at(nn)<<" ";
cout <<endl;
}
cout << endl;
}
CLUSTER::CLUSTER(const PARAMS& p, const LATTICE& latt)
{
conf.resize(latt.Nsite);
stack.resize(latt.Nsite);
pr=p.pr;// store prob in a private member of cluster
dfout.open("data.out");
}
CLUSTER::~CLUSTER()
{
dfout.close();
}
void CLUSTER::grow(const PARAMS& p, const LATTICE& latt, MTRand& ran)
{
conf.resize(latt.Nsite); // Initalize Nsite elements of lattice to 0 in conf
// 0 = Not Asked; 1 = Asked, Joined; 2 = Asked, Refused
for (int i = 0; i < p.Nclust; ++i) { // Iterate for Nclust values
z = ran.randInt(latt.Nsite - 1); // Random integer between 0 and Nsite; Selects first lattice element in the cluster algorithm per Nclus
stck_pnt.resize(0); // Set stck_pnt and stck_end vectors to size 0; Will be filled when iterating through each Nclust
stck_end.resize(0); //-----------------------------------------------------------------------------------------------
//while (conf[z] != 0) { z = ran.randInt(latt.Nsite - 1); } // Iterate through lattice elements until we select one that has not been asked to join
conf[z] = 1; // Set element z in conf to have been asked to join and accepted
stck_pnt.push_back(z); // Add z to both stck_pnt and stck_end
stck_end.push_back(z);
for (int j = 0; j = 3; ++j) { // Add z's nearest neighbors to stck_end; Ignore if already been asked
if (conf[latt.nrnbrs[z][j] == 0]) {
stck_end.push_back(latt.nrnbrs[z][j]);
}
}
pnt = 1; // Initialize pnt for trasnferral of stack_end values to stck_pnt
while (stck_pnt.size() < stck_end.size()) {
stck_pnt.push_back(stck_end[pnt]); // Add pnt element of stck_end to stck_pnt
double prob = ran.rand(); // Get probability value for testing if cluster grows
if (prob <= pr) {
conf[stck_pnt[pnt]] = 1; // Set the current stck_pnt element to joined in conf
for (int j = 0; j = 3; ++j) { // Add z's nearest neighbors to stck_end; Ignore if already been asked
if (find(stck_end.begin(), stck_end.end(), latt.nrnbrs[stck_pnt[pnt]][j]) != stck_end.end()) {
// The given value already exists in stck_end, don't add it again
}
else { // The given value is not contained in stck_end, add it to stck_end
stck_end.push_back(latt.nrnbrs[z][j]);
}
}
}
else {
conf[stck_pnt[pnt]] = 2; // Set the given value to haven been asked and refused in conf
}
++pnt; // Increment pnt; ++p is more efficient then p++ due to lack of copying value
}
}
}
/*
void CLUSTER::print(const LATTICE& latt, int index)
{
stringstream ss;
string file_name;
ss << index << ".clust";
file_name = ss.str();
ofstream clout;
clout.open(file_name.c_str());
clout << "#" << latt.Lx << " x " << latt.Ly << endl;
for (int y = 0; y < latt.Ly; y++)
{
for (int x = 0; x < latt.Lx; x++)
clout << conf[x + y*latt.Lx] << " ";
clout << endl;
}
clout.close();
}
*/
void CLUSTER::print(const PARAMS& p, const LATTICE& latt)
{
//vector< vector< vector<int> > > imax(latt.Lx, vector< vector<int>>(latt.Ly, vector<int>(1)));
// Resize and allocate memeory for imax
//-------------- Row = y-position = i/Lx --------------- Column = x-position = i%Lx ---------------- val = conf[i]
ofstream myFile;
myFile.open("imax.out");
cout << "THe following output was calculated for the input parameters; Recorded to 'imax.out'" << endl;
cout <<"[index]" << "\t" << "[x-position]" << "\t" << "[y-position]" << "\t" << "[conf val]" << endl << endl;
for (int i = 0; i < latt.Nsite; ++i) {
val = conf[i]; // Find color value
row = i / latt.Lx; // Find row number
column = i%latt.Lx; // Find column number
cout << i << "\t" << column << "\t" << row << "\t" << val << endl;
myFile << i << "\t" << column << "\t" << row << "\t" << val << endl;
}
myFile.close();
double size = 0.0; // Initialize size
for (int i = 0; i < latt.Nsite; ++i) {
if (conf[i] == 1) {
size += 1;
}
}
double avg_size = size / p.Nclust; // Find avg_size
}
void CLUSTER::meas(const LATTICE& latt)
{
avg_size+=(double)size;
}
void CLUSTER::meas_clear(const LATTICE& latt)
{
avg_size=0.;
}
void CLUSTER::binwrite(const PARAMS& p, const LATTICE& latt)
{
dfout << avg_size/((double)p.Nclust)<<endl;
}
When I set Nclust=0 in the input file, the code runs as expected and gives the proper output in the file and console. However, when I set Nclust equal to any other value, I get the proper lattice console output but the program hangs for the cluster algorithm. I at first assumed that my computer and algorithm were slow and inefficient and that the program was working in some non-linear time. However, after leaving the program running for around 30 minutes for a 4x4 lattice (only 16 elements in the conf[] vector), no progress had been made and I assumed that the program was stuck in a loop.
After spending several hours going over the CLUSTER::grow() method line-by-line and experimenting with changing various bits of code, I have been unable to resolve where this loop error originates from. I would assume it is somewhere in the while loop that compares the size of stck_pnt and stck_end, but I cannot figure out why or where this is. Any help with this would be very greatly appreciated.
Tl;dr: For Nclust !=0, CLUSTER:grow gets stuck in an infinite loop
You have infinite loop here:
stck_end.push_back(z);
for (int j = 0; j = 3; ++j) { // <======== HERE
and here:
conf[stck_pnt[pnt]] = 1; // Set the current stck_pnt element to joined in conf
for (int j = 0; j = 3; ++j) { // <======== HERE