Mentions légales du service

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

Allow to normalize a MatDense through another norm than Frobenius + add...

Allow to normalize a MatDense through another norm than Frobenius + add MatDense::nonzerosToOnes function.
parent 28e85449
No related branches found
No related tags found
No related merge requests found
...@@ -314,8 +314,9 @@ namespace Faust ...@@ -314,8 +314,9 @@ namespace Faust
//! \return the Frobenius norm //! \return the Frobenius norm
Real<FPP> norm() const {return mat.norm();} Real<FPP> norm() const {return mat.norm();}
//! \brief Normalize the matrix according to its Frobenius norm //! \brief Normalize the matrix according to its Frobenius norm (norm_type==-2) by default.
void normalize(); // \param norm_type: the type of norm used to normalize the matrix. -2 Frobenius norm, 1 1-norm, 2 2-norm (spectral norm), -1 inf-norm.
void normalize(int norm_type=-2);
//! \param nbr_iter_max : maximum number of iteration for the power algo //! \param nbr_iter_max : maximum number of iteration for the power algo
...@@ -351,6 +352,9 @@ namespace Faust ...@@ -351,6 +352,9 @@ namespace Faust
//! \brief replace this by lambda * (*this) using element by element multiplication //! \brief replace this by lambda * (*this) using element by element multiplication
void scalarMultiply(MatDense<FPP,Cpu> const& A); void scalarMultiply(MatDense<FPP,Cpu> const& A);
//! Replaces the matrix by its support (all nonzeros are set to ones).
void nonzerosToOnes();
//! \brief (*this) = (*this) + A //! \brief (*this) = (*this) + A
void add(MatDense<FPP,Cpu> const& A); void add(MatDense<FPP,Cpu> const& A);
// //
......
...@@ -921,6 +921,14 @@ void MatDense<FPP,Cpu>::operator+=(const MatSparse<FPP,Cpu>& S) ...@@ -921,6 +921,14 @@ void MatDense<FPP,Cpu>::operator+=(const MatSparse<FPP,Cpu>& S)
template<typename FPP>
void MatDense<FPP,Cpu>::nonzerosToOnes() //TODO: rename nonZerosToOnes
{
auto a = (Eigen::abs(mat.array()) > 0 || Eigen::isnan(mat.array()) || Eigen::isinf(mat.array())).select(mat, Eigen::Matrix<FPP, Eigen::Dynamic, Eigen::Dynamic>::Ones(this->dim1, this->dim2));
this->is_identity = false;
isZeros = false;
}
template<typename FPP> template<typename FPP>
void MatDense<FPP,Cpu>::scalarMultiply(MatDense<FPP,Cpu> const& A) void MatDense<FPP,Cpu>::scalarMultiply(MatDense<FPP,Cpu> const& A)
{ {
...@@ -933,7 +941,6 @@ void MatDense<FPP,Cpu>::scalarMultiply(MatDense<FPP,Cpu> const& A) ...@@ -933,7 +941,6 @@ void MatDense<FPP,Cpu>::scalarMultiply(MatDense<FPP,Cpu> const& A)
isZeros = false; isZeros = false;
} }
template<typename FPP> template<typename FPP>
void MatDense<FPP,Cpu>::multiplyLeft(const MatSparse<FPP,Cpu>& S,const char TransS) void MatDense<FPP,Cpu>::multiplyLeft(const MatSparse<FPP,Cpu>& S,const char TransS)
{ {
...@@ -1092,13 +1099,30 @@ Real<FPP> Faust::MatDense<FPP, Cpu>::normInf(faust_unsigned_int& row_id, const b ...@@ -1092,13 +1099,30 @@ Real<FPP> Faust::MatDense<FPP, Cpu>::normInf(faust_unsigned_int& row_id, const b
} }
template<typename FPP> template<typename FPP>
void MatDense<FPP,Cpu>::normalize() void MatDense<FPP,Cpu>::normalize(int norm_type/*=-2*/)
{ {
auto n = norm(); Real<FPP> n;
switch(norm_type)
{
case -2: // frobenius
n = norm();
break;
case 2:
int flag; // not used
n = spectralNorm(FAUST_NORM2_MAX_ITER, FAUST_PRECISION, flag);
case 1:
n = normL1();
break;
case -1:
n = normInf();
break;
default:
throw std::runtime_error("Unknown kind of norm asked for normalization.");
}
if(n != FPP(0)) if(n != FPP(0))
scalarMultiply(FPP(1.0/n)); scalarMultiply(FPP(1.0/n));
else else
throw std::domain_error("the norm is zero, can't normalize"); throw std::domain_error("the norm is zero, can't normalize.");
} }
template<typename FPP> template<typename FPP>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment