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