Commit 246642f7 authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

CrsMatrix closures checks pass

parent f538cb65
......@@ -80,18 +80,102 @@ void testMatrix()
std::cout<<v2;
}
template<typename T>
void createRandomCrs(PsimagLite::CrsMatrix<T>& crs,
SizeType seed,
SizeType nonZeros,
T maxValue)
{
srand48(seed);
PsimagLite::Matrix<T> m(crs.row(),crs.col());
for (SizeType i=0;i<nonZeros;i++) {
// pick a row
SizeType row = SizeType(drand48()*m.n_row());
// and a column
SizeType col = SizeType(drand48()*m.n_col());
// and a value
T val = drand48()*maxValue;
m(row,col) = val;
}
fullMatrixToCrsMatrix(crs,m);
}
template<typename T>
bool checkCrs(const PsimagLite::CrsMatrix<T>& m,
const PsimagLite::Matrix<T>& fm)
{
PsimagLite::Matrix<T> mf;
crsMatrixToFullMatrix(mf,m);
for (SizeType i = 0; i < fm.n_row(); ++i) {
for (SizeType j = 0; j < fm.n_col(); ++j) {
if (fabs(mf(i,j)-fm(i,j)) > 1e-6) {
std::cout<<mf;
std::cout<<fm;
return false;
}
}
}
return true;
}
void testCrsMatrix()
{
SizeType n = 4;
PsimagLite::CrsMatrix<double> s(n,n);
s.makeDiagonal(n,1.1);
std::cout<<s;
SizeType n = 10;
unsigned int long long seed = 343981;
double ratio = 0.5;
SizeType nonZeros = SizeType(ratio*n*n);
double maxValue = 10.0;
PsimagLite::CrsMatrix<double> m1(n,n);
createRandomCrs(m1,seed,nonZeros,maxValue);
PsimagLite::Matrix<double> fm1;
crsMatrixToFullMatrix(fm1,m1);
//std::cout<<m1;
//std::cout<<"-------------\n";
seed += 111;
PsimagLite::CrsMatrix<double> m2(n,n);
createRandomCrs(m2,seed,nonZeros,maxValue);
PsimagLite::Matrix<double> fm2;
crsMatrixToFullMatrix(fm2,m2);
//std::cout<<m2;
std::cout<<"---operator+=scalar*crs----TEST 1/4\n";
m1 += 1.2*m2;
// std::cout<<m1;
fm1 += 1.2*fm2;
std::cout<<"CHECK PASSES="<<checkCrs(m1,fm1)<<"\n";
std::cout<<"-------------\n";
std::cout<<"---ctor(crs*crs)----TEST 2/4\n";
PsimagLite::CrsMatrix<double> m3 = m1*m2;
PsimagLite::Matrix<double> fm3 = fm1*fm2;
std::cout<<"CHECK PASSES="<<checkCrs(m3,fm3)<<"\n";
// std::cout<<m3;
// std::cout<<"-------------\n";
std::cout<<"---ctor(scalar*crs)----TEST 3/4\n";
PsimagLite::CrsMatrix<double> m4 = 1.3*m2;
PsimagLite::Matrix<double> fm4;
fm4 = 1.3*fm2;
std::cout<<"CHECK PASSES="<<checkCrs(m4,fm4)<<"\n";
// std::cout<<m4;
// std::cout<<"-------------\n";
std::cout<<"---ctor(scalar*crs)----TEST 4/4\n";
m4 += 1.4*m1*m3;
PsimagLite::Matrix<double> fm5;
fm5 = 1.4*fm1*fm3;
fm4 += fm5;
// std::cout<<m4;
std::cout<<"CHECK PASSES="<<checkCrs(m4,fm4)<<"\n";
std::cout<<"-------------\n";
}
int main(int, char**)
{
// testVector();
// testMatrix();
testVector();
testMatrix();
testCrsMatrix();
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment