2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2928
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "CmplxLU.h" |
|
29 #include "dbleLU.h" |
|
30 |
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
|
33 #include "gripes.h" |
|
34 #include "oct-obj.h" |
|
35 #include "utils.h" |
|
36 |
|
37 DEFUN_DLD (lu, args, nargout, |
3548
|
38 "-*- texinfo -*-\n\ |
3372
|
39 @deftypefn {Loadable Function} {[@var{l}, @var{u}, @var{p}] =} lu (@var{a})\n\ |
|
40 @cindex LU decomposition\n\ |
|
41 Compute the LU decomposition of @var{a}, using subroutines from\n\ |
|
42 @sc{Lapack}. The result is returned in a permuted form, according to\n\ |
|
43 the optional return value @var{p}. For example, given the matrix\n\ |
|
44 @code{a = [1, 2; 3, 4]},\n\ |
|
45 \n\ |
|
46 @example\n\ |
|
47 [l, u, p] = lu (a)\n\ |
|
48 @end example\n\ |
|
49 \n\ |
|
50 @noindent\n\ |
|
51 returns\n\ |
|
52 \n\ |
|
53 @example\n\ |
|
54 l =\n\ |
|
55 \n\ |
|
56 1.00000 0.00000\n\ |
|
57 0.33333 1.00000\n\ |
|
58 \n\ |
|
59 u =\n\ |
|
60 \n\ |
|
61 3.00000 4.00000\n\ |
|
62 0.00000 0.66667\n\ |
|
63 \n\ |
|
64 p =\n\ |
|
65 \n\ |
|
66 0 1\n\ |
|
67 1 0\n\ |
|
68 @end example\n\ |
4329
|
69 \n\ |
5457
|
70 The matrix is not required to be square.\n\ |
3372
|
71 @end deftypefn") |
2928
|
72 { |
|
73 octave_value_list retval; |
|
74 |
|
75 int nargin = args.length (); |
|
76 |
|
77 if (nargin != 1 || nargout > 3) |
|
78 { |
|
79 print_usage ("lu"); |
|
80 return retval; |
|
81 } |
|
82 |
|
83 octave_value arg = args(0); |
|
84 |
5275
|
85 octave_idx_type nr = arg.rows (); |
|
86 octave_idx_type nc = arg.columns (); |
2928
|
87 |
|
88 int arg_is_empty = empty_arg ("lu", nr, nc); |
|
89 |
|
90 if (arg_is_empty < 0) |
|
91 return retval; |
|
92 else if (arg_is_empty > 0) |
|
93 return octave_value_list (3, Matrix ()); |
|
94 |
|
95 if (arg.is_real_type ()) |
|
96 { |
|
97 Matrix m = arg.matrix_value (); |
|
98 |
|
99 if (! error_state) |
|
100 { |
|
101 LU fact (m); |
|
102 |
|
103 switch (nargout) |
|
104 { |
|
105 case 0: |
|
106 case 1: |
|
107 case 2: |
|
108 { |
|
109 Matrix P = fact.P (); |
|
110 Matrix L = P.transpose () * fact.L (); |
|
111 retval(1) = fact.U (); |
|
112 retval(0) = L; |
|
113 } |
|
114 break; |
|
115 |
|
116 case 3: |
|
117 default: |
|
118 retval(2) = fact.P (); |
|
119 retval(1) = fact.U (); |
|
120 retval(0) = fact.L (); |
|
121 break; |
|
122 } |
|
123 } |
|
124 } |
|
125 else if (arg.is_complex_type ()) |
|
126 { |
|
127 ComplexMatrix m = arg.complex_matrix_value (); |
|
128 |
|
129 if (! error_state) |
|
130 { |
|
131 ComplexLU fact (m); |
|
132 |
|
133 switch (nargout) |
|
134 { |
|
135 case 0: |
|
136 case 1: |
|
137 case 2: |
|
138 { |
3587
|
139 Matrix P = fact.P (); |
2928
|
140 ComplexMatrix L = P.transpose () * fact.L (); |
|
141 retval(1) = fact.U (); |
|
142 retval(0) = L; |
|
143 } |
|
144 break; |
|
145 |
|
146 case 3: |
|
147 default: |
|
148 retval(2) = fact.P (); |
|
149 retval(1) = fact.U (); |
|
150 retval(0) = fact.L (); |
|
151 break; |
|
152 } |
|
153 } |
|
154 } |
|
155 else |
|
156 { |
|
157 gripe_wrong_type_arg ("lu", arg); |
|
158 } |
|
159 |
|
160 return retval; |
|
161 } |
|
162 |
|
163 /* |
|
164 ;;; Local Variables: *** |
|
165 ;;; mode: C++ *** |
|
166 ;;; End: *** |
|
167 */ |