457
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
457
|
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 |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
|
28 #include <math.h> |
|
29 |
|
30 #include "dbleGEPBAL.h" |
|
31 #include "f77-uscore.h" |
|
32 |
|
33 extern "C" |
|
34 { |
1253
|
35 int F77_FCN (dgebak, DGEBAK) (const char*, const char*, const int&, |
|
36 const int&, const int&, double*, |
|
37 const int&, double*, const int&, int&, |
|
38 long, long); |
457
|
39 |
1253
|
40 int F77_FCN (reduce, REDUCE) (const int&, const int&, double*, |
|
41 const int&, double*, int&, int&, |
|
42 double*, double*); |
457
|
43 |
1253
|
44 int F77_FCN (scaleg, SCALEG) (const int&, const int&, double*, |
|
45 const int&, double*, const int&, |
|
46 const int&, double*, double*, |
|
47 double*); |
457
|
48 |
1253
|
49 int F77_FCN (gradeq, GRADEQ) (const int&, const int&, double*, |
|
50 const int&, double*, int&, int&, |
|
51 double*, double*); |
457
|
52 } |
|
53 |
|
54 int |
|
55 GEPBALANCE::init (const Matrix& a, const Matrix& b, const char *balance_job) |
|
56 { |
|
57 int a_nr = a.rows (); |
|
58 int a_nc = a.cols (); |
|
59 int b_nr = b.rows (); |
|
60 if (a_nr != a_nc || a_nr != b_nr || b_nr != b.cols ()) |
|
61 { |
|
62 (*current_liboctave_error_handler) |
|
63 ("GEPBALANCE requires square matrices of the same size"); |
|
64 return -1; |
|
65 } |
|
66 |
|
67 int n = a_nc; |
|
68 |
|
69 // Parameters for balance call. |
|
70 |
|
71 int info; |
|
72 int ilo; |
|
73 int ihi; |
|
74 double *cscale = new double [n]; |
|
75 double *cperm = new double [n]; |
|
76 Matrix wk (n, 6, 0.0); |
|
77 |
|
78 // Back out the permutations: |
|
79 // |
|
80 // cscale contains the exponents of the column scaling factors in its |
|
81 // ilo through ihi locations and the reducing column permutations in |
|
82 // its first ilo-1 and its ihi+1 through n locations. |
|
83 // |
|
84 // cperm contains the column permutations applied in grading the a and b |
|
85 // submatrices in its ilo through ihi locations. |
|
86 // |
|
87 // wk contains the exponents of the row scaling factors in its ilo |
|
88 // through ihi locations, the reducing row permutations in its first |
|
89 // ilo-1 and its ihi+1 through n locations, and the row permutations |
|
90 // applied in grading the a and b submatrices in its n+ilo through |
|
91 // n+ihi locations. |
|
92 |
|
93 // Copy matrices into local structure. |
|
94 |
|
95 balanced_a_mat = a; |
|
96 balanced_b_mat = b; |
|
97 |
|
98 // Initialize balancing matrices to identity. |
|
99 |
|
100 left_balancing_mat = Matrix (n, n, 0.0); |
|
101 for (int i = 0; i < n; i++) |
|
102 left_balancing_mat (i, i) = 1.0; |
|
103 |
|
104 right_balancing_mat = left_balancing_mat; |
|
105 |
|
106 // Check for permutation option. |
|
107 |
|
108 if (*balance_job == 'P' || *balance_job == 'B') |
|
109 { |
1253
|
110 F77_FCN (reduce, REDUCE) (n, n, balanced_a_mat.fortran_vec (), |
|
111 n, balanced_b_mat.fortran_vec (), ilo, |
|
112 ihi, cscale, wk.fortran_vec ()); |
457
|
113 } |
|
114 else |
|
115 { |
|
116 |
|
117 // Set up for scaling later. |
|
118 |
|
119 ilo = 1; |
|
120 ihi = n; |
|
121 } |
|
122 |
|
123 // Check for scaling option. |
|
124 |
|
125 if ((*balance_job == 'S' || *balance_job == 'B') && ilo != ihi) |
|
126 { |
1253
|
127 F77_FCN (scaleg, SCALEG) (n, n, balanced_a_mat.fortran_vec (), |
|
128 n, balanced_b_mat.fortran_vec (), ilo, |
|
129 ihi, cscale, cperm, wk.fortran_vec ()); |
457
|
130 } |
|
131 else |
|
132 { |
|
133 |
|
134 // Set scaling data to 0's. |
|
135 |
|
136 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
137 { |
|
138 cscale[tmp] = 0.0; |
|
139 wk.elem (tmp, 0) = 0.0; |
|
140 } |
|
141 } |
|
142 |
|
143 // Scaleg returns exponents, not values, so... |
|
144 |
|
145 for (int tmp = ilo-1; tmp < ihi; tmp++) |
|
146 { |
|
147 cscale[tmp] = pow (2.0, cscale[tmp]); |
|
148 wk.elem (tmp, 0) = pow (2.0, -wk.elem (tmp, 0)); |
|
149 } |
|
150 |
|
151 // Column permutations/scaling. |
|
152 |
1253
|
153 F77_FCN (dgebak, DGEBAK) (balance_job, "R", n, ilo, ihi, cscale, n, |
|
154 right_balancing_mat.fortran_vec (), n, |
|
155 info, 1L, 1L); |
457
|
156 |
|
157 // Row permutations/scaling. |
|
158 |
1253
|
159 F77_FCN (dgebak, DGEBAK) (balance_job, "L", n, ilo, ihi, |
|
160 wk.fortran_vec (), n, |
|
161 left_balancing_mat.fortran_vec (), n, |
|
162 info, 1L, 1L); |
457
|
163 |
|
164 // XXX FIXME XXX --- these four lines need to be added and debugged. |
|
165 // GEPBALANCE::init will work without them, though, so here they are. |
|
166 |
|
167 #if 0 |
|
168 if ((*balance_job == 'P' || *balance_job == 'B') && ilo != ihi) |
|
169 { |
1253
|
170 F77_FCN (gradeq, GRADEQ) (n, n, balanced_a_mat.fortran_vec (), |
|
171 n, balanced_b_mat.fortran_vec (), ilo, |
|
172 ihi, cperm, wk.fortran_vec ()); |
457
|
173 } |
|
174 #endif |
|
175 |
|
176 // Transpose for aa = cc*a*dd convention... |
|
177 left_balancing_mat = left_balancing_mat.transpose (); |
|
178 |
|
179 delete [] cscale; |
|
180 delete [] cperm; |
|
181 |
|
182 return info; |
|
183 } |
|
184 |
|
185 /* |
|
186 ;;; Local Variables: *** |
|
187 ;;; mode: C++ *** |
|
188 ;;; page-delimiter: "^/\\*" *** |
|
189 ;;; End: *** |
|
190 */ |