Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 69267e7f authored by hhakim's avatar hhakim
Browse files

Fix bug on unit test of MatSparse::vstack which was due to...

Fix bug on unit test of MatSparse::vstack which was due to MatSparse::randMat() failure when FPP==float.
parent b1e8b1bf
Branches
Tags
No related merge requests found
......@@ -12,8 +12,8 @@ typedef @TEST_FPP@ FPP;
int main()
{
MatSparse<FPP,Cpu>* M1 = MatSparse<FPP,Cpu>::randMat(3, 4, .5);
MatSparse<FPP,Cpu>* M2 = MatSparse<FPP,Cpu>::randMat(13, 4, .5);
MatSparse<FPP,Cpu>* M1 = MatSparse<FPP,Cpu>::randMat(30, 40, .5);
MatSparse<FPP,Cpu>* M2 = MatSparse<FPP,Cpu>::randMat(130, 40, .5);
MatSparse<FPP,Cpu> M;
M.vstack(*M1, *M2);
cout << "M1:" << endl;
......@@ -29,7 +29,8 @@ int main()
memcpy(buf, M1->getValuePtr(), sizeof(FPP)*M1->getNonZeros());
memcpy(buf+M1->getNonZeros(), M2->getValuePtr(), sizeof(FPP)*M2->getNonZeros());
MatDense<FPP,Cpu> Mds(buf, M.getNonZeros(), 1);
assert(abs(Mds.norm()-M.norm()) < 1e-1);
auto diff = Mds.norm()-M.norm();
assert(abs(diff) < 1e-1);
for(int i=0;i < M.getNonZeros(); i++)
assert(abs(M.getValuePtr()[i] - buf[i] < 1e-6));
delete [] buf;
......
......@@ -372,9 +372,9 @@ namespace Faust
void print_bufs(const std::string name="");
void print_asarray(const std::string name="");
static MatSparse<FPP, Cpu>* randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, double density);
static MatSparse<FPP, Cpu>* randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, Real<FPP> density);
//\param : per_row means the density applies for each line rather than globally for the matrix
static MatSparse<FPP, Cpu>* randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, double density, bool per_row);
static MatSparse<FPP, Cpu>* randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, Real<FPP> density, bool per_row);
static MatSparse<FPP, Cpu>* eye(faust_unsigned_int num_rows, faust_unsigned_int num_cols);
// Permutation matrix set to exchange two rows or columns (depending on the multiplication side)
......
......@@ -959,21 +959,21 @@ Faust::MatSparse<FPP,Cpu>* Faust::MatSparse<FPP,Cpu>::get_rows(faust_unsigned_in
}
template<typename FPP>
Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, double density)
Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, Real<FPP> density)
{
std::default_random_engine generator(rand());
std::uniform_real_distribution<double> distribution(0, 1);
std::uniform_real_distribution<Real<FPP>> distribution(0, 1);
std::uniform_int_distribution<faust_unsigned_int> int_distribution(0, num_rows*num_cols-1);
typedef Eigen::Triplet<FPP,faust_unsigned_int> T;
std::vector<T> tripletList;
MatSparse<FPP, Cpu>* fsmat = new MatSparse<FPP,Cpu>();
Eigen::SparseMatrix<FPP,Eigen::RowMajor> mat(num_rows, num_cols);
FPP rand_number;
complex<double> rs;
complex<Real<FPP>> rs;
try {
faust_unsigned_int num_elts = (faust_unsigned_int)(num_rows*num_cols*density);
tripletList.reserve(num_elts);
faust_unsigned_int i,col_id,row_id, r;
faust_unsigned_int i, col_id,row_id, r;
i = 0;
while(i < num_elts)
{
......@@ -981,9 +981,10 @@ Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_i
row_id = r/num_cols;
col_id = r - row_id * num_cols;
assert(col_id >= 0 && col_id < num_cols);
rs = complex<double>(distribution(generator), distribution(generator));
rs = complex<Real<FPP>>(distribution(generator), distribution(generator));
memcpy(&rand_number, &rs, sizeof(FPP));
// cout << row_id << " " << col_id << " " << rand_number << endl;
// rand_number = complex2other<FPP>(rs);
// cout << row_id << " " << col_id << " " << rand_number << rs << sizeof(FPP) << sizeof(complex<FPP>)<< endl;
tripletList.push_back(T(row_id,col_id, rand_number));
i++;
}
......@@ -1001,7 +1002,7 @@ Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_i
}
template<typename FPP>
Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, double density, bool per_row)
Faust::MatSparse<FPP, Cpu>* Faust::MatSparse<FPP, Cpu>::randMat(faust_unsigned_int num_rows, faust_unsigned_int num_cols, Real<FPP> density, bool per_row)
{
std::default_random_engine generator(rand());
std::uniform_real_distribution<Real<FPP>> distribution(0, 1);
......@@ -1346,4 +1347,5 @@ void Faust::copy_sp_mat(Faust::MatSparse<FPP,Cpu>& src, Faust::MatSparse<FPP, Cp
memcpy(dst.getColInd(), src.getColInd(), src.getNonZeros()*sizeof(int));
memcpy(dst.getRowPtr(), src.getRowPtr(), (src.getNbRow()+1)*sizeof(int));
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment