| 1 |
/*! \file SparseMatrix.cpp |
| 2 |
\brief Sparse matrix definition |
| 3 |
\author Xavier Juvigny, ONERA |
| 4 |
\date Jan. 25th 2005 |
| 5 |
\modification allocation of array by STL vector class |
| 6 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 7 |
\date Jun. 11th 2013 |
| 8 |
\date Jul. 12th 2015 |
| 9 |
\date Nov. 30th 2016 |
| 10 |
*/ |
| 11 |
|
| 12 |
// This file is part of Dissection |
| 13 |
// |
| 14 |
// Dissection is free software: you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation, either version 3 of the License, or |
| 17 |
// (at your option) any later version. |
| 18 |
// |
| 19 |
// Linking Dissection statically or dynamically with other modules is making |
| 20 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 21 |
// the GNU General Public License cover the whole combination. |
| 22 |
// |
| 23 |
// As a special exception, the copyright holders of Dissection give you |
| 24 |
// permission to combine Dissection program with free software programs or |
| 25 |
// libraries that are released under the GNU LGPL and with independent modules |
| 26 |
// that communicate with Dissection solely through the Dissection-fortran |
| 27 |
// interface. You may copy and distribute such a system following the terms of |
| 28 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 29 |
// provided that you include the source code of that other code when and as |
| 30 |
// the GNU GPL requires distribution of source code and provided that you do |
| 31 |
// not modify the Dissection-fortran interface. |
| 32 |
// |
| 33 |
// Note that people who make modified versions of Dissection are not obligated |
| 34 |
// to grant this special exception for their modified versions; it is their |
| 35 |
// choice whether to do so. The GNU General Public License gives permission to |
| 36 |
// release a modified version without this exception; this exception also makes |
| 37 |
// it possible to release a modified version which carries forward this |
| 38 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 39 |
// does not apply to your modified version of Dissection, and you must remove |
| 40 |
// this exception when you distribute your modified version. |
| 41 |
// |
| 42 |
// This exception is an additional permission under section 7 of the GNU |
| 43 |
// General Public License, version 3 ("GPLv3") |
| 44 |
// |
| 45 |
// Dissection is distributed in the hope that it will be useful, |
| 46 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 47 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 48 |
// GNU General Public License for more details. |
| 49 |
// |
| 50 |
// You should have received a copy of the GNU General Public License |
| 51 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 52 |
// |
| 53 |
|
| 54 |
#include "Algebra/SparseMatrix.hpp" |
| 55 |
#include "Algebra/VectorArray.hpp" |
| 56 |
#include "Compiler/OptionCompiler.hpp" |
| 57 |
#include "Compiler/arithmetic.hpp" |
| 58 |
#include "Driver/DissectionDefault.hpp" // for definition of scaling option |
| 59 |
#include <cstring> |
| 60 |
#include <cstdio> |
| 61 |
#include <cstdlib> |
| 62 |
|
| 63 |
inline |
| 64 |
double SpMAX(double a, double b) |
| 65 |
{ |
| 66 |
return (a > b ? a : b); |
| 67 |
} |
| 68 |
|
| 69 |
inline |
| 70 |
quadruple SpMAX(quadruple a, quadruple b) |
| 71 |
{ |
| 72 |
return (a > b ? a : b); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
template<typename T, typename U> void |
| 77 |
normalize(const int type, const T* coefs0, SparseMatrix<T> *ptDA, U* u) |
| 78 |
{ |
| 79 |
const T zero(0.0); |
| 80 |
const T one(1.0); |
| 81 |
const U Uone(1.0); |
| 82 |
const U Uzero(0.0); |
| 83 |
const int n = ptDA->dimension(); |
| 84 |
VectorArray<U> v(n); // Z* v = new Z[n]; |
| 85 |
VectorArray<U> d(n); // Z* d = new Z[n]; |
| 86 |
int *ptUnsymRows, *indUnsymCols, *indVals; |
| 87 |
if (ptDA->isSymmetric() && (type == KKT_SCALING)) { |
| 88 |
const int nnz = ptDA->nnz(); |
| 89 |
if (ptDA->isWhole()) { |
| 90 |
ptUnsymRows = ptDA->getRows(); |
| 91 |
indUnsymCols = ptDA->getIndCols(); |
| 92 |
indVals = new int[nnz]; |
| 93 |
for (int i = 0; i < nnz; i++) { |
| 94 |
indVals[i] = i; |
| 95 |
} |
| 96 |
} |
| 97 |
else { |
| 98 |
const int nnz0 = nnz * 2 - n; |
| 99 |
ptUnsymRows = new int[n + 1]; |
| 100 |
indUnsymCols = new int[nnz0]; // diagonal entries exits though |
| 101 |
indVals = new int[nnz0]; // coeficient equals to zero |
| 102 |
// only used as extend symmetric symbolic structure to unsymmetric |
| 103 |
int nnz1; |
| 104 |
nnz1 = CSR_sym2unsym(ptUnsymRows, indUnsymCols, indVals, ptDA->getRows(), |
| 105 |
ptDA->getIndCols(), |
| 106 |
n, ptDA->isUpper()); |
| 107 |
if (nnz1 != nnz0) { |
| 108 |
fprintf(stderr, |
| 109 |
"%s %d : symmetric matrix has no diagonal entry %d != %d\n", |
| 110 |
__FILE__, __LINE__, nnz0, nnz1); |
| 111 |
exit(-1); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
for (int i = 0; i < n; i++) { |
| 117 |
d[i] = Uzero; |
| 118 |
u[i] = Uzero; |
| 119 |
v[i] = Uzero; |
| 120 |
} |
| 121 |
for (int i = 0; i < n; i++) { |
| 122 |
for (unsigned k = ptDA->getRows()[i]; k < ptDA->getRows()[i + 1]; k++) { |
| 123 |
int j = ptDA->getIndCols()[k]; |
| 124 |
if (i == j) { |
| 125 |
d[i] = blas_abs<T, U>(coefs0[k]); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
// 23 Jun.2011 Atsushi |
| 130 |
// u[i] = |a(i, i)|, v[i] = max_{k}{|a(i,k)|,|a(k,j)|} |
| 131 |
if (ptDA->isSymmetric() && (!ptDA->isWhole())) { |
| 132 |
for (int i = 0; i < n; i++) { |
| 133 |
u[i] = blas_abs<T, U>(coefs0[ptDA->getRows()[i]]); |
| 134 |
v[i] = SpMAX(v[i], u[i]); |
| 135 |
for (int k = (ptDA->getRows()[i] + 1); k < ptDA->getRows()[i + 1]; k++) { |
| 136 |
int j = ptDA->getIndCols()[k]; |
| 137 |
U acoef = blas_abs<T, U>(coefs0[k]); |
| 138 |
v[i] = SpMAX(v[i], acoef); |
| 139 |
v[j] = SpMAX(v[j], acoef); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
else { |
| 144 |
for (int i = 0; i < n; i++) { |
| 145 |
for (int k = ptDA->getRows()[i]; k < ptDA->getRows()[i + 1]; k++) { |
| 146 |
int j = ptDA->getIndCols()[k]; |
| 147 |
if (j == i) { |
| 148 |
u[i] = blas_abs<T, U>(coefs0[k]); |
| 149 |
v[i] = SpMAX(v[i],u[i]); |
| 150 |
} |
| 151 |
else { |
| 152 |
U acoef = blas_abs<T, U>(coefs0[k]); |
| 153 |
v[i] = SpMAX(v[i], acoef); |
| 154 |
v[j] = SpMAX(v[j], acoef); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
T alower, aupper, adiag; |
| 160 |
switch(type) { |
| 161 |
case DIAGONAL_SCALING: |
| 162 |
for (int i = 0; i < n; i++) { |
| 163 |
u[i] = ((u[i] != Uzero) ? sqrt<U>(Uone / u[i]) : |
| 164 |
((v[i] != Uzero) ? sqrt<U>(Uone / v[i]) : Uone)); |
| 165 |
} |
| 166 |
break; |
| 167 |
case KKT_SCALING: |
| 168 |
if (ptDA->isSymmetric() && (!ptDA->isWhole())) { |
| 169 |
for (int i = 0 ; i < n; i++) { |
| 170 |
if (d[i] != Uzero) { |
| 171 |
u[i] = sqrt<U>(Uone / u[i]); |
| 172 |
} |
| 173 |
else { |
| 174 |
T xtmp = zero; |
| 175 |
for (int m = ptUnsymRows[i]; m < ptUnsymRows[i + 1]; m++) { |
| 176 |
const int j = indUnsymCols[m]; |
| 177 |
alower = coefs0[indVals[m]]; |
| 178 |
int flag = 0; |
| 179 |
for (int n = ptUnsymRows[j]; n < ptUnsymRows[j + 1]; n++) { |
| 180 |
const int k = indUnsymCols[n]; |
| 181 |
if (k == j) { |
| 182 |
adiag = coefs0[indVals[n]]; |
| 183 |
if (adiag != zero) { |
| 184 |
flag++; |
| 185 |
} |
| 186 |
else { |
| 187 |
continue; |
| 188 |
} |
| 189 |
} |
| 190 |
if (k == i) { |
| 191 |
aupper = coefs0[indVals[n]]; |
| 192 |
flag++; |
| 193 |
} |
| 194 |
if (flag == 2) { |
| 195 |
break; |
| 196 |
} |
| 197 |
} // loop : n |
| 198 |
if (flag == 2) { |
| 199 |
xtmp += alower * aupper / adiag; |
| 200 |
} |
| 201 |
} // loop : m |
| 202 |
u[i] = Uone / sqrt<U>(blas_abs<T, U>(xtmp)); |
| 203 |
} // if (d[i] != zero) |
| 204 |
} // loop : i |
| 205 |
} // if (_isSymmetric && (!_isWhole)) |
| 206 |
else { |
| 207 |
for (int i = 0 ; i < n; i++) { |
| 208 |
if (d[i] != Uzero) { |
| 209 |
u[i] = sqrt<U>(Uone / u[i]); |
| 210 |
} |
| 211 |
else { |
| 212 |
T xtmp = zero; |
| 213 |
for (int m = ptDA->getRows()[i]; m < ptDA->getRows()[i + 1]; m++) { |
| 214 |
const int j = ptDA->getIndCols()[m]; |
| 215 |
alower = coefs0[m]; |
| 216 |
int flag = 0; |
| 217 |
for (int n = ptDA->getRows()[j]; n < ptDA->getRows()[j + 1]; n++) { |
| 218 |
const int k = ptDA->getIndCols()[n]; |
| 219 |
if (k == j) { |
| 220 |
adiag = coefs0[n]; |
| 221 |
if (adiag != zero) { |
| 222 |
flag++; |
| 223 |
} |
| 224 |
else { |
| 225 |
continue; |
| 226 |
} |
| 227 |
} |
| 228 |
if (k == i) { |
| 229 |
aupper = coefs0[n]; |
| 230 |
flag++; |
| 231 |
} |
| 232 |
if (flag == 2) { |
| 233 |
break; |
| 234 |
} |
| 235 |
} // loop : n |
| 236 |
if (flag == 2) { |
| 237 |
xtmp += alower * aupper / adiag; |
| 238 |
} |
| 239 |
} // loop : m |
| 240 |
u[i] = Uone / sqrt<U>(blas_abs<T, U>(xtmp)); |
| 241 |
} // if (d[i] != zero) |
| 242 |
} // loop : i |
| 243 |
} // if (_isSymmetric && (!_isWhole)) |
| 244 |
break; |
| 245 |
default: |
| 246 |
for (int i = 0; i < n; i++) { |
| 247 |
u[i] = Uone; |
| 248 |
} |
| 249 |
break; |
| 250 |
} // witch (type) |
| 251 |
// Scaling |
| 252 |
switch(type) { |
| 253 |
case NO_SCALING: |
| 254 |
for (int i = 0; i < ptDA->getRows()[n]; i++) { |
| 255 |
ptDA->getCoef()[i] = coefs0[i]; |
| 256 |
} |
| 257 |
break; |
| 258 |
case DIAGONAL_SCALING: |
| 259 |
case KKT_SCALING: |
| 260 |
for ( int i = 0; i < n; i++) { |
| 261 |
for (int k = ptDA->getRows()[i]; k < ptDA->getRows()[i+1]; k++) { |
| 262 |
int j = ptDA->getIndCols()[k]; |
| 263 |
ptDA->getCoef()[k] = coefs0[k] * u[i] *u[j]; |
| 264 |
} |
| 265 |
} |
| 266 |
break; |
| 267 |
} |
| 268 |
|
| 269 |
// delete [] v; |
| 270 |
// delete [] d; |
| 271 |
if (ptDA->isSymmetric() && (type == KKT_SCALING)) { |
| 272 |
if (!ptDA->isWhole()) { |
| 273 |
delete [] ptUnsymRows; |
| 274 |
delete [] indUnsymCols; |
| 275 |
} |
| 276 |
delete [] indVals; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
template void |
| 281 |
normalize<double, double>(const int type, const double *coefs0, |
| 282 |
SparseMatrix<double> *ptDA, double *u); |
| 283 |
|
| 284 |
template void |
| 285 |
normalize<quadruple, quadruple>(const int type, |
| 286 |
const quadruple *coefs0, |
| 287 |
SparseMatrix<quadruple> *ptDA, |
| 288 |
quadruple *u); |
| 289 |
|
| 290 |
template void |
| 291 |
normalize<complex<double>, double> |
| 292 |
(const int type, const complex<double> *coefs0, |
| 293 |
SparseMatrix<complex<double> > *ptDA, double *u); |
| 294 |
|
| 295 |
template void |
| 296 |
normalize<complex<quadruple>, quadruple> |
| 297 |
(const int type, const complex<quadruple> *coefs0, |
| 298 |
SparseMatrix<complex<quadruple> > *ptDA, quadruple *u); |
| 299 |
|
| 300 |
template void |
| 301 |
normalize<float, float>(const int type, const float *coefs0, |
| 302 |
SparseMatrix<float> *ptDA, float *u); |
| 303 |
|
| 304 |
template void |
| 305 |
normalize<complex<float>, float> |
| 306 |
(const int type, const complex<float> *coefs0, |
| 307 |
SparseMatrix<complex<float> > *ptDA, float *u); |
| 308 |
|
| 309 |
// |
| 310 |
|
| 311 |
template<typename T> void |
| 312 |
SparseMatrix<T>::extractSquareMatrix(T* DSsingCoefs, vector<int> &singVal) |
| 313 |
{ |
| 314 |
const T zero(0.0); |
| 315 |
const int nsing = singVal.size(); |
| 316 |
if (_isSymmetric && (!_isWhole)) { |
| 317 |
if (_isUpper) { |
| 318 |
for (int i = 0; i < nsing; i++) { |
| 319 |
for (int j = i; j < nsing; j++) { |
| 320 |
DSsingCoefs[i + j * nsing] = 0.0; |
| 321 |
const int icol = singVal[i]; |
| 322 |
int itmp = icol; |
| 323 |
for (int it = ptRow(icol); it < ptRow(icol + 1); it++) { |
| 324 |
itmp = indCol(it); |
| 325 |
if (itmp == singVal[j]) { |
| 326 |
DSsingCoefs[i + j * nsing] = Coef(it); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
//symmetrize |
| 331 |
for (int j = i + 1; j < nsing; j++) { |
| 332 |
DSsingCoefs[j + i * nsing] = DSsingCoefs[i + j * nsing]; |
| 333 |
} |
| 334 |
} |
| 335 |
} // if (isUpper()) |
| 336 |
else { |
| 337 |
for (int i = 0; i < nsing; i++) { |
| 338 |
for (int j = 0; j <= i; j++) { |
| 339 |
DSsingCoefs[i + j * nsing] = zero; |
| 340 |
const int icol = singVal[i]; |
| 341 |
int itmp = icol; |
| 342 |
for (int it = ptRow(icol); it < ptRow(icol + 1); it++) { |
| 343 |
itmp = indCol(it); |
| 344 |
if (itmp == singVal[j]) { |
| 345 |
DSsingCoefs[i + j * nsing] = Coef(it); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
//symmetrize |
| 350 |
for (int j = 0; j < i; j++) { |
| 351 |
DSsingCoefs[j + i * nsing] = DSsingCoefs[i + j * nsing]; |
| 352 |
} |
| 353 |
} |
| 354 |
} // if (isUpper()) |
| 355 |
} |
| 356 |
else { |
| 357 |
// |
| 358 |
for (int i = 0; i < nsing; i++) { |
| 359 |
for (int j = 0; j < nsing; j++) { |
| 360 |
DSsingCoefs[i + j * nsing] = zero; |
| 361 |
const int icol = singVal[i]; |
| 362 |
for (int it = ptRow(icol); it != ptRow(icol + 1); it++) { |
| 363 |
if (indCol(it) == singVal[j]) { |
| 364 |
DSsingCoefs[i + j * nsing] = Coef(it); |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
template void |
| 374 |
SparseMatrix<double>::extractSquareMatrix(double* DSsingCoefs, |
| 375 |
vector<int> &singVal); |
| 376 |
template void |
| 377 |
SparseMatrix<quadruple>::extractSquareMatrix(quadruple* DSsingCoefs, |
| 378 |
vector<int> &singVal); |
| 379 |
|
| 380 |
template void |
| 381 |
SparseMatrix<complex<double> >::extractSquareMatrix(complex<double>* DSsingCoefs, |
| 382 |
vector<int> &singVal); |
| 383 |
|
| 384 |
template void |
| 385 |
SparseMatrix<complex<quadruple> >::extractSquareMatrix(complex<quadruple>* DSsingCoefs, |
| 386 |
vector<int> &singVal); |
| 387 |
|
| 388 |
template void |
| 389 |
SparseMatrix<float>::extractSquareMatrix(float* DSsingCoefs, |
| 390 |
vector<int> &singVal); |
| 391 |
|
| 392 |
template void |
| 393 |
SparseMatrix<complex<float> >::extractSquareMatrix(complex<float>* DSsingCoefs, |
| 394 |
vector<int> &singVal); |
| 395 |
// |
| 396 |
|
| 397 |
template<typename T> |
| 398 |
void SparseMatrix<T>::prod(const T *u, T *v) const |
| 399 |
{ |
| 400 |
const T zero(0.0); |
| 401 |
for (int i = 0; i < dimension(); i++) { |
| 402 |
v[i] = zero; |
| 403 |
} |
| 404 |
if (_isSymmetric && (!_isWhole)) { |
| 405 |
for(int i = 0; i < dimension(); i++) { |
| 406 |
// assumption data structure |
| 407 |
// _indCols[k] < _indCols[k + 1] |
| 408 |
// _indCols[_ptRows[i]] = i for _isUpper == true |
| 409 |
// _indCols[_ptRows[i + 1] - 1] = i for _isUpper == false |
| 410 |
int ibegin, iend; |
| 411 |
if (_isUpper) { |
| 412 |
ibegin = _ptRows[i] + 1; |
| 413 |
iend = _ptRows[i + 1]; |
| 414 |
} |
| 415 |
else { |
| 416 |
ibegin = _ptRows[i]; |
| 417 |
iend = _ptRows[i + 1] - 1; |
| 418 |
} |
| 419 |
for (int k = ibegin; k < iend; k++) { |
| 420 |
const int icol = _indCols[k]; |
| 421 |
v[i] += _coefs[k] * u[icol]; |
| 422 |
v[icol] += _coefs[k] * u[i]; |
| 423 |
} |
| 424 |
const int k = _isUpper ? _ptRows[i] : (_ptRows[i + 1] - 1); // diagonal |
| 425 |
v[i] += _coefs[k] * u[_indCols[k]]; |
| 426 |
} // loop : i |
| 427 |
} |
| 428 |
else { |
| 429 |
for(int i = 0; i < dimension(); i++) { |
| 430 |
for (int k = _ptRows[i]; k < _ptRows[i + 1]; k++) { |
| 431 |
v[i] += _coefs[k] * u[_indCols[k]]; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
template |
| 438 |
void SparseMatrix<double>::prod(const double *u, double *v) const; |
| 439 |
|
| 440 |
template |
| 441 |
void SparseMatrix<quadruple>::prod(const quadruple *u, quadruple *v) const; |
| 442 |
|
| 443 |
template |
| 444 |
void SparseMatrix<complex<double> >::prod(const complex<double> *u, |
| 445 |
complex<double> *v) const; |
| 446 |
|
| 447 |
template |
| 448 |
void SparseMatrix<complex<quadruple> >::prod(const complex<quadruple> *u, |
| 449 |
complex<quadruple> *v) const; |
| 450 |
|
| 451 |
template |
| 452 |
void SparseMatrix<float>::prod(const float *u, float *v) const; |
| 453 |
|
| 454 |
template |
| 455 |
void SparseMatrix<complex<float> >::prod(const complex<float> *u, |
| 456 |
complex<float> *v) const; |
| 457 |
// |
| 458 |
|
| 459 |
template<typename T> |
| 460 |
void SparseMatrix<T>::prodt(const T *u, T *v ) const |
| 461 |
{ |
| 462 |
const T zero(0.0); |
| 463 |
for (int i = 0; i < dimension(); i++) { |
| 464 |
v[i] = zero; |
| 465 |
} |
| 466 |
for(int i = 0; i < dimension(); i++) { |
| 467 |
for (int k = _ptRows[i]; k < _ptRows[i + 1]; k++) { |
| 468 |
v[_indCols[k]] += _coefs[k]*u[i]; |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
template |
| 474 |
void SparseMatrix<double>::prodt(const double *u, double *v) const; |
| 475 |
|
| 476 |
template |
| 477 |
void SparseMatrix<quadruple>::prodt(const quadruple *u, quadruple *v) const; |
| 478 |
|
| 479 |
template |
| 480 |
void SparseMatrix<complex<double> >::prodt(const complex<double> *u, |
| 481 |
complex<double> *v) const; |
| 482 |
|
| 483 |
template |
| 484 |
void SparseMatrix<complex<quadruple> >::prodt(const complex<quadruple> *u, |
| 485 |
complex<quadruple> *v) const; |
| 486 |
|
| 487 |
template |
| 488 |
void SparseMatrix<float>::prodt(const float *u, float *v) const; |
| 489 |
|
| 490 |
template |
| 491 |
void SparseMatrix<complex<float> >::prodt(const complex<float> *u, |
| 492 |
complex<float> *v) const; |
| 493 |
// |
| 494 |
|
| 495 |
template<typename T> |
| 496 |
SparseMatrix<T>* SparseMatrix<T>::PartialCopyCSR(vector<int> &permute, |
| 497 |
const int n, |
| 498 |
bool transposed) |
| 499 |
{ |
| 500 |
vector<int> dist_ptRows; |
| 501 |
dist_ptRows.resize(n + 1); |
| 502 |
int *source_ptRows = getRows(); //&source->beginRows()[0]; |
| 503 |
SparseMatrix<T> *dist; |
| 504 |
if (_isSymmetric && !(_isWhole)) { |
| 505 |
int *width = new int[n]; |
| 506 |
for (int i = 0; i < n; i++) { |
| 507 |
width[i] = source_ptRows[permute[i] + 1] - source_ptRows[permute[i]]; |
| 508 |
} |
| 509 |
for (int i = 0; i < dimension(); i++) { |
| 510 |
for (int it = ptRow(i); it < ptRow(i + 1); it++) { |
| 511 |
const int icol = indCol(it); |
| 512 |
// does not count diagonal |
| 513 |
if (icol != i) { |
| 514 |
for (int k = 0; k < n; k++) { |
| 515 |
if (permute[k] == icol) { |
| 516 |
width[k]++; |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
} // if (icol != i) |
| 521 |
} |
| 522 |
} // loop : i |
| 523 |
dist_ptRows[0] = 0; |
| 524 |
for (int i = 0; i < n; i++) { |
| 525 |
dist_ptRows[i + 1] = dist_ptRows[i] + width[i]; |
| 526 |
} |
| 527 |
delete [] width; |
| 528 |
dist = new SparseMatrix<T>(n, dist_ptRows[n], dist_ptRows, |
| 529 |
false, false, false); |
| 530 |
if (isUpper()) { |
| 531 |
for (int k = 0; k < n; k++) { |
| 532 |
const int irow = permute[k]; |
| 533 |
int jt = dist->ptRow(k); |
| 534 |
for (int i = 0; i < irow; i++) { |
| 535 |
for (int it = ptRow(i); it < ptRow(i + 1); it++) { |
| 536 |
const int icol = indCol(it); |
| 537 |
// strictly lower == not touch the diagonal |
| 538 |
if (icol != i) { |
| 539 |
if (irow == icol) { |
| 540 |
dist->indCol(jt) = i; |
| 541 |
dist->Coef(jt) = Coef(it); |
| 542 |
jt++; |
| 543 |
break; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} // loop : i |
| 548 |
// copy upper to upper |
| 549 |
for (int it = ptRow(irow); it < ptRow(irow + 1); it++) { |
| 550 |
dist->indCol(jt) = indCol(it); |
| 551 |
dist->Coef(jt) = Coef(it); |
| 552 |
jt++; |
| 553 |
} |
| 554 |
} // loop : k |
| 555 |
} // order of filling nonzero elements is different from the upper |
| 556 |
else { // case to keep increaing indcols[] |
| 557 |
|
| 558 |
for (int k = 0; k < n; k++) { |
| 559 |
const int irow = permute[k]; |
| 560 |
int jt = dist->ptRow(k); |
| 561 |
// copy lower to lower |
| 562 |
for (int it = ptRow(irow); it < ptRow(irow + 1); it++) { |
| 563 |
dist->indCol(jt) = indCol(it); |
| 564 |
dist->Coef(jt) = Coef(it); |
| 565 |
jt++; |
| 566 |
} |
| 567 |
for (int i = irow; i < dimension(); i++) { |
| 568 |
for (int it = ptRow(i); it < ptRow(i + 1); it++) { |
| 569 |
const int icol = indCol(it); |
| 570 |
// strictly lower == not touch the diagonal |
| 571 |
if (icol != i) { |
| 572 |
if (irow == icol) { |
| 573 |
dist->indCol(jt) = i; |
| 574 |
dist->Coef(jt) = Coef(it); |
| 575 |
jt++; |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
} // loop : i |
| 581 |
} // loop : k |
| 582 |
} |
| 583 |
} // if (_isSymmetric) |
| 584 |
else { |
| 585 |
dist_ptRows[0] = 0; |
| 586 |
// non-zero pattern is symmetric |
| 587 |
for (int i = 0; i < n; i++) { |
| 588 |
const int m = permute[i]; |
| 589 |
dist_ptRows[i + 1] = (dist_ptRows[i] + |
| 590 |
(source_ptRows[m + 1] - source_ptRows[m])); |
| 591 |
} |
| 592 |
// isOwner = true, isSym = false |
| 593 |
dist = new SparseMatrix<T>(n, dist_ptRows[n], dist_ptRows, |
| 594 |
false, false, false); |
| 595 |
if (transposed) { |
| 596 |
for (int k = 0; k < n; k++) { |
| 597 |
const int irow = permute[k]; |
| 598 |
int jt = dist->ptRow(k); |
| 599 |
for (int i = 0; i < dimension(); i++) { |
| 600 |
for (int it = ptRow(i); it < ptRow(i + 1); it++){ |
| 601 |
const int icol = indCol(it); |
| 602 |
if (irow == icol) { |
| 603 |
dist->indCol(jt) = i; |
| 604 |
dist->Coef(jt) = Coef(it); |
| 605 |
jt++; |
| 606 |
break; |
| 607 |
} |
| 608 |
} |
| 609 |
} // loop : i |
| 610 |
} |
| 611 |
} |
| 612 |
else { |
| 613 |
for (int k = 0; k < n; k++) { |
| 614 |
const int irow = permute[k]; |
| 615 |
int it = ptRow(irow); |
| 616 |
int jt = dist->ptRow(k); |
| 617 |
// copy lower to lower |
| 618 |
for (; it < ptRow(irow + 1); it++, jt++) {; |
| 619 |
dist->indCol(jt) = indCol(it); |
| 620 |
dist->Coef(jt) = Coef(it); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
} // if (_isSymmetric) |
| 625 |
return dist; |
| 626 |
} |
| 627 |
|
| 628 |
template |
| 629 |
SparseMatrix<double>* SparseMatrix<double>:: |
| 630 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 631 |
bool transposed); |
| 632 |
|
| 633 |
template |
| 634 |
SparseMatrix<quadruple>* SparseMatrix<quadruple>:: |
| 635 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 636 |
bool transposed); |
| 637 |
|
| 638 |
template |
| 639 |
SparseMatrix<complex<double> >* SparseMatrix<complex<double> >:: |
| 640 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 641 |
bool transposed); |
| 642 |
|
| 643 |
template |
| 644 |
SparseMatrix<complex<quadruple> >* SparseMatrix<complex<quadruple> >:: |
| 645 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 646 |
bool transposed); |
| 647 |
|
| 648 |
template |
| 649 |
SparseMatrix<float>* SparseMatrix<float>:: |
| 650 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 651 |
bool transposed); |
| 652 |
|
| 653 |
template |
| 654 |
SparseMatrix<complex<float> >* SparseMatrix<complex<float> >:: |
| 655 |
PartialCopyCSR(vector<int> &permute, const int n, |
| 656 |
bool transposed); |
| 657 |
|
| 658 |
// |
| 659 |
int CSR_sym2unsym(int *ptRows, int *indCols, int *toSym, |
| 660 |
const int *ptSymRows, |
| 661 |
const int *indSymCols, |
| 662 |
const int dim, const bool upper_flag) |
| 663 |
{ |
| 664 |
int* nbIndPerRow = new int[dim]; |
| 665 |
|
| 666 |
memset(nbIndPerRow, 0, dim*sizeof(int)); |
| 667 |
|
| 668 |
for (int i = 0; i < dim; i++) { |
| 669 |
nbIndPerRow[i] += ptSymRows[i + 1] - ptSymRows[i]; |
| 670 |
int ibegin = ptSymRows[i] + (upper_flag ? 1 : 0); |
| 671 |
int iend = ptSymRows[i + 1] + (upper_flag ? 0 : (-1)); |
| 672 |
for (int k = ibegin; k < iend; k++) { |
| 673 |
nbIndPerRow[indSymCols[k]]++; |
| 674 |
} |
| 675 |
} |
| 676 |
// Build ptRows array : |
| 677 |
// ................... |
| 678 |
ptRows[0] = 0; |
| 679 |
for (int i = 0; i < dim; i++) { |
| 680 |
ptRows[i + 1] = ptRows[i] + nbIndPerRow[i]; |
| 681 |
} |
| 682 |
// CHECK(ptRows[dim] == (2 * nz - dim), |
| 683 |
// "error in sym2unsym() : Wrong number of non zeros elemnts in ptRows !"); |
| 684 |
// Allocate and fill indices columns : |
| 685 |
memset(nbIndPerRow, 0, (dim * sizeof(int))); |
| 686 |
// for upper case, nbIndPerRow[i] keeps entries added by transposed operation |
| 687 |
// but for lower case counts all nonzero entries in progress |
| 688 |
for (int i = 0; i < dim; i++) { |
| 689 |
int itmp = ptRows[i] + nbIndPerRow[i]; |
| 690 |
for (int k = ptSymRows[i]; k < ptSymRows[i + 1]; k++) { |
| 691 |
indCols[itmp] = indSymCols[k]; |
| 692 |
toSym[itmp] = k; |
| 693 |
itmp++; |
| 694 |
} // loop : k |
| 695 |
if (!upper_flag) { |
| 696 |
nbIndPerRow[i] = itmp - ptRows[i]; |
| 697 |
} |
| 698 |
// memcpy(indCols + (ptRows[i] + nbIndPerRow[i]), |
| 699 |
// indSymCols + ptSymRows[i], |
| 700 |
// (ptSymRows[i + 1] - ptSymRows[i]) * sizeof(int)); |
| 701 |
int ibegin = ptSymRows[i] + (upper_flag ? 1 : 0); |
| 702 |
int iend = ptSymRows[i + 1] + (upper_flag ? 0 : (-1)); |
| 703 |
for (int k = ibegin; k < iend; k++) { |
| 704 |
const int j = indSymCols[k]; |
| 705 |
const int jtmp = ptRows[j] + nbIndPerRow[j]; |
| 706 |
indCols[jtmp] = i; |
| 707 |
toSym[jtmp] = k; |
| 708 |
nbIndPerRow[j]++; |
| 709 |
} // loop : k |
| 710 |
} // loop : i |
| 711 |
// extract other toSym[] |
| 712 |
delete [] nbIndPerRow; |
| 713 |
return ptRows[dim]; |
| 714 |
} |