1
|
1 // tc-lu.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "Matrix.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "user-prefs.h" |
|
32 #include "gripes.h" |
|
33 |
|
34 #ifdef WITH_DLD |
|
35 tree_constant * |
|
36 builtin_lu_2 (tree_constant *args, int nargin, int nargout) |
|
37 { |
|
38 return lu (args[1], nargout); |
|
39 } |
|
40 #endif |
|
41 |
|
42 tree_constant * |
|
43 lu (tree_constant& a, int nargout) |
|
44 { |
|
45 tree_constant *retval = new tree_constant [4]; |
|
46 |
|
47 tree_constant tmp = a.make_numeric ();; |
|
48 |
|
49 if (tmp.rows () == 0 || tmp.columns () == 0) |
|
50 { |
|
51 int flag = user_pref.propagate_empty_matrices; |
|
52 if (flag != 0) |
|
53 { |
|
54 if (flag < 0) |
|
55 gripe_empty_arg ("lu", 0); |
|
56 Matrix m; |
|
57 retval = new tree_constant [4]; |
|
58 retval[0] = tree_constant (m); |
|
59 retval[1] = tree_constant (m); |
|
60 retval[2] = tree_constant (m); |
|
61 return retval; |
|
62 } |
|
63 else |
|
64 gripe_empty_arg ("lu", 1); |
|
65 } |
|
66 |
|
67 switch (tmp.const_type ()) |
|
68 { |
|
69 case tree_constant_rep::matrix_constant: |
|
70 { |
|
71 Matrix m = tmp.matrix_value (); |
|
72 if (m.rows () == m.columns ()) |
|
73 { |
|
74 LU fact (m); |
|
75 switch (nargout) |
|
76 { |
|
77 case 1: |
|
78 case 2: |
|
79 { |
|
80 Matrix P = fact.P (); |
|
81 Matrix L = P.transpose () * fact.L (); |
|
82 retval[0] = tree_constant (L); |
|
83 retval[1] = tree_constant (fact.U ()); |
|
84 } |
|
85 break; |
|
86 case 3: |
|
87 default: |
|
88 retval[0] = tree_constant (fact.L ()); |
|
89 retval[1] = tree_constant (fact.U ()); |
|
90 retval[2] = tree_constant (fact.P ()); |
|
91 break; |
|
92 } |
|
93 } |
|
94 else |
|
95 gripe_square_matrix_required ("lu"); |
|
96 } |
|
97 break; |
|
98 case tree_constant_rep::complex_matrix_constant: |
|
99 { |
|
100 ComplexMatrix m = tmp.complex_matrix_value (); |
|
101 if (m.rows () == m.columns ()) |
|
102 { |
|
103 ComplexLU fact (m); |
|
104 switch (nargout) |
|
105 { |
|
106 case 1: |
|
107 case 2: |
|
108 { |
|
109 ComplexMatrix P = fact.P (); |
|
110 ComplexMatrix L = P.transpose () * fact.L (); |
|
111 retval[0] = tree_constant (L); |
|
112 retval[1] = tree_constant (fact.U ()); |
|
113 } |
|
114 break; |
|
115 case 3: |
|
116 default: |
|
117 retval[0] = tree_constant (fact.L ()); |
|
118 retval[1] = tree_constant (fact.U ()); |
|
119 retval[2] = tree_constant (fact.P ()); |
|
120 break; |
|
121 } |
|
122 } |
|
123 else |
|
124 gripe_square_matrix_required ("lu"); |
|
125 } |
|
126 break; |
|
127 case tree_constant_rep::scalar_constant: |
|
128 { |
|
129 double d = tmp.double_value (); |
|
130 retval[0] = tree_constant (1.0); |
|
131 retval[1] = tree_constant (d); |
|
132 retval[2] = tree_constant (1.0); |
|
133 } |
|
134 break; |
|
135 case tree_constant_rep::complex_scalar_constant: |
|
136 { |
|
137 Complex c = tmp.complex_value (); |
|
138 retval[0] = tree_constant (1.0); |
|
139 retval[1] = tree_constant (c); |
|
140 retval[2] = tree_constant (1.0); |
|
141 } |
|
142 break; |
|
143 default: |
|
144 break; |
|
145 } |
|
146 return retval; |
|
147 } |
|
148 |
|
149 /* |
|
150 ;;; Local Variables: *** |
|
151 ;;; mode: C++ *** |
|
152 ;;; page-delimiter: "^/\\*" *** |
|
153 ;;; End: *** |
|
154 */ |