3
|
1 // LinConst.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 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 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
3
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
238
|
29 |
3
|
30 #include "LinConst.h" |
227
|
31 #include "lo-error.h" |
3
|
32 |
|
33 // error handling |
|
34 |
|
35 void |
|
36 LinConst::error (const char* msg) |
|
37 { |
227
|
38 (*current_liboctave_error_handler) ("fatal LinConst error: %s", msg); |
3
|
39 } |
|
40 |
|
41 LinConst::LinConst (const Matrix& a_eq, const Vector& b_eq, |
|
42 const Matrix& a_ineq, const Vector& b_ineq) |
|
43 { |
|
44 // Need some checks here. |
|
45 |
|
46 int nc_eq = b_eq.capacity (); |
|
47 int nc_ineq = b_ineq.capacity (); |
|
48 nb = nc_eq + nc_ineq; |
|
49 |
|
50 lb.resize (nb); |
|
51 ub.resize (nb); |
|
52 |
|
53 lb.insert (b_eq, 0); |
|
54 lb.insert (-b_ineq, nc_eq); |
|
55 |
|
56 ub.insert (b_eq, 0); |
|
57 ub.fill (DBL_MAX, nc_eq, nb-1); |
|
58 |
|
59 int nx = a_eq.columns (); |
|
60 |
|
61 A.resize (nb, nx); |
|
62 |
|
63 A.insert (a_eq, 0, 0); |
|
64 A.insert (a_ineq, nc_eq, 0); |
|
65 } |
|
66 |
|
67 LinConst& |
|
68 LinConst::resize (int nc, int n) |
|
69 { |
|
70 nb = nc; |
|
71 lb.resize (nb); |
|
72 A.resize (nb, n); |
|
73 ub.resize (nb); |
|
74 |
|
75 return *this; |
|
76 } |
|
77 |
|
78 Matrix |
|
79 LinConst::eq_constraint_matrix (void) const |
|
80 { |
|
81 int anr = A.rows (); |
|
82 int anc = A.columns (); |
|
83 Matrix retval (anr, anc); |
|
84 int count = 0; |
|
85 for (int i = 0; i < anr; i++) |
|
86 { |
|
87 if (lb.elem (i) == ub.elem (i)) |
|
88 { |
|
89 retval.insert (A.extract (i, 0, i, anc-1), count, 0); |
|
90 count++; |
|
91 } |
|
92 } |
|
93 retval.resize (count, anc); |
|
94 return retval; |
|
95 } |
|
96 |
|
97 Matrix |
|
98 LinConst::ineq_constraint_matrix (void) const |
|
99 { |
|
100 int anr = A.rows (); |
|
101 int anc = A.columns (); |
|
102 Matrix retval (2*anr, anc); |
|
103 int count = 0; |
|
104 for (int i = 0; i < anr; i++) |
|
105 { |
|
106 if (lb.elem (i) != ub.elem (i)) |
|
107 { |
|
108 Matrix tmp = A.extract (i, 0, i, anc-1); |
|
109 retval.insert (tmp, count, 0); |
|
110 count++; |
|
111 if (ub.elem (i) < DBL_MAX) |
|
112 { |
|
113 retval.insert (-tmp, count, 0); |
|
114 count++; |
|
115 } |
|
116 } |
|
117 } |
|
118 retval.resize (count, anc); |
|
119 return retval; |
|
120 } |
|
121 |
|
122 Vector |
|
123 LinConst::eq_constraint_vector (void) const |
|
124 { |
|
125 Vector retval (nb); |
|
126 int count = 0; |
|
127 for (int i = 0; i < nb; i++) |
|
128 { |
|
129 if (lb.elem (i) == ub.elem (i)) |
|
130 { |
|
131 retval.elem (count) = lb.elem (i); |
|
132 count++; |
|
133 } |
|
134 } |
|
135 retval.resize (count); |
|
136 return retval; |
|
137 } |
|
138 |
|
139 Vector |
|
140 LinConst::ineq_constraint_vector (void) const |
|
141 { |
|
142 Vector retval (2*nb); |
|
143 int count = 0; |
|
144 for (int i = 0; i < nb; i++) |
|
145 { |
|
146 if (lb.elem (i) != ub.elem (i)) |
|
147 { |
|
148 retval.elem (count) = -lb.elem (i); |
|
149 count++; |
|
150 if (ub.elem (i) < DBL_MAX) |
|
151 { |
|
152 retval.elem (count) = ub.elem (i); |
|
153 count++; |
|
154 } |
|
155 } |
|
156 } |
|
157 retval.resize (count); |
|
158 return retval; |
|
159 } |
|
160 |
|
161 ostream& |
|
162 operator << (ostream& os, const LinConst& c) |
|
163 { |
|
164 for (int i = 0; i < c.size (); i++) |
|
165 os << c.lower_bound (i) << " " << c.upper_bound (i) << "\n"; |
|
166 |
|
167 os << "\n"; |
|
168 os << c.constraint_matrix (); |
|
169 |
|
170 return os; |
|
171 } |
|
172 |
|
173 /* |
|
174 ;;; Local Variables: *** |
|
175 ;;; mode: C++ *** |
|
176 ;;; page-delimiter: "^/\\*" *** |
|
177 ;;; End: *** |
|
178 */ |