| 1 | //
 | 
|---|
| 2 | // blockedsymm.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 | // BlockedSymmSCMatrix member functions
 | 
|---|
| 42 | 
 | 
|---|
| 43 | static ClassDesc BlockedSymmSCMatrix_cd(
 | 
|---|
| 44 |   typeid(BlockedSymmSCMatrix),"BlockedSymmSCMatrix",1,"public SymmSCMatrix",
 | 
|---|
| 45 |   0, 0, 0);
 | 
|---|
| 46 | 
 | 
|---|
| 47 | void
 | 
|---|
| 48 | BlockedSymmSCMatrix::resize(SCDimension *a)
 | 
|---|
| 49 | {
 | 
|---|
| 50 |   if (mats_) {
 | 
|---|
| 51 |     delete[] mats_;
 | 
|---|
| 52 |     mats_=0;
 | 
|---|
| 53 |   }
 | 
|---|
| 54 |   
 | 
|---|
| 55 |   d = a;
 | 
|---|
| 56 |   
 | 
|---|
| 57 |   mats_ = new RefSymmSCMatrix[d->blocks()->nblock()];
 | 
|---|
| 58 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 59 |     if (d->blocks()->size(i))
 | 
|---|
| 60 |       mats_[i] = subkit->symmmatrix(d->blocks()->subdim(i));
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | BlockedSymmSCMatrix::BlockedSymmSCMatrix(const RefSCDimension&a,
 | 
|---|
| 64 |                                          BlockedSCMatrixKit*k):
 | 
|---|
| 65 |   SymmSCMatrix(a,k),
 | 
|---|
| 66 |   subkit(k->subkit()),
 | 
|---|
| 67 |   mats_(0)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |   resize(a);
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | BlockedSymmSCMatrix::~BlockedSymmSCMatrix()
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   if (mats_) {
 | 
|---|
| 75 |     delete[] mats_;
 | 
|---|
| 76 |     mats_=0;
 | 
|---|
| 77 |   }
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | double
 | 
|---|
| 81 | BlockedSymmSCMatrix::get_element(int i,int j) const
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   int block_i, block_j;
 | 
|---|
| 84 |   int elem_i, elem_j;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   d->blocks()->elem_to_block(i,block_i,elem_i);
 | 
|---|
| 87 |   d->blocks()->elem_to_block(j,block_j,elem_j);
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   if (block_i != block_j)
 | 
|---|
| 90 |     return 0;
 | 
|---|
| 91 |   
 | 
|---|
| 92 |   return mats_[block_i]->get_element(elem_i,elem_j);
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | void
 | 
|---|
| 96 | BlockedSymmSCMatrix::set_element(int i,int j,double a)
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   int block_i, block_j;
 | 
|---|
| 99 |   int elem_i, elem_j;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   d->blocks()->elem_to_block(i,block_i,elem_i);
 | 
|---|
| 102 |   d->blocks()->elem_to_block(j,block_j,elem_j);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   if (block_i != block_j)
 | 
|---|
| 105 |     return;
 | 
|---|
| 106 |   
 | 
|---|
| 107 |   mats_[block_i]->set_element(elem_i,elem_j,a);
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | void
 | 
|---|
| 111 | BlockedSymmSCMatrix::accumulate_element(int i,int j,double a)
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   int block_i, block_j;
 | 
|---|
| 114 |   int elem_i, elem_j;
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   d->blocks()->elem_to_block(i,block_i,elem_i);
 | 
|---|
| 117 |   d->blocks()->elem_to_block(j,block_j,elem_j);
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   if (block_i != block_j)
 | 
|---|
| 120 |     return;
 | 
|---|
| 121 |   
 | 
|---|
| 122 |   mats_[block_i]->accumulate_element(elem_i,elem_j,a);
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | SCMatrix *
 | 
|---|
| 126 | BlockedSymmSCMatrix::get_subblock(int br, int er, int bc, int ec)
 | 
|---|
| 127 | {
 | 
|---|
| 128 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::get_subblock: cannot get subblock\n";
 | 
|---|
| 129 |   abort();
 | 
|---|
| 130 |   return 0;
 | 
|---|
| 131 | }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | SymmSCMatrix *
 | 
|---|
| 134 | BlockedSymmSCMatrix::get_subblock(int br, int er)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::get_subblock: cannot get subblock\n";
 | 
|---|
| 137 |   abort();
 | 
|---|
| 138 |   return 0;
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | void
 | 
|---|
| 142 | BlockedSymmSCMatrix::assign_subblock(SCMatrix*sb,
 | 
|---|
| 143 |                                      int br, int er, int bc, int ec)
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::assign_subblock:"
 | 
|---|
| 146 |        << " cannot assign subblock\n";
 | 
|---|
| 147 |   abort();
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void
 | 
|---|
| 151 | BlockedSymmSCMatrix::assign_subblock(SymmSCMatrix*sb, int br, int er)
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::assign_subblock:"
 | 
|---|
| 154 |                  << " cannot assign subblock\n";
 | 
|---|
| 155 |   abort();
 | 
|---|
| 156 | }
 | 
|---|
| 157 | 
 | 
|---|
| 158 | void
 | 
|---|
| 159 | BlockedSymmSCMatrix::accumulate_subblock(SCMatrix*sb,
 | 
|---|
| 160 |                                          int br, int er, int bc, int ec)
 | 
|---|
| 161 | {
 | 
|---|
| 162 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate_subblock:"
 | 
|---|
| 163 |                  << " cannot accumulate subblock\n";
 | 
|---|
| 164 |   abort();
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | void
 | 
|---|
| 168 | BlockedSymmSCMatrix::accumulate_subblock(SymmSCMatrix*sb, int br, int er)
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate_subblock:"
 | 
|---|
| 171 |                  << " cannot accumulate subblock\n";
 | 
|---|
| 172 |   abort();
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | SCVector *
 | 
|---|
| 176 | BlockedSymmSCMatrix::get_row(int i)
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::get_row: cannot get row\n";
 | 
|---|
| 179 |   abort();
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   return 0;
 | 
|---|
| 182 | }
 | 
|---|
| 183 | 
 | 
|---|
| 184 | void
 | 
|---|
| 185 | BlockedSymmSCMatrix::assign_row(SCVector *v, int i)
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::assign_row: cannot assign row\n";
 | 
|---|
| 188 |   abort();
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | void
 | 
|---|
| 192 | BlockedSymmSCMatrix::accumulate_row(SCVector *v, int i)
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate_row:"
 | 
|---|
| 195 |                  << " cannot accumulate row\n";
 | 
|---|
| 196 |   abort();
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | double
 | 
|---|
| 200 | BlockedSymmSCMatrix::invert_this()
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   double res=1;
 | 
|---|
| 203 |   
 | 
|---|
| 204 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 205 |     if (mats_[i].nonnull())
 | 
|---|
| 206 |       res *= mats_[i]->invert_this();
 | 
|---|
| 207 | 
 | 
|---|
| 208 |   return res;
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | double
 | 
|---|
| 212 | BlockedSymmSCMatrix::determ_this()
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   double res=1;
 | 
|---|
| 215 |   
 | 
|---|
| 216 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 217 |     if (mats_[i].nonnull())
 | 
|---|
| 218 |       res *= mats_[i]->determ_this();
 | 
|---|
| 219 | 
 | 
|---|
| 220 |   return res;
 | 
|---|
| 221 | }
 | 
|---|
| 222 | 
 | 
|---|
| 223 | double
 | 
|---|
| 224 | BlockedSymmSCMatrix::trace()
 | 
|---|
| 225 | {
 | 
|---|
| 226 |   double res=0;
 | 
|---|
| 227 |   
 | 
|---|
| 228 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 229 |     if (mats_[i].nonnull())
 | 
|---|
| 230 |       res += mats_[i]->trace();
 | 
|---|
| 231 | 
 | 
|---|
| 232 |   return res;
 | 
|---|
| 233 | }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | double
 | 
|---|
| 236 | BlockedSymmSCMatrix::solve_this(SCVector*v)
 | 
|---|
| 237 | {
 | 
|---|
| 238 |   double res=1;
 | 
|---|
| 239 |   
 | 
|---|
| 240 |   BlockedSCVector* lv =
 | 
|---|
| 241 |     require_dynamic_cast<BlockedSCVector*>(v,"BlockedSymmSCMatrix::solve_this");
 | 
|---|
| 242 |   
 | 
|---|
| 243 |   // make sure that the dimensions match
 | 
|---|
| 244 |   if (!dim()->equiv(lv->dim())) {
 | 
|---|
| 245 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::solve_this(SCVector*v): "
 | 
|---|
| 246 |          << "dimensions don't match\n";
 | 
|---|
| 247 |     abort();
 | 
|---|
| 248 |   }
 | 
|---|
| 249 | 
 | 
|---|
| 250 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 251 |     if (mats_[i].nonnull())
 | 
|---|
| 252 |       res *= mats_[i]->solve_this(lv->vecs_[i].pointer());
 | 
|---|
| 253 | 
 | 
|---|
| 254 |   return res;
 | 
|---|
| 255 | }
 | 
|---|
| 256 | 
 | 
|---|
| 257 | void
 | 
|---|
| 258 | BlockedSymmSCMatrix::assign_val(double s)
 | 
|---|
| 259 | {
 | 
|---|
| 260 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 261 |     if (mats_[i].nonnull())
 | 
|---|
| 262 |       mats_[i]->assign(s);
 | 
|---|
| 263 | }
 | 
|---|
| 264 | 
 | 
|---|
| 265 | void
 | 
|---|
| 266 | BlockedSymmSCMatrix::assign_s(SymmSCMatrix*a)
 | 
|---|
| 267 | {
 | 
|---|
| 268 |   // make sure that the arguments is of the correct type
 | 
|---|
| 269 |   BlockedSymmSCMatrix* la = require_dynamic_cast<BlockedSymmSCMatrix*>(a,
 | 
|---|
| 270 |                                    "BlockedSymmSCMatrix::assign");
 | 
|---|
| 271 | 
 | 
|---|
| 272 |   // make sure that the dimensions match
 | 
|---|
| 273 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
| 274 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::assign_s(SymmSCMatrix*a): "
 | 
|---|
| 275 |          << "dimensions don't match\n";
 | 
|---|
| 276 |     abort();
 | 
|---|
| 277 |   }
 | 
|---|
| 278 | 
 | 
|---|
| 279 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 280 |     if (mats_[i].nonnull())
 | 
|---|
| 281 |       mats_[i]->assign(la->mats_[i].pointer());
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | void
 | 
|---|
| 285 | BlockedSymmSCMatrix::scale(double s)
 | 
|---|
| 286 | {
 | 
|---|
| 287 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 288 |     if (mats_[i].nonnull())
 | 
|---|
| 289 |       mats_[i]->scale(s);
 | 
|---|
| 290 | }
 | 
|---|
| 291 | 
 | 
|---|
| 292 | void
 | 
|---|
| 293 | BlockedSymmSCMatrix::gen_invert_this()
 | 
|---|
| 294 | {
 | 
|---|
| 295 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 296 |     if (mats_[i].nonnull())
 | 
|---|
| 297 |       mats_[i]->gen_invert_this();
 | 
|---|
| 298 | }
 | 
|---|
| 299 | 
 | 
|---|
| 300 | double
 | 
|---|
| 301 | BlockedSymmSCMatrix::scalar_product(SCVector*a)
 | 
|---|
| 302 | {
 | 
|---|
| 303 |   // make sure that the argument is of the correct type
 | 
|---|
| 304 |   BlockedSCVector* la
 | 
|---|
| 305 |     = require_dynamic_cast<BlockedSCVector*>(a,"BlockedSCVector::scalar_product");
 | 
|---|
| 306 | 
 | 
|---|
| 307 |   // make sure that the dimensions match
 | 
|---|
| 308 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
| 309 |     ExEnv::errn() << indent << "BlockedSCVector::scale_product(SCVector*a): "
 | 
|---|
| 310 |          << "dimensions don't match\n";
 | 
|---|
| 311 |     abort();
 | 
|---|
| 312 |   }
 | 
|---|
| 313 | 
 | 
|---|
| 314 |   double result = 0.0;
 | 
|---|
| 315 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 316 |     if (mats_[i].nonnull())
 | 
|---|
| 317 |       result += mats_[i]->scalar_product(la->vecs_[i].pointer());
 | 
|---|
| 318 | 
 | 
|---|
| 319 |   return result;
 | 
|---|
| 320 | }
 | 
|---|
| 321 | 
 | 
|---|
| 322 | void
 | 
|---|
| 323 | BlockedSymmSCMatrix::diagonalize(DiagSCMatrix*a,SCMatrix*b)
 | 
|---|
| 324 | {
 | 
|---|
| 325 |   const char* name = "BlockedSymmSCMatrix::diagonalize";
 | 
|---|
| 326 |   // make sure that the arguments is of the correct type
 | 
|---|
| 327 |   BlockedDiagSCMatrix* la = require_dynamic_cast<BlockedDiagSCMatrix*>(a,name);
 | 
|---|
| 328 |   BlockedSCMatrix* lb = require_dynamic_cast<BlockedSCMatrix*>(b,name);
 | 
|---|
| 329 | 
 | 
|---|
| 330 |   if (!dim()->equiv(la->dim()) ||
 | 
|---|
| 331 |       !dim()->equiv(lb->coldim()) || !dim()->equiv(lb->rowdim())) {
 | 
|---|
| 332 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::"
 | 
|---|
| 333 |          << "diagonalize(DiagSCMatrix*a,SCMatrix*b): bad dims\n";
 | 
|---|
| 334 |     abort();
 | 
|---|
| 335 |   }
 | 
|---|
| 336 | 
 | 
|---|
| 337 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 338 |     if (mats_[i].nonnull())
 | 
|---|
| 339 |       mats_[i]->diagonalize(la->mats_[i].pointer(),lb->mats_[i].pointer());
 | 
|---|
| 340 | }
 | 
|---|
| 341 | 
 | 
|---|
| 342 | void
 | 
|---|
| 343 | BlockedSymmSCMatrix::accumulate(const SymmSCMatrix*a)
 | 
|---|
| 344 | {
 | 
|---|
| 345 |   // make sure that the arguments is of the correct type
 | 
|---|
| 346 |   const BlockedSymmSCMatrix* la = require_dynamic_cast<const BlockedSymmSCMatrix*>(a,
 | 
|---|
| 347 |                                    "BlockedSymmSCMatrix::accumulate");
 | 
|---|
| 348 | 
 | 
|---|
| 349 |   // make sure that the dimensions match
 | 
|---|
| 350 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
| 351 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate(SymmSCMatrix*a): "
 | 
|---|
| 352 |          << "dimensions don't match\n";
 | 
|---|
| 353 |     abort();
 | 
|---|
| 354 |   }
 | 
|---|
| 355 | 
 | 
|---|
| 356 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 357 |     if (mats_[i].nonnull())
 | 
|---|
| 358 |       mats_[i]->accumulate(la->mats_[i].pointer());
 | 
|---|
| 359 | }
 | 
|---|
| 360 | 
 | 
|---|
| 361 | // computes this += a * a.t
 | 
|---|
| 362 | void
 | 
|---|
| 363 | BlockedSymmSCMatrix::accumulate_symmetric_product(SCMatrix*a)
 | 
|---|
| 364 | {
 | 
|---|
| 365 |   int i, zero=0;
 | 
|---|
| 366 |   
 | 
|---|
| 367 |   // make sure that the argument is of the correct type
 | 
|---|
| 368 |   BlockedSCMatrix* la
 | 
|---|
| 369 |     = require_dynamic_cast<BlockedSCMatrix*>(a,"BlockedSymmSCMatrix::"
 | 
|---|
| 370 |                                           "accumulate_symmetric_product");
 | 
|---|
| 371 | 
 | 
|---|
| 372 |   if (!dim()->equiv(la->rowdim())) {
 | 
|---|
| 373 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::"
 | 
|---|
| 374 |          << "accumulate_symmetric_product(SCMatrix*a): bad dim\n";
 | 
|---|
| 375 |     abort();
 | 
|---|
| 376 |   }
 | 
|---|
| 377 | 
 | 
|---|
| 378 |   int mxnb = (d->blocks()->nblock() > la->nblocks_)
 | 
|---|
| 379 |              ? d->blocks()->nblock()
 | 
|---|
| 380 |              : la->nblocks_;
 | 
|---|
| 381 |   int &mi = (d->blocks()->nblock()==1) ? zero : i;
 | 
|---|
| 382 | 
 | 
|---|
| 383 |   for (i=0; i < mxnb; i++)
 | 
|---|
| 384 |     if (mats_[mi].nonnull() && la->mats_[i].nonnull())
 | 
|---|
| 385 |       mats_[mi]->accumulate_symmetric_product(la->mats_[i].pointer());
 | 
|---|
| 386 | }
 | 
|---|
| 387 | 
 | 
|---|
| 388 | // computes this += a + a.t
 | 
|---|
| 389 | void
 | 
|---|
| 390 | BlockedSymmSCMatrix::accumulate_symmetric_sum(SCMatrix*a)
 | 
|---|
| 391 | {
 | 
|---|
| 392 |   // make sure that the argument is of the correct type
 | 
|---|
| 393 |   BlockedSCMatrix* la
 | 
|---|
| 394 |     = require_dynamic_cast<BlockedSCMatrix*>(a,"BlockedSymmSCMatrix::"
 | 
|---|
| 395 |                                           "accumulate_symmetric_sum");
 | 
|---|
| 396 | 
 | 
|---|
| 397 |   if (!dim()->equiv(la->rowdim()) || !dim()->equiv(la->coldim())) {
 | 
|---|
| 398 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::"
 | 
|---|
| 399 |          << "accumulate_symmetric_sum(SCMatrix*a): bad dim\n";
 | 
|---|
| 400 |     abort();
 | 
|---|
| 401 |   }
 | 
|---|
| 402 | 
 | 
|---|
| 403 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 404 |     if (mats_[i].nonnull())
 | 
|---|
| 405 |       mats_[i]->accumulate_symmetric_sum(la->mats_[i].pointer());
 | 
|---|
| 406 | }
 | 
|---|
| 407 | 
 | 
|---|
| 408 | void
 | 
|---|
| 409 | BlockedSymmSCMatrix::accumulate_symmetric_outer_product(SCVector*a)
 | 
|---|
| 410 | {
 | 
|---|
| 411 |   // make sure that the argument is of the correct type
 | 
|---|
| 412 |   BlockedSCVector* la
 | 
|---|
| 413 |     = require_dynamic_cast<BlockedSCVector*>(a,"BlockedSymmSCMatrix::"
 | 
|---|
| 414 |                                       "accumulate_symmetric_outer_product");
 | 
|---|
| 415 | 
 | 
|---|
| 416 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
| 417 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::"
 | 
|---|
| 418 |          << "accumulate_symmetric_outer_product(SCMatrix*a): bad dim\n";
 | 
|---|
| 419 |     abort();
 | 
|---|
| 420 |   }
 | 
|---|
| 421 | 
 | 
|---|
| 422 |   for (int i=0; i < d->blocks()->nblock(); i++)
 | 
|---|
| 423 |     if (mats_[i].nonnull())
 | 
|---|
| 424 |       mats_[i]->accumulate_symmetric_outer_product(la->vecs_[i].pointer());
 | 
|---|
| 425 | }
 | 
|---|
| 426 | 
 | 
|---|
| 427 | // this += a * b * transpose(a)
 | 
|---|
| 428 | void
 | 
|---|
| 429 | BlockedSymmSCMatrix::accumulate_transform(SCMatrix*a,SymmSCMatrix*b,
 | 
|---|
| 430 |                                           SCMatrix::Transform t)
 | 
|---|
| 431 | {
 | 
|---|
| 432 |   int i, zero=0;
 | 
|---|
| 433 |   
 | 
|---|
| 434 |   // do the necessary castdowns
 | 
|---|
| 435 |   BlockedSCMatrix*la
 | 
|---|
| 436 |     = require_dynamic_cast<BlockedSCMatrix*>(a,"%s::accumulate_transform",
 | 
|---|
| 437 |                                       class_name());
 | 
|---|
| 438 |   BlockedSymmSCMatrix*lb = require_dynamic_cast<BlockedSymmSCMatrix*>(
 | 
|---|
| 439 |       b,"%s::accumulate_transform", class_name());
 | 
|---|
| 440 | 
 | 
|---|
| 441 |   // check the dimensions
 | 
|---|
| 442 |   if (t == SCMatrix::NormalTransform) {
 | 
|---|
| 443 |       if (!dim()->equiv(la->rowdim()) || !lb->dim()->equiv(la->coldim())) {
 | 
|---|
| 444 |           ExEnv::outn() << indent << "BlockedSymmSCMatrix::accumulate_transform: bad dim (not transposed)\n";
 | 
|---|
| 445 |           ExEnv::outn() << "target dim:" << endl;
 | 
|---|
| 446 |           dim()->print();
 | 
|---|
| 447 |           ExEnv::outn() << "source dim" << endl;
 | 
|---|
| 448 |           b->dim()->print();
 | 
|---|
| 449 |           ExEnv::outn() << "transform dims" << endl;
 | 
|---|
| 450 |           a->rowdim()->print();
 | 
|---|
| 451 |           a->coldim()->print();
 | 
|---|
| 452 |           abort();
 | 
|---|
| 453 |         }
 | 
|---|
| 454 |     }
 | 
|---|
| 455 |   else {
 | 
|---|
| 456 |       if (!dim()->equiv(la->coldim()) || !lb->dim()->equiv(la->rowdim())) {
 | 
|---|
| 457 |           ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate_transform: bad dim\n";
 | 
|---|
| 458 |           abort();
 | 
|---|
| 459 |         }
 | 
|---|
| 460 |     }
 | 
|---|
| 461 | 
 | 
|---|
| 462 |   int mxnb = (d->blocks()->nblock() > la->nblocks_)
 | 
|---|
| 463 |              ? d->blocks()->nblock()
 | 
|---|
| 464 |              : la->nblocks_;
 | 
|---|
| 465 | 
 | 
|---|
| 466 |   int &mi = (d->blocks()->nblock()==1) ? zero : i;
 | 
|---|
| 467 |   int &bi = (lb->d->blocks()->nblock()==1) ? zero : i;
 | 
|---|
| 468 |   
 | 
|---|
| 469 |   for (i=0; i < mxnb; i++) {
 | 
|---|
| 470 |     if (mats_[mi].null() || la->mats_[i].null() || lb->mats_[bi].null())
 | 
|---|
| 471 |       continue;
 | 
|---|
| 472 |                              
 | 
|---|
| 473 |     mats_[mi]->accumulate_transform(la->mats_[i].pointer(),
 | 
|---|
| 474 |                                     lb->mats_[bi].pointer(),t);
 | 
|---|
| 475 |   }
 | 
|---|
| 476 | }
 | 
|---|
| 477 | 
 | 
|---|
| 478 | // this += a * b * transpose(a)
 | 
|---|
| 479 | void
 | 
|---|
| 480 | BlockedSymmSCMatrix::accumulate_transform(SCMatrix*a,DiagSCMatrix*b,
 | 
|---|
| 481 |                                           SCMatrix::Transform t)
 | 
|---|
| 482 | {
 | 
|---|
| 483 |   int i, zero=0;
 | 
|---|
| 484 |   
 | 
|---|
| 485 |   // do the necessary castdowns
 | 
|---|
| 486 |   BlockedSCMatrix*la
 | 
|---|
| 487 |     = require_dynamic_cast<BlockedSCMatrix*>(a,"%s::accumulate_transform",
 | 
|---|
| 488 |                                       class_name());
 | 
|---|
| 489 |   BlockedDiagSCMatrix*lb
 | 
|---|
| 490 |     = require_dynamic_cast<BlockedDiagSCMatrix*>(b,"%s::accumulate_transform",
 | 
|---|
| 491 |                                           class_name());
 | 
|---|
| 492 | 
 | 
|---|
| 493 |   // check the dimensions
 | 
|---|
| 494 |   if (!dim()->equiv(la->rowdim()) || !lb->dim()->equiv(la->coldim())) {
 | 
|---|
| 495 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix::accumulate_transform: bad dim\n";
 | 
|---|
| 496 |     abort();
 | 
|---|
| 497 |   }
 | 
|---|
| 498 | 
 | 
|---|
| 499 |   int mxnb = (d->blocks()->nblock() > la->nblocks_)
 | 
|---|
| 500 |              ? d->blocks()->nblock()
 | 
|---|
| 501 |              : la->nblocks_;
 | 
|---|
| 502 | 
 | 
|---|
| 503 |   int &mi = (d->blocks()->nblock()==1) ? zero : i;
 | 
|---|
| 504 |   int &bi = (lb->d->blocks()->nblock()==1) ? zero : i;
 | 
|---|
| 505 | 
 | 
|---|
| 506 |   for (i=0; i < mxnb; i++) {
 | 
|---|
| 507 |     if (mats_[mi].null() || la->mats_[i].null() || lb->mats_[bi].null())
 | 
|---|
| 508 |       continue;
 | 
|---|
| 509 |                              
 | 
|---|
| 510 |     mats_[mi]->accumulate_transform(la->mats_[i].pointer(),
 | 
|---|
| 511 |                                     lb->mats_[bi].pointer());
 | 
|---|
| 512 |   }
 | 
|---|
| 513 | }
 | 
|---|
| 514 | 
 | 
|---|
| 515 | void
 | 
|---|
| 516 | BlockedSymmSCMatrix::accumulate_transform(SymmSCMatrix*a,SymmSCMatrix*b)
 | 
|---|
| 517 | {
 | 
|---|
| 518 |   SymmSCMatrix::accumulate_transform(a,b);
 | 
|---|
| 519 | }
 | 
|---|
| 520 | 
 | 
|---|
| 521 | void
 | 
|---|
| 522 | BlockedSymmSCMatrix::element_op(const Ref<SCElementOp>& op)
 | 
|---|
| 523 | {
 | 
|---|
| 524 |   BlockedSCElementOp *bop = dynamic_cast<BlockedSCElementOp*>(op.pointer());
 | 
|---|
| 525 | 
 | 
|---|
| 526 |   int nb = d->blocks()->nblock();
 | 
|---|
| 527 |   
 | 
|---|
| 528 |   op->defer_collect(1);
 | 
|---|
| 529 |   for (int i=0; i < nb; i++) {
 | 
|---|
| 530 |     if (bop)
 | 
|---|
| 531 |       bop->working_on(i);
 | 
|---|
| 532 |     if (mats_[i].nonnull())
 | 
|---|
| 533 |       mats_[i]->element_op(op);
 | 
|---|
| 534 |   }
 | 
|---|
| 535 |   op->defer_collect(0);
 | 
|---|
| 536 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
| 537 | }
 | 
|---|
| 538 | 
 | 
|---|
| 539 | void
 | 
|---|
| 540 | BlockedSymmSCMatrix::element_op(const Ref<SCElementOp2>& op,
 | 
|---|
| 541 |                                 SymmSCMatrix* m)
 | 
|---|
| 542 | {
 | 
|---|
| 543 |   BlockedSymmSCMatrix *lm = require_dynamic_cast<BlockedSymmSCMatrix*>(m,
 | 
|---|
| 544 |                                           "BlockedSymSCMatrix::element_op");
 | 
|---|
| 545 |   if (!dim()->equiv(lm->dim())) {
 | 
|---|
| 546 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix: bad element_op\n";
 | 
|---|
| 547 |     abort();
 | 
|---|
| 548 |   }
 | 
|---|
| 549 | 
 | 
|---|
| 550 |   BlockedSCElementOp2 *bop = dynamic_cast<BlockedSCElementOp2*>(op.pointer());
 | 
|---|
| 551 | 
 | 
|---|
| 552 |   int nb = d->blocks()->nblock();
 | 
|---|
| 553 |   
 | 
|---|
| 554 |   op->defer_collect(1);
 | 
|---|
| 555 |   for (int i=0; i < nb; i++) {
 | 
|---|
| 556 |     if (bop)
 | 
|---|
| 557 |       bop->working_on(i);
 | 
|---|
| 558 |     if (mats_[i].nonnull())
 | 
|---|
| 559 |       mats_[i]->element_op(op,lm->mats_[i].pointer());
 | 
|---|
| 560 |   }
 | 
|---|
| 561 |   op->defer_collect(0);
 | 
|---|
| 562 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
| 563 | }
 | 
|---|
| 564 | 
 | 
|---|
| 565 | void
 | 
|---|
| 566 | BlockedSymmSCMatrix::element_op(const Ref<SCElementOp3>& op,
 | 
|---|
| 567 |                               SymmSCMatrix* m,SymmSCMatrix* n)
 | 
|---|
| 568 | {
 | 
|---|
| 569 |   BlockedSymmSCMatrix *lm = require_dynamic_cast<BlockedSymmSCMatrix*>(m,
 | 
|---|
| 570 |                                         "BlockedSymSCMatrix::element_op");
 | 
|---|
| 571 |   BlockedSymmSCMatrix *ln = require_dynamic_cast<BlockedSymmSCMatrix*>(n,
 | 
|---|
| 572 |                                         "BlockedSymSCMatrix::element_op");
 | 
|---|
| 573 | 
 | 
|---|
| 574 |   if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
 | 
|---|
| 575 |     ExEnv::errn() << indent << "BlockedSymmSCMatrix: bad element_op\n";
 | 
|---|
| 576 |     abort();
 | 
|---|
| 577 |   }
 | 
|---|
| 578 | 
 | 
|---|
| 579 |   BlockedSCElementOp3 *bop = dynamic_cast<BlockedSCElementOp3*>(op.pointer());
 | 
|---|
| 580 | 
 | 
|---|
| 581 |   int nb = d->blocks()->nblock();
 | 
|---|
| 582 |   
 | 
|---|
| 583 |   op->defer_collect(1);
 | 
|---|
| 584 |   for (int i=0; i < nb; i++) {
 | 
|---|
| 585 |     if (bop)
 | 
|---|
| 586 |       bop->working_on(i);
 | 
|---|
| 587 |     if (mats_[i].nonnull())
 | 
|---|
| 588 |       mats_[i]->element_op(op,lm->mats_[i].pointer(),
 | 
|---|
| 589 |                             ln->mats_[i].pointer());
 | 
|---|
| 590 |   }
 | 
|---|
| 591 |   op->defer_collect(0);
 | 
|---|
| 592 |   if (op->has_collect()) op->collect(messagegrp());
 | 
|---|
| 593 | }
 | 
|---|
| 594 | 
 | 
|---|
| 595 | void
 | 
|---|
| 596 | BlockedSymmSCMatrix::vprint(const char *title, ostream& os, int prec) const
 | 
|---|
| 597 | {
 | 
|---|
| 598 |   int len = (title) ? strlen(title) : 0;
 | 
|---|
| 599 |   char *newtitle = new char[len + 80];
 | 
|---|
| 600 | 
 | 
|---|
| 601 |   for (int i=0; i < d->blocks()->nblock(); i++) {
 | 
|---|
| 602 |     if (mats_[i].null())
 | 
|---|
| 603 |       continue;
 | 
|---|
| 604 |     
 | 
|---|
| 605 |     sprintf(newtitle,"%s:  block %d",title,i+1);
 | 
|---|
| 606 |     mats_[i]->print(newtitle, os, prec);
 | 
|---|
| 607 |   }
 | 
|---|
| 608 | 
 | 
|---|
| 609 |   delete[] newtitle;
 | 
|---|
| 610 | }
 | 
|---|
| 611 | 
 | 
|---|
| 612 | RefSCDimension
 | 
|---|
| 613 | BlockedSymmSCMatrix::dim(int i) const
 | 
|---|
| 614 | {
 | 
|---|
| 615 |   return d->blocks()->subdim(i);
 | 
|---|
| 616 | }
 | 
|---|
| 617 | 
 | 
|---|
| 618 | int
 | 
|---|
| 619 | BlockedSymmSCMatrix::nblocks() const
 | 
|---|
| 620 | {
 | 
|---|
| 621 |   return d->blocks()->nblock();
 | 
|---|
| 622 | }
 | 
|---|
| 623 | 
 | 
|---|
| 624 | RefSymmSCMatrix
 | 
|---|
| 625 | BlockedSymmSCMatrix::block(int i)
 | 
|---|
| 626 | {
 | 
|---|
| 627 |   return mats_[i];
 | 
|---|
| 628 | }
 | 
|---|
| 629 | 
 | 
|---|
| 630 | Ref<SCMatrixSubblockIter>
 | 
|---|
| 631 | BlockedSymmSCMatrix::local_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
| 632 | {
 | 
|---|
| 633 |   Ref<SCMatrixCompositeSubblockIter> iter
 | 
|---|
| 634 |       = new SCMatrixCompositeSubblockIter(access,nblocks());
 | 
|---|
| 635 |   for (int i=0; i<nblocks(); i++) {
 | 
|---|
| 636 |       if (block(i).null())
 | 
|---|
| 637 |           iter->set_iter(i, new SCMatrixNullSubblockIter(access));
 | 
|---|
| 638 |       else
 | 
|---|
| 639 |           iter->set_iter(i, block(i)->local_blocks(access));
 | 
|---|
| 640 |     }
 | 
|---|
| 641 |   Ref<SCMatrixSubblockIter> ret = iter.pointer();
 | 
|---|
| 642 |   return ret;
 | 
|---|
| 643 | }
 | 
|---|
| 644 | 
 | 
|---|
| 645 | Ref<SCMatrixSubblockIter>
 | 
|---|
| 646 | BlockedSymmSCMatrix::all_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
| 647 | {
 | 
|---|
| 648 |   Ref<SCMatrixCompositeSubblockIter> iter
 | 
|---|
| 649 |       = new SCMatrixCompositeSubblockIter(access,nblocks());
 | 
|---|
| 650 |   for (int i=0; i<nblocks(); i++) {
 | 
|---|
| 651 |       if (block(i).null())
 | 
|---|
| 652 |           iter->set_iter(i, new SCMatrixNullSubblockIter(access));
 | 
|---|
| 653 |       else
 | 
|---|
| 654 |           iter->set_iter(i, block(i)->all_blocks(access));
 | 
|---|
| 655 |     }
 | 
|---|
| 656 |   Ref<SCMatrixSubblockIter> ret = iter.pointer();
 | 
|---|
| 657 |   return ret;
 | 
|---|
| 658 | }
 | 
|---|
| 659 | 
 | 
|---|
| 660 | void
 | 
|---|
| 661 | BlockedSymmSCMatrix::save(StateOut&s)
 | 
|---|
| 662 | {
 | 
|---|
| 663 |   int ndim = n();
 | 
|---|
| 664 |   s.put(ndim);
 | 
|---|
| 665 |   int has_subblocks = 1;
 | 
|---|
| 666 |   s.put(has_subblocks);
 | 
|---|
| 667 |   s.put(nblocks());
 | 
|---|
| 668 |   for (int i=0; i<nblocks(); i++) {
 | 
|---|
| 669 |       block(i).save(s);
 | 
|---|
| 670 |     }
 | 
|---|
| 671 | }
 | 
|---|
| 672 | 
 | 
|---|
| 673 | void
 | 
|---|
| 674 | BlockedSymmSCMatrix::restore(StateIn& s)
 | 
|---|
| 675 | {
 | 
|---|
| 676 |   int ndimt, ndim = n();
 | 
|---|
| 677 |   s.get(ndimt);
 | 
|---|
| 678 |   if (ndimt != ndim) {
 | 
|---|
| 679 |       ExEnv::errn() << indent
 | 
|---|
| 680 |            << "BlockedSymmSCMatrix::restore(): bad dimension" << endl;
 | 
|---|
| 681 |       abort();
 | 
|---|
| 682 |     }
 | 
|---|
| 683 |   int has_subblocks;
 | 
|---|
| 684 |   s.get(has_subblocks);
 | 
|---|
| 685 |   if (has_subblocks) {
 | 
|---|
| 686 |       int nblock;
 | 
|---|
| 687 |       s.get(nblock);
 | 
|---|
| 688 |       if (nblock != nblocks()) {
 | 
|---|
| 689 |           ExEnv::errn() << indent
 | 
|---|
| 690 |                << "BlockedSymmSCMatrix::restore(): nblock differs\n" << endl;
 | 
|---|
| 691 |           abort();
 | 
|---|
| 692 |         }
 | 
|---|
| 693 |       for (int i=0; i<nblocks(); i++) {
 | 
|---|
| 694 |           block(i).restore(s);
 | 
|---|
| 695 |         }
 | 
|---|
| 696 |     }
 | 
|---|
| 697 |   else {
 | 
|---|
| 698 |       ExEnv::errn() << indent
 | 
|---|
| 699 |            << "BlockedSymmSCMatrix::restore(): no subblocks--cannot restore"
 | 
|---|
| 700 |            << endl;
 | 
|---|
| 701 |       abort();
 | 
|---|
| 702 |     }
 | 
|---|
| 703 | }
 | 
|---|
| 704 | 
 | 
|---|
| 705 | void
 | 
|---|
| 706 | BlockedSymmSCMatrix::convert_accumulate(SymmSCMatrix*a)
 | 
|---|
| 707 | {
 | 
|---|
| 708 |   // ok, are we converting a non-blocked matrix to a blocked one, or are
 | 
|---|
| 709 |   // we converting a blocked matrix from one specialization to another?
 | 
|---|
| 710 |   
 | 
|---|
| 711 |   if (dynamic_cast<BlockedSymmSCMatrix*>(a)) {
 | 
|---|
| 712 |     BlockedSymmSCMatrix *ba = dynamic_cast<BlockedSymmSCMatrix*>(a);
 | 
|---|
| 713 |     if (ba->nblocks() == this->nblocks()) {
 | 
|---|
| 714 |       for (int i=0; i < nblocks(); i++)
 | 
|---|
| 715 |         mats_[i]->convert_accumulate(ba->mats_[i]);
 | 
|---|
| 716 |     } else {
 | 
|---|
| 717 |       ExEnv::errn() << indent
 | 
|---|
| 718 |            << "BlockedSymmSCMatrix::convert_accumulate: "
 | 
|---|
| 719 |            << "can't convert from BlockedSymmSCMatrix with different nblock"
 | 
|---|
| 720 |            << endl;
 | 
|---|
| 721 |       abort();
 | 
|---|
| 722 |     }
 | 
|---|
| 723 |   }
 | 
|---|
| 724 |   else {
 | 
|---|
| 725 |     if (nblocks()==1) {
 | 
|---|
| 726 |       mats_[0]->convert_accumulate(a);
 | 
|---|
| 727 |     } else {
 | 
|---|
| 728 |       ExEnv::errn() << indent
 | 
|---|
| 729 |            << "BlockedSymmSCMatrix::convert_accumulate: "
 | 
|---|
| 730 |            << "can't convert from SymmSCMatrix when nblocks != 1"
 | 
|---|
| 731 |            << endl;
 | 
|---|
| 732 |       abort();
 | 
|---|
| 733 |     }
 | 
|---|
| 734 |   }
 | 
|---|
| 735 | }
 | 
|---|
| 736 | 
 | 
|---|
| 737 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 738 | 
 | 
|---|
| 739 | // Local Variables:
 | 
|---|
| 740 | // mode: c++
 | 
|---|
| 741 | // c-file-style: "CLJ"
 | 
|---|
| 742 | // End:
 | 
|---|