457
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 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 HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include "dbleLU.h" |
|
33 #include "mx-inlines.cc" |
|
34 #include "lo-error.h" |
|
35 #include "f77-uscore.h" |
|
36 |
|
37 extern "C" |
|
38 { |
|
39 int F77_FCN (dgesv) (const int*, const int*, double*, const int*, |
|
40 int*, double*, const int*, int*); |
|
41 } |
|
42 |
|
43 LU::LU (const Matrix& a) |
|
44 { |
|
45 int a_nr = a.rows (); |
|
46 int a_nc = a.cols (); |
|
47 if (a_nr == 0 || a_nc == 0 || a_nr != a_nc) |
|
48 { |
|
49 (*current_liboctave_error_handler) ("LU requires square matrix"); |
|
50 return; |
|
51 } |
|
52 |
|
53 int n = a_nr; |
|
54 |
|
55 int *ipvt = new int [n]; |
|
56 int *pvt = new int [n]; |
|
57 double *tmp_data = dup (a.data (), a.length ()); |
|
58 int info = 0; |
|
59 int zero = 0; |
|
60 double b; |
|
61 |
|
62 F77_FCN (dgesv) (&n, &zero, tmp_data, &n, ipvt, &b, &n, &info); |
|
63 |
|
64 Matrix A_fact (tmp_data, n, n); |
|
65 |
|
66 int i; |
|
67 |
|
68 for (i = 0; i < n; i++) |
|
69 { |
|
70 ipvt[i] -= 1; |
|
71 pvt[i] = i; |
|
72 } |
|
73 |
|
74 for (i = 0; i < n - 1; i++) |
|
75 { |
|
76 int k = ipvt[i]; |
|
77 if (k != i) |
|
78 { |
|
79 int tmp = pvt[k]; |
|
80 pvt[k] = pvt[i]; |
|
81 pvt[i] = tmp; |
|
82 } |
|
83 } |
|
84 |
|
85 l.resize (n, n, 0.0); |
|
86 u.resize (n, n, 0.0); |
|
87 p.resize (n, n, 0.0); |
|
88 |
|
89 for (i = 0; i < n; i++) |
|
90 { |
|
91 p.elem (i, pvt[i]) = 1.0; |
|
92 |
|
93 int j; |
|
94 |
|
95 l.elem (i, i) = 1.0; |
|
96 for (j = 0; j < i; j++) |
|
97 l.elem (i, j) = A_fact.elem (i, j); |
|
98 |
|
99 for (j = i; j < n; j++) |
|
100 u.elem (i, j) = A_fact.elem (i, j); |
|
101 } |
|
102 |
|
103 delete [] ipvt; |
|
104 delete [] pvt; |
|
105 } |
|
106 |
|
107 /* |
|
108 ;;; Local Variables: *** |
|
109 ;;; mode: C++ *** |
|
110 ;;; page-delimiter: "^/\\*" *** |
|
111 ;;; End: *** |
|
112 */ |