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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "CmplxLU.h" |
|
28 #include "dbleLU.h" |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "utils.h" |
|
35 |
|
36 DEFUN_DLD (lu, args, nargout, |
3548
|
37 "-*- texinfo -*-\n\ |
3372
|
38 @deftypefn {Loadable Function} {[@var{l}, @var{u}, @var{p}] =} lu (@var{a})\n\ |
|
39 @cindex LU decomposition\n\ |
|
40 Compute the LU decomposition of @var{a}, using subroutines from\n\ |
|
41 @sc{Lapack}. The result is returned in a permuted form, according to\n\ |
|
42 the optional return value @var{p}. For example, given the matrix\n\ |
|
43 @code{a = [1, 2; 3, 4]},\n\ |
|
44 \n\ |
|
45 @example\n\ |
|
46 [l, u, p] = lu (a)\n\ |
|
47 @end example\n\ |
|
48 \n\ |
|
49 @noindent\n\ |
|
50 returns\n\ |
|
51 \n\ |
|
52 @example\n\ |
|
53 l =\n\ |
|
54 \n\ |
|
55 1.00000 0.00000\n\ |
|
56 0.33333 1.00000\n\ |
|
57 \n\ |
|
58 u =\n\ |
|
59 \n\ |
|
60 3.00000 4.00000\n\ |
|
61 0.00000 0.66667\n\ |
|
62 \n\ |
|
63 p =\n\ |
|
64 \n\ |
|
65 0 1\n\ |
|
66 1 0\n\ |
|
67 @end example\n\ |
|
68 @end deftypefn") |
2928
|
69 { |
|
70 octave_value_list retval; |
|
71 |
|
72 int nargin = args.length (); |
|
73 |
|
74 if (nargin != 1 || nargout > 3) |
|
75 { |
|
76 print_usage ("lu"); |
|
77 return retval; |
|
78 } |
|
79 |
|
80 octave_value arg = args(0); |
|
81 |
|
82 int nr = arg.rows (); |
|
83 int nc = arg.columns (); |
|
84 |
|
85 int arg_is_empty = empty_arg ("lu", nr, nc); |
|
86 |
|
87 if (arg_is_empty < 0) |
|
88 return retval; |
|
89 else if (arg_is_empty > 0) |
|
90 return octave_value_list (3, Matrix ()); |
|
91 |
|
92 if (nr != nc) |
|
93 { |
|
94 gripe_square_matrix_required ("lu"); |
|
95 return retval; |
|
96 } |
|
97 |
|
98 if (arg.is_real_type ()) |
|
99 { |
|
100 Matrix m = arg.matrix_value (); |
|
101 |
|
102 if (! error_state) |
|
103 { |
|
104 LU fact (m); |
|
105 |
|
106 switch (nargout) |
|
107 { |
|
108 case 0: |
|
109 case 1: |
|
110 case 2: |
|
111 { |
|
112 Matrix P = fact.P (); |
|
113 Matrix L = P.transpose () * fact.L (); |
|
114 retval(1) = fact.U (); |
|
115 retval(0) = L; |
|
116 } |
|
117 break; |
|
118 |
|
119 case 3: |
|
120 default: |
|
121 retval(2) = fact.P (); |
|
122 retval(1) = fact.U (); |
|
123 retval(0) = fact.L (); |
|
124 break; |
|
125 } |
|
126 } |
|
127 } |
|
128 else if (arg.is_complex_type ()) |
|
129 { |
|
130 ComplexMatrix m = arg.complex_matrix_value (); |
|
131 |
|
132 if (! error_state) |
|
133 { |
|
134 ComplexLU fact (m); |
|
135 |
|
136 switch (nargout) |
|
137 { |
|
138 case 0: |
|
139 case 1: |
|
140 case 2: |
|
141 { |
3587
|
142 Matrix P = fact.P (); |
2928
|
143 ComplexMatrix L = P.transpose () * fact.L (); |
|
144 retval(1) = fact.U (); |
|
145 retval(0) = L; |
|
146 } |
|
147 break; |
|
148 |
|
149 case 3: |
|
150 default: |
|
151 retval(2) = fact.P (); |
|
152 retval(1) = fact.U (); |
|
153 retval(0) = fact.L (); |
|
154 break; |
|
155 } |
|
156 } |
|
157 } |
|
158 else |
|
159 { |
|
160 gripe_wrong_type_arg ("lu", arg); |
|
161 } |
|
162 |
|
163 return retval; |
|
164 } |
|
165 |
|
166 /* |
|
167 ;;; Local Variables: *** |
|
168 ;;; mode: C++ *** |
|
169 ;;; End: *** |
|
170 */ |