1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
#include <complex>
#include <string.h>
#include <algorithm>
#include "GmshConfig.h"
#include "fullMatrix.h"
#include "GmshMessage.h"
#if !defined(F77NAME)
#define F77NAME(x) (x##_)
#endif
// Specialisation of fullVector/Matrix operations using BLAS and LAPACK
#if defined(HAVE_BLAS) && !defined(HAVE_EIGEN)
extern "C" {
void F77NAME(daxpy)(int *n, double *alpha, double *x, int *incx, double *y,
int *incy);
void F77NAME(zaxpy)(int *n, std::complex<double> *alpha,
std::complex<double> *x, int *incx, std::complex<double> *y,
int *incy);
void F77NAME(dcopy)(int *n, double *a, int *inca, double *b, int *incb);
void F77NAME(zcopy)(int *n, std::complex<double> *a, int *inca,
std::complex<double> *b, int *incb);
void F77NAME(dgemm)(const char *transa, const char *transb, int *m, int *n,
int *k, double *alpha, double *a, int *lda, double *b,
int *ldb, double *beta, double *c, int *ldc);
void F77NAME(zgemm)(const char *transa, const char *transb, int *m, int *n,
int *k, std::complex<double> *alpha,
std::complex<double> *a, int *lda, std::complex<double> *b,
int *ldb, std::complex<double> *beta,
std::complex<double> *c, int *ldc);
void F77NAME(dgemv)(const char *trans, int *m, int *n, double *alpha, double *a,
int *lda, double *x, int *incx, double *beta, double *y,
int *incy);
void F77NAME(zgemv)(const char *trans, int *m, int *n,
std::complex<double> *alpha, std::complex<double> *a,
int *lda, std::complex<double> *x, int *incx,
std::complex<double> *beta, std::complex<double> *y,
int *incy);
void F77NAME(dscal)(int *n, double *alpha, double *x, int *incx);
void F77NAME(zscal)(int *n, std::complex<double> *alpha,
std::complex<double> *x, int *incx);
}
template <> void fullVector<double>::setAll(const fullVector<double> &m)
{
int stride = 1;
F77NAME(dcopy)(&_r, m._data, &stride, _data, &stride);
}
template <>
void fullVector<std::complex<double> >::setAll(
const fullVector<std::complex<double> > &m)
{
int stride = 1;
F77NAME(zcopy)(&_r, m._data, &stride, _data, &stride);
}
template <>
void fullVector<double>::axpy(const fullVector<double> &x, double alpha)
{
int M = _r, INCX = 1, INCY = 1;
F77NAME(daxpy)(&M, &alpha, x._data, &INCX, _data, &INCY);
}
template <>
void fullVector<std::complex<double> >::axpy(
const fullVector<std::complex<double> > &x, std::complex<double> alpha)
{
int M = _r, INCX = 1, INCY = 1;
F77NAME(zaxpy)(&M, &alpha, x._data, &INCX, _data, &INCY);
}
template <> void fullMatrix<int>::setAll(const fullMatrix<int> &m)
{
if(_r != m._r || _c != m._c) {
Msg::Error("fullMatrix size does not match");
return;
}
int N = _r * _c;
for(int i = 0; i < N; ++i) _data[i] = m._data[i];
}
template <> void fullMatrix<double>::setAll(const fullMatrix<double> &m)
{
if(_r != m._r || _c != m._c) {
Msg::Error("fullMatrix size does not match");
return;
}
int N = _r * _c;
int stride = 1;
F77NAME(dcopy)(&N, m._data, &stride, _data, &stride);
}
template <>
void fullMatrix<std::complex<double> >::setAll(
const fullMatrix<std::complex<double> > &m)
{
if(_r != m._r || _c != m._c) {
Msg::Error("fullMatrix size does not match");
return;
}
int N = _r * _c;
int stride = 1;
F77NAME(zcopy)(&N, m._data, &stride, _data, &stride);
}
template <> void fullMatrix<double>::scale(const double s)
{
int N = _r * _c;
int stride = 1;
double ss = s;
F77NAME(dscal)(&N, &ss, _data, &stride);
}
template <> void fullMatrix<std::complex<double> >::scale(const double s)
{
int N = _r * _c;
int stride = 1;
std::complex<double> ss(s, 0.);
F77NAME(zscal)(&N, &ss, _data, &stride);
}
template <> void fullMatrix<int>::scale(const double s)
{
for(int i = 0; i < _r * _c; ++i) _data[i] *= s;
}
template <>
void fullMatrix<double>::mult(const fullMatrix<double> &b,
fullMatrix<double> &c) const
{
int M = c.size1(), N = c.size2(), K = _c;
int LDA = _r, LDB = b.size1(), LDC = c.size1();
double alpha = 1., beta = 0.;
F77NAME(dgemm)
("N", "N", &M, &N, &K, &alpha, _data, &LDA, b._data, &LDB, &beta, c._data,
&LDC);
}
template <>
void fullMatrix<std::complex<double> >::mult(
const fullMatrix<std::complex<double> > &b,
fullMatrix<std::complex<double> > &c) const
{
int M = c.size1(), N = c.size2(), K = _c;
int LDA = _r, LDB = b.size1(), LDC = c.size1();
std::complex<double> alpha = 1., beta = 0.;
F77NAME(zgemm)
("N", "N", &M, &N, &K, &alpha, _data, &LDA, b._data, &LDB, &beta, c._data,
&LDC);
}
template <>
void fullMatrix<double>::mult(const fullVector<double> &x,
fullVector<double> &y) const
{
int M = _r, N = _c, LDA = _r, INCX = 1, INCY = 1;
double alpha = 1., beta = 0.;
F77NAME(dgemv)
("N", &M, &N, &alpha, _data, &LDA, x._data, &INCX, &beta, y._data, &INCY);
}
template <>
void fullMatrix<std::complex<double> >::mult(
const fullVector<std::complex<double> > &x,
fullVector<std::complex<double> > &y) const
{
int M = _r, N = _c, LDA = _r, INCX = 1, INCY = 1;
std::complex<double> alpha = 1., beta = 0.;
F77NAME(zgemv)
("N", &M, &N, &alpha, _data, &LDA, x._data, &INCX, &beta, y._data, &INCY);
}
template <>
void fullMatrix<double>::multAddy(const fullVector<double> &x,
fullVector<double> &y) const
{
int M = _r, N = _c, LDA = _r, INCX = 1, INCY = 1;
double alpha = 1., beta = 1.;
F77NAME(dgemv)
("N", &M, &N, &alpha, _data, &LDA, x._data, &INCX, &beta, y._data, &INCY);
}
template <>
void fullMatrix<std::complex<double> >::multAddy(
const fullVector<std::complex<double> > &x,
fullVector<std::complex<double> > &y) const
{
int M = _r, N = _c, LDA = _r, INCX = 1, INCY = 1;
std::complex<double> alpha = 1., beta = 1.;
F77NAME(zgemv)
("N", &M, &N, &alpha, _data, &LDA, x._data, &INCX, &beta, y._data, &INCY);
}
template <>
void fullMatrix<double>::multOnBlock(const fullMatrix<double> &b,
const int ncol, const int fcol,
const int alpha_, const int beta_,
fullVector<double> &c) const
{
int M = 1, N = ncol, K = b.size1();
int LDA = _r, LDB = b.size1(), LDC = 1;
double alpha = alpha_, beta = beta_;
F77NAME(dgemm)
("N", "N", &M, &N, &K, &alpha, _data, &LDA, &(b._data[fcol * K]), &LDB, &beta,
&(c._data[fcol]), &LDC);
}
template <>
void fullMatrix<double>::multWithATranspose(const fullVector<double> &x,
double alpha, double beta,
fullVector<double> &y) const
{
int M = _r, N = _c, LDA = _r, INCX = 1, INCY = 1;
F77NAME(dgemv)
("T", &M, &N, &alpha, _data, &LDA, x._data, &INCX, &beta, y._data, &INCY);
}
template <>
void fullMatrix<double>::gemm(const fullMatrix<double> &a,
const fullMatrix<double> &b, double alpha,
double beta, bool transposeA, bool transposeB)
{
int M = size1(), N = size2(), K = transposeA ? a.size1() : a.size2();
int LDA = a.size1(), LDB = b.size1(), LDC = size1();
F77NAME(dgemm)
(transposeA ? "T" : "N", transposeB ? "T" : "N", &M, &N, &K, &alpha, a._data,
&LDA, b._data, &LDB, &beta, _data, &LDC);
}
template <>
void fullMatrix<std::complex<double> >::gemm(
const fullMatrix<std::complex<double> > &a,
const fullMatrix<std::complex<double> > &b, std::complex<double> alpha,
std::complex<double> beta, bool transposeA, bool transposeB)
{
int M = size1(), N = size2(), K = transposeA ? a.size1() : a.size2();
int LDA = a.size1(), LDB = b.size1(), LDC = size1();
F77NAME(zgemm)
(transposeA ? "T" : "N", transposeB ? "T" : "N", &M, &N, &K, &alpha, a._data,
&LDA, b._data, &LDB, &beta, _data, &LDC);
}
template <>
void fullMatrix<double>::axpy(const fullMatrix<double> &x, double alpha)
{
int M = _r * _c, INCX = 1, INCY = 1;
F77NAME(daxpy)(&M, &alpha, x._data, &INCX, _data, &INCY);
}
#endif
#if defined(HAVE_LAPACK) && !defined(HAVE_EIGEN)
extern "C" {
void F77NAME(dgesv)(int *N, int *nrhs, double *A, int *lda, int *ipiv,
double *b, int *ldb, int *info);
void F77NAME(dgetrf)(int *M, int *N, double *A, int *lda, int *ipiv, int *info);
void F77NAME(dgetrs)(char *trans, int *N, int *nrhs, double *A, int *lda,
int *ipiv, double *b, int *ldb, int *info);
void F77NAME(dgetri)(int *M, double *A, int *lda, int *ipiv, double *work,
int *lwork, int *info);
void F77NAME(dgesvd)(const char *jobu, const char *jobvt, int *M, int *N,
double *A, int *lda, double *S, double *U, int *ldu,
double *VT, int *ldvt, double *work, int *lwork,
int *info);
void F77NAME(dgeev)(const char *jobvl, const char *jobvr, int *n, double *a,
int *lda, double *wr, double *wi, double *vl, int *ldvl,
double *vr, int *ldvr, double *work, int *lwork, int *info);
}
template <>
bool fullMatrix<double>::luSolve(const fullVector<double> &rhs,
fullVector<double> &result)
{
int N = size1(), nrhs = 1, lda = N, ldb = N, info;
int *ipiv = new int[N];
for(int i = 0; i < N; i++) result(i) = rhs(i);
F77NAME(dgesv)(&N, &nrhs, _data, &lda, ipiv, result._data, &ldb, &info);
delete[] ipiv;
if(info == 0) return true;
return false;
}
template <> bool fullMatrix<double>::luFactor(fullVector<int> &ipiv)
{
int M = size1(), N = size2(), lda = size1(), info;
ipiv.resize(std::min(M, N));
F77NAME(dgetrf)(&M, &N, _data, &lda, &ipiv(0), &info);
if(info == 0) return true;
return false;
}
template <>
bool fullMatrix<double>::luSubstitute(const fullVector<double> &rhs,
fullVector<int> &ipiv,
fullVector<double> &result)
{
int N = size1(), nrhs = 1, lda = N, ldb = N, info;
char trans = 'N';
for(int i = 0; i < N; i++) result(i) = rhs(i);
F77NAME(dgetrs)
(&trans, &N, &nrhs, _data, &lda, &ipiv(0), result._data, &ldb, &info);
if(info == 0) return true;
return false;
}
template <> bool fullMatrix<double>::invert(fullMatrix<double> &result) const
{
int M = size1(), N = size2(), lda = size1(), info;
int *ipiv = new int[std::min(M, N)];
if(result.size2() != M || result.size1() != N) {
if(result._ownData || !result._data)
result.resize(M, N, false);
else {
Msg::Error("FullMatrix: Bad dimension, I cannot write in proxy");
return false;
}
}
result.setAll(*this);
F77NAME(dgetrf)(&M, &N, result._data, &lda, ipiv, &info);
if(info == 0) {
int lwork = M * 4;
double *work = new double[lwork];
F77NAME(dgetri)(&M, result._data, &lda, ipiv, work, &lwork, &info);
delete[] work;
}
delete[] ipiv;
if(info == 0)
return true;
else if(info > 0)
Msg::Warning("U(%d,%d)=0 in matrix inversion", info, info);
else
Msg::Error("Wrong %d-th argument in matrix inversion", -info);
return false;
}
template <> bool fullMatrix<double>::invertInPlace()
{
int N = size1(), nrhs = N, lda = N, ldb = N, info;
int *ipiv = new int[N];
double *invA = new double[N * N];
for(int i = 0; i < N * N; i++) invA[i] = 0.;
for(int i = 0; i < N; i++) invA[i * N + i] = 1.;
F77NAME(dgesv)(&N, &nrhs, _data, &lda, ipiv, invA, &ldb, &info);
memcpy(_data, invA, N * N * sizeof(double));
delete[] invA;
delete[] ipiv;
if(info == 0) return true;
if(info > 0)
Msg::Warning("U(%d,%d)=0 in matrix in place inversion", info, info);
else
Msg::Error("Wrong %d-th argument in matrix inversion", -info);
return false;
}
template <> double fullMatrix<double>::determinant() const
{
fullMatrix<double> tmp(*this);
int M = size1(), N = size2(), lda = size1(), info;
int *ipiv = new int[std::min(M, N)];
F77NAME(dgetrf)(&M, &N, tmp._data, &lda, ipiv, &info);
double det = 1.;
if(info == 0) {
for(int i = 0; i < size1(); i++) {
det *= tmp(i, i);
if(ipiv[i] != i + 1) det = -det;
}
}
else if(info > 0)
det = 0.;
else
Msg::Error("Wrong %d-th argument in matrix factorization", -info);
delete[] ipiv;
return det;
}
template <>
bool fullMatrix<double>::eig(fullVector<double> &DR, fullVector<double> &DI,
fullMatrix<double> &VL, fullMatrix<double> &VR,
bool sortRealPart)
{
int N = size1(), info;
int lwork = 10 * N;
double *work = new double[lwork];
F77NAME(dgeev)
("V", "V", &N, _data, &N, DR._data, DI._data, VL._data, &N, VR._data, &N,
work, &lwork, &info);
delete[] work;
if(info > 0)
Msg::Error("QR Algorithm failed to compute all the eigenvalues", info,
info);
else if(info < 0)
Msg::Error("Wrong %d-th argument in eig", -info);
else if(sortRealPart)
eigSort(N, DR._data, DI._data, VL._data, VR._data);
return true;
}
template <>
bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S)
{
fullMatrix<double> VT(V.size2(), V.size1());
int M = size1(), N = size2(), LDA = size1(), LDVT = VT.size1(), info;
int lwork = std::max(3 * std::min(M, N) + std::max(M, N), 5 * std::min(M, N));
fullVector<double> WORK(lwork);
F77NAME(dgesvd)
("O", "A", &M, &N, _data, &LDA, S._data, _data, &LDA, VT._data, &LDVT,
WORK._data, &lwork, &info);
V = VT.transpose();
if(info == 0) return true;
if(info > 0)
Msg::Error("SVD did not converge");
else
Msg::Error("Wrong %d-th argument in SVD decomposition", -info);
return false;
}
#endif
// Specialisation of norm() and print()
template <> double fullVector<double>::norm() const
{
double n = 0.;
for(int i = 0; i < _r; ++i) n += _data[i] * _data[i];
return sqrt(n);
}
template <> std::complex<double> fullVector<std::complex<double> >::norm() const
{
double n = 0.;
for(int i = 0; i < _r; ++i)
n += _data[i].real() * _data[i].real() + _data[i].imag() * _data[i].imag();
return std::complex<double>(sqrt(n), 0.);
}
template <>
void fullVector<double>::print(const std::string name,
const std::string format) const
{
std::string rformat = (format == "") ? "%12.5E " : format;
printf("double %s[%d]=\n", name.c_str(), size());
printf("{ ");
for(int I = 0; I < size(); I++) { printf(rformat.c_str(), (*this)(I)); }
printf("};\n");
}
template <>
void fullVector<int>::print(const std::string name,
const std::string format) const
{
std::string rformat = (format == "") ? "%12d " : format;
printf("double %s[%d]=\n", name.c_str(), size());
printf("{ ");
for(int I = 0; I < size(); I++) { printf(rformat.c_str(), (*this)(I)); }
printf("};\n");
}
template <>
void fullMatrix<double>::print(const std::string &name,
const std::string &format) const
{
std::string rformat = (format == "") ? "%12.5E " : format;
int ni = size1();
int nj = size2();
printf("double %s [ %d ][ %d ]= { \n", name.c_str(), ni, nj);
for(int I = 0; I < ni; I++) {
printf("{ ");
for(int J = 0; J < nj; J++) {
printf(rformat.c_str(), (*this)(I, J));
if(J != nj - 1) printf(",");
}
if(I != ni - 1)
printf("},\n");
else
printf("}\n");
}
printf("};\n");
}
template <>
void fullMatrix<int>::print(const std::string &name,
const std::string &format) const
{
std::string rformat = (format == "") ? "%12d " : format;
int ni = size1();
int nj = size2();
printf("int %s [ %d ][ %d ]= { \n", name.c_str(), ni, nj);
for(int I = 0; I < ni; I++) {
printf("{ ");
for(int J = 0; J < nj; J++) {
printf(rformat.c_str(), (*this)(I, J));
if(J != nj - 1) printf(",");
}
if(I != ni - 1)
printf("},\n");
else
printf("}\n");
}
printf("};\n");
}
|