49 DEFUN (balance, args, nargout,
51 @deftypefn {Built-in Function} {@var{AA} =} balance (@var{A})\n\
52 @deftypefnx {Built-in Function} {@var{AA} =} balance (@var{A}, @var{opt})\n\
53 @deftypefnx {Built-in Function} {[@var{DD}, @var{AA}] =} balance (@var{A}, @var{opt})\n\
54 @deftypefnx {Built-in Function} {[@var{D}, @var{P}, @var{AA}] =} balance (@var{A}, @var{opt})\n\
55 @deftypefnx {Built-in Function} {[@var{CC}, @var{DD}, @var{AA}, @var{BB}] =} balance (@var{A}, @var{B}, @var{opt})\n\
57 Compute @code{@var{AA} = @var{DD} \\ @var{A} * @var{DD}} in which @var{AA}\n\
58 is a matrix whose row and column norms are roughly equal in magnitude, and\n\
59 @code{@var{DD} = @var{P} * @var{D}}, in which @var{P} is a permutation\n\
60 matrix and @var{D} is a diagonal matrix of powers of two. This allows the\n\
61 equilibration to be computed without round-off. Results of eigenvalue\n\
62 calculation are typically improved by balancing first.\n\
64 If two output values are requested, @code{balance} returns\n\
65 the diagonal @var{D} and the permutation @var{P} separately as vectors.\n\
66 In this case, @code{@var{DD} = eye(n)(:,@var{P}) * diag (@var{D})}, where\n\
67 @math{n} is the matrix size.\n\
69 If four output values are requested, compute @code{@var{AA} =\n\
70 @var{CC}*@var{A}*@var{DD}} and @code{@var{BB} = @var{CC}*@var{B}*@var{DD}},\n\
71 in which @var{AA} and @var{BB} have non-zero elements of approximately the\n\
72 same magnitude and @var{CC} and @var{DD} are permuted diagonal matrices as\n\
73 in @var{DD} for the algebraic eigenvalue problem.\n\
75 The eigenvalue balancing option @var{opt} may be one of:\n\
78 @item @qcode{\"noperm\"}, @qcode{\"S\"}\n\
79 Scale only; do not permute.\n\
81 @item @qcode{\"noscal\"}, @qcode{\"P\"}\n\
82 Permute only; do not scale.\n\
85 Algebraic eigenvalue balancing uses standard @sc{lapack} routines.\n\
87 Generalized eigenvalue problem balancing uses Ward's algorithm\n\
88 (SIAM Journal on Scientific and Statistical Computing, 1981).\n\
93 int nargin = args.
length ();
95 if (nargin < 1 || nargin > 3 || nargout < 0 || nargout > 4)
102 bool AEPcase = nargin == 1 || args(1).is_string ();
107 if (nn != args(0).columns ())
113 bool isfloat = args(0).is_single_type ()
114 || (! AEPcase && args(1).is_single_type ());
116 bool complex_case = args(0).is_complex_type ()
117 || (! AEPcase && args(1).is_complex_type ());
128 fcaa = args(0).float_complex_matrix_value ();
130 faa = args(0).float_matrix_value ();
135 caa = args(0).complex_matrix_value ();
137 aa = args(0).matrix_value ();
147 bool noperm =
false, noscal =
false;
150 std::string a1s = args(1).string_value ();
151 noperm = a1s ==
"noperm" || a1s ==
"S";
152 noscal = a1s ==
"noscal" || a1s ==
"P";
162 if (nargout == 0 || nargout == 1)
164 else if (nargout == 2)
181 if (nargout == 0 || nargout == 1)
183 else if (nargout == 2)
202 if (nargout == 0 || nargout == 1)
204 else if (nargout == 2)
220 if (nargout == 0 || nargout == 1)
222 else if (nargout == 2)
240 warning (
"balance: used GEP, should have two output arguments");
245 else if (args(2).is_string ())
246 bal_job = args(2).string_value ();
249 error (
"balance: OPT argument must be a string");
253 if ((nn != args(1).columns ()) || (nn != args(1).rows ()))
267 fcbb = args(1).float_complex_matrix_value ();
269 fbb = args(1).float_matrix_value ();
274 cbb = args(1).complex_matrix_value ();
276 bb = args(1).matrix_value ();
303 error (
"balance: invalid number of output arguments");
328 error (
"balance: invalid number of output arguments");
356 error (
"balance: invalid number of output arguments");
381 error (
"balance: invalid number of output arguments");