From: <jpi...@us...> - 2011-10-21 15:23:18
|
Revision: 8811 http://octave.svn.sourceforge.net/octave/?rev=8811&view=rev Author: jpicarbajal Date: 2011-10-21 15:23:11 +0000 (Fri, 21 Oct 2011) Log Message: ----------- general. Updating unvech. All 3 tests pased with octave running with --norc Modified Paths: -------------- trunk/octave-forge/main/general/inst/unvech.m Modified: trunk/octave-forge/main/general/inst/unvech.m =================================================================== --- trunk/octave-forge/main/general/inst/unvech.m 2011-10-21 15:18:07 UTC (rev 8810) +++ trunk/octave-forge/main/general/inst/unvech.m 2011-10-21 15:23:11 UTC (rev 8811) @@ -15,7 +15,7 @@ ## ## You should have received a copy of the GNU General Public License ## along with this program; If not, see <http://www.gnu.org/licenses/>. - + ## -*- texinfo -*- ## @deftypefn {Function File} {@var{m} =} unvech (@var{v}, @var{scale}) ## Performs the reverse of @code{vech} on the vector @var{v}. @@ -27,31 +27,44 @@ ## that 1 and -1 can be used for symmetric and antisymmetric matrix respectively. ## @var{scale} must be a scalar and defaults to 1. ## -## @seealso{vech, ind2sub, sub2ind_tril} +## @seealso{vech, ind2sub} ## @end deftypefn - + function M = unvech (v, scale = 1) - + if ( nargin < 1 || nargin > 2 ) print_usage; elseif ( !ismatrix (v) && any (size (v) != 1) ) - error ("V must be a row orcolumn matrix") + error ("V must be a row or column matrix") elseif ( !isnumeric (scale) || !isscalar (scale) ) error ("SCALE must be a scalar") endif - + N = length (v); dim = (sqrt ( 1 + 8*N ) - 1)/2; [r, c] = ind2sub_tril (dim, 1:N); M = accumarray ([r; c].', v); M += scale * tril (M, -1).'; - + endfunction -%!assert(unvech([1;0;0;1;0;1]), full(eye(3,3)) ); +function [r c] = ind2sub_tril(N,idx) -#%!error <does not generate a square matrix> unvech([1;0;0;1;0;1;1]); + %% Horrible lengthly check + if nargin < 2 ||( !isnumeric (N) && all(size(N)==1) )|| ... + !( ismatrix (idx) && any (size (idx)==1) ) + print_usage; + endif + + endofrow = 0.5*(1:N) .* (2*N:-1:N + 1); + c = lookup(endofrow, idx-1)+1; + r = N - endofrow(c) + idx ; + +end + +%!assert(unvech([1;0;0;1;0;1]), full(eye(3,3)) ); + %!test %symmetric %! dim = 10; %! A = tril( floor ( 5*(2*rand(dim)-1) ) ); @@ -59,7 +72,7 @@ %! M = vech(A); %! M = unvech(M, 1); %! assert (A, M); - + %!test %antisymmetric %! dim = 10; %! A = tril( floor ( 5*(2*rand(dim)-1) ) ); @@ -67,3 +80,4 @@ %! M = vech(A); %! M = unvech(M, -1); %! assert (A, M); + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |