Mercurial > octave
view scripts/linear-algebra/condeig.m @ 28942:fc4bb4bd1d5e
maint: Use '##' as lead-in for full-line comments.
* condeig.m, mustBeMember.m, runge_kutta_interpolate.m, lsqnonneg.m,
pqpnonneg.m, sqp.m, pkg.m, fplot.m, isosurface.m, __gnuplot_draw_axes__.m,
profexport.m, bicgstab.m, factorial.m, movmad.m, movmax.m, movmean.m,
movmedian.m, movmin.m, movprod.m, movstd.m, movsum.m, movvar.m, fA.m, fB.m,
fC.m, one.m, two.m, df_vr.m, u_vr.m, myclass2.m, foo_subsref_subsasgn.m,
Blork.m, CPrecedenceTester1.m, Cork.m, display.m, display.m, get.m, set.m,
display.m, Sneetch.m, Snork.m, Spork.m, jit.tst, line-continue.tst,
mk_bc_overloads_expected.m, arg_nest.m, no_closure.m, persistent_nest.m,
recursive_nest.m, recursive_nest2.m, scope0.m, scope1.m, scope2.m, scope3.m,
script_nest.m, script_nest_2.m, script_nest_script.m, script_nest_script_2.m,
pkg.tst:
Use '##' as lead-in for full-line comments.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 15 Oct 2020 14:30:01 -0700 |
parents | 90fea9cc9caa |
children | 7854d5752dd2 |
line wrap: on
line source
######################################################################## ## ## Copyright (C) 2006-2020 The Octave Project Developers ## ## See the file COPYRIGHT.md in the top-level directory of this ## distribution or <https://octave.org/copyright/>. ## ## 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 3 of the License, 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, see ## <https://www.gnu.org/licenses/>. ## ######################################################################## ## -*- texinfo -*- ## @deftypefn {} {@var{c} =} condeig (@var{a}) ## @deftypefnx {} {[@var{v}, @var{lambda}, @var{c}] =} condeig (@var{a}) ## Compute condition numbers of a matrix with respect to eigenvalues. ## ## The condition numbers are the reciprocals of the cosines of the angles ## between the left and right eigenvectors; Large values indicate that the ## matrix has multiple distinct eigenvalues. ## ## The input @var{a} must be a square numeric matrix. ## ## The outputs are: ## ## @itemize @bullet ## @item ## @var{c} is a vector of condition numbers for the eigenvalues of ## @var{a}. ## ## @item ## @var{v} is the matrix of right eigenvectors of @var{a}. The result is ## equivalent to calling @code{[@var{v}, @var{lambda}] = eig (@var{a})}. ## ## @item ## @var{lambda} is the diagonal matrix of eigenvalues of @var{a}. The ## result is equivalent to calling ## @code{[@var{v}, @var{lambda}] = eig (@var{a})}. ## @end itemize ## ## Example ## ## @example ## @group ## a = [1, 2; 3, 4]; ## c = condeig (a) ## @result{} c = ## 1.0150 ## 1.0150 ## @end group ## @end example ## @seealso{eig, cond, balance} ## @end deftypefn function [v, lambda, c] = condeig (a) if (nargin < 1) print_usage (); endif if (! (isnumeric (a) && issquare (a))) error ("condeig: A must be a square numeric matrix"); endif if (issparse (a) && nargout <= 1) ## Try to use svds to calculate the condition number as it will typically ## be much faster than calling eig as only the smallest and largest ## eigenvalue are calculated. ## FIXME: This calculates one condition number for the entire matrix. ## In the full case, separate condition numbers are calculated for every ## eigenvalue. try s0 = svds (a, 1, 0); # min eigenvalue v = svds (a, 1) / s0; # max eigenvalue catch ## Caught an error as there is a singular value exactly at zero!! v = Inf; end_try_catch return; endif ## Right eigenvectors [v, lambda] = eig (a); if (isempty (a)) c = []; else ## Corresponding left eigenvectors ## Use 2-argument form to suppress possible singular matrix warning. [vl, ~] = inv (v); vl = vl'; ## Normalize vectors vl ./= repmat (sqrt (sum (abs (vl .^ 2))), rows (vl), 1); ## Condition numbers ## Definition: cos (angle) = (norm (v1) * norm (v2)) / dot (v1, v2) ## Eigenvectors have been normalized so 'norm (v1) * norm (v2)' = 1 c = abs (1 ./ dot (vl, v)'); endif if (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); ## Test empty input %!assert (condeig ([]), []) ## Test input validation %!error <Invalid call> condeig () %!error <A must be a square numeric matrix> condeig ({1}) %!error <A must be a square numeric matrix> condeig (ones (3,2)) %!error <A must be a square numeric matrix> condeig (ones (2,2,2))