7
|
1 // f-lu.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
453
|
28 #include "dbleLU.h" |
|
29 #include "CmplxLU.h" |
1
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
636
|
34 #include "utils.h" |
544
|
35 #include "help.h" |
519
|
36 #include "defun-dld.h" |
1
|
37 |
701
|
38 DEFUN_DLD_BUILTIN ("lu", Flu, Slu, 2, 3, |
519
|
39 "[L, U, P] = lu (A): LU factorization") |
1
|
40 { |
519
|
41 Octave_object retval; |
|
42 |
712
|
43 int nargin = args.length (); |
|
44 |
|
45 if (nargin != 1 || nargout > 3) |
519
|
46 { |
|
47 print_usage ("lu"); |
|
48 return retval; |
|
49 } |
1
|
50 |
712
|
51 tree_constant arg = args(0); |
636
|
52 |
|
53 int nr = arg.rows (); |
|
54 int nc = arg.columns (); |
|
55 |
718
|
56 int arg_is_empty = empty_arg ("lu", nr, nc); |
|
57 |
|
58 if (arg_is_empty < 0) |
636
|
59 return retval; |
718
|
60 else if (arg_is_empty > 0) |
|
61 return Octave_object (3, Matrix ()); |
636
|
62 |
|
63 if (nr != nc) |
1
|
64 { |
636
|
65 gripe_square_matrix_required ("lu"); |
|
66 return retval; |
1
|
67 } |
|
68 |
636
|
69 if (arg.is_real_type ()) |
1
|
70 { |
636
|
71 Matrix m = arg.matrix_value (); |
|
72 |
|
73 if (! error_state) |
620
|
74 { |
|
75 LU fact (m); |
636
|
76 |
620
|
77 switch (nargout) |
|
78 { |
|
79 case 0: |
|
80 case 1: |
|
81 case 2: |
1
|
82 { |
620
|
83 Matrix P = fact.P (); |
|
84 Matrix L = P.transpose () * fact.L (); |
516
|
85 retval(1) = fact.U (); |
620
|
86 retval(0) = L; |
1
|
87 } |
620
|
88 break; |
636
|
89 |
620
|
90 case 3: |
|
91 default: |
|
92 retval(2) = fact.P (); |
|
93 retval(1) = fact.U (); |
|
94 retval(0) = fact.L (); |
|
95 break; |
|
96 } |
|
97 } |
1
|
98 } |
785
|
99 else if (arg.is_complex_type ()) |
620
|
100 { |
636
|
101 ComplexMatrix m = arg.complex_matrix_value (); |
|
102 |
|
103 if (! error_state) |
620
|
104 { |
|
105 ComplexLU fact (m); |
636
|
106 |
620
|
107 switch (nargout) |
|
108 { |
|
109 case 0: |
|
110 case 1: |
|
111 case 2: |
|
112 { |
|
113 ComplexMatrix P = fact.P (); |
|
114 ComplexMatrix L = P.transpose () * fact.L (); |
|
115 retval(1) = fact.U (); |
|
116 retval(0) = L; |
|
117 } |
|
118 break; |
636
|
119 |
620
|
120 case 3: |
|
121 default: |
|
122 retval(2) = fact.P (); |
|
123 retval(1) = fact.U (); |
|
124 retval(0) = fact.L (); |
|
125 break; |
|
126 } |
|
127 } |
|
128 } |
|
129 else |
|
130 { |
636
|
131 gripe_wrong_type_arg ("lu", arg); |
620
|
132 } |
|
133 |
1
|
134 return retval; |
|
135 } |
|
136 |
|
137 /* |
|
138 ;;; Local Variables: *** |
|
139 ;;; mode: C++ *** |
|
140 ;;; page-delimiter: "^/\\*" *** |
|
141 ;;; End: *** |
|
142 */ |