| [0b990d] | 1 | //
 | 
|---|
 | 2 | // localvect.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 <math/scmat/local.h>
 | 
|---|
 | 33 | #include <math/scmat/cmatrix.h>
 | 
|---|
 | 34 | #include <math/scmat/elemop.h>
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | using namespace std;
 | 
|---|
 | 37 | using namespace sc;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 40 | // LocalSCVector member functions
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | static ClassDesc LocalSCVector_cd(
 | 
|---|
 | 43 |   typeid(LocalSCVector),"LocalSCVector",1,"public SCVector",
 | 
|---|
 | 44 |   0, 0, 0);
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | LocalSCVector::LocalSCVector(const RefSCDimension&a,
 | 
|---|
 | 47 |                              LocalSCMatrixKit *kit):
 | 
|---|
 | 48 |   SCVector(a,kit)
 | 
|---|
 | 49 | {
 | 
|---|
 | 50 |   resize(a->n());
 | 
|---|
 | 51 | }
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | void
 | 
|---|
 | 54 | LocalSCVector::resize(int n)
 | 
|---|
 | 55 | {
 | 
|---|
 | 56 |   block = new SCVectorSimpleBlock(0,n);
 | 
|---|
 | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | LocalSCVector::~LocalSCVector()
 | 
|---|
 | 60 | {
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | double *
 | 
|---|
 | 64 | LocalSCVector::get_data()
 | 
|---|
 | 65 | {
 | 
|---|
 | 66 |   return block->data;
 | 
|---|
 | 67 | }
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | double
 | 
|---|
 | 70 | LocalSCVector::get_element(int i) const
 | 
|---|
 | 71 | {
 | 
|---|
 | 72 |   int size = block->iend - block->istart;
 | 
|---|
 | 73 |   if (i < 0 || i >= size) {
 | 
|---|
 | 74 |       ExEnv::errn() << indent << "LocalSCVector::get_element: bad index\n";
 | 
|---|
 | 75 |       abort();
 | 
|---|
 | 76 |     }
 | 
|---|
 | 77 |   return block->data[i];
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | void
 | 
|---|
 | 81 | LocalSCVector::set_element(int i,double a)
 | 
|---|
 | 82 | {
 | 
|---|
 | 83 |   int size = block->iend - block->istart;
 | 
|---|
 | 84 |   if (i < 0 || i >= size) {
 | 
|---|
 | 85 |       ExEnv::errn() << indent << "LocalSCVector::set_element: bad index\n";
 | 
|---|
 | 86 |       abort();
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 |   block->data[i] = a;
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | void
 | 
|---|
 | 92 | LocalSCVector::accumulate_element(int i,double a)
 | 
|---|
 | 93 | {
 | 
|---|
 | 94 |   int size = block->iend - block->istart;
 | 
|---|
 | 95 |   if (i < 0 || i >= size) {
 | 
|---|
 | 96 |       ExEnv::errn() << indent << "LocalSCVector::accumulate_element: bad index\n";
 | 
|---|
 | 97 |       abort();
 | 
|---|
 | 98 |     }
 | 
|---|
 | 99 |   block->data[i] += a;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | void
 | 
|---|
 | 103 | LocalSCVector::accumulate_product_rv(SCMatrix*a,SCVector*b)
 | 
|---|
 | 104 | {
 | 
|---|
 | 105 |   const char* name = "LocalSCVector::accumulate_product";
 | 
|---|
 | 106 |   // make sure that the arguments are of the correct type
 | 
|---|
 | 107 |   LocalSCMatrix* la = require_dynamic_cast<LocalSCMatrix*>(a,name);
 | 
|---|
 | 108 |   LocalSCVector* lb = require_dynamic_cast<LocalSCVector*>(b,name);
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   // make sure that the dimensions match
 | 
|---|
 | 111 |   if (!dim()->equiv(la->rowdim()) || !la->coldim()->equiv(lb->dim())) {
 | 
|---|
 | 112 |       ExEnv::errn() << indent
 | 
|---|
 | 113 |            << "LocalSCVector:: accumulate_product(SCMatrix*a,SCVector*b): "
 | 
|---|
 | 114 |            << "dimensions don't match\n";
 | 
|---|
 | 115 |       abort();
 | 
|---|
 | 116 |     }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |   cmat_mxm(la->rows, 0,
 | 
|---|
 | 119 |            &(lb->block->data), 1,
 | 
|---|
 | 120 |            &(block->data), 1,
 | 
|---|
 | 121 |            n(), la->ncol(), 1,
 | 
|---|
 | 122 |            1);
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void
 | 
|---|
 | 126 | LocalSCVector::accumulate_product_sv(SymmSCMatrix*a,SCVector*b)
 | 
|---|
 | 127 | {
 | 
|---|
 | 128 |   const char* name = "LocalSCVector::accumulate_product";
 | 
|---|
 | 129 |   // make sure that the arguments are of the correct type
 | 
|---|
 | 130 |   LocalSymmSCMatrix* la = require_dynamic_cast<LocalSymmSCMatrix*>(a,name);
 | 
|---|
 | 131 |   LocalSCVector* lb = require_dynamic_cast<LocalSCVector*>(b,name);
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   // make sure that the dimensions match
 | 
|---|
 | 134 |   if (!dim()->equiv(la->dim()) || !la->dim()->equiv(lb->dim())) {
 | 
|---|
 | 135 |       ExEnv::errn() << indent
 | 
|---|
 | 136 |            << "LocalSCVector:: accumulate_product(SymmSCMatrix*a,SCVector*b): "
 | 
|---|
 | 137 |            << "dimensions don't match\n";
 | 
|---|
 | 138 |       abort();
 | 
|---|
 | 139 |     }
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   double* thisdat = block->data;
 | 
|---|
 | 142 |   double** adat = la->rows;
 | 
|---|
 | 143 |   double* bdat = lb->block->data;
 | 
|---|
 | 144 |   double tmp;
 | 
|---|
 | 145 |   int n = dim()->n();
 | 
|---|
 | 146 |   int i, j;
 | 
|---|
 | 147 |   for (i=0; i<n; i++) {
 | 
|---|
 | 148 |       tmp = 0.0;
 | 
|---|
 | 149 |       for (j=0; j<=i; j++) {
 | 
|---|
 | 150 |           tmp += adat[i][j] * bdat[j];
 | 
|---|
 | 151 |         }
 | 
|---|
 | 152 |       for (; j<n; j++) {
 | 
|---|
 | 153 |           tmp += adat[j][i] * bdat[j];
 | 
|---|
 | 154 |         }
 | 
|---|
 | 155 |       thisdat[i] += tmp;
 | 
|---|
 | 156 |     }
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | void
 | 
|---|
 | 160 | LocalSCVector::accumulate(const SCVector*a)
 | 
|---|
 | 161 | {
 | 
|---|
 | 162 |   // make sure that the argument is of the correct type
 | 
|---|
 | 163 |   const LocalSCVector* la
 | 
|---|
 | 164 |     = require_dynamic_cast<const LocalSCVector*>(a,"LocalSCVector::accumulate");
 | 
|---|
 | 165 | 
 | 
|---|
 | 166 |   // make sure that the dimensions match
 | 
|---|
 | 167 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 168 |       ExEnv::errn() << indent << "LocalSCVector::accumulate(SCVector*a): "
 | 
|---|
 | 169 |            << "dimensions don't match\n";
 | 
|---|
 | 170 |       abort();
 | 
|---|
 | 171 |     }
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 |   int nelem = d->n();
 | 
|---|
 | 174 |   int i;
 | 
|---|
 | 175 |   for (i=0; i<nelem; i++) block->data[i] += la->block->data[i];
 | 
|---|
 | 176 | }
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 | void
 | 
|---|
 | 179 | LocalSCVector::accumulate(const SCMatrix*a)
 | 
|---|
 | 180 | {
 | 
|---|
 | 181 |   // make sure that the argument is of the correct type
 | 
|---|
 | 182 |   const LocalSCMatrix* la
 | 
|---|
 | 183 |     = require_dynamic_cast<const LocalSCMatrix*>(a,"LocalSCVector::accumulate");
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |   // make sure that the dimensions match
 | 
|---|
 | 186 |   if (!((la->rowdim()->equiv(dim()) && la->coldim()->n() == 1)
 | 
|---|
 | 187 |         || (la->coldim()->equiv(dim()) && la->rowdim()->n() == 1))) {
 | 
|---|
 | 188 |       ExEnv::errn() << indent << "LocalSCVector::accumulate(SCMatrix*a): "
 | 
|---|
 | 189 |            << "dimensions don't match\n";
 | 
|---|
 | 190 |       abort();
 | 
|---|
 | 191 |     }
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |   int nelem = d->n();
 | 
|---|
 | 194 |   int i;
 | 
|---|
 | 195 |   for (i=0; i<nelem; i++) block->data[i] += la->block->data[i];
 | 
|---|
 | 196 | }
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 | void
 | 
|---|
 | 199 | LocalSCVector::assign_val(double a)
 | 
|---|
 | 200 | {
 | 
|---|
 | 201 |   int nelem = d->n();
 | 
|---|
 | 202 |   int i;
 | 
|---|
 | 203 |   for (i=0; i<nelem; i++) block->data[i] = a;
 | 
|---|
 | 204 | }
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 | void
 | 
|---|
 | 207 | LocalSCVector::assign_v(SCVector*a)
 | 
|---|
 | 208 | {
 | 
|---|
 | 209 |   // make sure that the argument is of the correct type
 | 
|---|
 | 210 |   LocalSCVector* la
 | 
|---|
 | 211 |     = require_dynamic_cast<LocalSCVector*>(a,"LocalSCVector::assign_v");
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 |   // make sure that the dimensions match
 | 
|---|
 | 214 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 215 |       ExEnv::errn() << indent << "LocalSCVector::assign_v(SCVector*a): "
 | 
|---|
 | 216 |            << "dimensions don't match\n";
 | 
|---|
 | 217 |       abort();
 | 
|---|
 | 218 |     }
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 |   int nelem = d->n();
 | 
|---|
 | 221 |   int i;
 | 
|---|
 | 222 |   for (i=0; i<nelem; i++) block->data[i] = la->block->data[i];
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 | void
 | 
|---|
 | 226 | LocalSCVector::assign_p(const double*a)
 | 
|---|
 | 227 | {
 | 
|---|
 | 228 |   int nelem = d->n();
 | 
|---|
 | 229 |   int i;
 | 
|---|
 | 230 |   for (i=0; i<nelem; i++) block->data[i] = a[i];
 | 
|---|
 | 231 | }
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 | double
 | 
|---|
 | 234 | LocalSCVector::scalar_product(SCVector*a)
 | 
|---|
 | 235 | {
 | 
|---|
 | 236 |   // make sure that the argument is of the correct type
 | 
|---|
 | 237 |   LocalSCVector* la
 | 
|---|
 | 238 |     = require_dynamic_cast<LocalSCVector*>(a,"LocalSCVector::scalar_product");
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |   // make sure that the dimensions match
 | 
|---|
 | 241 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 242 |       ExEnv::errn() << indent << "LocalSCVector::scalar_product(SCVector*a): "
 | 
|---|
 | 243 |            << "dimensions don't match\n";
 | 
|---|
 | 244 |       abort();
 | 
|---|
 | 245 |     }
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |   int nelem = d->n();
 | 
|---|
 | 248 |   int i;
 | 
|---|
 | 249 |   double result = 0.0;
 | 
|---|
 | 250 |   for (i=0; i<nelem; i++) result += block->data[i] * la->block->data[i];
 | 
|---|
 | 251 |   return result;
 | 
|---|
 | 252 | }
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 | void
 | 
|---|
 | 255 | LocalSCVector::element_op(const Ref<SCElementOp>& op)
 | 
|---|
 | 256 | {
 | 
|---|
 | 257 |   op->process_spec_vsimp(block.pointer());
 | 
|---|
 | 258 | }
 | 
|---|
 | 259 | 
 | 
|---|
 | 260 | void
 | 
|---|
 | 261 | LocalSCVector::element_op(const Ref<SCElementOp2>& op,
 | 
|---|
 | 262 |                           SCVector* m)
 | 
|---|
 | 263 | {
 | 
|---|
 | 264 |   LocalSCVector *lm
 | 
|---|
 | 265 |       = require_dynamic_cast<LocalSCVector*>(m, "LocalSCVector::element_op");
 | 
|---|
 | 266 | 
 | 
|---|
 | 267 |   if (!dim()->equiv(lm->dim())) {
 | 
|---|
 | 268 |       ExEnv::errn() << indent << "LocalSCVector: bad element_op\n";
 | 
|---|
 | 269 |       abort();
 | 
|---|
 | 270 |     }
 | 
|---|
 | 271 |   op->process_spec_vsimp(block.pointer(), lm->block.pointer());
 | 
|---|
 | 272 | }
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 | void
 | 
|---|
 | 275 | LocalSCVector::element_op(const Ref<SCElementOp3>& op,
 | 
|---|
 | 276 |                           SCVector* m,SCVector* n)
 | 
|---|
 | 277 | {
 | 
|---|
 | 278 |   LocalSCVector *lm
 | 
|---|
 | 279 |       = require_dynamic_cast<LocalSCVector*>(m, "LocalSCVector::element_op");
 | 
|---|
 | 280 |   LocalSCVector *ln
 | 
|---|
 | 281 |       = require_dynamic_cast<LocalSCVector*>(n, "LocalSCVector::element_op");
 | 
|---|
 | 282 | 
 | 
|---|
 | 283 |   if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
 | 
|---|
 | 284 |       ExEnv::errn() << indent << "LocalSCVector: bad element_op\n";
 | 
|---|
 | 285 |       abort();
 | 
|---|
 | 286 |     }
 | 
|---|
 | 287 |   op->process_spec_vsimp(block.pointer(),
 | 
|---|
 | 288 |                          lm->block.pointer(), ln->block.pointer());
 | 
|---|
 | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | // from Ed Seidl at the NIH (with a bit of hacking)
 | 
|---|
 | 292 | void
 | 
|---|
 | 293 | LocalSCVector::vprint(const char *title, ostream& os, int prec) const
 | 
|---|
 | 294 | {
 | 
|---|
 | 295 |   int i;
 | 
|---|
 | 296 |   int lwidth;
 | 
|---|
 | 297 |   double max=this->maxabs();
 | 
|---|
 | 298 | 
 | 
|---|
 | 299 |   max = (max==0.0) ? 1.0 : log10(max);
 | 
|---|
 | 300 |   if (max < 0.0) max=1.0;
 | 
|---|
 | 301 | 
 | 
|---|
 | 302 |   lwidth = prec + 5 + (int) max;
 | 
|---|
 | 303 | 
 | 
|---|
 | 304 |   if (title)
 | 
|---|
 | 305 |     os << endl << indent << title << endl;
 | 
|---|
 | 306 |   else
 | 
|---|
 | 307 |     os << endl;
 | 
|---|
 | 308 | 
 | 
|---|
 | 309 |   if (n()==0) {
 | 
|---|
 | 310 |     os << indent << "empty vector\n";
 | 
|---|
 | 311 |     return;
 | 
|---|
 | 312 |   }
 | 
|---|
 | 313 | 
 | 
|---|
 | 314 |   for (i=0; i<n(); i++)
 | 
|---|
 | 315 |     os << indent
 | 
|---|
 | 316 |        << scprintf("%5d %*.*f\n",i+1,lwidth,prec,block->data[i]);
 | 
|---|
 | 317 |   os << endl;
 | 
|---|
 | 318 | 
 | 
|---|
 | 319 |   os.flush();
 | 
|---|
 | 320 | }
 | 
|---|
 | 321 | 
 | 
|---|
 | 322 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 323 | LocalSCVector::local_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 324 | {
 | 
|---|
 | 325 |   if (messagegrp()->n() > 1) {
 | 
|---|
 | 326 |       ExEnv::errn() << indent
 | 
|---|
 | 327 |            << "LocalSCVector::local_blocks: not valid for local matrices"
 | 
|---|
 | 328 |            << endl;
 | 
|---|
 | 329 |       abort();
 | 
|---|
 | 330 |     }
 | 
|---|
 | 331 |   Ref<SCMatrixSubblockIter> iter
 | 
|---|
 | 332 |       = new SCMatrixSimpleSubblockIter(access, block.pointer());
 | 
|---|
 | 333 |   return iter;
 | 
|---|
 | 334 | }
 | 
|---|
 | 335 | 
 | 
|---|
 | 336 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 337 | LocalSCVector::all_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 338 | {
 | 
|---|
 | 339 |   if (access == SCMatrixSubblockIter::Write) {
 | 
|---|
 | 340 |       ExEnv::errn() << indent << "LocalVectorSCMatrix::all_blocks: "
 | 
|---|
 | 341 |            << "Write access permitted for local blocks only"
 | 
|---|
 | 342 |            << endl;
 | 
|---|
 | 343 |       abort();
 | 
|---|
 | 344 |     }
 | 
|---|
 | 345 |   return local_blocks(access);
 | 
|---|
 | 346 | }
 | 
|---|
 | 347 | 
 | 
|---|
 | 348 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 349 | 
 | 
|---|
 | 350 | // Local Variables:
 | 
|---|
 | 351 | // mode: c++
 | 
|---|
 | 352 | // c-file-style: "CLJ"
 | 
|---|
 | 353 | // End:
 | 
|---|