Mentions légales du service

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

Impl. for matfaust the mul. of a real Faust by a complex scalar, add related...

Impl. for matfaust the mul. of a real Faust by a complex scalar, add related unit test along with Faust.complex().

It should get rid of #18.
parent e597a186
Branches
Tags
No related merge requests found
...@@ -314,7 +314,7 @@ classdef FaustTest < matlab.unittest.TestCase ...@@ -314,7 +314,7 @@ classdef FaustTest < matlab.unittest.TestCase
ref_mat = ref_full_faust*cmat; ref_mat = ref_full_faust*cmat;
test_mat = F*cmat; test_mat = F*cmat;
this.verifyEqual(test_mat,ref_mat, 'RelTol', 10^-3) this.verifyEqual(test_mat,ref_mat, 'RelTol', 10^-3)
% mul by real scalar disp('test real mul by real scalar')
r = rand(); r = rand();
test_rF = full(F*r); test_rF = full(F*r);
test_commu_rF = full(r*F) test_commu_rF = full(r*F)
...@@ -325,7 +325,18 @@ classdef FaustTest < matlab.unittest.TestCase ...@@ -325,7 +325,18 @@ classdef FaustTest < matlab.unittest.TestCase
this.verifyNotEqual(test_commu_rF, ref_full_faust*(r+1)) this.verifyNotEqual(test_commu_rF, ref_full_faust*(r+1))
this.assertLessThan(norm(full(F'*r)-full(F)'*r)/norm(full(F)'*r), eps(1.)) this.assertLessThan(norm(full(F'*r)-full(F)'*r)/norm(full(F)'*r), eps(1.))
this.assertLessThan(norm(full(F.'*r)-full(F).'*r)/norm(full(F).'*r), eps(1.)) this.assertLessThan(norm(full(F.'*r)-full(F).'*r)/norm(full(F).'*r), eps(1.))
% TODO: mul by complex scalar (when impl.) disp('test mul by complex scalar')
r = rand()+j*rand();
test_rF = full(F*r);
test_commu_rF = full(r*F)
ref_rF = ref_full_faust*r;
this.verifyEqual(test_rF,ref_rF, 'RelTol', 10^-3);
this.verifyEqual(test_commu_rF,ref_rF, 'RelTol', 10^-3);
this.verifyNotEqual(test_rF, ref_full_faust*(r+1))
this.verifyNotEqual(test_commu_rF, ref_full_faust*(r+1))
this.assertLessThan(norm(full(F'*r)-full(F)'*r)/norm(full(F)'*r), eps(1.))
this.assertLessThan(norm(full(F.'*r)-full(F).'*r)/norm(full(F).'*r), eps(1.))
end end
function testcat(this) function testcat(this)
......
...@@ -340,7 +340,7 @@ classdef Faust ...@@ -340,7 +340,7 @@ classdef Faust
%>You cannot multiply a real Faust by a complex scalar (not yet implemented). %>You cannot multiply a real Faust by a complex scalar (not yet implemented).
%> @endcode %> @endcode
%> %>
%> <p>@b See @b also Faust.Faust, Faust.rcg, Faust.ctranspose. %> <p>@b See @b also Faust.Faust, Faust.rcg, Faust.ctranspose, Faust.complex
%> %>
%====================================================================== %======================================================================
function G = mtimes(F,A) function G = mtimes(F,A)
...@@ -351,7 +351,7 @@ classdef Faust ...@@ -351,7 +351,7 @@ classdef Faust
% %
% See also mtimes_trans % See also mtimes_trans
if(isa(A, 'matfaust.Faust') && isscalar(F)) if(isa(A, 'matfaust.Faust') && isscalar(F))
G = mtimes_trans(A,F, 0); G = mtimes_trans(A, F, 0);
else else
G = mtimes_trans(F, A, 0); G = mtimes_trans(F, A, 0);
end end
...@@ -394,6 +394,9 @@ classdef Faust ...@@ -394,6 +394,9 @@ classdef Faust
if (trans ~= 1) && (trans ~= 0) if (trans ~= 1) && (trans ~= 0)
error('invalid argument trans, must be equal to 0 or 1'); error('invalid argument trans, must be equal to 0 or 1');
end end
% TODO: take trans into account when mul F to a scal or a Faust
% it's not a serious issue because mtimes_trans() shouln't be called by final user
% (func's doc is filtered out in doxydoc)
if(issparse(A)) if(issparse(A))
error('Faust multiplication to a sparse matrix isn''t supported.') error('Faust multiplication to a sparse matrix isn''t supported.')
elseif(isa(A,'matfaust.Faust')) elseif(isa(A,'matfaust.Faust'))
...@@ -404,7 +407,12 @@ classdef Faust ...@@ -404,7 +407,12 @@ classdef Faust
end end
elseif(isscalar(A)) elseif(isscalar(A))
if (F.isReal) if (F.isReal)
C = matfaust.Faust(F, mexFaustReal('mul_scalar', F.matrix.objectHandle, A)); if(isreal(A))
C = matfaust.Faust(F, mexFaustReal('mul_scalar', F.matrix.objectHandle, A));
else
cF = complex(F)
C = mtimes_trans(cF, A, trans)
end
else else
C = matfaust.Faust(F, mexFaustCplx('mul_scalar', F.matrix.objectHandle, A)); C = matfaust.Faust(F, mexFaustCplx('mul_scalar', F.matrix.objectHandle, A));
end end
...@@ -487,6 +495,7 @@ classdef Faust ...@@ -487,6 +495,7 @@ classdef Faust
%> %>
%> @retval bool 1 if F is a real Faust, 0 if it's a complex Faust. %> @retval bool 1 if F is a real Faust, 0 if it's a complex Faust.
%> %>
%> <p/>@b See @b also Faust.complex
%====================================================================== %======================================================================
function bool = isreal(F) function bool = isreal(F)
%% ISREAL True for real scalar Faust (overloaded Matlab built-in function). %% ISREAL True for real scalar Faust (overloaded Matlab built-in function).
...@@ -567,7 +576,7 @@ classdef Faust ...@@ -567,7 +576,7 @@ classdef Faust
%> % F_ctrans == F_ctrans2 %> % F_ctrans == F_ctrans2
%> @endcode %> @endcode
%> %>
%> <p/>@b See @b also Faust.transpose, Faust.conj %> <p/>@b See @b also Faust.transpose, Faust.conj, Faust.complex
%> %>
%====================================================================== %======================================================================
function F_ctrans = ctranspose(F) function F_ctrans = ctranspose(F)
...@@ -605,7 +614,7 @@ classdef Faust ...@@ -605,7 +614,7 @@ classdef Faust
%> F_conj = conj(F) %> F_conj = conj(F)
%> @endcode %> @endcode
%> %>
%> <p/>@b See @b also Faust.transpose, Faust.ctranspose %> <p/>@b See @b also Faust.transpose, Faust.ctranspose, Faust.complex
%> %>
%====================================================================== %======================================================================
function F_conj = conj(F) function F_conj = conj(F)
...@@ -1633,6 +1642,37 @@ classdef Faust ...@@ -1633,6 +1642,37 @@ classdef Faust
X = pinv(full(F)) X = pinv(full(F))
end end
%================================================================
%> Converts F to a complex Faust.
%===
%> This function overloads a Matlab built-in function.
%>
%> @b Usage
%>
%> &nbsp;&nbsp;&nbsp; <b> cF = COMPLEX(F) </b> for real Faust F returns the complex result cF
%> with real part F and zero matrix as imaginary part of all cF's factors.
%>
%> <p> @b See @b also Faust.isreal, Faust.conj
%================================================================
function cF = complex(F)
if(~ isreal(F))
cF = F;
else
n = get_num_factors(F);
factors = cell(1,n);
for i=1:n
factors{i} = get_factor(F,i);
if(issparse(factors{i}))
% avoid complex() error: Input A must be numeric and full.
factors{i} = sparse(complex(full(factors{i})));
else
factors{i} = complex(factors{i});
end
end
cF = matfaust.Faust(factors);
end
end
end end
methods(Static) methods(Static)
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment