| [0b990d] | 1 | //
 | 
|---|
 | 2 | // localdiag.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 | // LocalDiagSCMatrix member functions
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | static ClassDesc LocalDiagSCMatrix_cd(
 | 
|---|
 | 43 |   typeid(LocalDiagSCMatrix),"LocalDiagSCMatrix",1,"public DiagSCMatrix",
 | 
|---|
 | 44 |   0, 0, 0);
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | LocalDiagSCMatrix::LocalDiagSCMatrix(const RefSCDimension&a,
 | 
|---|
 | 47 |                                      LocalSCMatrixKit *kit):
 | 
|---|
 | 48 |   DiagSCMatrix(a,kit)
 | 
|---|
 | 49 | {
 | 
|---|
 | 50 |   resize(a->n());
 | 
|---|
 | 51 | }
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | LocalDiagSCMatrix::~LocalDiagSCMatrix()
 | 
|---|
 | 54 | {
 | 
|---|
 | 55 | }
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | void
 | 
|---|
 | 58 | LocalDiagSCMatrix::resize(int n)
 | 
|---|
 | 59 | {
 | 
|---|
 | 60 |   block = new SCMatrixDiagBlock(0,n);
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | double *
 | 
|---|
 | 64 | LocalDiagSCMatrix::get_data()
 | 
|---|
 | 65 | {
 | 
|---|
 | 66 |   return block->data;
 | 
|---|
 | 67 | }
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | double
 | 
|---|
 | 70 | LocalDiagSCMatrix::get_element(int i) const
 | 
|---|
 | 71 | {
 | 
|---|
 | 72 |   return block->data[i];
 | 
|---|
 | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | void
 | 
|---|
 | 76 | LocalDiagSCMatrix::set_element(int i,double a)
 | 
|---|
 | 77 | {
 | 
|---|
 | 78 |   block->data[i] = a;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | void
 | 
|---|
 | 82 | LocalDiagSCMatrix::accumulate_element(int i,double a)
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   block->data[i] += a;
 | 
|---|
 | 85 | }
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | void
 | 
|---|
 | 88 | LocalDiagSCMatrix::accumulate(const DiagSCMatrix*a)
 | 
|---|
 | 89 | {
 | 
|---|
 | 90 |   // make sure that the argument is of the correct type
 | 
|---|
 | 91 |   const LocalDiagSCMatrix* la
 | 
|---|
 | 92 |     = require_dynamic_cast<const LocalDiagSCMatrix*>(a,"LocalDiagSCMatrix::accumulate");
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |   // make sure that the dimensions match
 | 
|---|
 | 95 |   if (!dim()->equiv(la->dim())) {
 | 
|---|
 | 96 |       ExEnv::errn() << indent << "LocalDiagSCMatrix::accumulate(SCMatrix*a): "
 | 
|---|
 | 97 |            << "dimensions don't match\n";
 | 
|---|
 | 98 |       abort();
 | 
|---|
 | 99 |     }
 | 
|---|
 | 100 | 
 | 
|---|
 | 101 |   int nelem = n();
 | 
|---|
 | 102 |   for (int i=0; i<nelem; i++) block->data[i] += la->block->data[i];
 | 
|---|
 | 103 | }
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 | double
 | 
|---|
 | 106 | LocalDiagSCMatrix::invert_this()
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |   double det = 1.0;
 | 
|---|
 | 109 |   int nelem = n();
 | 
|---|
 | 110 |   double*data = block->data;
 | 
|---|
 | 111 |   for (int i=0; i<nelem; i++) {
 | 
|---|
 | 112 |       det *= data[i];
 | 
|---|
 | 113 |       data[i] = 1.0/data[i];
 | 
|---|
 | 114 |     }
 | 
|---|
 | 115 |   return det;
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | double
 | 
|---|
 | 119 | LocalDiagSCMatrix::determ_this()
 | 
|---|
 | 120 | {
 | 
|---|
 | 121 |   double det = 1.0;
 | 
|---|
 | 122 |   int nelem = n();
 | 
|---|
 | 123 |   double *data = block->data;
 | 
|---|
 | 124 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 125 |     det *= data[i];
 | 
|---|
 | 126 |   }
 | 
|---|
 | 127 |   return det;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | double
 | 
|---|
 | 131 | LocalDiagSCMatrix::trace()
 | 
|---|
 | 132 | {
 | 
|---|
 | 133 |   double det = 0;
 | 
|---|
 | 134 |   int nelem = n();
 | 
|---|
 | 135 |   double *data = block->data;
 | 
|---|
 | 136 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 137 |     det += data[i];
 | 
|---|
 | 138 |   }
 | 
|---|
 | 139 |   return det;
 | 
|---|
 | 140 | }
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 | void
 | 
|---|
 | 143 | LocalDiagSCMatrix::gen_invert_this()
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |   int nelem = n();
 | 
|---|
 | 146 |   double *data = block->data;
 | 
|---|
 | 147 |   for (int i=0; i < nelem; i++) {
 | 
|---|
 | 148 |     if (fabs(data[i]) > 1.0e-8)
 | 
|---|
 | 149 |       data[i] = 1.0/data[i];
 | 
|---|
 | 150 |     else
 | 
|---|
 | 151 |       data[i] = 0;
 | 
|---|
 | 152 |   }
 | 
|---|
 | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | void
 | 
|---|
 | 156 | LocalDiagSCMatrix::element_op(const Ref<SCElementOp>& op)
 | 
|---|
 | 157 | {
 | 
|---|
 | 158 |   op->process_spec_diag(block.pointer());
 | 
|---|
 | 159 | }
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 | void
 | 
|---|
 | 162 | LocalDiagSCMatrix::element_op(const Ref<SCElementOp2>& op,
 | 
|---|
 | 163 |                               DiagSCMatrix* m)
 | 
|---|
 | 164 | {
 | 
|---|
 | 165 |   LocalDiagSCMatrix *lm
 | 
|---|
 | 166 |       = require_dynamic_cast<LocalDiagSCMatrix*>(m,"LocalDiagSCMatrix::element_op");
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |   if (!dim()->equiv(lm->dim())) {
 | 
|---|
 | 169 |       ExEnv::errn() << indent << "LocalDiagSCMatrix: bad element_op\n";
 | 
|---|
 | 170 |       abort();
 | 
|---|
 | 171 |     }
 | 
|---|
 | 172 |   op->process_spec_diag(block.pointer(), lm->block.pointer());
 | 
|---|
 | 173 | }
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | void
 | 
|---|
 | 176 | LocalDiagSCMatrix::element_op(const Ref<SCElementOp3>& op,
 | 
|---|
 | 177 |                               DiagSCMatrix* m,DiagSCMatrix* n)
 | 
|---|
 | 178 | {
 | 
|---|
 | 179 |   LocalDiagSCMatrix *lm
 | 
|---|
 | 180 |       = require_dynamic_cast<LocalDiagSCMatrix*>(m,"LocalDiagSCMatrix::element_op");
 | 
|---|
 | 181 |   LocalDiagSCMatrix *ln
 | 
|---|
 | 182 |       = require_dynamic_cast<LocalDiagSCMatrix*>(n,"LocalDiagSCMatrix::element_op");
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 |   if (!dim()->equiv(lm->dim()) || !dim()->equiv(ln->dim())) {
 | 
|---|
 | 185 |       ExEnv::errn() << indent << "LocalDiagSCMatrix: bad element_op\n";
 | 
|---|
 | 186 |       abort();
 | 
|---|
 | 187 |     }
 | 
|---|
 | 188 |   op->process_spec_diag(block.pointer(),
 | 
|---|
 | 189 |                         lm->block.pointer(), ln->block.pointer());
 | 
|---|
 | 190 | }
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 | // from Ed Seidl at the NIH (with a bit of hacking)
 | 
|---|
 | 193 | void
 | 
|---|
 | 194 | LocalDiagSCMatrix::vprint(const char *title, ostream& os, int prec) const
 | 
|---|
 | 195 | {
 | 
|---|
 | 196 |   int i;
 | 
|---|
 | 197 |   int lwidth;
 | 
|---|
 | 198 |   double max=this->maxabs();
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 |   max = (max==0.0) ? 1.0 : log10(max);
 | 
|---|
 | 201 |   if (max < 0.0) max=1.0;
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |   lwidth = prec + 5 + (int) max;
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |   if (title)
 | 
|---|
 | 206 |     os << endl << indent << title << endl;
 | 
|---|
 | 207 |   else
 | 
|---|
 | 208 |     os << endl;
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 |   if (n()==0) {
 | 
|---|
 | 211 |     os << indent << "empty matrix\n";
 | 
|---|
 | 212 |     return;
 | 
|---|
 | 213 |   }
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   for (i=0; i<n(); i++)
 | 
|---|
 | 216 |     os << indent
 | 
|---|
 | 217 |        << scprintf("%5d %*.*f\n",i+1,lwidth,prec,block->data[i]);
 | 
|---|
 | 218 |   os << endl;
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 |   os.flush();
 | 
|---|
 | 221 | }
 | 
|---|
 | 222 | 
 | 
|---|
 | 223 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 224 | LocalDiagSCMatrix::local_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 225 | {
 | 
|---|
 | 226 |   if (messagegrp()->n() > 1) {
 | 
|---|
 | 227 |       ExEnv::errn() << indent
 | 
|---|
 | 228 |            << "LocalDiagSCMatrix::local_blocks: not valid for local matrices"
 | 
|---|
 | 229 |            << endl;
 | 
|---|
 | 230 |       abort();
 | 
|---|
 | 231 |     }
 | 
|---|
 | 232 |   Ref<SCMatrixSubblockIter> iter
 | 
|---|
 | 233 |       = new SCMatrixSimpleSubblockIter(access, block.pointer());
 | 
|---|
 | 234 |   return iter;
 | 
|---|
 | 235 | }
 | 
|---|
 | 236 | 
 | 
|---|
 | 237 | Ref<SCMatrixSubblockIter>
 | 
|---|
 | 238 | LocalDiagSCMatrix::all_blocks(SCMatrixSubblockIter::Access access)
 | 
|---|
 | 239 | {
 | 
|---|
 | 240 |   if (access == SCMatrixSubblockIter::Write) {
 | 
|---|
 | 241 |       ExEnv::errn() << indent << "LocalDiagSCMatrix::all_blocks: "
 | 
|---|
 | 242 |            << "Write access permitted for local blocks only"
 | 
|---|
 | 243 |            << endl;
 | 
|---|
 | 244 |       abort();
 | 
|---|
 | 245 |     }
 | 
|---|
 | 246 |   return local_blocks(access);
 | 
|---|
 | 247 | }
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
 | 250 | 
 | 
|---|
 | 251 | // Local Variables:
 | 
|---|
 | 252 | // mode: c++
 | 
|---|
 | 253 | // c-file-style: "CLJ"
 | 
|---|
 | 254 | // End:
 | 
|---|