[0b990d] | 1 | //
|
---|
| 2 | // localrect.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 | extern "C" {
|
---|
| 40 | int sing_(double *q, int *lq, int *iq, double *s, double *p,
|
---|
| 41 | int *lp, int *ip, double *a, int *la, int *m, int *n,
|
---|
| 42 | double *w);
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 46 | // LocalSCMatrix member functions
|
---|
| 47 |
|
---|
| 48 | static ClassDesc LocalSCMatrix_cd(
|
---|
| 49 | typeid(LocalSCMatrix),"LocalSCMatrix",1,"public SCMatrix",
|
---|
| 50 | 0, 0, 0);
|
---|
| 51 |
|
---|
| 52 | static double **
|
---|
| 53 | init_rect_rows(double *data, int ni,int nj)
|
---|
| 54 | {
|
---|
| 55 | double** r = new double*[ni];
|
---|
| 56 | int i;
|
---|
| 57 | for (i=0; i<ni; i++) r[i] = &data[i*nj];
|
---|
| 58 | return r;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | LocalSCMatrix::LocalSCMatrix(const RefSCDimension&a,const RefSCDimension&b,
|
---|
| 62 | LocalSCMatrixKit*kit):
|
---|
| 63 | SCMatrix(a,b,kit),
|
---|
| 64 | rows(0)
|
---|
| 65 | {
|
---|
| 66 | resize(a->n(),b->n());
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | LocalSCMatrix::~LocalSCMatrix()
|
---|
| 70 | {
|
---|
| 71 | if (rows) delete[] rows;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | int
|
---|
| 75 | LocalSCMatrix::compute_offset(int i,int j) const
|
---|
| 76 | {
|
---|
| 77 | if (i<0 || j<0 || i>=d1->n() || j>=d2->n()) {
|
---|
| 78 | ExEnv::errn() << indent << "LocalSCMatrix: index out of bounds\n";
|
---|
| 79 | abort();
|
---|
| 80 | }
|
---|
| 81 | return i*(d2->n()) + j;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void
|
---|
| 85 | LocalSCMatrix::resize(int nr, int nc)
|
---|
| 86 | {
|
---|
| 87 | block = new SCMatrixRectBlock(0,nr,0,nc);
|
---|
| 88 | if (rows) delete[] rows;
|
---|
| 89 | rows = init_rect_rows(block->data,nr,nc);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | double *
|
---|
| 93 | LocalSCMatrix::get_data()
|
---|
| 94 | {
|
---|
| 95 | return block->data;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | double **
|
---|
| 99 | LocalSCMatrix::get_rows()
|
---|
| 100 | {
|
---|
| 101 | return rows;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | double
|
---|
| 105 | LocalSCMatrix::get_element(int i,int j) const
|
---|
| 106 | {
|
---|
| 107 | int off = compute_offset(i,j);
|
---|
| 108 | return block->data[off];
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void
|
---|
| 112 | LocalSCMatrix::set_element(int i,int j,double a)
|
---|
| 113 | {
|
---|
| 114 | int off = compute_offset(i,j);
|
---|
| 115 | block->data[off] = a;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void
|
---|
| 119 | LocalSCMatrix::accumulate_element(int i,int j,double a)
|
---|
| 120 | {
|
---|
| 121 | int off = compute_offset(i,j);
|
---|
| 122 | block->data[off] += a;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | SCMatrix *
|
---|
| 126 | LocalSCMatrix::get_subblock(int br, int er, int bc, int ec)
|
---|
| 127 | {
|
---|
| 128 | int nsrow = er-br+1;
|
---|
| 129 | int nscol = ec-bc+1;
|
---|
| 130 |
|
---|
| 131 | if (nsrow > nrow() || nscol > ncol()) {
|
---|
| 132 | ExEnv::errn() << indent <<
|
---|
| 133 | "LocalSCMatrix::get_subblock: trying to get too big a subblock (" <<
|
---|
| 134 | nsrow << "," << nscol << ") from (" <<
|
---|
| 135 | nrow() << "," << ncol() << ")\n";
|
---|
| 136 | abort();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | RefSCDimension dnrow;
|
---|
| 140 | if (nsrow==nrow()) dnrow = rowdim();
|
---|
| 141 | else dnrow = new SCDimension(nsrow);
|
---|
| 142 |
|
---|
| 143 | RefSCDimension dncol;
|
---|
| 144 | if (nscol==ncol()) dncol = coldim();
|
---|
| 145 | else dncol = new SCDimension(nscol);
|
---|
| 146 |
|
---|
| 147 | SCMatrix * sb = kit()->matrix(dnrow,dncol);
|
---|
| 148 | sb->assign(0.0);
|
---|
| 149 |
|
---|
| 150 | LocalSCMatrix *lsb =
|
---|
| 151 | require_dynamic_cast<LocalSCMatrix*>(sb, "LocalSCMatrix::get_subblock");
|
---|
| 152 |
|
---|
| 153 | for (int i=0; i < nsrow; i++)
|
---|
| 154 | for (int j=0; j < nscol; j++)
|
---|
| 155 | lsb->rows[i][j] = rows[i+br][j+bc];
|
---|
| 156 |
|
---|
| 157 | return sb;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | void
|
---|
| 161 | LocalSCMatrix::assign_subblock(SCMatrix*sb, int br, int er, int bc, int ec,
|
---|
| 162 | int source_br, int source_bc)
|
---|
| 163 | {
|
---|
| 164 | LocalSCMatrix *lsb =
|
---|
| 165 | require_dynamic_cast<LocalSCMatrix*>(sb, "LocalSCMatrix::assign_subblock");
|
---|
| 166 |
|
---|
| 167 | int nsrow = er-br+1;
|
---|
| 168 | int nscol = ec-bc+1;
|
---|
| 169 |
|
---|
| 170 | if (nsrow > nrow() || nscol > ncol()) {
|
---|
| 171 | ExEnv::errn() << indent <<
|
---|
| 172 | "LocalSCMatrix::assign_subblock: trying to assign too big a " <<
|
---|
| 173 | "subblock (" << nsrow << "," << nscol << " to (" <<
|
---|
| 174 | nrow() << "," << ncol() << ")\n";
|
---|
| 175 | abort();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | for (int i=0; i < nsrow; i++)
|
---|
| 179 | for (int j=0; j < nscol; j++)
|
---|
| 180 | rows[i+br][j+bc] = lsb->rows[source_br + i][source_bc + j];
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void
|
---|
| 184 | LocalSCMatrix::accumulate_subblock(SCMatrix*sb, int br, int er, int bc, int ec,
|
---|
| 185 | int source_br, int source_bc)
|
---|
| 186 | {
|
---|
| 187 | LocalSCMatrix *lsb =
|
---|
| 188 | require_dynamic_cast<LocalSCMatrix*>(sb, "LocalSCMatrix::accumulate_subblock");
|
---|
| 189 |
|
---|
| 190 | int nsrow = er-br+1;
|
---|
| 191 | int nscol = ec-bc+1;
|
---|
| 192 |
|
---|
| 193 | if (nsrow > nrow() || nscol > ncol()) {
|
---|
| 194 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate_subblock: " <<
|
---|
| 195 | "trying to accumulate too big a subblock (" <<
|
---|
| 196 | nsrow << "," << nscol << " to (" <<
|
---|
| 197 | nrow() << "," << ncol() << ")\n";
|
---|
| 198 | abort();
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | for (int i=0; i < nsrow; i++)
|
---|
| 202 | for (int j=0; j < nscol; j++)
|
---|
| 203 | rows[i+br][j+bc] += lsb->rows[source_br + i][source_bc + j];
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | SCVector *
|
---|
| 207 | LocalSCMatrix::get_row(int i)
|
---|
| 208 | {
|
---|
| 209 | if (i >= nrow()) {
|
---|
| 210 | ExEnv::errn() << indent << "LocalSCMatrix::get_row: trying to get invalid row " <<
|
---|
| 211 | i << " max " << nrow() << endl;
|
---|
| 212 | abort();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | SCVector * v = kit()->vector(coldim());
|
---|
| 216 |
|
---|
| 217 | LocalSCVector *lv =
|
---|
| 218 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::get_row");
|
---|
| 219 |
|
---|
| 220 | for (int j=0; j < ncol(); j++)
|
---|
| 221 | lv->set_element(j,rows[i][j]);
|
---|
| 222 |
|
---|
| 223 | return v;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void
|
---|
| 227 | LocalSCMatrix::assign_row(SCVector *v, int i)
|
---|
| 228 | {
|
---|
| 229 | if (i >= nrow()) {
|
---|
| 230 | ExEnv::errn() << indent <<
|
---|
| 231 | "LocalSCMatrix::assign_row: trying to assign invalid row " <<
|
---|
| 232 | i << " max " << nrow() << endl;
|
---|
| 233 | abort();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | if (v->n() != ncol()) {
|
---|
| 237 | ExEnv::errn() << indent << "LocalSCMatrix::assign_row: vector is wrong size " <<
|
---|
| 238 | " is " << v->n() << ", should be " << ncol() << endl;
|
---|
| 239 | abort();
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | LocalSCVector *lv =
|
---|
| 243 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::assign_row");
|
---|
| 244 |
|
---|
| 245 | for (int j=0; j < ncol(); j++)
|
---|
| 246 | rows[i][j] = lv->get_element(j);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | void
|
---|
| 250 | LocalSCMatrix::accumulate_row(SCVector *v, int i)
|
---|
| 251 | {
|
---|
| 252 | if (i >= nrow()) {
|
---|
| 253 | ExEnv::errn() << indent <<
|
---|
| 254 | "LocalSCMatrix::accumulate_row: trying to assign invalid row " <<
|
---|
| 255 | i << " max " << nrow() << endl;
|
---|
| 256 | abort();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (v->n() != ncol()) {
|
---|
| 260 | ExEnv::errn() << indent <<
|
---|
| 261 | "LocalSCMatrix::accumulate_row: vector is wrong size " <<
|
---|
| 262 | "is " << v->n() << ", should be " << ncol() << endl;
|
---|
| 263 | abort();
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | LocalSCVector *lv =
|
---|
| 267 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::accumulate_row");
|
---|
| 268 |
|
---|
| 269 | for (int j=0; j < ncol(); j++)
|
---|
| 270 | rows[i][j] += lv->get_element(j);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | SCVector *
|
---|
| 274 | LocalSCMatrix::get_column(int i)
|
---|
| 275 | {
|
---|
| 276 | if (i >= ncol()) {
|
---|
| 277 | ExEnv::errn() << indent <<
|
---|
| 278 | "LocalSCMatrix::get_column: trying to get invalid column " <<
|
---|
| 279 | i << " max " << ncol() << endl;
|
---|
| 280 | abort();
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | SCVector * v = kit()->vector(rowdim());
|
---|
| 284 |
|
---|
| 285 | LocalSCVector *lv =
|
---|
| 286 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::get_column");
|
---|
| 287 |
|
---|
| 288 | for (int j=0; j < nrow(); j++)
|
---|
| 289 | lv->set_element(j,rows[j][i]);
|
---|
| 290 |
|
---|
| 291 | return v;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | void
|
---|
| 295 | LocalSCMatrix::assign_column(SCVector *v, int i)
|
---|
| 296 | {
|
---|
| 297 | if (i >= ncol()) {
|
---|
| 298 | ExEnv::errn() << indent <<
|
---|
| 299 | "LocalSCMatrix::assign_column: trying to assign invalid column " <<
|
---|
| 300 | i << " max " << ncol() << endl;
|
---|
| 301 | abort();
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | if (v->n() != nrow()) {
|
---|
| 305 | ExEnv::errn() << indent <<
|
---|
| 306 | "LocalSCMatrix::assign_column: vector is wrong size " <<
|
---|
| 307 | "is " << v->n() << ", should be " << nrow() << endl;
|
---|
| 308 | abort();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | LocalSCVector *lv =
|
---|
| 312 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::assign_column");
|
---|
| 313 |
|
---|
| 314 | for (int j=0; j < nrow(); j++)
|
---|
| 315 | rows[j][i] = lv->get_element(j);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | void
|
---|
| 319 | LocalSCMatrix::accumulate_column(SCVector *v, int i)
|
---|
| 320 | {
|
---|
| 321 | if (i >= ncol()) {
|
---|
| 322 | ExEnv::errn() << indent <<
|
---|
| 323 | "LocalSCMatrix::accumulate_column: trying to assign invalid column "
|
---|
| 324 | << i << " max " << ncol() << endl;
|
---|
| 325 | abort();
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | if (v->n() != nrow()) {
|
---|
| 329 | ExEnv::errn() << indent <<
|
---|
| 330 | "LocalSCMatrix::accumulate_column: vector is wrong size " <<
|
---|
| 331 | "is " << v->n() << ", should be " << nrow() << endl;
|
---|
| 332 | abort();
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | LocalSCVector *lv =
|
---|
| 336 | require_dynamic_cast<LocalSCVector*>(v, "LocalSCMatrix::accumulate_column");
|
---|
| 337 |
|
---|
| 338 | for (int j=0; j < nrow(); j++)
|
---|
| 339 | rows[j][i] += lv->get_element(j);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | void
|
---|
| 343 | LocalSCMatrix::assign_val(double a)
|
---|
| 344 | {
|
---|
| 345 | int n = d1->n() * d2->n();
|
---|
| 346 | double *data = block->data;
|
---|
| 347 | for (int i=0; i<n; i++) data[i] = a;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | void
|
---|
| 351 | LocalSCMatrix::accumulate_product_rr(SCMatrix*a,SCMatrix*b)
|
---|
| 352 | {
|
---|
| 353 | const char* name = "LocalSCMatrix::accumulate_product";
|
---|
| 354 | // make sure that the arguments are of the correct type
|
---|
| 355 | LocalSCMatrix* la = require_dynamic_cast<LocalSCMatrix*>(a,name);
|
---|
| 356 | LocalSCMatrix* lb = require_dynamic_cast<LocalSCMatrix*>(b,name);
|
---|
| 357 |
|
---|
| 358 | // make sure that the dimensions match
|
---|
| 359 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->coldim()) ||
|
---|
| 360 | !la->coldim()->equiv(lb->rowdim())) {
|
---|
| 361 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate_product: bad dim" << endl;
|
---|
| 362 | ExEnv::errn() << indent << "this row and col dimension:" << endl;
|
---|
| 363 | rowdim()->print(ExEnv::errn());
|
---|
| 364 | coldim()->print(ExEnv::errn());
|
---|
| 365 | ExEnv::errn() << indent << "a row and col dimension:" << endl;
|
---|
| 366 | a->rowdim()->print(ExEnv::errn());
|
---|
| 367 | a->coldim()->print(ExEnv::errn());
|
---|
| 368 | ExEnv::errn() << indent << "b row and col dimension:" << endl;
|
---|
| 369 | b->rowdim()->print(ExEnv::errn());
|
---|
| 370 | b->coldim()->print(ExEnv::errn());
|
---|
| 371 | abort();
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | cmat_mxm(la->rows, 0,
|
---|
| 375 | lb->rows, 0,
|
---|
| 376 | rows, 0,
|
---|
| 377 | nrow(), la->ncol(), this->ncol(),
|
---|
| 378 | 1);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | // does the outer product a x b. this must have rowdim() == a->dim() and
|
---|
| 382 | // coldim() == b->dim()
|
---|
| 383 | void
|
---|
| 384 | LocalSCMatrix::accumulate_outer_product(SCVector*a,SCVector*b)
|
---|
| 385 | {
|
---|
| 386 | const char* name = "LocalSCMatrix::accumulate_outer_product";
|
---|
| 387 | // make sure that the arguments are of the correct type
|
---|
| 388 | LocalSCVector* la = require_dynamic_cast<LocalSCVector*>(a,name);
|
---|
| 389 | LocalSCVector* lb = require_dynamic_cast<LocalSCVector*>(b,name);
|
---|
| 390 |
|
---|
| 391 | // make sure that the dimensions match
|
---|
| 392 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(lb->dim())) {
|
---|
| 393 | ExEnv::errn() << indent <<
|
---|
| 394 | "LocalSCMatrix::accumulate_outer_product(SCVector*a,SCVector*b): " <<
|
---|
| 395 | "dimensions don't match" << endl;
|
---|
| 396 | abort();
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | int nr = a->n();
|
---|
| 400 | int nc = b->n();
|
---|
| 401 | int i, j;
|
---|
| 402 | double* adat = la->block->data;
|
---|
| 403 | double* bdat = lb->block->data;
|
---|
| 404 | double** thisdat = rows;
|
---|
| 405 | for (i=0; i<nr; i++) {
|
---|
| 406 | for (j=0; j<nc; j++) {
|
---|
| 407 | thisdat[i][j] += adat[i] * bdat[j];
|
---|
| 408 | }
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | void
|
---|
| 413 | LocalSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b)
|
---|
| 414 | {
|
---|
| 415 | const char* name = "LocalSCMatrix::accumulate_product";
|
---|
| 416 | // make sure that the arguments are of the correct type
|
---|
| 417 | LocalSCMatrix* la = require_dynamic_cast<LocalSCMatrix*>(a,name);
|
---|
| 418 | LocalSymmSCMatrix* lb = require_dynamic_cast<LocalSymmSCMatrix*>(b,name);
|
---|
| 419 |
|
---|
| 420 | // make sure that the dimensions match
|
---|
| 421 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) ||
|
---|
| 422 | !la->coldim()->equiv(lb->dim())) {
|
---|
| 423 | ExEnv::errn() << indent <<
|
---|
| 424 | "LocalSCMatrix::accumulate_product_rs(SCMatrix*a,SymmSCMatrix*b): " <<
|
---|
| 425 | "dimensions don't match" << endl;
|
---|
| 426 | abort();
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | double **cd = rows;
|
---|
| 430 | double **ad = la->rows;
|
---|
| 431 | double **bd = lb->rows;
|
---|
| 432 | int ni = a->rowdim().n();
|
---|
| 433 | int njk = b->dim().n();
|
---|
| 434 | int i, j, k;
|
---|
| 435 | for (i=0; i<ni; i++) {
|
---|
| 436 | for (j=0; j<njk; j++) {
|
---|
| 437 | for (k=0; k<=j; k++) {
|
---|
| 438 | cd[i][k] += ad[i][j]*bd[j][k];
|
---|
| 439 | }
|
---|
| 440 | for (; k<njk; k++) {
|
---|
| 441 | cd[i][k] += ad[i][j]*bd[k][j];
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | void
|
---|
| 448 | LocalSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b)
|
---|
| 449 | {
|
---|
| 450 | const char* name = "LocalSCMatrix::accumulate_product_rd";
|
---|
| 451 | // make sure that the arguments are of the correct type
|
---|
| 452 | LocalSCMatrix* la = require_dynamic_cast<LocalSCMatrix*>(a,name);
|
---|
| 453 | LocalDiagSCMatrix* lb = require_dynamic_cast<LocalDiagSCMatrix*>(b,name);
|
---|
| 454 |
|
---|
| 455 | // make sure that the dimensions match
|
---|
| 456 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(lb->dim()) ||
|
---|
| 457 | !la->coldim()->equiv(lb->dim())) {
|
---|
| 458 | ExEnv::errn() << indent <<
|
---|
| 459 | "LocalSCMatrix::accumulate_product_rd(SCMatrix*a,DiagSCMatrix*b): " <<
|
---|
| 460 | "dimensions don't match" << endl;
|
---|
| 461 | abort();
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | double **cd = rows;
|
---|
| 465 | double **ad = la->rows;
|
---|
| 466 | double *bd = lb->block->data;
|
---|
| 467 | int ni = a->rowdim().n();
|
---|
| 468 | int nj = b->dim().n();
|
---|
| 469 | int i, j;
|
---|
| 470 | for (i=0; i<ni; i++) {
|
---|
| 471 | for (j=0; j<nj; j++) {
|
---|
| 472 | cd[i][j] += ad[i][j]*bd[j];
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | void
|
---|
| 478 | LocalSCMatrix::accumulate(const SCMatrix*a)
|
---|
| 479 | {
|
---|
| 480 | // make sure that the arguments is of the correct type
|
---|
| 481 | const LocalSCMatrix* la
|
---|
| 482 | = require_dynamic_cast<const LocalSCMatrix*>(a,"LocalSCMatrix::accumulate");
|
---|
| 483 |
|
---|
| 484 | // make sure that the dimensions match
|
---|
| 485 | if (!rowdim()->equiv(la->rowdim()) || !coldim()->equiv(la->coldim())) {
|
---|
| 486 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate(SCMatrix*a): " <<
|
---|
| 487 | "dimensions don't match" << endl;
|
---|
| 488 | abort();
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | int nelem = this->ncol() * this->nrow();
|
---|
| 492 | int i;
|
---|
| 493 | for (i=0; i<nelem; i++) block->data[i] += la->block->data[i];
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | void
|
---|
| 497 | LocalSCMatrix::accumulate(const SymmSCMatrix*a)
|
---|
| 498 | {
|
---|
| 499 | // make sure that the arguments is of the correct type
|
---|
| 500 | const LocalSymmSCMatrix* la
|
---|
| 501 | = require_dynamic_cast<const LocalSymmSCMatrix*>(a,"LocalSCMatrix::accumulate");
|
---|
| 502 |
|
---|
| 503 | // make sure that the dimensions match
|
---|
| 504 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) {
|
---|
| 505 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate(SymmSCMatrix*a): " <<
|
---|
| 506 | "dimensions don't match" << endl;
|
---|
| 507 | abort();
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | int n = this->ncol();
|
---|
| 511 | double *dat = la->block->data;
|
---|
| 512 | int i, j;
|
---|
| 513 | for (i=0; i<n; i++) {
|
---|
| 514 | for (j=0; j<i; j++) {
|
---|
| 515 | double tmp = *dat;
|
---|
| 516 | block->data[i*n+j] += tmp;
|
---|
| 517 | block->data[j*n+i] += tmp;
|
---|
| 518 | dat++;
|
---|
| 519 | }
|
---|
| 520 | block->data[i*n+i] += *dat++;
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | void
|
---|
| 525 | LocalSCMatrix::accumulate(const DiagSCMatrix*a)
|
---|
| 526 | {
|
---|
| 527 | // make sure that the arguments is of the correct type
|
---|
| 528 | const LocalDiagSCMatrix* la
|
---|
| 529 | = require_dynamic_cast<const LocalDiagSCMatrix*>(a,"LocalSCMatrix::accumulate");
|
---|
| 530 |
|
---|
| 531 | // make sure that the dimensions match
|
---|
| 532 | if (!rowdim()->equiv(la->dim()) || !coldim()->equiv(la->dim())) {
|
---|
| 533 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate(DiagSCMatrix*a): " <<
|
---|
| 534 | "dimensions don't match\n";
|
---|
| 535 | abort();
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | int n = this->ncol();
|
---|
| 539 | double *dat = la->block->data;
|
---|
| 540 | int i;
|
---|
| 541 | for (i=0; i<n; i++) {
|
---|
| 542 | block->data[i*n+i] += *dat++;
|
---|
| 543 | }
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | void
|
---|
| 547 | LocalSCMatrix::accumulate(const SCVector*a)
|
---|
| 548 | {
|
---|
| 549 | // make sure that the arguments is of the correct type
|
---|
| 550 | const LocalSCVector* la
|
---|
| 551 | = require_dynamic_cast<const LocalSCVector*>(a,"LocalSCVector::accumulate");
|
---|
| 552 |
|
---|
| 553 | // make sure that the dimensions match
|
---|
| 554 | if (!((rowdim()->equiv(la->dim()) && coldim()->n() == 1)
|
---|
| 555 | || (coldim()->equiv(la->dim()) && rowdim()->n() == 1))) {
|
---|
| 556 | ExEnv::errn() << indent << "LocalSCMatrix::accumulate(SCVector*a): " <<
|
---|
| 557 | "dimensions don't match" << endl;
|
---|
| 558 | abort();
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | int n = this->ncol();
|
---|
| 562 | double *dat = la->block->data;
|
---|
| 563 | int i;
|
---|
| 564 | for (i=0; i<n; i++) {
|
---|
| 565 | block->data[i*n+i] += *dat++;
|
---|
| 566 | }
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | void
|
---|
| 570 | LocalSCMatrix::transpose_this()
|
---|
| 571 | {
|
---|
| 572 | cmat_transpose_matrix(rows,nrow(),ncol());
|
---|
| 573 | delete[] rows;
|
---|
| 574 | rows = new double*[ncol()];
|
---|
| 575 | cmat_matrix_pointers(rows,block->data,ncol(),nrow());
|
---|
| 576 | RefSCDimension tmp = d1;
|
---|
| 577 | d1 = d2;
|
---|
| 578 | d2 = tmp;
|
---|
| 579 |
|
---|
| 580 | int itmp = block->istart;
|
---|
| 581 | block->istart = block->jstart;
|
---|
| 582 | block->jstart = itmp;
|
---|
| 583 |
|
---|
| 584 | itmp = block->iend;
|
---|
| 585 | block->iend = block->jend;
|
---|
| 586 | block->jend = itmp;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | double
|
---|
| 590 | LocalSCMatrix::invert_this()
|
---|
| 591 | {
|
---|
| 592 | if (nrow() != ncol()) {
|
---|
| 593 | ExEnv::errn() << indent << "LocalSCMatrix::invert_this: matrix is not square\n";
|
---|
| 594 | abort();
|
---|
| 595 | }
|
---|
| 596 | return cmat_invert(rows,0,nrow());
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | double
|
---|
| 600 | LocalSCMatrix::determ_this()
|
---|
| 601 | {
|
---|
| 602 | if (nrow() != ncol()) {
|
---|
| 603 | ExEnv::errn() << indent << "LocalSCMatrix::determ_this: matrix is not square\n";
|
---|
| 604 | abort();
|
---|
| 605 | }
|
---|
| 606 | return cmat_determ(rows,0,nrow());
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | double
|
---|
| 610 | LocalSCMatrix::trace()
|
---|
| 611 | {
|
---|
| 612 | if (nrow() != ncol()) {
|
---|
| 613 | ExEnv::errn() << indent << "LocalSCMatrix::trace: matrix is not square\n";
|
---|
| 614 | abort();
|
---|
| 615 | }
|
---|
| 616 | double ret=0;
|
---|
| 617 | int i;
|
---|
| 618 | for (i=0; i < nrow(); i++)
|
---|
| 619 | ret += rows[i][i];
|
---|
| 620 | return ret;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | void
|
---|
| 624 | LocalSCMatrix::svd_this(SCMatrix *U, DiagSCMatrix *sigma, SCMatrix *V)
|
---|
| 625 | {
|
---|
| 626 | LocalSCMatrix* lU =
|
---|
| 627 | require_dynamic_cast<LocalSCMatrix*>(U,"LocalSCMatrix::svd_this");
|
---|
| 628 | LocalSCMatrix* lV =
|
---|
| 629 | require_dynamic_cast<LocalSCMatrix*>(V,"LocalSCMatrix::svd_this");
|
---|
| 630 | LocalDiagSCMatrix* lsigma =
|
---|
| 631 | require_dynamic_cast<LocalDiagSCMatrix*>(sigma,"LocalSCMatrix::svd_this");
|
---|
| 632 |
|
---|
| 633 | RefSCDimension mdim = rowdim();
|
---|
| 634 | RefSCDimension ndim = coldim();
|
---|
| 635 | int m = mdim.n();
|
---|
| 636 | int n = ndim.n();
|
---|
| 637 |
|
---|
| 638 | RefSCDimension pdim;
|
---|
| 639 | if (m == n && m == sigma->dim().n())
|
---|
| 640 | pdim = sigma->dim();
|
---|
| 641 | else if (m<n)
|
---|
| 642 | pdim = mdim;
|
---|
| 643 | else
|
---|
| 644 | pdim = ndim;
|
---|
| 645 |
|
---|
| 646 | int p = pdim.n();
|
---|
| 647 |
|
---|
| 648 | if (!mdim->equiv(lU->rowdim()) ||
|
---|
| 649 | !mdim->equiv(lU->coldim()) ||
|
---|
| 650 | !ndim->equiv(lV->rowdim()) ||
|
---|
| 651 | !ndim->equiv(lV->coldim()) ||
|
---|
| 652 | !pdim->equiv(sigma->dim())) {
|
---|
| 653 | ExEnv::errn() << indent << "LocalSCMatrix: svd_this: dimension mismatch\n";
|
---|
| 654 | abort();
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | // form a fortran style matrix for the SVD routines
|
---|
| 658 | double *dA = new double[m*n];
|
---|
| 659 | double *dU = new double[m*m];
|
---|
| 660 | double *dV = new double[n*n];
|
---|
| 661 | double *dsigma = new double[n];
|
---|
| 662 | double *w = new double[(3*p-1>m)?(3*p-1):m];
|
---|
| 663 |
|
---|
| 664 | int i,j;
|
---|
| 665 | for (i=0; i<m; i++) {
|
---|
| 666 | for (j=0; j<n; j++) {
|
---|
| 667 | dA[i + j*m] = this->block->data[i*n + j];
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | int three = 3;
|
---|
| 672 |
|
---|
| 673 | sing_(dU, &m, &three, dsigma, dV, &n, &three, dA, &m, &m, &n, w);
|
---|
| 674 |
|
---|
| 675 | for (i=0; i<m; i++) {
|
---|
| 676 | for (j=0; j<m; j++) {
|
---|
| 677 | lU->block->data[i*m + j] = dU[i + j*m];
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | for (i=0; i<n; i++) {
|
---|
| 682 | for (j=0; j<n; j++) {
|
---|
| 683 | lV->block->data[i*n + j] = dV[i + j*n];
|
---|
| 684 | }
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | for (i=0; i<p; i++) {
|
---|
| 688 | lsigma->block->data[i] = dsigma[i];
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | delete[] dA;
|
---|
| 692 | delete[] dU;
|
---|
| 693 | delete[] dV;
|
---|
| 694 | delete[] dsigma;
|
---|
| 695 | delete[] w;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | double
|
---|
| 699 | LocalSCMatrix::solve_this(SCVector*v)
|
---|
| 700 | {
|
---|
| 701 | LocalSCVector* lv =
|
---|
| 702 | require_dynamic_cast<LocalSCVector*>(v,"LocalSCMatrix::solve_this");
|
---|
| 703 |
|
---|
| 704 | // make sure that the dimensions match
|
---|
| 705 | if (!rowdim()->equiv(lv->dim())) {
|
---|
| 706 | ExEnv::errn() << indent << "LocalSCMatrix::solve_this(SCVector*v): " <<
|
---|
| 707 | "dimensions don't match" << endl;
|
---|
| 708 | abort();
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | return cmat_solve_lin(rows,0,lv->block->data,nrow());
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | void
|
---|
| 715 | LocalSCMatrix::schmidt_orthog(SymmSCMatrix *S, int nc)
|
---|
| 716 | {
|
---|
| 717 | LocalSymmSCMatrix* lS =
|
---|
| 718 | require_dynamic_cast<LocalSymmSCMatrix*>(S,"LocalSCMatrix::schmidt_orthog");
|
---|
| 719 |
|
---|
| 720 | // make sure that the dimensions match
|
---|
| 721 | if (!rowdim()->equiv(lS->dim())) {
|
---|
| 722 | ExEnv::errn() << indent << "LocalSCMatrix::schmidt_orthog(): " <<
|
---|
| 723 | "dimensions don't match\n";
|
---|
| 724 | abort();
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | cmat_schmidt(rows,lS->block->data,nrow(),nc);
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | int
|
---|
| 731 | LocalSCMatrix::schmidt_orthog_tol(SymmSCMatrix *S, double tol, double *res)
|
---|
| 732 | {
|
---|
| 733 | LocalSymmSCMatrix* lS =
|
---|
| 734 | require_dynamic_cast<LocalSymmSCMatrix*>(S,"LocalSCMatrix::schmidt_orthog");
|
---|
| 735 |
|
---|
| 736 | // make sure that the dimensions match
|
---|
| 737 | if (!rowdim()->equiv(lS->dim())) {
|
---|
| 738 | ExEnv::errn() << indent << "LocalSCMatrix::schmidt_orthog(): " <<
|
---|
| 739 | "dimensions don't match\n";
|
---|
| 740 | abort();
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | return cmat_schmidt_tol(rows,lS->block->data,nrow(),ncol(),tol,res);
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | void
|
---|
| 747 | LocalSCMatrix::element_op(const Ref<SCElementOp>& op)
|
---|
| 748 | {
|
---|
| 749 | op->process_spec_rect(block.pointer());
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | void
|
---|
| 753 | LocalSCMatrix::element_op(const Ref<SCElementOp2>& op,
|
---|
| 754 | SCMatrix* m)
|
---|
| 755 | {
|
---|
| 756 | LocalSCMatrix *lm
|
---|
| 757 | = require_dynamic_cast<LocalSCMatrix*>(m,"LocalSCMatrix::element_op");
|
---|
| 758 |
|
---|
| 759 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim())) {
|
---|
| 760 | ExEnv::errn() << indent << "LocalSCMatrix: bad element_op\n";
|
---|
| 761 | abort();
|
---|
| 762 | }
|
---|
| 763 | op->process_spec_rect(block.pointer(), lm->block.pointer());
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | void
|
---|
| 767 | LocalSCMatrix::element_op(const Ref<SCElementOp3>& op,
|
---|
| 768 | SCMatrix* m,SCMatrix* n)
|
---|
| 769 | {
|
---|
| 770 | LocalSCMatrix *lm
|
---|
| 771 | = require_dynamic_cast<LocalSCMatrix*>(m,"LocalSCMatrix::element_op");
|
---|
| 772 | LocalSCMatrix *ln
|
---|
| 773 | = require_dynamic_cast<LocalSCMatrix*>(n,"LocalSCMatrix::element_op");
|
---|
| 774 |
|
---|
| 775 | if (!rowdim()->equiv(lm->rowdim()) || !coldim()->equiv(lm->coldim()) ||
|
---|
| 776 | !rowdim()->equiv(ln->rowdim()) || !coldim()->equiv(ln->coldim())) {
|
---|
| 777 | ExEnv::errn() << indent << "LocalSCMatrix: bad element_op\n";
|
---|
| 778 | abort();
|
---|
| 779 | }
|
---|
| 780 | op->process_spec_rect(block.pointer(),
|
---|
| 781 | lm->block.pointer(), ln->block.pointer());
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | // from Ed Seidl at the NIH
|
---|
| 785 | void
|
---|
| 786 | LocalSCMatrix::vprint(const char *title, ostream& os, int prec) const
|
---|
| 787 | {
|
---|
| 788 | int ii,jj,kk,nn;
|
---|
| 789 | int i,j;
|
---|
| 790 | int lwidth,width;
|
---|
| 791 | double max=this->maxabs();
|
---|
| 792 |
|
---|
| 793 | max = (max==0.0) ? 1.0 : log10(max);
|
---|
| 794 | if (max < 0.0) max=1.0;
|
---|
| 795 |
|
---|
| 796 | lwidth = prec + 5 + (int) max;
|
---|
| 797 | width = 75/(lwidth+SCFormIO::getindent(os));
|
---|
| 798 |
|
---|
| 799 | if (title)
|
---|
| 800 | os << endl << indent << title << endl;
|
---|
| 801 | else
|
---|
| 802 | os << endl;
|
---|
| 803 |
|
---|
| 804 | if (nrow()==0 || ncol()==0) {
|
---|
| 805 | os << indent << "empty matrix\n";
|
---|
| 806 | return;
|
---|
| 807 | }
|
---|
| 808 |
|
---|
| 809 | for (ii=jj=0;;) {
|
---|
| 810 | ii++; jj++;
|
---|
| 811 | kk=width*jj;
|
---|
| 812 | nn = (ncol()>kk) ? kk : ncol();
|
---|
| 813 |
|
---|
| 814 | // print column indices
|
---|
| 815 | os << indent;
|
---|
| 816 | for (i=ii; i <= nn; i++)
|
---|
| 817 | os << scprintf("%*d",lwidth,i);
|
---|
| 818 | os << endl;
|
---|
| 819 |
|
---|
| 820 | // print the rows
|
---|
| 821 | for (i=0; i < nrow() ; i++) {
|
---|
| 822 | os << indent << scprintf("%5d",i+1);
|
---|
| 823 | for (j=ii-1; j < nn; j++)
|
---|
| 824 | os << scprintf("%*.*f",lwidth,prec,rows[i][j]);
|
---|
| 825 | os << endl;
|
---|
| 826 | }
|
---|
| 827 | os << endl;
|
---|
| 828 |
|
---|
| 829 | if (ncol() <= kk) {
|
---|
| 830 | os.flush();
|
---|
| 831 | return;
|
---|
| 832 | }
|
---|
| 833 |
|
---|
| 834 | ii=kk;
|
---|
| 835 | }
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | Ref<SCMatrixSubblockIter>
|
---|
| 839 | LocalSCMatrix::local_blocks(SCMatrixSubblockIter::Access access)
|
---|
| 840 | {
|
---|
| 841 | if (messagegrp()->n() > 1) {
|
---|
| 842 | ExEnv::errn() << indent
|
---|
| 843 | << "LocalSCMatrix::local_blocks: not valid for local matrices"
|
---|
| 844 | << endl;
|
---|
| 845 | abort();
|
---|
| 846 | }
|
---|
| 847 | Ref<SCMatrixSubblockIter> iter
|
---|
| 848 | = new SCMatrixSimpleSubblockIter(access, block.pointer());
|
---|
| 849 | return iter;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | Ref<SCMatrixSubblockIter>
|
---|
| 853 | LocalSCMatrix::all_blocks(SCMatrixSubblockIter::Access access)
|
---|
| 854 | {
|
---|
| 855 | if (access == SCMatrixSubblockIter::Write) {
|
---|
| 856 | ExEnv::errn() << indent << "LocalSCMatrix::all_blocks: "
|
---|
| 857 | << "Write access permitted for local blocks only"
|
---|
| 858 | << endl;
|
---|
| 859 | abort();
|
---|
| 860 | }
|
---|
| 861 | return local_blocks(access);
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 865 |
|
---|
| 866 | // Local Variables:
|
---|
| 867 | // mode: c++
|
---|
| 868 | // c-file-style: "CLJ"
|
---|
| 869 | // End:
|
---|