| 1 | // | 
|---|
| 2 | // blockedrect.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 | #include <math/scmat/local.h> | 
|---|
| 38 | #include <math/scmat/repl.h> | 
|---|
| 39 |  | 
|---|
| 40 | using namespace std; | 
|---|
| 41 | using namespace sc; | 
|---|
| 42 |  | 
|---|
| 43 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 44 | // BlockedSCMatrix member functions | 
|---|
| 45 |  | 
|---|
| 46 | static ClassDesc BlockedSCMatrix_cd( | 
|---|
| 47 | typeid(BlockedSCMatrix),"BlockedSCMatrix",1,"public SCMatrix", | 
|---|
| 48 | 0, 0, 0); | 
|---|
| 49 |  | 
|---|
| 50 | void | 
|---|
| 51 | BlockedSCMatrix::resize(SCDimension *a, SCDimension *b) | 
|---|
| 52 | { | 
|---|
| 53 | if (mats_) { | 
|---|
| 54 | delete[] mats_; | 
|---|
| 55 | mats_=0; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | d1 = a; | 
|---|
| 59 | d2 = b; | 
|---|
| 60 |  | 
|---|
| 61 | if (!a || !b || !a->blocks()->nblock() || !b->blocks()->nblock()) | 
|---|
| 62 | return; | 
|---|
| 63 |  | 
|---|
| 64 | if (a->blocks()->nblock() > 1 && b->blocks()->nblock() == 1) { | 
|---|
| 65 | nblocks_ = d1->blocks()->nblock(); | 
|---|
| 66 | mats_ = new RefSCMatrix[d1->blocks()->nblock()]; | 
|---|
| 67 | for (int i=0; i < d1->blocks()->nblock(); i++) | 
|---|
| 68 | if (d1->blocks()->size(i) && d2->blocks()->size(0)) | 
|---|
| 69 | mats_[i] = subkit->matrix(d1->blocks()->subdim(i), | 
|---|
| 70 | d2->blocks()->subdim(0)); | 
|---|
| 71 |  | 
|---|
| 72 | } else if (a->blocks()->nblock() == 1 && b->blocks()->nblock() > 1) { | 
|---|
| 73 | nblocks_ = d2->blocks()->nblock(); | 
|---|
| 74 | mats_ = new RefSCMatrix[d2->blocks()->nblock()]; | 
|---|
| 75 | for (int i=0; i < d2->blocks()->nblock(); i++) | 
|---|
| 76 | if (d2->blocks()->size(i) && d1->blocks()->size(0)) | 
|---|
| 77 | mats_[i] = subkit->matrix(d1->blocks()->subdim(0), | 
|---|
| 78 | d2->blocks()->subdim(i)); | 
|---|
| 79 |  | 
|---|
| 80 | } else if (a->blocks()->nblock() == b->blocks()->nblock()) { | 
|---|
| 81 | nblocks_ = d2->blocks()->nblock(); | 
|---|
| 82 | mats_ = new RefSCMatrix[d1->blocks()->nblock()]; | 
|---|
| 83 | for (int i=0; i < d1->blocks()->nblock(); i++) | 
|---|
| 84 | if (d2->blocks()->size(i) && d1->blocks()->size(i)) | 
|---|
| 85 | mats_[i] = subkit->matrix(d1->blocks()->subdim(i), | 
|---|
| 86 | d2->blocks()->subdim(i)); | 
|---|
| 87 |  | 
|---|
| 88 | } else { | 
|---|
| 89 | ExEnv::errn() << indent << "BlockedSCMatrix::resize: wrong number of blocks\n"; | 
|---|
| 90 | abort(); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | BlockedSCMatrix::BlockedSCMatrix(const RefSCDimension&a, | 
|---|
| 96 | const RefSCDimension&b, | 
|---|
| 97 | BlockedSCMatrixKit*k): | 
|---|
| 98 | SCMatrix(a,b,k), | 
|---|
| 99 | subkit(k->subkit()), | 
|---|
| 100 | mats_(0), nblocks_(0) | 
|---|
| 101 | { | 
|---|
| 102 | resize(a,b); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | BlockedSCMatrix::~BlockedSCMatrix() | 
|---|
| 106 | { | 
|---|
| 107 | if (mats_) { | 
|---|
| 108 | delete[] mats_; | 
|---|
| 109 | mats_=0; | 
|---|
| 110 | } | 
|---|
| 111 | nblocks_=0; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void | 
|---|
| 115 | BlockedSCMatrix::assign_val(double v) | 
|---|
| 116 | { | 
|---|
| 117 | for (int i=0; i < nblocks_; i++) | 
|---|
| 118 | if (mats_[i].nonnull()) | 
|---|
| 119 | mats_[i]->assign(v); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | double | 
|---|
| 123 | BlockedSCMatrix::get_element(int i,int j) const | 
|---|
| 124 | { | 
|---|
| 125 | int block_i, block_j; | 
|---|
| 126 | int elem_i, elem_j; | 
|---|
| 127 |  | 
|---|
| 128 | d1->blocks()->elem_to_block(i,block_i,elem_i); | 
|---|
| 129 | d2->blocks()->elem_to_block(j,block_j,elem_j); | 
|---|
| 130 |  | 
|---|
| 131 | if (d1->blocks()->nblock() == 1 && d2->blocks()->nblock() > 1) { | 
|---|
| 132 | return mats_[block_j]->get_element(elem_i,elem_j); | 
|---|
| 133 |  | 
|---|
| 134 | } else if (d1->blocks()->nblock() > 1 && d2->blocks()->nblock() == 1) { | 
|---|
| 135 | return mats_[block_i]->get_element(elem_i,elem_j); | 
|---|
| 136 | } else if (d1->blocks()->nblock() == d2->blocks()->nblock() | 
|---|
| 137 | && block_i == block_j) { | 
|---|
| 138 | return mats_[block_i]->get_element(elem_i,elem_j); | 
|---|
| 139 | } else { | 
|---|
| 140 | return 0; | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | void | 
|---|
| 145 | BlockedSCMatrix::set_element(int i,int j,double a) | 
|---|
| 146 | { | 
|---|
| 147 | int block_i, block_j; | 
|---|
| 148 | int elem_i, elem_j; | 
|---|
| 149 |  | 
|---|
| 150 | d1->blocks()->elem_to_block(i,block_i,elem_i); | 
|---|
| 151 | d2->blocks()->elem_to_block(j,block_j,elem_j); | 
|---|
| 152 |  | 
|---|
| 153 | if (d1->blocks()->nblock() == 1 && d2->blocks()->nblock() > 1) { | 
|---|
| 154 | mats_[block_j]->set_element(elem_i,elem_j,a); | 
|---|
| 155 |  | 
|---|
| 156 | } else if (d1->blocks()->nblock() > 1 && d2->blocks()->nblock() == 1) { | 
|---|
| 157 | mats_[block_i]->set_element(elem_i,elem_j,a); | 
|---|
| 158 | } else if (d1->blocks()->nblock() == d2->blocks()->nblock() | 
|---|
| 159 | && block_i == block_j) { | 
|---|
| 160 | mats_[block_i]->set_element(elem_i,elem_j,a); | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | void | 
|---|
| 165 | BlockedSCMatrix::accumulate_element(int i,int j,double a) | 
|---|
| 166 | { | 
|---|
| 167 | int block_i, block_j; | 
|---|
| 168 | int elem_i, elem_j; | 
|---|
| 169 |  | 
|---|
| 170 | d1->blocks()->elem_to_block(i,block_i,elem_i); | 
|---|
| 171 | d2->blocks()->elem_to_block(j,block_j,elem_j); | 
|---|
| 172 |  | 
|---|
| 173 | if (d1->blocks()->nblock() == 1 && d2->blocks()->nblock() > 1) { | 
|---|
| 174 | mats_[block_j]->accumulate_element(elem_i,elem_j,a); | 
|---|
| 175 |  | 
|---|
| 176 | } else if (d1->blocks()->nblock() > 1 && d2->blocks()->nblock() == 1) { | 
|---|
| 177 | mats_[block_i]->accumulate_element(elem_i,elem_j,a); | 
|---|
| 178 | } | 
|---|
| 179 | else if (d1->blocks()->nblock() == d2->blocks()->nblock() | 
|---|
| 180 | && block_i == block_j) { | 
|---|
| 181 | mats_[block_i]->accumulate_element(elem_i,elem_j,a); | 
|---|
| 182 | } | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | SCMatrix * | 
|---|
| 186 | BlockedSCMatrix::get_subblock(int br, int er, int bc, int ec) | 
|---|
| 187 | { | 
|---|
| 188 | ExEnv::errn() << indent << "BlockedSCMatrix::get_subblock: cannot get subblock\n"; | 
|---|
| 189 | abort(); | 
|---|
| 190 | return 0; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | void | 
|---|
| 194 | BlockedSCMatrix::assign_subblock(SCMatrix*sb, int br, int er, int bc, int ec, | 
|---|
| 195 | int source_br, int source_bc) | 
|---|
| 196 | { | 
|---|
| 197 | ExEnv::errn() << indent | 
|---|
| 198 | << "BlockedSCMatrix::assign_subblock: cannot assign subblock\n"; | 
|---|
| 199 | abort(); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | void | 
|---|
| 203 | BlockedSCMatrix::accumulate_subblock(SCMatrix*sb, | 
|---|
| 204 | int br, int er, int bc, int ec, | 
|---|
| 205 | int source_br, int source_bc) | 
|---|
| 206 | { | 
|---|
| 207 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate_subblock:" | 
|---|
| 208 | << " cannot accumulate subblock\n"; | 
|---|
| 209 | abort(); | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | SCVector * | 
|---|
| 213 | BlockedSCMatrix::get_row(int i) | 
|---|
| 214 | { | 
|---|
| 215 | ExEnv::errn() << indent << "BlockedSCMatrix::get_row: cannot get row\n"; | 
|---|
| 216 | abort(); | 
|---|
| 217 |  | 
|---|
| 218 | return 0; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void | 
|---|
| 222 | BlockedSCMatrix::assign_row(SCVector *v, int i) | 
|---|
| 223 | { | 
|---|
| 224 | ExEnv::errn() << indent << "BlockedSCMatrix::assign_row: cannot assign row\n"; | 
|---|
| 225 | abort(); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | void | 
|---|
| 229 | BlockedSCMatrix::accumulate_row(SCVector *v, int i) | 
|---|
| 230 | { | 
|---|
| 231 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate_row: cannot accumulate row\n"; | 
|---|
| 232 | abort(); | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | SCVector * | 
|---|
| 236 | BlockedSCMatrix::get_column(int i) | 
|---|
| 237 | { | 
|---|
| 238 | ExEnv::errn() << indent << "BlockedSCMatrix::get_column: cannot get column\n"; | 
|---|
| 239 | abort(); | 
|---|
| 240 |  | 
|---|
| 241 | return 0; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | void | 
|---|
| 245 | BlockedSCMatrix::assign_column(SCVector *v, int i) | 
|---|
| 246 | { | 
|---|
| 247 | ExEnv::errn() << indent << "BlockedSCMatrix::assign_column: cannot assign column\n"; | 
|---|
| 248 | abort(); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | void | 
|---|
| 252 | BlockedSCMatrix::accumulate_column(SCVector *v, int i) | 
|---|
| 253 | { | 
|---|
| 254 | ExEnv::errn() << indent | 
|---|
| 255 | << "BlockedSCMatrix::accumulate_column: cannot accumulate column\n"; | 
|---|
| 256 | abort(); | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | // does the outer product a x b.  this must have rowdim() == a->dim() and | 
|---|
| 260 | // coldim() == b->dim() | 
|---|
| 261 | void | 
|---|
| 262 | BlockedSCMatrix::accumulate_outer_product(SCVector*a,SCVector*b) | 
|---|
| 263 | { | 
|---|
| 264 | const char* name = "BlockedSCMatrix::accumulate_outer_product"; | 
|---|
| 265 | // make sure that the arguments are of the correct type | 
|---|
| 266 | BlockedSCVector* la = require_dynamic_cast<BlockedSCVector*>(a,name); | 
|---|
| 267 | BlockedSCVector* lb = require_dynamic_cast<BlockedSCVector*>(b,name); | 
|---|
| 268 |  | 
|---|
| 269 | // make sure that the dimensions match | 
|---|
| 270 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(lb->dim())) { | 
|---|
| 271 | ExEnv::errn() << indent | 
|---|
| 272 | << "BlockedSCMatrix::accumulate_outer_product(SCVector*,SCVector*): " | 
|---|
| 273 | << "dimensions don't match\n"; | 
|---|
| 274 | abort(); | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | for (int i=0; i < d1->blocks()->nblock(); i++) | 
|---|
| 278 | if (mats_[i].nonnull()) | 
|---|
| 279 | mats_[i]->accumulate_outer_product(la->vecs_[i], lb->vecs_[i]); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | void | 
|---|
| 283 | BlockedSCMatrix::accumulate_product_rr(SCMatrix*a,SCMatrix*b) | 
|---|
| 284 | { | 
|---|
| 285 | int i, zero = 0; | 
|---|
| 286 |  | 
|---|
| 287 | const char* name = "BlockedSCMatrix::accumulate_product"; | 
|---|
| 288 | // make sure that the arguments are of the correct type | 
|---|
| 289 | BlockedSCMatrix* la = require_dynamic_cast<BlockedSCMatrix*>(a,name); | 
|---|
| 290 | BlockedSCMatrix* lb = require_dynamic_cast<BlockedSCMatrix*>(b,name); | 
|---|
| 291 |  | 
|---|
| 292 | // make sure that the dimensions match | 
|---|
| 293 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->coldim()) || | 
|---|
| 294 | !la->coldim()->equiv(lb->rowdim())) { | 
|---|
| 295 | ExEnv::errn() << indent | 
|---|
| 296 | << "BlockedSCMatrix::accumulate_product_rr(SCMatrix*a,SCMatrix*b): " | 
|---|
| 297 | << "dimensions don't match\n"; | 
|---|
| 298 | abort(); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | // find out the number of blocks we need to process. | 
|---|
| 302 | int mxnb = (nblocks_ > la->nblocks_) ? nblocks_ : la->nblocks_; | 
|---|
| 303 |  | 
|---|
| 304 | int nrba = la->d1->blocks()->nblock(); | 
|---|
| 305 | int ncba = la->d2->blocks()->nblock(); | 
|---|
| 306 | int nrbb = lb->d1->blocks()->nblock(); | 
|---|
| 307 | int ncbb = lb->d2->blocks()->nblock(); | 
|---|
| 308 |  | 
|---|
| 309 | int &mi = (nrba==1 && ncba > 1 && nrbb > 1 && ncbb==1) ? zero : i; | 
|---|
| 310 | int &ai = (nrba==1 && ncba==1) ? zero : i; | 
|---|
| 311 | int &bi = (nrbb==1 && ncbb==1) ? zero : i; | 
|---|
| 312 |  | 
|---|
| 313 | for (i=0; i < mxnb; i++) { | 
|---|
| 314 | if (mats_[mi].null() || la->mats_[ai].null() || lb->mats_[bi].null()) | 
|---|
| 315 | continue; | 
|---|
| 316 | mats_[mi]->accumulate_product(la->mats_[ai], lb->mats_[bi]); | 
|---|
| 317 | } | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | void | 
|---|
| 321 | BlockedSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b) | 
|---|
| 322 | { | 
|---|
| 323 | int i, zero=0; | 
|---|
| 324 |  | 
|---|
| 325 | const char* name = "BlockedSCMatrix::accumulate_product"; | 
|---|
| 326 | // make sure that the arguments are of the correct type | 
|---|
| 327 | BlockedSCMatrix* la = require_dynamic_cast<BlockedSCMatrix*>(a,name); | 
|---|
| 328 | BlockedSymmSCMatrix* lb = require_dynamic_cast<BlockedSymmSCMatrix*>(b,name); | 
|---|
| 329 |  | 
|---|
| 330 | // make sure that the dimensions match | 
|---|
| 331 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) || | 
|---|
| 332 | !la->coldim()->equiv(lb->dim())) { | 
|---|
| 333 | ExEnv::errn() << indent | 
|---|
| 334 | << "BlockedSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b): " | 
|---|
| 335 | << "dimensions don't match\n"; | 
|---|
| 336 | abort(); | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | int &bi = (lb->d->blocks()->nblock()==1) ? zero : i; | 
|---|
| 340 |  | 
|---|
| 341 | for (i=0; i < nblocks_; i++) { | 
|---|
| 342 | if (mats_[i].null() || la->mats_[i].null() || lb->mats_[bi].null()) | 
|---|
| 343 | continue; | 
|---|
| 344 | mats_[i]->accumulate_product(la->mats_[i], lb->mats_[bi]); | 
|---|
| 345 | } | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 |  | 
|---|
| 349 | void | 
|---|
| 350 | BlockedSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b) | 
|---|
| 351 | { | 
|---|
| 352 | int i, zero=0; | 
|---|
| 353 |  | 
|---|
| 354 | const char* name = "BlockedSCMatrix::accumulate_product"; | 
|---|
| 355 | // make sure that the arguments are of the correct type | 
|---|
| 356 | BlockedSCMatrix* la = require_dynamic_cast<BlockedSCMatrix*>(a,name); | 
|---|
| 357 | BlockedDiagSCMatrix* lb = require_dynamic_cast<BlockedDiagSCMatrix*>(b,name); | 
|---|
| 358 |  | 
|---|
| 359 | // make sure that the dimensions match | 
|---|
| 360 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) || | 
|---|
| 361 | !la->coldim()->equiv(lb->dim())) { | 
|---|
| 362 | ExEnv::errn() << indent | 
|---|
| 363 | << "BlockedSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b): " | 
|---|
| 364 | << "dimensions don't match\n"; | 
|---|
| 365 | abort(); | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | int &bi = (lb->d->blocks()->nblock()==1) ? zero : i; | 
|---|
| 369 |  | 
|---|
| 370 | for (i=0; i < nblocks_; i++) { | 
|---|
| 371 | if (mats_[i].null() || la->mats_[i].null() || lb->mats_[bi].null()) | 
|---|
| 372 | continue; | 
|---|
| 373 | mats_[i]->accumulate_product(la->mats_[i], lb->mats_[bi]); | 
|---|
| 374 | } | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | void | 
|---|
| 378 | BlockedSCMatrix::accumulate(const SCMatrix*a) | 
|---|
| 379 | { | 
|---|
| 380 | // make sure that the arguments is of the correct type | 
|---|
| 381 | const BlockedSCMatrix* la | 
|---|
| 382 | = require_dynamic_cast<const BlockedSCMatrix*>(a,"BlockedSCMatrix::accumulate"); | 
|---|
| 383 |  | 
|---|
| 384 | // make sure that the dimensions match | 
|---|
| 385 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(la->coldim())) { | 
|---|
| 386 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate(SCMatrix*a): " | 
|---|
| 387 | << "dimensions don't match\n"; | 
|---|
| 388 | abort(); | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | for (int i=0; i < nblocks_; i++) | 
|---|
| 392 | if (mats_[i].nonnull()) | 
|---|
| 393 | mats_[i]->accumulate(la->mats_[i]); | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|
| 396 | void | 
|---|
| 397 | BlockedSCMatrix::accumulate(const SymmSCMatrix*a) | 
|---|
| 398 | { | 
|---|
| 399 | // make sure that the arguments is of the correct type | 
|---|
| 400 | const BlockedSymmSCMatrix* la | 
|---|
| 401 | = require_dynamic_cast<const BlockedSymmSCMatrix*>(a,"BlockedSCMatrix::accumulate"); | 
|---|
| 402 |  | 
|---|
| 403 | // make sure that the dimensions match | 
|---|
| 404 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) { | 
|---|
| 405 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate(SymmSCMatrix*a): " | 
|---|
| 406 | << "dimensions don't match\n"; | 
|---|
| 407 | abort(); | 
|---|
| 408 | } | 
|---|
| 409 |  | 
|---|
| 410 | for (int i=0; i < nblocks_; i++) | 
|---|
| 411 | if (mats_[i].nonnull()) | 
|---|
| 412 | mats_[i]->accumulate(la->mats_[i]); | 
|---|
| 413 | } | 
|---|
| 414 |  | 
|---|
| 415 | void | 
|---|
| 416 | BlockedSCMatrix::accumulate(const DiagSCMatrix*a) | 
|---|
| 417 | { | 
|---|
| 418 | // make sure that the arguments is of the correct type | 
|---|
| 419 | const BlockedDiagSCMatrix* la | 
|---|
| 420 | = require_dynamic_cast<const BlockedDiagSCMatrix*>(a,"BlockedSCMatrix::accumulate"); | 
|---|
| 421 |  | 
|---|
| 422 | // make sure that the dimensions match | 
|---|
| 423 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) { | 
|---|
| 424 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate(DiagSCMatrix*a): " | 
|---|
| 425 | << "dimensions don't match\n"; | 
|---|
| 426 | abort(); | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | for (int i=0; i < nblocks_; i++) | 
|---|
| 430 | if (mats_[i].nonnull()) | 
|---|
| 431 | mats_[i]->accumulate(la->mats_[i]); | 
|---|
| 432 | } | 
|---|
| 433 |  | 
|---|
| 434 | void | 
|---|
| 435 | BlockedSCMatrix::accumulate(const SCVector*a) | 
|---|
| 436 | { | 
|---|
| 437 | // make sure that the arguments is of the correct type | 
|---|
| 438 | const BlockedSCVector* la | 
|---|
| 439 | = require_dynamic_cast<const BlockedSCVector*>(a,"BlockedSCVector::accumulate"); | 
|---|
| 440 |  | 
|---|
| 441 | // make sure that the dimensions match | 
|---|
| 442 | if (!((rowdim()->equiv(la->dim()) && coldim()->n() == 1) | 
|---|
| 443 | || (coldim()->equiv(la->dim()) && rowdim()->n() == 1))) { | 
|---|
| 444 | ExEnv::errn() << indent << "BlockedSCMatrix::accumulate(SCVector*a): " | 
|---|
| 445 | << "dimensions don't match\n"; | 
|---|
| 446 | abort(); | 
|---|
| 447 | } | 
|---|
| 448 |  | 
|---|
| 449 | for (int i=0; i < nblocks_; i++) | 
|---|
| 450 | if (mats_[i].nonnull()) | 
|---|
| 451 | mats_[i]->accumulate(la->vecs_[i]); | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 | void | 
|---|
| 455 | BlockedSCMatrix::transpose_this() | 
|---|
| 456 | { | 
|---|
| 457 | for (int i=0; i < nblocks_; i++) | 
|---|
| 458 | if (mats_[i].nonnull()) | 
|---|
| 459 | mats_[i]->transpose_this(); | 
|---|
| 460 |  | 
|---|
| 461 | RefSCDimension tmp = d1; | 
|---|
| 462 | d1 = d2; | 
|---|
| 463 | d2 = tmp; | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | // hack, hack, hack!  One day we'll get svd working everywhere. | 
|---|
| 467 | double | 
|---|
| 468 | BlockedSCMatrix::invert_this() | 
|---|
| 469 | { | 
|---|
| 470 | int i; | 
|---|
| 471 | double res=1; | 
|---|
| 472 |  | 
|---|
| 473 | // if this matrix is block diagonal, then give a normal inversion a shot | 
|---|
| 474 | if (d1->blocks()->nblock() == d2->blocks()->nblock()) { | 
|---|
| 475 | for (i=0; i < nblocks_; i++) | 
|---|
| 476 | if (mats_[i].nonnull()) res *= mats_[i]->invert_this(); | 
|---|
| 477 | return res; | 
|---|
| 478 | } | 
|---|
| 479 |  | 
|---|
| 480 | // ok, let's make sure that the matrix is at least square | 
|---|
| 481 | if (d1->n() != d2->n()) { | 
|---|
| 482 | ExEnv::errn() << indent | 
|---|
| 483 | << "BlockedSCMatrix::invert_this: SVD not implemented yet\n"; | 
|---|
| 484 | abort(); | 
|---|
| 485 | } | 
|---|
| 486 |  | 
|---|
| 487 | if (d1->blocks()->nblock() == 1) { | 
|---|
| 488 | RefSCMatrix tdim = subkit->matrix(d1->blocks()->subdim(0), | 
|---|
| 489 | d1->blocks()->subdim(0)); | 
|---|
| 490 | tdim->convert(this); | 
|---|
| 491 | res = tdim->invert_this(); | 
|---|
| 492 | transpose_this(); | 
|---|
| 493 |  | 
|---|
| 494 | // d1 and d2 were swapped by now | 
|---|
| 495 | for (i=0; i < d1->blocks()->nblock(); i++) | 
|---|
| 496 | if (mats_[i].nonnull()) | 
|---|
| 497 | mats_[i]->convert(tdim.get_subblock(d1->blocks()->start(i), | 
|---|
| 498 | d1->blocks()->fence(i)-1, | 
|---|
| 499 | 0, d2->n()-1)); | 
|---|
| 500 |  | 
|---|
| 501 | return res; | 
|---|
| 502 |  | 
|---|
| 503 | } else if (d2->blocks()->nblock() == 1) { | 
|---|
| 504 | RefSCMatrix tdim = subkit->matrix(d2->blocks()->subdim(0), | 
|---|
| 505 | d2->blocks()->subdim(0)); | 
|---|
| 506 |  | 
|---|
| 507 | tdim->convert(this); | 
|---|
| 508 | res = tdim->invert_this(); | 
|---|
| 509 | transpose_this(); | 
|---|
| 510 |  | 
|---|
| 511 | // d1 and d2 were swapped by now | 
|---|
| 512 | for (i=0; i < d2->blocks()->nblock(); i++) | 
|---|
| 513 | if (mats_[i].nonnull()) | 
|---|
| 514 | mats_[i]->convert(tdim.get_subblock(0, d1->n()-1, | 
|---|
| 515 | d2->blocks()->start(i), | 
|---|
| 516 | d2->blocks()->fence(i)-1)); | 
|---|
| 517 |  | 
|---|
| 518 | return res; | 
|---|
| 519 |  | 
|---|
| 520 | } else { | 
|---|
| 521 | ExEnv::errn() << indent | 
|---|
| 522 | << "BlockedSCMatrix::invert_this: SVD not implemented yet\n"; | 
|---|
| 523 | abort(); | 
|---|
| 524 | } | 
|---|
| 525 |  | 
|---|
| 526 | return 0.0; | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 | void | 
|---|
| 530 | BlockedSCMatrix::gen_invert_this() | 
|---|
| 531 | { | 
|---|
| 532 | ExEnv::errn() << indent | 
|---|
| 533 | << "BlockedSCMatrix::gen_invert_this: SVD not implemented yet\n"; | 
|---|
| 534 | abort(); | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | double | 
|---|
| 538 | BlockedSCMatrix::determ_this() | 
|---|
| 539 | { | 
|---|
| 540 | double res=1; | 
|---|
| 541 |  | 
|---|
| 542 | for (int i=0; i < nblocks_; i++) | 
|---|
| 543 | if (mats_[i].nonnull()) | 
|---|
| 544 | res *= mats_[i]->determ_this(); | 
|---|
| 545 |  | 
|---|
| 546 | return res; | 
|---|
| 547 | } | 
|---|
| 548 |  | 
|---|
| 549 | double | 
|---|
| 550 | BlockedSCMatrix::trace() | 
|---|
| 551 | { | 
|---|
| 552 | double ret=0; | 
|---|
| 553 | for (int i=0; i < nblocks_; i++) | 
|---|
| 554 | if (mats_[i].nonnull()) | 
|---|
| 555 | ret += mats_[i]->trace(); | 
|---|
| 556 |  | 
|---|
| 557 | return ret; | 
|---|
| 558 | } | 
|---|
| 559 |  | 
|---|
| 560 | void | 
|---|
| 561 | BlockedSCMatrix::svd_this(SCMatrix *U, DiagSCMatrix *sigma, SCMatrix *V) | 
|---|
| 562 | { | 
|---|
| 563 | BlockedSCMatrix* lU = | 
|---|
| 564 | require_dynamic_cast<BlockedSCMatrix*>(U,"BlockedSCMatrix::svd_this"); | 
|---|
| 565 | BlockedSCMatrix* lV = | 
|---|
| 566 | require_dynamic_cast<BlockedSCMatrix*>(V,"BlockedSCMatrix::svd_this"); | 
|---|
| 567 | BlockedDiagSCMatrix* lsigma = | 
|---|
| 568 | require_dynamic_cast<BlockedDiagSCMatrix*>(sigma,"BlockedSCMatrix::svd_this"); | 
|---|
| 569 |  | 
|---|
| 570 | for (int i=0; i < nblocks_; i++) | 
|---|
| 571 | if (mats_[i].nonnull()) | 
|---|
| 572 | mats_[i]->svd_this(lU->mats_[i], lsigma->mats_[i], lV->mats_[i]); | 
|---|
| 573 | } | 
|---|
| 574 |  | 
|---|
| 575 | double | 
|---|
| 576 | BlockedSCMatrix::solve_this(SCVector*v) | 
|---|
| 577 | { | 
|---|
| 578 | double res=1; | 
|---|
| 579 |  | 
|---|
| 580 | BlockedSCVector* lv = | 
|---|
| 581 | require_dynamic_cast<BlockedSCVector*>(v,"BlockedSCMatrix::solve_this"); | 
|---|
| 582 |  | 
|---|
| 583 | // make sure that the dimensions match | 
|---|
| 584 | if (!rowdim()->equiv(lv->dim())) { | 
|---|
| 585 | ExEnv::errn() << indent << "BlockedSCMatrix::solve_this(SCVector*v): " | 
|---|
| 586 | << "dimensions don't match\n"; | 
|---|
| 587 | abort(); | 
|---|
| 588 | } | 
|---|
| 589 |  | 
|---|
| 590 | for (int i=0; i < nblocks_; i++) | 
|---|
| 591 | if (mats_[i].nonnull()) | 
|---|
| 592 | res *= mats_[i]->solve_this(lv->vecs_[i]); | 
|---|
| 593 |  | 
|---|
| 594 | return res; | 
|---|
| 595 | } | 
|---|
| 596 |  | 
|---|
| 597 | void | 
|---|
| 598 | BlockedSCMatrix::schmidt_orthog(SymmSCMatrix *S, int nc) | 
|---|
| 599 | { | 
|---|
| 600 | BlockedSymmSCMatrix* lS = | 
|---|
| 601 | require_dynamic_cast<BlockedSymmSCMatrix*>(S,"BlockedSCMatrix::schmidt_orthog"); | 
|---|
| 602 |  | 
|---|
| 603 | // make sure that the dimensions match | 
|---|
| 604 | if (!rowdim()->equiv(lS->dim())) { | 
|---|
| 605 | ExEnv::errn() << indent << "BlockedSCMatrix::schmidt_orthog(): " | 
|---|
| 606 | << "dimensions don't match\n"; | 
|---|
| 607 | abort(); | 
|---|
| 608 | } | 
|---|
| 609 |  | 
|---|
| 610 | for (int i=0; i < nblocks_; i++) | 
|---|
| 611 | if (mats_[i].nonnull()) | 
|---|
| 612 | mats_[i]->schmidt_orthog(lS->mats_[i].pointer(), | 
|---|
| 613 | lS->dim()->blocks()->subdim(i).n()); | 
|---|
| 614 | } | 
|---|
| 615 |  | 
|---|
| 616 | int | 
|---|
| 617 | BlockedSCMatrix::schmidt_orthog_tol(SymmSCMatrix *S, double tol, double *res) | 
|---|
| 618 | { | 
|---|
| 619 | ExEnv::err0() | 
|---|
| 620 | << "ERROR: BlockedSCMatrix::schmidt_orthog_tol doesn't exist" | 
|---|
| 621 | << endl; | 
|---|
| 622 | abort(); | 
|---|
| 623 | return 0; | 
|---|
| 624 | } | 
|---|
| 625 |  | 
|---|
| 626 | void | 
|---|
| 627 | BlockedSCMatrix::element_op(const Ref<SCElementOp>& op) | 
|---|
| 628 | { | 
|---|
| 629 | BlockedSCElementOp *bop = dynamic_cast<BlockedSCElementOp*>(op.pointer()); | 
|---|
| 630 |  | 
|---|
| 631 | op->defer_collect(1); | 
|---|
| 632 | for (int i=0; i < nblocks_; i++) { | 
|---|
| 633 | if (bop) | 
|---|
| 634 | bop->working_on(i); | 
|---|
| 635 | if (mats_[i].nonnull()) | 
|---|
| 636 | mats_[i]->element_op(op); | 
|---|
| 637 | } | 
|---|
| 638 | op->defer_collect(0); | 
|---|
| 639 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 640 | } | 
|---|
| 641 |  | 
|---|
| 642 | void | 
|---|
| 643 | BlockedSCMatrix::element_op(const Ref<SCElementOp2>& op, | 
|---|
| 644 | SCMatrix* m) | 
|---|
| 645 | { | 
|---|
| 646 | BlockedSCMatrix *lm | 
|---|
| 647 | = require_dynamic_cast<BlockedSCMatrix*>(m,"BlockedSCMatrix::element_op"); | 
|---|
| 648 |  | 
|---|
| 649 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim())) { | 
|---|
| 650 | ExEnv::errn() << indent << "BlockedSCMatrix: bad element_op\n"; | 
|---|
| 651 | abort(); | 
|---|
| 652 | } | 
|---|
| 653 |  | 
|---|
| 654 | BlockedSCElementOp2 *bop = dynamic_cast<BlockedSCElementOp2*>(op.pointer()); | 
|---|
| 655 |  | 
|---|
| 656 | op->defer_collect(1); | 
|---|
| 657 | for (int i=0; i < nblocks_; i++) { | 
|---|
| 658 | if (bop) | 
|---|
| 659 | bop->working_on(i); | 
|---|
| 660 | if (mats_[i].nonnull()) | 
|---|
| 661 | mats_[i]->element_op(op,lm->mats_[i].pointer()); | 
|---|
| 662 | } | 
|---|
| 663 | op->defer_collect(0); | 
|---|
| 664 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 665 | } | 
|---|
| 666 |  | 
|---|
| 667 | void | 
|---|
| 668 | BlockedSCMatrix::element_op(const Ref<SCElementOp3>& op, | 
|---|
| 669 | SCMatrix* m,SCMatrix* n) | 
|---|
| 670 | { | 
|---|
| 671 | BlockedSCMatrix *lm | 
|---|
| 672 | = require_dynamic_cast<BlockedSCMatrix*>(m,"BlockedSCMatrix::element_op"); | 
|---|
| 673 | BlockedSCMatrix *ln | 
|---|
| 674 | = require_dynamic_cast<BlockedSCMatrix*>(n,"BlockedSCMatrix::element_op"); | 
|---|
| 675 |  | 
|---|
| 676 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim()) || | 
|---|
| 677 | !rowdim()->equiv(ln->rowdim()) || !coldim()->equiv(ln->coldim())) { | 
|---|
| 678 | ExEnv::errn() << indent << "BlockedSCMatrix: bad element_op\n"; | 
|---|
| 679 | abort(); | 
|---|
| 680 | } | 
|---|
| 681 |  | 
|---|
| 682 | BlockedSCElementOp3 *bop = dynamic_cast<BlockedSCElementOp3*>(op.pointer()); | 
|---|
| 683 |  | 
|---|
| 684 | op->defer_collect(1); | 
|---|
| 685 | for (int i=0; i < nblocks_; i++) { | 
|---|
| 686 | if (bop) | 
|---|
| 687 | bop->working_on(i); | 
|---|
| 688 | if (mats_[i].nonnull()) | 
|---|
| 689 | mats_[i]->element_op(op,lm->mats_[i].pointer(), | 
|---|
| 690 | ln->mats_[i].pointer()); | 
|---|
| 691 | } | 
|---|
| 692 | op->defer_collect(0); | 
|---|
| 693 | if (op->has_collect()) op->collect(messagegrp()); | 
|---|
| 694 | } | 
|---|
| 695 |  | 
|---|
| 696 | void | 
|---|
| 697 | BlockedSCMatrix::vprint(const char *title, ostream& os, int prec) const | 
|---|
| 698 | { | 
|---|
| 699 | int len = (title) ? strlen(title) : 0; | 
|---|
| 700 | char *newtitle = new char[len + 80]; | 
|---|
| 701 |  | 
|---|
| 702 | for (int i=0; i < nblocks_; i++) { | 
|---|
| 703 | if (mats_[i].null()) | 
|---|
| 704 | continue; | 
|---|
| 705 |  | 
|---|
| 706 | sprintf(newtitle,"%s:  block %d",title,i+1); | 
|---|
| 707 | mats_[i]->print(newtitle, os, prec); | 
|---|
| 708 | } | 
|---|
| 709 |  | 
|---|
| 710 | delete[] newtitle; | 
|---|
| 711 | } | 
|---|
| 712 |  | 
|---|
| 713 | RefSCDimension | 
|---|
| 714 | BlockedSCMatrix::rowdim(int i) const | 
|---|
| 715 | { | 
|---|
| 716 | return d1->blocks()->subdim(i); | 
|---|
| 717 | } | 
|---|
| 718 |  | 
|---|
| 719 | RefSCDimension | 
|---|
| 720 | BlockedSCMatrix::coldim(int i) const | 
|---|
| 721 | { | 
|---|
| 722 | return d2->blocks()->subdim(i); | 
|---|
| 723 | } | 
|---|
| 724 |  | 
|---|
| 725 | int | 
|---|
| 726 | BlockedSCMatrix::nblocks() const | 
|---|
| 727 | { | 
|---|
| 728 | return nblocks_; | 
|---|
| 729 | } | 
|---|
| 730 |  | 
|---|
| 731 | RefSCMatrix | 
|---|
| 732 | BlockedSCMatrix::block(int i) | 
|---|
| 733 | { | 
|---|
| 734 | if (mats_) | 
|---|
| 735 | return mats_[i]; | 
|---|
| 736 | else | 
|---|
| 737 | return (SCMatrix*)0; | 
|---|
| 738 | } | 
|---|
| 739 |  | 
|---|
| 740 | Ref<SCMatrixSubblockIter> | 
|---|
| 741 | BlockedSCMatrix::local_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 742 | { | 
|---|
| 743 | Ref<SCMatrixCompositeSubblockIter> iter | 
|---|
| 744 | = new SCMatrixCompositeSubblockIter(access, nblocks()); | 
|---|
| 745 | for (int i=0; i<nblocks(); i++) { | 
|---|
| 746 | if (block(i).null()) | 
|---|
| 747 | iter->set_iter(i, new SCMatrixNullSubblockIter(access)); | 
|---|
| 748 | else | 
|---|
| 749 | iter->set_iter(i, block(i)->local_blocks(access)); | 
|---|
| 750 | } | 
|---|
| 751 | Ref<SCMatrixSubblockIter> ret = iter.pointer(); | 
|---|
| 752 | return ret; | 
|---|
| 753 | } | 
|---|
| 754 |  | 
|---|
| 755 | Ref<SCMatrixSubblockIter> | 
|---|
| 756 | BlockedSCMatrix::all_blocks(SCMatrixSubblockIter::Access access) | 
|---|
| 757 | { | 
|---|
| 758 | Ref<SCMatrixCompositeSubblockIter> iter | 
|---|
| 759 | = new SCMatrixCompositeSubblockIter(access, nblocks()); | 
|---|
| 760 | for (int i=0; i<nblocks(); i++) { | 
|---|
| 761 | if (block(i).null()) | 
|---|
| 762 | iter->set_iter(i, new SCMatrixNullSubblockIter(access)); | 
|---|
| 763 | else | 
|---|
| 764 | iter->set_iter(i, block(i)->all_blocks(access)); | 
|---|
| 765 | } | 
|---|
| 766 | Ref<SCMatrixSubblockIter> ret = iter.pointer(); | 
|---|
| 767 | return ret; | 
|---|
| 768 | } | 
|---|
| 769 |  | 
|---|
| 770 | void | 
|---|
| 771 | BlockedSCMatrix::save(StateOut&s) | 
|---|
| 772 | { | 
|---|
| 773 | int nr = nrow(); | 
|---|
| 774 | int nc = ncol(); | 
|---|
| 775 | s.put(nr); | 
|---|
| 776 | s.put(nc); | 
|---|
| 777 | int has_subblocks = 1; | 
|---|
| 778 | s.put(has_subblocks); | 
|---|
| 779 | s.put(nblocks()); | 
|---|
| 780 | for (int i=0; i<nblocks(); i++) { | 
|---|
| 781 | block(i).save(s); | 
|---|
| 782 | } | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 | void | 
|---|
| 786 | BlockedSCMatrix::restore(StateIn& s) | 
|---|
| 787 | { | 
|---|
| 788 | int nrt, nr = nrow(); | 
|---|
| 789 | int nct, nc = ncol(); | 
|---|
| 790 | s.get(nrt); | 
|---|
| 791 | s.get(nct); | 
|---|
| 792 | if (nrt != nr || nct != nc) { | 
|---|
| 793 | ExEnv::errn() << indent << "BlockedSCMatrix::restore(): bad dimensions" << endl; | 
|---|
| 794 | abort(); | 
|---|
| 795 | } | 
|---|
| 796 | int has_subblocks; | 
|---|
| 797 | s.get(has_subblocks); | 
|---|
| 798 | if (has_subblocks) { | 
|---|
| 799 | int nblock; | 
|---|
| 800 | s.get(nblock); | 
|---|
| 801 | if (nblock != nblocks()) { | 
|---|
| 802 | ExEnv::errn() << indent | 
|---|
| 803 | << "BlockedSCMatrix::restore(): nblock differs\n" << endl; | 
|---|
| 804 | abort(); | 
|---|
| 805 | } | 
|---|
| 806 | for (int i=0; i<nblocks(); i++) { | 
|---|
| 807 | block(i).restore(s); | 
|---|
| 808 | } | 
|---|
| 809 | } | 
|---|
| 810 | else { | 
|---|
| 811 | ExEnv::errn() << indent | 
|---|
| 812 | << "BlockedSCMatrix::restore(): no subblocks--cannot restore" | 
|---|
| 813 | << endl; | 
|---|
| 814 | abort(); | 
|---|
| 815 | } | 
|---|
| 816 | } | 
|---|
| 817 |  | 
|---|
| 818 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 819 |  | 
|---|
| 820 | // Local Variables: | 
|---|
| 821 | // mode: c++ | 
|---|
| 822 | // c-file-style: "CLJ" | 
|---|
| 823 | // End: | 
|---|