Mentions légales du service

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

Extend pyfaust/matfaust.Faust.get_factor() to return a Faust composed of a...

Extend pyfaust/matfaust.Faust.get_factor() to return a Faust composed of a sub-sequence of another Faust factors.

Document the new functionality.
This extension was resqueted by #69 comment 2, point 1.
parent 51a4403d
No related branches found
No related tags found
No related merge requests found
......@@ -919,22 +919,24 @@ classdef Faust
%>
%> @b Usage
%>
%>     @b factor = get_factor(F, i)
%>     @b factor = get_factor(F, i) returns the i-th factor of F.
%>     @b factor = get_factor(F, i:j) returns a new Faust formed of the F's factors from the i-th to the j-th included.
%>
%> @param F the Faust object.
%> @param i the factor index.
%> @param varargin the factor indices.
%>
%> @retval factor a copy of the i-th factor without altering its storage organization in the Faust F (full or sparse).
%> @retval factors a matrix copy of the i-th factor if i is a single index or a new Faust composed of i-th to the j-th factors of F. The factors copies keep the storage organization of the source matrix (full or sparse).
%>
%> @b Example
%> @code
%> import matfaust.FaustFactory
%> F = FaustFactory.rand(5, [50, 100], .5, 'mixed', 'complex')
%> f1 = get_factor(F, 1)
%> F = FaustFactory.rand(5, [50, 100], .5, 'mixed', 'complex');
%> f1 = get_factor(F, 1);
%> G = get_factor(F, 4:5); % a new Faust composed of the two last factors of F
%> @endcode
%> <p>@b See @b also Faust.get_num_factors
%=====================================================================
function factor = get_factor(F, i)
function factors = get_factor(F, varargin)
%% GET_FACT Ith factor of the Faust.
%
% A=get_factor(F,i) return the i factor A of the Faust F as a full storage matrix.
......@@ -944,21 +946,32 @@ classdef Faust
% A=get_factor(F,4) returns the 4th factor of the Faust F.
%
% See also get_num_factors.
factors = cell(1, size(varargin{1},2));
for j=1:length(factors)
i = varargin{1};
if(j < length(factors) && i(j+1) - i(j) ~= 1)
error('Indices must be contiguous.')
end
i = i(j);
if (~isa(i,'double'))
error('get_factor second argument (indice) must either be real positive integers or logicals.');
end
if (~isa(i,'double'))
error('get_factor second argument (indice) must either be real positive integers or logicals.');
end
if (floor(i) ~= i)
error('get_factor second argument (indice) must either be real positive integers or logicals.');
end
if (floor(i) ~= i)
error('get_factor second argument (indice) must either be real positive integers or logicals.');
if (F.isReal)
factors{j} = mexFaustReal('get_fact',F.matrix.objectHandle,i);
else
factors{j} = mexFaustCplx('get_fact',F.matrix.objectHandle,i);
end
end
if (F.isReal)
factor = mexFaustReal('get_fact',F.matrix.objectHandle,i);
if(length(factors) > 1)
factors = matfaust.Faust(factors);
else
factor = mexFaustCplx('get_fact',F.matrix.objectHandle,i);
factors = factors{j};
end
end
%=====================================================================
......
......@@ -1093,16 +1093,20 @@ class Faust:
"""
return F.m_faust.get_nb_factors()
def get_factor(F, i):
def get_factor(F, indices):
"""
Returns the i-th factor of F.
Args:
F: the Faust object.
i: the factor index.
indices: the factor contiguous indices.
Returns:
a copy of the i-th factor, the copy type is:
if indices is a single index: a copy of the i-th factor.
Otherwise: a new Faust composed of copies of the contigous factors of F
pointed by indices.
Each copy type is:
- numpy.ndarray if it is a full storage matrix or,
- scipy.sparse.csc.matrix_csc if it's a sparse matrix of a
transposed Faust,
......@@ -1117,11 +1121,27 @@ class Faust:
>>> from pyfaust import FaustFactory
>>> F = FaustFactory.rand(5, [50, 100], .5)
>>> f0 = F.get_factor(0)
>>> G = F.get_factor(range(3:5)) # a new Faust composed of the two last factors of F
<b/> See also Faust.get_num_factors, Faust.transpose
"""
fact = F.m_faust.get_fact_opt(i)
return fact
if(hasattr(indices, '__iter__')):
indices = list(indices)
else:
indices = list([indices])
factors = []
oi = None
for i in indices:
if(not isinstance(i, int)):
raise not_int_e
if(oi != None and i-oi != 1):
raise Exception("Index must be contiguous.")
factors += [F.m_faust.get_fact_opt(i)]
oi = i
if(len(factors) == 1):
return factors[0]
else:
return pyfaust.Faust(factors)
def get_factor_nonopt(F, i):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment