| 1 |
/*! \file SquareMatrix.hpp |
| 2 |
\brief Definition of a template of square plain matrix |
| 3 |
\author X. Juvigny |
| 4 |
\date January 19th 2005 |
| 5 |
\date Nov. 30th 2016 |
| 6 |
*/ |
| 7 |
|
| 8 |
// This file is part of Dissection |
| 9 |
// |
| 10 |
// Dissection is free software: you can redistribute it and/or modify |
| 11 |
// it under the terms of the GNU General Public License as published by |
| 12 |
// the Free Software Foundation, either version 3 of the License, or |
| 13 |
// (at your option) any later version. |
| 14 |
// |
| 15 |
// Linking Dissection statically or dynamically with other modules is making |
| 16 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 17 |
// the GNU General Public License cover the whole combination. |
| 18 |
// |
| 19 |
// As a special exception, the copyright holders of Dissection give you |
| 20 |
// permission to combine Dissection program with free software programs or |
| 21 |
// libraries that are released under the GNU LGPL and with independent modules |
| 22 |
// that communicate with Dissection solely through the Dissection-fortran |
| 23 |
// interface. You may copy and distribute such a system following the terms of |
| 24 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 25 |
// provided that you include the source code of that other code when and as |
| 26 |
// the GNU GPL requires distribution of source code and provided that you do |
| 27 |
// not modify the Dissection-fortran interface. |
| 28 |
// |
| 29 |
// Note that people who make modified versions of Dissection are not obligated |
| 30 |
// to grant this special exception for their modified versions; it is their |
| 31 |
// choice whether to do so. The GNU General Public License gives permission to |
| 32 |
// release a modified version without this exception; this exception also makes |
| 33 |
// it possible to release a modified version which carries forward this |
| 34 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 35 |
// does not apply to your modified version of Dissection, and you must remove |
| 36 |
// this exception when you distribute your modified version. |
| 37 |
// |
| 38 |
// This exception is an additional permission under section 7 of the GNU |
| 39 |
// General Public License, version 3 ("GPLv3") |
| 40 |
// |
| 41 |
// Dissection is distributed in the hope that it will be useful, |
| 42 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 43 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 44 |
// GNU General Public License for more details. |
| 45 |
// |
| 46 |
// You should have received a copy of the GNU General Public License |
| 47 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 48 |
// |
| 49 |
|
| 50 |
#ifndef _ALGEBRA_SQUAREMATRIX_HPP |
| 51 |
#define _ALGEBRA_SQUAREMATRIX_HPP |
| 52 |
#include <cassert> |
| 53 |
#include <cmath> |
| 54 |
#include <vector> |
| 55 |
#include "Algebra/PlainMatrix.hpp" |
| 56 |
#include "Algebra/ColumnMatrix.hpp" |
| 57 |
|
| 58 |
using std::vector; |
| 59 |
|
| 60 |
/** \brief Square plain matrix |
| 61 |
|
| 62 |
This matrix is a square plain matrix with a global indices array |
| 63 |
to have correspondence with local numerotation. |
| 64 |
*/ |
| 65 |
template<typename T> |
| 66 |
class SquareMatrix : public PlainMatrix<T> |
| 67 |
{ |
| 68 |
public: |
| 69 |
|
| 70 |
using PlainMatrix<T>::coefs; |
| 71 |
using PlainMatrix<T>::addrCoefs; |
| 72 |
using PlainMatrix<T>::addrCoefs_pt; |
| 73 |
|
| 74 |
SquareMatrix() : PlainMatrix<T>(), _rank(-1), _permute(), |
| 75 |
_isFactorized(false), _kernelDetected(-1), |
| 76 |
_singIdx(), |
| 77 |
_singIdx0() |
| 78 |
{ |
| 79 |
_dim = 0; |
| 80 |
_num_blocks = 0; |
| 81 |
_isblocked = false; |
| 82 |
} |
| 83 |
|
| 84 |
SquareMatrix(int dim, int block_size) : |
| 85 |
PlainMatrix<T>(dim * dim), _dim(dim), _rank(-1), |
| 86 |
_permute(dim), _singIdx(), _singIdx0(), |
| 87 |
_kernelDetected(-1), _isFactorized(false) |
| 88 |
{ |
| 89 |
_block_size = block_size; |
| 90 |
_num_blocks = (dim + _block_size - 1) / _block_size; |
| 91 |
if (_num_blocks > 0) { |
| 92 |
_nsing_block = new int[_num_blocks]; |
| 93 |
for (int i = 0; i < _num_blocks; i++) { |
| 94 |
_nsing_block[i] = 0; |
| 95 |
} |
| 96 |
} |
| 97 |
_isblocked = true; |
| 98 |
} |
| 99 |
|
| 100 |
SquareMatrix(int dim, int block_size, T* coefs, |
| 101 |
int nsing, int *singidx0, int *singidx, |
| 102 |
int *permute, |
| 103 |
bool isblocked, |
| 104 |
bool isOwner) : |
| 105 |
PlainMatrix<T>(isOwner) |
| 106 |
{ |
| 107 |
_dim = dim; |
| 108 |
_block_size = block_size; |
| 109 |
_num_blocks = 0; |
| 110 |
_isblocked = isblocked; |
| 111 |
PlainMatrix<T>::init(dim * dim, coefs, isOwner); |
| 112 |
_singIdx.resize(nsing); |
| 113 |
for (int i = 0; i < nsing; i++) { |
| 114 |
_singIdx[i] = singidx[i]; |
| 115 |
} |
| 116 |
_singIdx0.resize(nsing); |
| 117 |
for (int i = 0; i < nsing; i++) { |
| 118 |
_singIdx0[i] = singidx0[i]; |
| 119 |
} |
| 120 |
_permute.resize(dim); |
| 121 |
for (int i = 0; i < dim; i++) { |
| 122 |
_permute[i] = permute[i]; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
SquareMatrix(const SquareMatrix& A) : PlainMatrix<T>(A), |
| 127 |
_rank(A._rank), _permute(A._permute), |
| 128 |
_eps(A._eps), _singIdx(), _singIdx0(), |
| 129 |
_dim(A._dim), |
| 130 |
_kernelDetected(-1), |
| 131 |
_isFactorized(A._isFactorized), |
| 132 |
_isblocked(A._isblocked) |
| 133 |
{} |
| 134 |
|
| 135 |
~SquareMatrix() { |
| 136 |
if (_num_blocks > 0) { |
| 137 |
delete [] _nsing_block; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
int dimension() const { return _dim; } |
| 142 |
|
| 143 |
virtual T& operator () (int i, int j) |
| 144 |
{ |
| 145 |
# ifdef DISDEBUG |
| 146 |
assert(int(i)<dimension()); |
| 147 |
assert(int(j)<dimension()); |
| 148 |
#endif |
| 149 |
// return coefs()[j+i*dimension()]; |
| 150 |
// 05 Jul.2012 : Atsushi |
| 151 |
return coefs()[i + j * _dim]; |
| 152 |
} |
| 153 |
/** \brief Return \f$A_{ij}\f$ as constant reference |
| 154 |
|
| 155 |
\param i The ith row of the matrix |
| 156 |
\param j The jth column of the matrix |
| 157 |
\pre 0 <= i < dimension() |
| 158 |
\pre 0 <= j < dimension() |
| 159 |
*/ |
| 160 |
virtual const T& operator () (int i, int j) const |
| 161 |
{ |
| 162 |
# ifdef DISDEBUG |
| 163 |
assert(int(i)<dimension()); |
| 164 |
assert(int(j)<dimension()); |
| 165 |
#endif |
| 166 |
// return coefs()[j+i*dimension()]; |
| 167 |
// 05 Jul.2012 : Atsushi |
| 168 |
return coefs()[i + j * _dim]; |
| 169 |
} |
| 170 |
/** \brief Tell if the squarematrix is factorized or not. |
| 171 |
|
| 172 |
\return true if factorized, else false |
| 173 |
*/ |
| 174 |
bool isFactorized() const { return _isFactorized; } |
| 175 |
/** \brief Return the rank of the factorized matrix |
| 176 |
|
| 177 |
\pre The squarematrix must be factorized or return -1 |
| 178 |
*/ |
| 179 |
int rank() const |
| 180 |
{ |
| 181 |
# ifdef DISDEBUG |
| 182 |
assert(isFactorized()); |
| 183 |
# endif |
| 184 |
return _rank; |
| 185 |
} |
| 186 |
int* rank_val() { return &_rank; } |
| 187 |
/** \brief Return the last non zero pivot of the matrix |
| 188 |
|
| 189 |
\pre The squarematrix must be factorized else return 0 |
| 190 |
*/ |
| 191 |
int dim_kern() const |
| 192 |
{ |
| 193 |
return _nsing; |
| 194 |
} |
| 195 |
void set_dim_kern(int nsing) |
| 196 |
{ _nsing = nsing; } |
| 197 |
|
| 198 |
int dim_kern_block(int k) const |
| 199 |
{ |
| 200 |
#ifdef DISDEBUG |
| 201 |
assert(k >= 0); |
| 202 |
assert(k < _num_blocks); |
| 203 |
#endif |
| 204 |
return _nsing_block[k]; |
| 205 |
} |
| 206 |
void set_dim_kern_block(int k, int nsing) |
| 207 |
{ |
| 208 |
#ifdef DISDEBUG |
| 209 |
assert(k >= 0); |
| 210 |
assert(k < _num_blocks); |
| 211 |
#endif |
| 212 |
_nsing_block[k] = nsing; |
| 213 |
} |
| 214 |
|
| 215 |
void set_lastPivot(T lastpiv) |
| 216 |
{ |
| 217 |
_lastpiv = lastpiv; |
| 218 |
} |
| 219 |
|
| 220 |
T lastPivot() const |
| 221 |
{ |
| 222 |
# ifdef DISDEBUG |
| 223 |
assert(isFactorized()); |
| 224 |
# endif |
| 225 |
return _lastpiv; |
| 226 |
} |
| 227 |
T *lastPivot_val() { return &_lastpiv; } |
| 228 |
|
| 229 |
/** \brief Return the pivot obtained after factorization |
| 230 |
|
| 231 |
\pre The squarematrix must be factorized |
| 232 |
*/ |
| 233 |
vector<int>& getPermute() |
| 234 |
{ |
| 235 |
#ifdef DISDEBUG |
| 236 |
assert(isFactorized()); |
| 237 |
#endif |
| 238 |
return _permute; |
| 239 |
} |
| 240 |
/** \brief Return the pivot obtained after factorization |
| 241 |
\pre The squarematrix must be factorized |
| 242 |
*/ |
| 243 |
const vector<int>& getPermute() const |
| 244 |
{ |
| 245 |
#ifdef DISDEBUG |
| 246 |
assert(isFactorized()); |
| 247 |
#endif |
| 248 |
return _permute; |
| 249 |
} |
| 250 |
/** \name Operations on the Squarematrix |
| 251 |
*/ |
| 252 |
//@{ |
| 253 |
/** \brief Copy operator |
| 254 |
|
| 255 |
\param A The squareMatrix to copy |
| 256 |
\return A reference on the current squareMatrix |
| 257 |
*/ |
| 258 |
SquareMatrix& operator = (const SquareMatrix& A) |
| 259 |
{ |
| 260 |
if (this != &A) |
| 261 |
{ |
| 262 |
PlainMatrix<T>::operator = (A); // _loc2glob=A._loc2glob; |
| 263 |
_rank = A._rank; |
| 264 |
_permute = A._permute; |
| 265 |
_eps = A._eps; |
| 266 |
_lastpiv = A._lastpiv; |
| 267 |
_singIdx = A._singIdx; |
| 268 |
_singIdx0 = A._singIdx0; |
| 269 |
_isFactorized = A._isFactorized; |
| 270 |
_kernelDetected = A._kernelDetected; |
| 271 |
} |
| 272 |
return(*this); |
| 273 |
} |
| 274 |
/** \brief Clone operator |
| 275 |
|
| 276 |
\return a new copy of the current squareMatrix |
| 277 |
*/ |
| 278 |
|
| 279 |
virtual SquareMatrix<T>* clone() const |
| 280 |
{ |
| 281 |
//return new SquareMatrix<T>(*this); |
| 282 |
SquareMatrix<T> *ret = new SquareMatrix<T>; |
| 283 |
ret->copy(*this); |
| 284 |
return(ret); |
| 285 |
} |
| 286 |
|
| 287 |
/** \brief Deep copy of a square matrix in the current SquareMatrix |
| 288 |
|
| 289 |
\param A The matrix to copy |
| 290 |
*/ |
| 291 |
void copy (const SquareMatrix& A) |
| 292 |
{ |
| 293 |
PlainMatrix<T>::copy(A); //_loc2glob=A._loc2glob; |
| 294 |
_rank = A._rank; |
| 295 |
_permute = A._permute; |
| 296 |
_eps = A._eps; |
| 297 |
_lastpiv = A._lastpiv; |
| 298 |
_singIdx = A._singIdx; |
| 299 |
_singIdx0 = A._singIdx0; |
| 300 |
_isFactorized = A._isFactorized; |
| 301 |
_kernelDetected = A._kernelDetected; |
| 302 |
} |
| 303 |
|
| 304 |
void init(const int dim) |
| 305 |
{ |
| 306 |
_dim = dim; |
| 307 |
PlainMatrix<T>::init(dim * dim); |
| 308 |
_rank = -1; |
| 309 |
_permute.resize(dim); |
| 310 |
_singIdx.resize(0); |
| 311 |
_singIdx0.resize(0); |
| 312 |
_isFactorized = false; |
| 313 |
} |
| 314 |
|
| 315 |
void free() |
| 316 |
{ |
| 317 |
PlainMatrix<T>::free(); |
| 318 |
_dim = 0; |
| 319 |
_permute.clear(); |
| 320 |
_singIdx.clear(); |
| 321 |
_singIdx0.clear(); |
| 322 |
} |
| 323 |
|
| 324 |
bool isBlocked() |
| 325 |
{ |
| 326 |
return _isblocked; |
| 327 |
} |
| 328 |
void unsetBlocked() |
| 329 |
{ |
| 330 |
_isblocked = false; |
| 331 |
} |
| 332 |
void setBlocked() |
| 333 |
{ |
| 334 |
_isblocked = true; |
| 335 |
} |
| 336 |
|
| 337 |
void setFactorizedState_pub() { _isFactorized = true; } |
| 338 |
void set_rank(int rank) { |
| 339 |
_rank = rank; |
| 340 |
_nsing = _dim - _rank; |
| 341 |
} |
| 342 |
|
| 343 |
void set_KernelDetected(int state) { |
| 344 |
_kernelDetected = state; |
| 345 |
} |
| 346 |
int KernelDetected() const { |
| 347 |
return _kernelDetected; |
| 348 |
} |
| 349 |
void set_epspiv(T eps) { _eps = eps; } |
| 350 |
|
| 351 |
vector<int> &getSingIdx() { |
| 352 |
return _singIdx; |
| 353 |
} |
| 354 |
vector<int> &getSingIdx0() { |
| 355 |
return _singIdx0; |
| 356 |
} |
| 357 |
// void set_lastpiv(int lastpiv) { _lastpiv = lastpiv; } |
| 358 |
|
| 359 |
protected: |
| 360 |
// Internal methods |
| 361 |
/// \brief Set the matrix as factorized |
| 362 |
void setFactorizedState() { _isFactorized = true; } |
| 363 |
/// \brief Set dimension of the matrix |
| 364 |
void setDimension(int n) { _dim = n; } |
| 365 |
/// \brief Return the diagonal issued from the factorization |
| 366 |
// Attributs |
| 367 |
int _rank; //!< Rank of the matrix (initialized after factorization) |
| 368 |
int _nsing; |
| 369 |
int *_nsing_block; |
| 370 |
int _block_size; |
| 371 |
int _num_blocks; |
| 372 |
T _lastpiv; //!< Last non null pivot found during the factorization |
| 373 |
T _eps; |
| 374 |
private: |
| 375 |
// Private attributs |
| 376 |
int _dim; //<! Dimension of the matrix |
| 377 |
vector<int> _permute; //<! |
| 378 |
bool _isFactorized; //<! Tell if the matrix is factorized or not |
| 379 |
int _kernelDetected; // 0 : without, 1 : with kernel (-1) : unknown |
| 380 |
// IndiceArray _loc2glob; //!< Indice array describing local to global indices for columns |
| 381 |
// int _sing_max; |
| 382 |
vector<int> _singIdx; // with permutation |
| 383 |
vector<int> _singIdx0; // without permutation |
| 384 |
// |
| 385 |
bool _isblocked; |
| 386 |
}; // End class SquareMatrix |
| 387 |
|
| 388 |
// # include "Algebra/SquareMatrix.tpp" |
| 389 |
// ------------------------------------------------------------------------ |
| 390 |
/** \brief Output stream operator |
| 391 |
|
| 392 |
Print the square matrix in human readable form. |
| 393 |
|
| 394 |
\param out The stream outpu |
| 395 |
\param A The square matrix to print on the out stream |
| 396 |
\return The modified output stream operator |
| 397 |
*/ |
| 398 |
|
| 399 |
template<typename T> |
| 400 |
class SubSquareMatrix : public SquareMatrix<T> |
| 401 |
{ |
| 402 |
public: |
| 403 |
SubSquareMatrix() : SquareMatrix<T>(), _loc2glob() |
| 404 |
{ |
| 405 |
_offdiag_2x2_status = false; |
| 406 |
_full_pivoting = false; |
| 407 |
} |
| 408 |
|
| 409 |
SubSquareMatrix(const vector<int>& l2g) : SquareMatrix<T>() |
| 410 |
{ |
| 411 |
init(l2g); |
| 412 |
} |
| 413 |
|
| 414 |
void init(const vector<int>& l2g) |
| 415 |
{ |
| 416 |
int n = l2g.size(); |
| 417 |
_full_pivoting = false; |
| 418 |
_loc2glob = l2g; // copy constructor |
| 419 |
SquareMatrix<T>::setDimension(n); |
| 420 |
PlainMatrix<T>::init(n * n); |
| 421 |
SquareMatrix<T>::getPermute().resize(n); |
| 422 |
_pivot_width.resize(n); |
| 423 |
_pivot_2x2.resize(0); |
| 424 |
_offdiag_2x2 = new T[n]; |
| 425 |
_offdiag_2x2_status = true; |
| 426 |
} |
| 427 |
|
| 428 |
void init(const bool full_pivoting, const vector<int>& l2g, |
| 429 |
const vector<int>& l2g_left) |
| 430 |
{ |
| 431 |
int n = l2g.size(); |
| 432 |
_full_pivoting = true; |
| 433 |
_loc2glob = l2g; // copy constructor |
| 434 |
_loc2glob_left = l2g_left; |
| 435 |
SquareMatrix<T>::setDimension(n); |
| 436 |
PlainMatrix<T>::init(n * n); |
| 437 |
SquareMatrix<T>::getPermute().resize(n); |
| 438 |
_pivot_width.resize(n); |
| 439 |
_pivot_2x2.resize(0); |
| 440 |
_offdiag_2x2 = new T[n]; |
| 441 |
_offdiag_2x2_status = true; |
| 442 |
} |
| 443 |
|
| 444 |
vector<int>& loc2glob() |
| 445 |
{ |
| 446 |
return _loc2glob; |
| 447 |
} |
| 448 |
const vector<int>& loc2glob() const |
| 449 |
{ |
| 450 |
return _loc2glob; |
| 451 |
} |
| 452 |
|
| 453 |
vector<int>& loc2glob_left() |
| 454 |
{ |
| 455 |
return _full_pivoting ? _loc2glob_left : _loc2glob; |
| 456 |
} |
| 457 |
const vector<int>& loc2glob_left() const |
| 458 |
{ |
| 459 |
return _full_pivoting ? _loc2glob_left : _loc2glob; |
| 460 |
} |
| 461 |
|
| 462 |
virtual ~SubSquareMatrix() {} |
| 463 |
|
| 464 |
void free() |
| 465 |
{ |
| 466 |
SquareMatrix<T>::free(); |
| 467 |
_loc2glob.clear(); |
| 468 |
_pivot_width.clear(); |
| 469 |
_pivot_2x2.clear(); |
| 470 |
if (_offdiag_2x2_status) { |
| 471 |
delete [] _offdiag_2x2; |
| 472 |
_offdiag_2x2_status = false; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
vector<int>& getPivotWidth() |
| 477 |
{ |
| 478 |
return _pivot_width; |
| 479 |
} |
| 480 |
/** \brief Return the pivot obtained after factorization |
| 481 |
\pre The squarematrix must be factorized |
| 482 |
*/ |
| 483 |
const vector<int>& getPivotWidth() const |
| 484 |
{ |
| 485 |
return _pivot_width; |
| 486 |
} |
| 487 |
vector<int>& getPivot2x2() |
| 488 |
{ |
| 489 |
return _pivot_2x2; |
| 490 |
} |
| 491 |
/** \brief Return the pivot obtained after factorization |
| 492 |
\pre The squarematrix must be factorized |
| 493 |
*/ |
| 494 |
const vector<int>& getPivot2x2() const |
| 495 |
{ |
| 496 |
return _pivot_2x2; |
| 497 |
} |
| 498 |
|
| 499 |
T *addr2x2() |
| 500 |
{ |
| 501 |
return _offdiag_2x2; |
| 502 |
} |
| 503 |
private: |
| 504 |
vector<int> _loc2glob; |
| 505 |
vector<int> _loc2glob_left; |
| 506 |
vector<int> _pivot_width; |
| 507 |
vector<int> _pivot_2x2; |
| 508 |
T* _offdiag_2x2; |
| 509 |
bool _offdiag_2x2_status; |
| 510 |
bool _full_pivoting; |
| 511 |
}; |
| 512 |
#endif |