Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 89f7e1b0 authored by hhakim's avatar hhakim
Browse files

Add MatPerm::isPerm.

parent a3bdf0a9
No related branches found
No related tags found
No related merge requests found
...@@ -96,6 +96,13 @@ namespace Faust ...@@ -96,6 +96,13 @@ namespace Faust
MatSparse<FPP, Cpu> toMatSparse() const; MatSparse<FPP, Cpu> toMatSparse() const;
void faust_gemm(const MatDense<FPP,Cpu> & B, MatDense<FPP,Cpu> & C,const FPP & alpha, const FPP & beta, char typeA, char typeB)const; //from LinearOperator void faust_gemm(const MatDense<FPP,Cpu> & B, MatDense<FPP,Cpu> & C,const FPP & alpha, const FPP & beta, char typeA, char typeB)const; //from LinearOperator
/**
* \brief Returns true if the MatSparse S is permutation (in which case it is eligible to a MatPerm conversion).
*
* If the matrix S is well-formed the cost is O(nnz^2) in the worst case.
*/
static bool isPerm(const MatSparse<FPP, Cpu> &S, bool verify_ones=true);
}; };
} }
......
...@@ -459,4 +459,44 @@ namespace Faust ...@@ -459,4 +459,44 @@ namespace Faust
return sp; return sp;
} }
template<typename FPP>
bool MatPerm<FPP, Cpu>::isPerm(const MatSparse<FPP, Cpu> &S, bool verify_ones/*=true*/)
{
// verify the matrix is square
if(S.getNbRow() != S.getNbCol()) return false;
// verify the nnz
if(S.getNonZeros() != S.getNbRow()) return false;
// verify only one nz is set per row
auto rowptr = S.getRowPtr();
for(int i=0;i < S.getNbRow(); i++)
{
if(rowptr[i+1] - rowptr[i] > 1)
return false;
}
// verify all columns are covered
std::vector<int> cols(S.getNbCol());
std::iota(cols.begin(), cols.end(), 0);
auto colind = S.getColInd();
for(int i=0;i<S.getNonZeros();i++)
{
auto it = std::find(cols.begin(), cols.end(), colind[i]);
if(it == cols.end()) return false; // a column is zero
cols.erase(it);
}
if(cols.size() != 0)
// not all columns are covered by the nz
return false;
if(verify_ones)
{
auto valptr = S.getValuePtr();
for(int i=0;i<S.getNonZeros();i++)
{
if(valptr[i] != FPP(1.0))
return false;
}
}
return true;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment