1 | //
|
---|
2 | // blockedvect.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #include <math.h>
|
---|
29 |
|
---|
30 | #include <util/misc/formio.h>
|
---|
31 | #include <util/keyval/keyval.h>
|
---|
32 | #include <util/state/stateio.h>
|
---|
33 | #include <math/scmat/blocked.h>
|
---|
34 | #include <math/scmat/cmatrix.h>
|
---|
35 | #include <math/scmat/elemop.h>
|
---|
36 |
|
---|
37 | using namespace std;
|
---|
38 | using namespace sc;
|
---|
39 |
|
---|
40 | /////////////////////////////////////////////////////////////////////////////
|
---|
41 | // BlockedSCVector member functions
|
---|
42 |
|
---|
43 | static ClassDesc BlockedSCVector_cd(
|
---|
44 | typeid(BlockedSCVector),"BlockedSCVector",1,"public SCVector",
|
---|
45 | 0, 0, 0);
|
---|
46 |
|
---|
47 | void
|
---|
48 | BlockedSCVector::resize(SCDimension *bsd)
|
---|
49 | {
|
---|
50 | if (vecs_) {
|
---|
51 | delete[] vecs_;
|
---|
52 | vecs_=0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | d = bsd;
|
---|
56 |
|
---|
57 | if (!bsd || !bsd->blocks()->nblock())
|
---|
58 | return;
|
---|
59 |
|
---|
60 | vecs_ = new RefSCVector[d->blocks()->nblock()];
|
---|
61 |
|
---|
62 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
63 | if (d->blocks()->size(i))
|
---|
64 | vecs_[i] = subkit->vector(d->blocks()->subdim(i));
|
---|
65 | }
|
---|
66 |
|
---|
67 | BlockedSCVector::BlockedSCVector(const RefSCDimension&a,
|
---|
68 | BlockedSCMatrixKit*k):
|
---|
69 | SCVector(a,k),
|
---|
70 | subkit(k->subkit()),
|
---|
71 | vecs_(0)
|
---|
72 | {
|
---|
73 | resize(a);
|
---|
74 | }
|
---|
75 |
|
---|
76 | BlockedSCVector::~BlockedSCVector()
|
---|
77 | {
|
---|
78 | if (vecs_) {
|
---|
79 | delete[] vecs_;
|
---|
80 | vecs_=0;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | void
|
---|
85 | BlockedSCVector::assign_val(double a)
|
---|
86 | {
|
---|
87 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
88 | if (vecs_[i].nonnull())
|
---|
89 | vecs_[i]->assign(a);
|
---|
90 | }
|
---|
91 |
|
---|
92 | void
|
---|
93 | BlockedSCVector::assign_v(SCVector*a)
|
---|
94 | {
|
---|
95 | // make sure that the argument is of the correct type
|
---|
96 | BlockedSCVector* la
|
---|
97 | = require_dynamic_cast<BlockedSCVector*>(a,"BlockedSCVector::assign");
|
---|
98 |
|
---|
99 | // make sure that the dimensions match
|
---|
100 | if (!dim()->equiv(la->dim())) {
|
---|
101 | ExEnv::errn() << indent << "BlockedSCVector::assign(SCVector*a): "
|
---|
102 | << "dimensions don't match\n";
|
---|
103 | abort();
|
---|
104 | }
|
---|
105 |
|
---|
106 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
107 | if (vecs_[i].nonnull())
|
---|
108 | vecs_[i]->assign(la->vecs_[i]);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void
|
---|
112 | BlockedSCVector::assign_p(const double*a)
|
---|
113 | {
|
---|
114 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
115 | if (vecs_[i].nonnull())
|
---|
116 | vecs_[i]->assign(a+d->blocks()->start(i));
|
---|
117 | }
|
---|
118 |
|
---|
119 | double
|
---|
120 | BlockedSCVector::get_element(int i) const
|
---|
121 | {
|
---|
122 | int size = d->n();
|
---|
123 | if (i < 0 || i >= size) {
|
---|
124 | ExEnv::errn() << indent << "BlockedSCVector::get_element: bad index\n";
|
---|
125 | abort();
|
---|
126 | }
|
---|
127 |
|
---|
128 | int bi, bo;
|
---|
129 | d->blocks()->elem_to_block(i,bi,bo);
|
---|
130 | return vecs_[bi].get_element(bo);
|
---|
131 | }
|
---|
132 |
|
---|
133 | void
|
---|
134 | BlockedSCVector::set_element(int i,double a)
|
---|
135 | {
|
---|
136 | int size = d->n();
|
---|
137 | if (i < 0 || i >= size) {
|
---|
138 | ExEnv::errn() << indent << "BlockedSCVector::set_element: bad index\n";
|
---|
139 | abort();
|
---|
140 | }
|
---|
141 |
|
---|
142 | int bi, bo;
|
---|
143 | d->blocks()->elem_to_block(i,bi,bo);
|
---|
144 | vecs_[bi].set_element(bo,a);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void
|
---|
148 | BlockedSCVector::accumulate_element(int i,double a)
|
---|
149 | {
|
---|
150 | int size = d->n();
|
---|
151 | if (i < 0 || i >= size) {
|
---|
152 | ExEnv::errn() << indent << "BlockedSCVector::accumulate_element: bad index\n";
|
---|
153 | abort();
|
---|
154 | }
|
---|
155 |
|
---|
156 | int bi, bo;
|
---|
157 | d->blocks()->elem_to_block(i,bi,bo);
|
---|
158 | vecs_[bi].accumulate_element(bo,a);
|
---|
159 | }
|
---|
160 |
|
---|
161 | void
|
---|
162 | BlockedSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b)
|
---|
163 | {
|
---|
164 | const char* name = "BlockedSCVector::accumulate_product";
|
---|
165 | // make sure that the arguments are of the correct type
|
---|
166 | BlockedSCMatrix* la = require_dynamic_cast<BlockedSCMatrix*>(a,name);
|
---|
167 | BlockedSCVector* lb = require_dynamic_cast<BlockedSCVector*>(b,name);
|
---|
168 |
|
---|
169 | // make sure that the dimensions match
|
---|
170 | if (!dim()->equiv(la->rowdim()) || !la->coldim()->equiv(lb->dim())) {
|
---|
171 | ExEnv::errn() << indent
|
---|
172 | << "BlockedSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b): "
|
---|
173 | << "dimensions don't match\n";
|
---|
174 | abort();
|
---|
175 | }
|
---|
176 |
|
---|
177 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
178 | if (vecs_[i].nonnull())
|
---|
179 | vecs_[i]->accumulate_product(la->mats_[i], lb->vecs_[i]);
|
---|
180 | }
|
---|
181 |
|
---|
182 | void
|
---|
183 | BlockedSCVector::accumulate_product_sv(SymmSCMatrix*a,SCVector*b)
|
---|
184 | {
|
---|
185 | const char* name = "BlockedSCVector::accumulate_product";
|
---|
186 | // make sure that the arguments are of the correct type
|
---|
187 | BlockedSymmSCMatrix* la = require_dynamic_cast<BlockedSymmSCMatrix*>(a,name);
|
---|
188 | BlockedSCVector* lb = require_dynamic_cast<BlockedSCVector*>(b,name);
|
---|
189 |
|
---|
190 | // make sure that the dimensions match
|
---|
191 | if (!dim()->equiv(la->dim()) || !la->dim()->equiv(lb->dim())) {
|
---|
192 | ExEnv::errn() << indent
|
---|
193 | << "BlockedSCVector::accumulate_product_sv(SymmSCMatrix*a,SCVector*b): "
|
---|
194 | << "dimensions don't match\n";
|
---|
195 | abort();
|
---|
196 | }
|
---|
197 |
|
---|
198 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
199 | if (vecs_[i].nonnull())
|
---|
200 | vecs_[i]->accumulate_product(la->mats_[i], lb->vecs_[i]);
|
---|
201 | }
|
---|
202 |
|
---|
203 | void
|
---|
204 | BlockedSCVector::accumulate(const SCVector*a)
|
---|
205 | {
|
---|
206 | // make sure that the argument is of the correct type
|
---|
207 | const BlockedSCVector* la
|
---|
208 | = require_dynamic_cast<const BlockedSCVector*>(a,"BlockedSCVector::accumulate");
|
---|
209 |
|
---|
210 | // make sure that the dimensions match
|
---|
211 | if (!dim()->equiv(la->dim())) {
|
---|
212 | ExEnv::errn() << indent << "BlockedSCVector::accumulate(SCVector*a): "
|
---|
213 | << "dimensions don't match\n";
|
---|
214 | abort();
|
---|
215 | }
|
---|
216 |
|
---|
217 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
218 | if (vecs_[i].nonnull())
|
---|
219 | vecs_[i]->accumulate(la->vecs_[i]);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void
|
---|
223 | BlockedSCVector::accumulate(const SCMatrix*a)
|
---|
224 | {
|
---|
225 | // make sure that the argument is of the correct type
|
---|
226 | const BlockedSCMatrix* la
|
---|
227 | = require_dynamic_cast<const BlockedSCMatrix*>(a,"BlockedSCVector::accumulate");
|
---|
228 |
|
---|
229 | // make sure that the dimensions match
|
---|
230 | if (!((la->rowdim()->equiv(dim()) && la->coldim()->n() == 1)
|
---|
231 | || (la->coldim()->equiv(dim()) && la->rowdim()->n() == 1))) {
|
---|
232 | ExEnv::errn() << indent << "BlockedSCVector::accumulate(SCMatrix*a): "
|
---|
233 | << "dimensions don't match\n";
|
---|
234 | abort();
|
---|
235 | }
|
---|
236 |
|
---|
237 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
238 | if (vecs_[i].nonnull())
|
---|
239 | vecs_[i]->accumulate(la->mats_[i]);
|
---|
240 | }
|
---|
241 |
|
---|
242 | double
|
---|
243 | BlockedSCVector::scalar_product(SCVector*a)
|
---|
244 | {
|
---|
245 | // make sure that the argument is of the correct type
|
---|
246 | BlockedSCVector* la
|
---|
247 | = require_dynamic_cast<BlockedSCVector*>(a,"BlockedSCVector::scalar_product");
|
---|
248 |
|
---|
249 | // make sure that the dimensions match
|
---|
250 | if (!dim()->equiv(la->dim())) {
|
---|
251 | ExEnv::errn() << indent << "BlockedSCVector::scale_product(SCVector*a): "
|
---|
252 | << "dimensions don't match\n";
|
---|
253 | abort();
|
---|
254 | }
|
---|
255 |
|
---|
256 | double result=0;
|
---|
257 |
|
---|
258 | for (int i=0; i < d->blocks()->nblock(); i++)
|
---|
259 | if (vecs_[i].nonnull())
|
---|
260 | result += vecs_[i]->scalar_product(la->vecs_[i]);
|
---|
261 |
|
---|
262 | return result;
|
---|
263 | }
|
---|
264 |
|
---|
265 | void
|
---|
266 | BlockedSCVector::element_op(const Ref<SCElementOp>& op)
|
---|
267 | {
|
---|
268 | BlockedSCElementOp *bop = dynamic_cast<BlockedSCElementOp*>(op.pointer());
|
---|
269 |
|
---|
270 | int nb = d->blocks()->nblock();
|
---|
271 |
|
---|
272 | op->defer_collect(1);
|
---|
273 | for (int i=0; i < nb; i++) {
|
---|
274 | if (bop)
|
---|
275 | bop->working_on(i);
|
---|
276 | if (vecs_[i].nonnull())
|
---|
277 | vecs_[i]->element_op(op);
|
---|
278 | }
|
---|
279 | op->defer_collect(0);
|
---|
280 | if (op->has_collect()) op->collect(messagegrp());
|
---|
281 | }
|
---|
282 |
|
---|
283 | void
|
---|
284 | BlockedSCVector::element_op(const Ref<SCElementOp2>& op,
|
---|
285 | SCVector* m)
|
---|
286 | {
|
---|
287 | BlockedSCVector *lm
|
---|
288 | = require_dynamic_cast<BlockedSCVector*>(m, "BlockedSCVector::element_op");
|
---|
289 |
|
---|
290 | if (!dim()->equiv(lm->dim())) {
|
---|
291 | ExEnv::errn() << indent << "BlockedSCVector: bad element_op\n";
|
---|
292 | abort();
|
---|
293 | }
|
---|
294 |
|
---|
295 | BlockedSCElementOp2 *bop = dynamic_cast<BlockedSCElementOp2*>(op.pointer());
|
---|
296 |
|
---|
297 | int nb = d->blocks()->nblock();
|
---|
298 |
|
---|
299 | op->defer_collect(1);
|
---|
300 | for (int i=0; i < nb; i++) {
|
---|
301 | if (bop)
|
---|
302 | bop->working_on(i);
|
---|
303 | if (vecs_[i].nonnull())
|
---|
304 | vecs_[i]->element_op(op, lm->vecs_[i]);
|
---|
305 | }
|
---|
306 | op->defer_collect(0);
|
---|
307 | if (op->has_collect()) op->collect(messagegrp());
|
---|
308 | }
|
---|
309 |
|
---|
310 | void
|
---|
311 | BlockedSCVector::element_op(const Ref<SCElementOp3>& op,
|
---|
312 | SCVector* m,SCVector* n)
|
---|
313 | {
|
---|
314 | BlockedSCVector *lm
|
---|
315 | = require_dynamic_cast<BlockedSCVector*>(m, "BlockedSCVector::element_op");
|
---|
316 | BlockedSCVector *ln
|
---|
317 | = require_dynamic_cast<BlockedSCVector*>(n, "BlockedSCVector::element_op");
|
---|
318 |
|
---|
319 | if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
|
---|
320 | ExEnv::errn() << indent << "BlockedSCVector: bad element_op\n";
|
---|
321 | abort();
|
---|
322 | }
|
---|
323 |
|
---|
324 | BlockedSCElementOp3 *bop = dynamic_cast<BlockedSCElementOp3*>(op.pointer());
|
---|
325 |
|
---|
326 | int nb = d->blocks()->nblock();
|
---|
327 |
|
---|
328 | op->defer_collect(1);
|
---|
329 | for (int i=0; i < nb; i++) {
|
---|
330 | if (bop)
|
---|
331 | bop->working_on(i);
|
---|
332 | if (vecs_[i].nonnull())
|
---|
333 | vecs_[i]->element_op(op, lm->vecs_[i], ln->vecs_[i]);
|
---|
334 | }
|
---|
335 | op->defer_collect(0);
|
---|
336 | if (op->has_collect()) op->collect(messagegrp());
|
---|
337 | }
|
---|
338 |
|
---|
339 | void
|
---|
340 | BlockedSCVector::vprint(const char *title, ostream& os, int prec) const
|
---|
341 | {
|
---|
342 | int len = (title) ? strlen(title) : 0;
|
---|
343 | char *newtitle = new char[len + 80];
|
---|
344 |
|
---|
345 | for (int i=0; i < d->blocks()->nblock(); i++) {
|
---|
346 | if (vecs_[i].null())
|
---|
347 | continue;
|
---|
348 |
|
---|
349 | sprintf(newtitle,"%s: block %d",title,i+1);
|
---|
350 | vecs_[i]->print(newtitle, os, prec);
|
---|
351 | }
|
---|
352 |
|
---|
353 | delete[] newtitle;
|
---|
354 | }
|
---|
355 |
|
---|
356 | RefSCDimension
|
---|
357 | BlockedSCVector::dim(int i) const
|
---|
358 | {
|
---|
359 | return d->blocks()->subdim(i);
|
---|
360 | }
|
---|
361 |
|
---|
362 | int
|
---|
363 | BlockedSCVector::nblocks() const
|
---|
364 | {
|
---|
365 | return d->blocks()->nblock();
|
---|
366 | }
|
---|
367 |
|
---|
368 | RefSCVector
|
---|
369 | BlockedSCVector::block(int i)
|
---|
370 | {
|
---|
371 | return vecs_[i];
|
---|
372 | }
|
---|
373 |
|
---|
374 | Ref<SCMatrixSubblockIter>
|
---|
375 | BlockedSCVector::local_blocks(SCMatrixSubblockIter::Access access)
|
---|
376 | {
|
---|
377 | Ref<SCMatrixCompositeSubblockIter> iter
|
---|
378 | = new SCMatrixCompositeSubblockIter(access,nblocks());
|
---|
379 | for (int i=0; i<nblocks(); i++) {
|
---|
380 | if (block(i).null())
|
---|
381 | iter->set_iter(i, new SCMatrixNullSubblockIter(access));
|
---|
382 | else
|
---|
383 | iter->set_iter(i, block(i)->local_blocks(access));
|
---|
384 | }
|
---|
385 | Ref<SCMatrixSubblockIter> ret = iter.pointer();
|
---|
386 | return ret;
|
---|
387 | }
|
---|
388 |
|
---|
389 | Ref<SCMatrixSubblockIter>
|
---|
390 | BlockedSCVector::all_blocks(SCMatrixSubblockIter::Access access)
|
---|
391 | {
|
---|
392 | Ref<SCMatrixCompositeSubblockIter> iter
|
---|
393 | = new SCMatrixCompositeSubblockIter(access,nblocks());
|
---|
394 | for (int i=0; i<nblocks(); i++) {
|
---|
395 | if (block(i).null())
|
---|
396 | iter->set_iter(i, new SCMatrixNullSubblockIter(access));
|
---|
397 | else
|
---|
398 | iter->set_iter(i, block(i)->all_blocks(access));
|
---|
399 | }
|
---|
400 | Ref<SCMatrixSubblockIter> ret = iter.pointer();
|
---|
401 | return ret;
|
---|
402 | }
|
---|
403 |
|
---|
404 | void
|
---|
405 | BlockedSCVector::save(StateOut&s)
|
---|
406 | {
|
---|
407 | int ndim = n();
|
---|
408 | s.put(ndim);
|
---|
409 | int has_subblocks = 1;
|
---|
410 | s.put(has_subblocks);
|
---|
411 | s.put(nblocks());
|
---|
412 | for (int i=0; i<nblocks(); i++) {
|
---|
413 | block(i).save(s);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | void
|
---|
418 | BlockedSCVector::restore(StateIn&s)
|
---|
419 | {
|
---|
420 | int ndimt, ndim = n();
|
---|
421 | s.get(ndimt);
|
---|
422 | if (ndimt != ndim) {
|
---|
423 | ExEnv::errn() << indent << "BlockedSCVector::restore(): bad dimension" << endl;
|
---|
424 | abort();
|
---|
425 | }
|
---|
426 | int has_subblocks;
|
---|
427 | s.get(has_subblocks);
|
---|
428 | if (has_subblocks) {
|
---|
429 | int nblock;
|
---|
430 | s.get(nblock);
|
---|
431 | if (nblock != nblocks()) {
|
---|
432 | ExEnv::errn() << indent
|
---|
433 | << "BlockedSCVector::restore(): nblock differs\n" << endl;
|
---|
434 | abort();
|
---|
435 | }
|
---|
436 | for (int i=0; i<nblocks(); i++) {
|
---|
437 | block(i).restore(s);
|
---|
438 | }
|
---|
439 | }
|
---|
440 | else {
|
---|
441 | ExEnv::errn() << indent
|
---|
442 | << "BlockedSCVector::restore(): no subblocks--cannot restore"
|
---|
443 | << endl;
|
---|
444 | abort();
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | /////////////////////////////////////////////////////////////////////////////
|
---|
449 |
|
---|
450 | // Local Variables:
|
---|
451 | // mode: c++
|
---|
452 | // c-file-style: "CLJ"
|
---|
453 | // End:
|
---|