condeig: Condition of eigenvalues
Arno Onken <[email protected]> Sun, 15 Oct 2006 18:22:04 +0200
| Newsgroups | gmane.comp.gnu.octave.sources |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------000801080001020405090807
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hi,
I implemented the condeig function for computing the condition numbers
for eigenvalues (reciprocals of the cosines of the angles between the
left and right eigenvectors).
Regards,
Arno
--------------000801080001020405090807
Content-Type: text/x-objcsrc;
name="condeig.m"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="condeig.m"
## Copyright (C) 2006 Arno Onken
##
## This file is part of Octave.
##
## Octave 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 2, or (at your option)
## any later version.
##
## Octave 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 Octave; see the file COPYING. If not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
## 02110-1301, USA.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{c} =} condeig (@var{a})
## @deftypefnx {Function File} {[@var{v}, @var{lambda}, @var{c}] =} condeig (@var{a})
## Computes condition numbers for the eigenvalues of a matrix. The
## condition numbers are the reciprocals of the cosines of the angles
## between the left and right eigenvectors.
##
## Arguments are
##
## @itemize @bullet
## @item
## @var{a} must be a square numeric matrix.
## @end itemize
##
## Return values are
##
## @itemize @bullet
## @item
## @var{c} is a vector of condition numbers for the eigenvalue of
## @var{a}.
##
## @item
## @var{v} is the matrix of right eigenvectors of @var{a}. The result is
## the same as for @code{[v, lambda] = eig (a)}.
##
## @item
## @var{lambda} is the diagonal matrix of eigenvalues of @var{a}. The
## result is the same as for @code{[v, lambda] = eig (a)}.
## @end itemize
##
## Example:
##
## @example
## @group
## a = [1, 2; 3, 4];
## c = condeig (a)
## @result{} [1.0150; 1.0150]
## @end group
## @end example
## @end deftypefn
## Author: Arno Onken <[email protected]>
## Description: Condition numbers for eigenvalues
function [v, lambda, c] = condeig (a)
# Check arguments
if (nargin != 1 || nargout > 3)
usage ("[v, lambda, c] = condeig (a)");
endif
if (! isempty (a) && ! ismatrix (a))
error ("condeig: a must be a numeric matrix");
endif
if (columns (a) != rows (a))
error ("condeig: a must be a square matrix");
endif
# Right eigenvectors
[v, lambda] = eig (a);
if (isempty (a))
c = lambda;
else
# Corresponding left eigenvectors
vl = inv (v)';
# Normalize vectors
vl = vl ./ repmat (sqrt (sum (abs (vl .^ 2))), rows (vl), 1);
# Condition numbers
# cos (angle) = (norm (v1) * norm (v2)) / dot (v1, v2)
# Norm of the eigenvectors is 1 => norm (v1) * norm (v2) = 1
c = abs (1 ./ dot (vl, v)');
endif
if (nargout == 0 || nargout == 1)
v = c;
endif
endfunction
%!test
%! a = [1, 2; 3, 4];
%! c = condeig (a);
%! expected_c = [1.0150; 1.0150];
%! assert (c, expected_c, 0.001);
%!test
%! a = [1, 3; 5, 8];
%! [v, lambda, c] = condeig (a);
%! [expected_v, expected_lambda] = eig (a);
%! expected_c = [1.0182; 1.0182];
%! assert (v, expected_v, 0.001);
%! assert (lambda, expected_lambda, 0.001);
%! assert (c, expected_c, 0.001);
--------------000801080001020405090807
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Octave-sources mailing list
[email protected]
https://www.cae.wisc.edu/mailman/listinfo/octave-sources
--------------000801080001020405090807--