Mentions légales du service

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

Update/Review Faust.issparse doc. and make possible to consider BSR matrices as sparse matrix too.

parent 953f7edc
Branches
Tags
No related merge requests found
......@@ -40,7 +40,7 @@ namespace Faust
bool isTransposed() const;
bool isConjugate() const;
bool isReal() const;
bool is_all_sparse() const;
bool is_all_sparse(bool csr=true, bool bsr=false) const;
bool is_all_dense() const;
virtual faust_unsigned_int size() const=0;
......
......@@ -480,11 +480,13 @@ namespace Faust
}
template<typename FPP, FDevice DEV>
bool TransformHelperGen<FPP, DEV>::is_all_sparse() const
bool TransformHelperGen<FPP, DEV>::is_all_sparse(bool csr/*=true*/, bool bsr/*=false*/) const
{
if(this->size() == 0)
return false;
for(int i=0;i<this->size();i++)
{
if(! dynamic_cast<const MatSparse<FPP,DEV>*>(this->get_gen_fact(i)))
if(! (csr && dynamic_cast<const MatSparse<FPP,DEV>*>(this->get_gen_fact(i)) || bsr && dynamic_cast<const MatBSR<FPP,DEV>*>(this->get_gen_fact(i))))
return false;
}
return true;
......
......@@ -1279,7 +1279,7 @@ classdef Faust
%> nf = numfactors(F)
%> @endcode
%>
%> <p>@b See @b also Faust.factors.
%> <p>@b See @b also Faust.factors
%==========================================================================================
function num_factors = numfactors(F)
num_factors = call_mex(F, 'numfactors');
......@@ -1288,9 +1288,94 @@ classdef Faust
%==========================================================================================
%> @brief Returns true if F factors are all sparse matrices false otherwise.
%>
%> What a sparse factor is, depends on csr and bsr arguments. Defaultly,
%> only a Faust full of CSR matrices is taken as a sparse Faust.
%>
%> @param csr, bool true to consider a CSR matrix as a sparse matrix, false otherwise (true by default).
%> @param bsr, bool true to consider a BSR matrix as a sparse matrix, false otherwise (false by default).
%>
%> @b Example:
%> @code
%> >> F = matfaust.rand(10, 10, 'fac_type', 'sparse')
%>
%> F =
%>
%> Faust size 10x10, density 2.5, nnz_sum 250, 5 factor(s):
%> - FACTOR 0 (double) SPARSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 1 (double) SPARSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 2 (double) SPARSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 3 (double) SPARSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 4 (double) SPARSE, size 10x10, density 0.5, nnz 50
%> >> issparse(F) % equivalent to issparse(F, 'csr', true)
%>
%> ans =
%>
%> 1
%>
%> >> issparse(F, 'csr', false, 'bsr', false)
%>
%> ans =
%>
%> 0
%>
%> >> issparse(F, 'csr', false, 'bsr', false)
%> ans =
%>
%> 0
%> >> issparse(F, 'csr', false, 'bsr', false)
%> Error using matfaust.Faust/issparse (line 1945)
%> It doesn't make sense to set csr=false and bsr=false as the function always return false
%>
%> >> F = matfaust.rand_bsr(10, 10, 2, 2, 2, .1)
%>
%> F =
%>
%> Faust size 10x10, density 0.6, nnz_sum 60, 5 factor(s):
%> - FACTOR 0 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
%> - FACTOR 1 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
%> - FACTOR 2 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
%> - FACTOR 3 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
%> - FACTOR 4 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
%> >> issparse(F) % default config. recognizes only csr
%>
%> ans =
%>
%> 0
%>
%> >> issparse(F, 'bsr', true)
%>
%> ans =
%>
%> 1
%>
%> >> F = matfaust.rand(10, 10, 'fac_type', 'dense')
%>
%> F =
%>
%> Faust size 10x10, density 2.5, nnz_sum 250, 5 factor(s):
%> - FACTOR 0 (double) DENSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 1 (double) DENSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 2 (double) DENSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 3 (double) DENSE, size 10x10, density 0.5, nnz 50
%> - FACTOR 4 (double) DENSE, size 10x10, density 0.5, nnz 50
%> >> issparse(F)
%>
%> ans =
%>
%> 0
%> @endcode
%>
%> <p>@b See @b also Faust.isdense, matfaust.rand, matfaust.rand_bsr
%==========================================================================================
function is_sparse = issparse(F)
is_sparse = call_mex(F, 'is_all_sparse');
function is_sparse = issparse(F, varargin)
p = inputParser;
addOptional(p, 'csr', true, @islogical);
addOptional(p, 'bsr', false, @islogical);
parse(p, varargin{:});
if(~ p.Results.csr && ~ p.Results.bsr)
error('It doesn''t make sense to set csr=false and bsr=false as the function will always return false')
end
is_sparse = call_mex(F, 'is_all_sparse', p.Results.csr, p.Results.bsr);
end
%==========================================================================================
......@@ -1347,6 +1432,8 @@ classdef Faust
%> 0
%>
%> @endcode
%>
%> <p>@b See @b also Faust.issparse, matfaust.rand, matfaust.rand_bsr
%==========================================================================================
function is_dense = isdense(F)
is_dense = call_mex(F, 'is_all_dense');
......
......@@ -9,9 +9,15 @@ void faust_is_all_sparse(const mxArray **prhs, const int nrhs, mxArray **plhs, c
Faust::TransformHelper<SCALAR,DEV>* core_ptr = convertMat2Ptr<Faust::TransformHelper<SCALAR,DEV> >(prhs[1]);
if (nlhs != 1)
{
mexErrMsgTxt("factors : incorrect number of arguments.");
mexErrMsgTxt("is_all_sparse: incorrect number of output arguments.");
}
plhs[0] = mxCreateDoubleScalar(core_ptr->is_all_sparse());
if (nrhs != 4)
{
mexErrMsgTxt("is_all_sparse: incorrect number of input arguments.");
}
bool csr = (bool) mxGetScalar(prhs[2]);
bool bsr = (bool) mxGetScalar(prhs[3]);
plhs[0] = mxCreateDoubleScalar(core_ptr->is_all_sparse(csr, bsr));
}
template <typename SCALAR, FDevice DEV>
......@@ -20,7 +26,7 @@ void faust_is_all_dense(const mxArray **prhs, const int nrhs, mxArray **plhs, co
Faust::TransformHelper<SCALAR,DEV>* core_ptr = convertMat2Ptr<Faust::TransformHelper<SCALAR,DEV> >(prhs[1]);
if (nlhs != 1)
{
mexErrMsgTxt("factors : incorrect number of arguments.");
mexErrMsgTxt("is_all_dense: incorrect number of output arguments.");
}
plhs[0] = mxCreateDoubleScalar(core_ptr->is_all_dense());
}
......@@ -2085,11 +2085,55 @@ class Faust(numpy.lib.mixins.NDArrayOperatorsMixin):
"""
return isinstance(obj, Faust)
def issparse(F):
def issparse(F, csr=True, bsr=False):
"""
Returns True if all factors are sparse (csr_matrix format) False otherwise.
Returns True if all F factors are sparse False otherwise.
What a sparse factor is, depends on csr and bsr arguments.
Args:
csr: True to consider CSR matrices in F as sparse matrices, False otherwise.
bsr: True to consider BSR matrices in F as sparse matrices, False otherwise.
Example:
>>> import pyfaust as pf
>>> F = pf.rand(10, 10, fac_type='sparse')
>>> F
Faust size 10x10, density 2.5, nnz_sum 250, 5 factor(s):
- FACTOR 0 (double) SPARSE, size 10x10, density 0.5, nnz 50
- FACTOR 1 (double) SPARSE, size 10x10, density 0.5, nnz 50
- FACTOR 2 (double) SPARSE, size 10x10, density 0.5, nnz 50
- FACTOR 3 (double) SPARSE, size 10x10, density 0.5, nnz 50
- FACTOR 4 (double) SPARSE, size 10x10, density 0.5, nnz 50
>>> F.issparse()
True
>>> F.issparse(csr=True)
True
>>> F.issparse(csr=False)
ValueError: It doesn't make sense to set csr=False and bsr=False as the function will always return False
>>> F = pf.rand_bsr(10, 10, 2, 2, 2, .1)
>>> F
Faust size 10x10, density 0.24, nnz_sum 24, 2 factor(s):
- FACTOR 0 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
- FACTOR 1 (double) BSR, size 10x10 (blocksize = 2x2), density 0.12, nnz 12 (nnz blocks: 3)
>>> F.issparse() # default config. recognizes only csr
False
>>> F.issparse(bsr=True)
True
>>> F = pf.rand(10, 10, fac_type='dense')
>>> F.issparse()
False
<b>See also</b> Faust.isdense, pyfaust.rand, pyfaust.rand_bsr
"""
return F.m_faust.is_all_sparse()
if not csr and not bsr:
raise ValueError('It doesn\'t make sense to set csr=False and'
' bsr=False as the function will always return'
' False')
return F.m_faust.is_all_sparse(csr, bsr)
def isdense(F):
"""
......@@ -2130,6 +2174,8 @@ class Faust(numpy.lib.mixins.NDArrayOperatorsMixin):
>>> F.isdense()
False
<b>See also</b> Faust.issparse, pyfaust.rand, pyfaust.rand_bsr
"""
return F.m_faust.is_all_dense()
......
......@@ -115,7 +115,7 @@ class FaustCoreCpp
double get_nb_factors() const;
unsigned int get_fact_nb_rows(unsigned int& i) const;
unsigned int get_fact_nb_cols(unsigned int& i) const;
bool is_all_sparse() const;
bool is_all_sparse(bool csr, bool bsr) const;
bool is_all_dense() const;
void get_fact(const unsigned int& i, FPP* fact_ptr) const;
void get_fact_sparse(const unsigned int& i,
......
......@@ -686,9 +686,9 @@ FaustCoreCpp<FPP,DEV>::~FaustCoreCpp()
}
template<typename FPP, FDevice DEV>
bool FaustCoreCpp<FPP,DEV>::is_all_sparse() const
bool FaustCoreCpp<FPP,DEV>::is_all_sparse(bool csr, bool bsr) const
{
return transform->is_all_sparse();
return transform->is_all_sparse(csr, bsr);
}
template<typename FPP, FDevice DEV>
......
......@@ -50,7 +50,7 @@ cdef extern from "FaustCoreCpp.h":
size_t& bnrows,
size_t& bncols) const
void get_fact_bsr(const size_t id, FPP* bdata, int* brow_ptr, int* bcol_inds) const
bool is_all_sparse() const
bool is_all_sparse(bool csr, bool bsr) const
bool is_all_dense() const
@CPP_CORE_CLASS@[FPP]* right(const unsigned int) const
@CPP_CORE_CLASS@[FPP]* left(const unsigned int) const
......
......@@ -384,8 +384,8 @@ cdef class FaustCoreGen@TYPE_NAME@@PROC@:
nb_factors = int(self.@CORE_OBJ@.get_nb_factors())
return nb_factors
def is_all_sparse(self):
return self.@CORE_OBJ@.is_all_sparse()
def is_all_sparse(self, csr, bsr):
return self.@CORE_OBJ@.is_all_sparse(csr, bsr)
def is_all_dense(self):
return self.@CORE_OBJ@.is_all_dense()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment