Home > matlab > vnorm.m

vnorm

PURPOSE ^

VNORM - Return the vector norm along specified dimension of A

SYNOPSIS ^

function y = vnorm(A,varargin)

DESCRIPTION ^

 VNORM - Return the vector norm along specified dimension of A

   VNORM(A) returns the 2-norm along the first non-singleton
   dimension of A
   VNORM(A,dim) return the 2-norm along the dimension 'dim'
   VNORM(A,dim,normtype) returns the norm specified by normtype
   along the dimension 'dim'
   VNORM(A,[],normtype) returns the norm specified by normtype along
   the first non-singleton dimension of A

   normtype may be one of {inf,-inf,positive integer}.
   For a given vector, v, these norms are defined as
   inf: max(abs(v))
   -inf: min(abs(v))
   p (where p is a positive integer): sum(abs(v).^p)^(1/p)

   Examples:
       A =  [8 1 6; 3 5 7; 4 -9 2];

       %Columnwise 2-norm (Euclidean norm)
       vnorm(A,1) = [9.4340 10.3441 9.4340];
       vnorm(A,[],2) % Same as above (since first non-singleton dimensions
                     % is columnwise and default norm is 2-norm.
       vnorm(A,[],[])% Again, same as above

       % Row-wise maximum of absolute values
       vnorm(A,2,inf) = [8 7 9]';

       % Columnwise minimum of absolute values
       vnorm(A,[],-inf) = [3 1 2];

       % Error: Use the inf type and not the string 'inf'
       vnorm(A,[],'inf')   % Wrong
       vnorm(A,[],inf)     % Correct


 Copyright (C) 2009 Dynare Team

 This file is part of Dynare.

 Dynare is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Dynare is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y = vnorm(A,varargin)
0002 % VNORM - Return the vector norm along specified dimension of A
0003 %
0004 %   VNORM(A) returns the 2-norm along the first non-singleton
0005 %   dimension of A
0006 %   VNORM(A,dim) return the 2-norm along the dimension 'dim'
0007 %   VNORM(A,dim,normtype) returns the norm specified by normtype
0008 %   along the dimension 'dim'
0009 %   VNORM(A,[],normtype) returns the norm specified by normtype along
0010 %   the first non-singleton dimension of A
0011 %
0012 %   normtype may be one of {inf,-inf,positive integer}.
0013 %   For a given vector, v, these norms are defined as
0014 %   inf: max(abs(v))
0015 %   -inf: min(abs(v))
0016 %   p (where p is a positive integer): sum(abs(v).^p)^(1/p)
0017 %
0018 %   Examples:
0019 %       A =  [8 1 6; 3 5 7; 4 -9 2];
0020 %
0021 %       %Columnwise 2-norm (Euclidean norm)
0022 %       vnorm(A,1) = [9.4340 10.3441 9.4340];
0023 %       vnorm(A,[],2) % Same as above (since first non-singleton dimensions
0024 %                     % is columnwise and default norm is 2-norm.
0025 %       vnorm(A,[],[])% Again, same as above
0026 %
0027 %       % Row-wise maximum of absolute values
0028 %       vnorm(A,2,inf) = [8 7 9]';
0029 %
0030 %       % Columnwise minimum of absolute values
0031 %       vnorm(A,[],-inf) = [3 1 2];
0032 %
0033 %       % Error: Use the inf type and not the string 'inf'
0034 %       vnorm(A,[],'inf')   % Wrong
0035 %       vnorm(A,[],inf)     % Correct
0036 %
0037 %
0038 % Copyright (C) 2009 Dynare Team
0039 %
0040 % This file is part of Dynare.
0041 %
0042 % Dynare is free software: you can redistribute it and/or modify
0043 % it under the terms of the GNU General Public License as published by
0044 % the Free Software Foundation, either version 3 of the License, or
0045 % (at your option) any later version.
0046 %
0047 % Dynare is distributed in the hope that it will be useful,
0048 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0049 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0050 % GNU General Public License for more details.
0051 %
0052 % You should have received a copy of the GNU General Public License
0053 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0054 dim = [];
0055 ntype = [];
0056 
0057 if nargin>1
0058     dim = varargin{1};
0059     if isempty(dim)
0060         idx = find(size(A)~=1);
0061         dim = idx(1);
0062     elseif dim~=floor(dim) || dim<1
0063         error('Dimension must be positive integer');
0064     end
0065     if nargin>2
0066         ntype = varargin{2};
0067     end
0068 end
0069 
0070 if isempty(dim)
0071     idx = find(size(A)~=1);
0072     dim = idx(1);
0073 end
0074     
0075 if isempty(ntype)
0076     y = sqrt(sum( abs(A).^2 , dim) );
0077 elseif ntype==1
0078     y = sum( abs(A) , dim );
0079 elseif isinf(ntype)
0080     if ntype > 0
0081         y=max(abs(A), [], dim);
0082     else
0083         y=min(abs(A), [], dim);
0084     end
0085 elseif ntype~=floor(ntype) || ntype<1
0086     error(['Norm type must be one of inf,-inf or a positive ' ...
0087            'integer']);
0088 else
0089     y = (sum( abs(A).^ntype , dim) ).^(1/ntype);
0090 end
0091

Generated on Wed 23-May-2012 02:40:54 by m2html © 2005