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