| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 1999 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_full_matrix_templates_h |
| 16 |
#define dealii_full_matrix_templates_h |
| 17 |
|
| 18 |
|
| 19 |
// TODO: this file has a lot of operations between matrices and matrices or |
| 20 |
// matrices and vectors of different precision. we should go through the |
| 21 |
// file and in each case pick the more accurate data type for intermediate |
| 22 |
// results. currently, the choice is pretty much random. this may also allow |
| 23 |
// us some operations where one operand is complex and the other is not |
| 24 |
// -> use ProductType<T,U> type trait for the results |
| 25 |
|
| 26 |
#include <deal.II/base/config.h> |
| 27 |
|
| 28 |
#include <deal.II/base/template_constraints.h> |
| 29 |
|
| 30 |
#include <deal.II/lac/full_matrix.h> |
| 31 |
#include <deal.II/lac/lapack_full_matrix.h> |
| 32 |
#include <deal.II/lac/lapack_templates.h> |
| 33 |
#include <deal.II/lac/vector.h> |
| 34 |
|
| 35 |
#include <algorithm> |
| 36 |
#include <cmath> |
| 37 |
#include <cstdio> |
| 38 |
#include <cstdlib> |
| 39 |
#include <iomanip> |
| 40 |
#include <limits> |
| 41 |
#include <vector> |
| 42 |
|
| 43 |
DEAL_II_NAMESPACE_OPEN |
| 44 |
|
| 45 |
|
| 46 |
template <typename number> |
| 47 |
FullMatrix<number>::FullMatrix(const size_type n) |
| 48 |
: Table<2, number>(n, n) |
| 49 |
{} |
| 50 |
|
| 51 |
|
| 52 |
template <typename number> |
| 53 |
FullMatrix<number>::FullMatrix(const size_type m, const size_type n) |
| 54 |
: Table<2, number>(m, n) |
| 55 |
{} |
| 56 |
|
| 57 |
|
| 58 |
template <typename number> |
| 59 |
FullMatrix<number>::FullMatrix(const size_type m, |
| 60 |
const size_type n, |
| 61 |
const number *entries) |
| 62 |
: Table<2, number>(m, n) |
| 63 |
{ |
| 64 |
this->fill(entries); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
template <typename number> |
| 70 |
FullMatrix<number>::FullMatrix(const IdentityMatrix &id) |
| 71 |
: Table<2, number>(id.m(), id.n()) |
| 72 |
{ |
| 73 |
for (size_type i = 0; i < id.m(); ++i) |
| 74 |
(*this)(i, i) = 1; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
template <typename number> |
| 80 |
template <typename number2> |
| 81 |
FullMatrix<number> & |
| 82 |
FullMatrix<number>::operator=(const FullMatrix<number2> &M) |
| 83 |
{ |
| 84 |
TableBase<2, number>::operator=(M); |
| 85 |
return *this; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
template <typename number> |
| 91 |
FullMatrix<number> & |
| 92 |
FullMatrix<number>::operator=(const IdentityMatrix &id) |
| 93 |
{ |
| 94 |
this->reinit(id.m(), id.n()); |
| 95 |
for (size_type i = 0; i < id.m(); ++i) |
| 96 |
(*this)(i, i) = 1.; |
| 97 |
|
| 98 |
return *this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
template <typename number> |
| 104 |
template <typename number2> |
| 105 |
FullMatrix<number> & |
| 106 |
FullMatrix<number>::operator=(const LAPACKFullMatrix<number2> &M) |
| 107 |
{ |
| 108 |
Assert(this->m() == M.n_rows(), ExcDimensionMismatch(this->m(), M.n_rows())); |
| 109 |
Assert(this->n() == M.n_cols(), ExcDimensionMismatch(this->n(), M.n_cols())); |
| 110 |
for (size_type i = 0; i < this->m(); ++i) |
| 111 |
for (size_type j = 0; j < this->n(); ++j) |
| 112 |
(*this)(i, j) = M(i, j); |
| 113 |
|
| 114 |
return *this; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
template <typename number> |
| 120 |
bool |
| 121 |
FullMatrix<number>::all_zero() const |
| 122 |
{ |
| 123 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 124 |
|
| 125 |
const number *p = this->values.data(); |
| 126 |
const number *const e = this->values.data() + this->n_elements(); |
| 127 |
while (p != e) |
| 128 |
if (*p++ != number(0.0)) |
| 129 |
return false; |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
template <typename number> |
| 137 |
FullMatrix<number> & |
| 138 |
FullMatrix<number>::operator*=(const number factor) |
| 139 |
{ |
| 140 |
AssertIsFinite(factor); |
| 141 |
for (number &v : this->values) |
| 142 |
v *= factor; |
| 143 |
|
| 144 |
return *this; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
template <typename number> |
| 150 |
FullMatrix<number> & |
| 151 |
FullMatrix<number>::operator/=(const number factor) |
| 152 |
{ |
| 153 |
AssertIsFinite(factor); |
| 154 |
const number factor_inv = number(1.) / factor; |
| 155 |
|
| 156 |
return *this *= factor_inv; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
template <typename number> |
| 162 |
template <typename number2> |
| 163 |
void |
| 164 |
FullMatrix<number>::vmult(Vector<number2> &dst, |
| 165 |
const Vector<number2> &src, |
| 166 |
const bool adding) const |
| 167 |
{ |
| 168 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 169 |
|
| 170 |
Assert(dst.size() == m(), ExcDimensionMismatch(dst.size(), m())); |
| 171 |
Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n())); |
| 172 |
|
| 173 |
Assert(&src != &dst, ExcSourceEqualsDestination()); |
| 174 |
|
| 175 |
const number *e = this->values.data(); |
| 176 |
// get access to the data in order to |
| 177 |
// avoid copying it when using the () |
| 178 |
// operator |
| 179 |
const number2 *src_ptr = src.begin(); |
| 180 |
const size_type size_m = m(), size_n = n(); |
| 181 |
for (size_type i = 0; i < size_m; ++i) |
| 182 |
{ |
| 183 |
number2 s = adding ? dst(i) : 0.; |
| 184 |
for (size_type j = 0; j < size_n; ++j) |
| 185 |
s += src_ptr[j] * number2(*(e++)); |
| 186 |
dst(i) = s; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
template <typename number> |
| 193 |
template <typename number2> |
| 194 |
void |
| 195 |
FullMatrix<number>::Tvmult(Vector<number2> &dst, |
| 196 |
const Vector<number2> &src, |
| 197 |
const bool adding) const |
| 198 |
{ |
| 199 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 200 |
|
| 201 |
Assert(dst.size() == n(), ExcDimensionMismatch(dst.size(), n())); |
| 202 |
Assert(src.size() == m(), ExcDimensionMismatch(src.size(), m())); |
| 203 |
|
| 204 |
Assert(&src != &dst, ExcSourceEqualsDestination()); |
| 205 |
|
| 206 |
const number *e = this->values.data(); |
| 207 |
number2 *dst_ptr = &dst(0); |
| 208 |
const size_type size_m = m(), size_n = n(); |
| 209 |
|
| 210 |
// zero out data if we are not adding |
| 211 |
if (!adding) |
| 212 |
for (size_type j = 0; j < size_n; ++j) |
| 213 |
dst_ptr[j] = 0.; |
| 214 |
|
| 215 |
// write the loop in a way that we can |
| 216 |
// access the data contiguously |
| 217 |
for (size_type i = 0; i < size_m; ++i) |
| 218 |
{ |
| 219 |
const number2 d = src(i); |
| 220 |
for (size_type j = 0; j < size_n; ++j) |
| 221 |
dst_ptr[j] += d * number2(*(e++)); |
| 222 |
}; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
template <typename number> |
| 227 |
template <typename number2, typename number3> |
| 228 |
number |
| 229 |
FullMatrix<number>::residual(Vector<number2> &dst, |
| 230 |
const Vector<number2> &src, |
| 231 |
const Vector<number3> &right) const |
| 232 |
{ |
| 233 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 234 |
|
| 235 |
Assert(dst.size() == m(), ExcDimensionMismatch(dst.size(), m())); |
| 236 |
Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n())); |
| 237 |
Assert(right.size() == m(), ExcDimensionMismatch(right.size(), m())); |
| 238 |
|
| 239 |
Assert(&src != &dst, ExcSourceEqualsDestination()); |
| 240 |
|
| 241 |
number res = 0.; |
| 242 |
const size_type size_m = m(), size_n = n(); |
| 243 |
for (size_type i = 0; i < size_m; ++i) |
| 244 |
{ |
| 245 |
number s = number(right(i)); |
| 246 |
for (size_type j = 0; j < size_n; ++j) |
| 247 |
s -= number(src(j)) * (*this)(i, j); |
| 248 |
dst(i) = s; |
| 249 |
res += s * s; |
| 250 |
} |
| 251 |
return std::sqrt(res); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
template <typename number> |
| 257 |
template <typename number2> |
| 258 |
void |
| 259 |
FullMatrix<number>::forward(Vector<number2> &dst, |
| 260 |
const Vector<number2> &src) const |
| 261 |
{ |
| 262 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 263 |
|
| 264 |
Assert(dst.size() == m(), ExcDimensionMismatch(dst.size(), m())); |
| 265 |
Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n())); |
| 266 |
|
| 267 |
size_type i, j; |
| 268 |
size_type nu = ((m() < n()) ? m() : n()); |
| 269 |
for (i = 0; i < nu; ++i) |
| 270 |
{ |
| 271 |
typename ProductType<number, number2>::type s = src(i); |
| 272 |
for (j = 0; j < i; ++j) |
| 273 |
s -= |
| 274 |
typename ProductType<number, number2>::type(dst(j)) * (*this)(i, j); |
| 275 |
dst(i) = number2(s) / number2((*this)(i, i)); |
| 276 |
AssertIsFinite(dst(i)); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
template <typename number> |
| 283 |
template <typename number2> |
| 284 |
void |
| 285 |
FullMatrix<number>::backward(Vector<number2> &dst, |
| 286 |
const Vector<number2> &src) const |
| 287 |
{ |
| 288 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 289 |
|
| 290 |
size_type j; |
| 291 |
size_type nu = (m() < n() ? m() : n()); |
| 292 |
for (std::make_signed_t<size_type> i = nu - 1; i >= 0; --i) |
| 293 |
{ |
| 294 |
typename ProductType<number, number2>::type s = src(i); |
| 295 |
for (j = i + 1; j < nu; ++j) |
| 296 |
s -= |
| 297 |
typename ProductType<number, number2>::type(dst(j)) * (*this)(i, j); |
| 298 |
dst(i) = number2(s) / number2((*this)(i, i)); |
| 299 |
AssertIsFinite(dst(i)); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
template <typename number> |
| 306 |
void |
| 307 |
FullMatrix<number>::compress(VectorOperation::values) |
| 308 |
{} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
template <typename number> |
| 313 |
template <typename number2> |
| 314 |
void |
| 315 |
FullMatrix<number>::fill(const FullMatrix<number2> &src, |
| 316 |
const size_type dst_offset_i, |
| 317 |
const size_type dst_offset_j, |
| 318 |
const size_type src_offset_i, |
| 319 |
const size_type src_offset_j) |
| 320 |
{ |
| 321 |
AssertIndexRange(dst_offset_i, m()); |
| 322 |
AssertIndexRange(dst_offset_j, n()); |
| 323 |
AssertIndexRange(src_offset_i, src.m()); |
| 324 |
AssertIndexRange(src_offset_j, src.n()); |
| 325 |
|
| 326 |
// Compute maximal size of copied block |
| 327 |
const size_type rows = std::min(m() - dst_offset_i, src.m() - src_offset_i); |
| 328 |
const size_type cols = std::min(n() - dst_offset_j, src.n() - src_offset_j); |
| 329 |
|
| 330 |
for (size_type i = 0; i < rows; ++i) |
| 331 |
for (size_type j = 0; j < cols; ++j) |
| 332 |
(*this)(dst_offset_i + i, dst_offset_j + j) = |
| 333 |
src(src_offset_i + i, src_offset_j + j); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
template <typename number> |
| 338 |
template <typename number2> |
| 339 |
void |
| 340 |
FullMatrix<number>::fill_permutation(const FullMatrix<number2> &src, |
| 341 |
const std::vector<size_type> &p_rows, |
| 342 |
const std::vector<size_type> &p_cols) |
| 343 |
{ |
| 344 |
Assert(p_rows.size() == this->n_rows(), |
| 345 |
ExcDimensionMismatch(p_rows.size(), this->n_rows())); |
| 346 |
Assert(p_cols.size() == this->n_cols(), |
| 347 |
ExcDimensionMismatch(p_cols.size(), this->n_cols())); |
| 348 |
|
| 349 |
for (size_type i = 0; i < this->n_rows(); ++i) |
| 350 |
for (size_type j = 0; j < this->n_cols(); ++j) |
| 351 |
(*this)(i, j) = src(p_rows[i], p_cols[j]); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
template <typename number> |
| 357 |
void |
| 358 |
FullMatrix<number>::add_row(const size_type i, |
| 359 |
const number s, |
| 360 |
const size_type j) |
| 361 |
{ |
| 362 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 363 |
|
| 364 |
for (size_type k = 0; k < n(); ++k) |
| 365 |
(*this)(i, k) += s * (*this)(j, k); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
template <typename number> |
| 370 |
void |
| 371 |
FullMatrix<number>::add_row(const size_type i, |
| 372 |
const number s, |
| 373 |
const size_type j, |
| 374 |
const number t, |
| 375 |
const size_type k) |
| 376 |
{ |
| 377 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 378 |
|
| 379 |
const size_type size_m = n(); |
| 380 |
for (size_type l = 0; l < size_m; ++l) |
| 381 |
(*this)(i, l) += s * (*this)(j, l) + t * (*this)(k, l); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
template <typename number> |
| 386 |
void |
| 387 |
FullMatrix<number>::add_col(const size_type i, |
| 388 |
const number s, |
| 389 |
const size_type j) |
| 390 |
{ |
| 391 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 392 |
|
| 393 |
for (size_type k = 0; k < m(); ++k) |
| 394 |
(*this)(k, i) += s * (*this)(k, j); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
template <typename number> |
| 399 |
void |
| 400 |
FullMatrix<number>::add_col(const size_type i, |
| 401 |
const number s, |
| 402 |
const size_type j, |
| 403 |
const number t, |
| 404 |
const size_type k) |
| 405 |
{ |
| 406 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 407 |
|
| 408 |
for (std::size_t l = 0; l < m(); ++l) |
| 409 |
(*this)(l, i) += s * (*this)(l, j) + t * (*this)(l, k); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
template <typename number> |
| 415 |
void |
| 416 |
FullMatrix<number>::swap_row(const size_type i, const size_type j) |
| 417 |
{ |
| 418 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 419 |
|
| 420 |
for (size_type k = 0; k < n(); ++k) |
| 421 |
std::swap((*this)(i, k), (*this)(j, k)); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
template <typename number> |
| 426 |
void |
| 427 |
FullMatrix<number>::swap_col(const size_type i, const size_type j) |
| 428 |
{ |
| 429 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 430 |
|
| 431 |
for (size_type k = 0; k < m(); ++k) |
| 432 |
std::swap((*this)(k, i), (*this)(k, j)); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
template <typename number> |
| 437 |
void |
| 438 |
FullMatrix<number>::diagadd(const number src) |
| 439 |
{ |
| 440 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 441 |
Assert(m() == n(), ExcDimensionMismatch(m(), n())); |
| 442 |
|
| 443 |
for (size_type i = 0; i < n(); ++i) |
| 444 |
(*this)(i, i) += src; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
template <typename number> |
| 449 |
template <typename number2> |
| 450 |
void |
| 451 |
FullMatrix<number>::equ(const number a, const FullMatrix<number2> &A) |
| 452 |
{ |
| 453 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 454 |
|
| 455 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 456 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 457 |
|
| 458 |
for (size_type i = 0; i < m(); ++i) |
| 459 |
for (size_type j = 0; j < n(); ++j) |
| 460 |
(*this)(i, j) = a * number(A(i, j)); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
template <typename number> |
| 465 |
template <typename number2> |
| 466 |
void |
| 467 |
FullMatrix<number>::equ(const number a, |
| 468 |
const FullMatrix<number2> &A, |
| 469 |
const number b, |
| 470 |
const FullMatrix<number2> &B) |
| 471 |
{ |
| 472 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 473 |
|
| 474 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 475 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 476 |
Assert(m() == B.m(), ExcDimensionMismatch(m(), B.m())); |
| 477 |
Assert(n() == B.n(), ExcDimensionMismatch(n(), B.n())); |
| 478 |
|
| 479 |
for (size_type i = 0; i < m(); ++i) |
| 480 |
for (size_type j = 0; j < n(); ++j) |
| 481 |
(*this)(i, j) = a * number(A(i, j)) + b * number(B(i, j)); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
template <typename number> |
| 486 |
template <typename number2> |
| 487 |
void |
| 488 |
FullMatrix<number>::equ(const number a, |
| 489 |
const FullMatrix<number2> &A, |
| 490 |
const number b, |
| 491 |
const FullMatrix<number2> &B, |
| 492 |
const number c, |
| 493 |
const FullMatrix<number2> &C) |
| 494 |
{ |
| 495 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 496 |
|
| 497 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 498 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 499 |
Assert(m() == B.m(), ExcDimensionMismatch(m(), B.m())); |
| 500 |
Assert(n() == B.n(), ExcDimensionMismatch(n(), B.n())); |
| 501 |
Assert(m() == C.m(), ExcDimensionMismatch(m(), C.m())); |
| 502 |
Assert(n() == C.n(), ExcDimensionMismatch(n(), C.n())); |
| 503 |
|
| 504 |
for (size_type i = 0; i < m(); ++i) |
| 505 |
for (size_type j = 0; j < n(); ++j) |
| 506 |
(*this)(i, j) = |
| 507 |
a * number(A(i, j)) + b * number(B(i, j)) + c * number(C(i, j)); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
template <typename number> |
| 513 |
template <typename number2> |
| 514 |
void |
| 515 |
FullMatrix<number>::mmult(FullMatrix<number2> &dst, |
| 516 |
const FullMatrix<number2> &src, |
| 517 |
const bool adding) const |
| 518 |
{ |
| 519 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 520 |
Assert(n() == src.m(), ExcDimensionMismatch(n(), src.m())); |
| 521 |
Assert(dst.n() == src.n(), ExcDimensionMismatch(dst.n(), src.n())); |
| 522 |
Assert(dst.m() == m(), ExcDimensionMismatch(m(), dst.m())); |
| 523 |
if constexpr (std::is_same_v<number, number2>) |
| 524 |
{ |
| 525 |
Assert(&dst != this, |
| 526 |
ExcMessage( |
| 527 |
"The output matrix cannot be the same as the current matrix.")); |
| 528 |
Assert(&dst != &src, |
| 529 |
ExcMessage( |
| 530 |
"The output matrix cannot be the same as the input matrix.")); |
| 531 |
} |
| 532 |
|
| 533 |
// see if we can use BLAS algorithms for this and if the type for 'number' |
| 534 |
// works for us (it is usually not efficient to use BLAS for very small |
| 535 |
// matrices): |
| 536 |
#ifdef DEAL_II_WITH_LAPACK |
| 537 |
const size_type max_blas_int = std::numeric_limits<types::blas_int>::max(); |
| 538 |
if ((std::is_same_v<number, double> || |
| 539 |
std::is_same_v<number, float>)&&std::is_same_v<number, number2>) |
| 540 |
if (this->n() * this->m() * src.n() > 300 && src.n() <= max_blas_int && |
| 541 |
this->m() <= max_blas_int && this->n() <= max_blas_int) |
| 542 |
{ |
| 543 |
// In case we have the BLAS function gemm detected by CMake, we |
| 544 |
// use that algorithm for matrix-matrix multiplication since it |
| 545 |
// provides better performance than the deal.II native function (it |
| 546 |
// uses cache and register blocking in order to access local data). |
| 547 |
// |
| 548 |
// Note that BLAS/LAPACK stores matrix elements column-wise (i.e., all |
| 549 |
// values in one column, then all in the next, etc.), whereas the |
| 550 |
// FullMatrix stores them row-wise. We ignore that difference, and |
| 551 |
// give our row-wise data to BLAS, let BLAS build the product of |
| 552 |
// transpose matrices, and read the result as if it were row-wise |
| 553 |
// again. In other words, we calculate (B^T A^T)^T, which is AB. |
| 554 |
|
| 555 |
const types::blas_int m = static_cast<types::blas_int>(src.n()); |
| 556 |
const types::blas_int n = static_cast<types::blas_int>(this->m()); |
| 557 |
const types::blas_int k = static_cast<types::blas_int>(this->n()); |
| 558 |
const char *notrans = "n"; |
| 559 |
|
| 560 |
const number alpha = 1.; |
| 561 |
const number beta = (adding == true) ? 1. : 0.; |
| 562 |
|
| 563 |
// Use the BLAS function gemm for calculating the matrix-matrix |
| 564 |
// product. |
| 565 |
gemm(notrans, |
| 566 |
notrans, |
| 567 |
&m, |
| 568 |
&n, |
| 569 |
&k, |
| 570 |
&alpha, |
| 571 |
&src(0, 0), |
| 572 |
&m, |
| 573 |
this->values.data(), |
| 574 |
&k, |
| 575 |
&beta, |
| 576 |
&dst(0, 0), |
| 577 |
&m); |
| 578 |
|
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
#endif |
| 583 |
|
| 584 |
const size_type m = this->m(), n = src.n(), l = this->n(); |
| 585 |
|
| 586 |
// arrange the loops in a way that we keep write operations low, (writing is |
| 587 |
// usually more costly than reading), even though we need to access the data |
| 588 |
// in src not in a contiguous way. |
| 589 |
for (size_type i = 0; i < m; ++i) |
| 590 |
for (size_type j = 0; j < n; ++j) |
| 591 |
{ |
| 592 |
number2 add_value = adding ? dst(i, j) : 0.; |
| 593 |
for (size_type k = 0; k < l; ++k) |
| 594 |
add_value += static_cast<number2>((*this)(i, k)) * |
| 595 |
static_cast<number2>((src(k, j))); |
| 596 |
dst(i, j) = add_value; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
template <typename number> |
| 603 |
template <typename number2> |
| 604 |
void |
| 605 |
FullMatrix<number>::Tmmult(FullMatrix<number2> &dst, |
| 606 |
const FullMatrix<number2> &src, |
| 607 |
const bool adding) const |
| 608 |
{ |
| 609 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 610 |
Assert(m() == src.m(), ExcDimensionMismatch(m(), src.m())); |
| 611 |
Assert(n() == dst.m(), ExcDimensionMismatch(n(), dst.m())); |
| 612 |
Assert(src.n() == dst.n(), ExcDimensionMismatch(src.n(), dst.n())); |
| 613 |
|
| 614 |
|
| 615 |
// see if we can use BLAS algorithms for this and if the type for 'number' |
| 616 |
// works for us (it is usually not efficient to use BLAS for very small |
| 617 |
// matrices): |
| 618 |
#ifdef DEAL_II_WITH_LAPACK |
| 619 |
const size_type max_blas_int = std::numeric_limits<types::blas_int>::max(); |
| 620 |
if ((std::is_same_v<number, double> || |
| 621 |
std::is_same_v<number, float>)&&std::is_same_v<number, number2>) |
| 622 |
if (this->n() * this->m() * src.n() > 300 && src.n() <= max_blas_int && |
| 623 |
this->n() <= max_blas_int && this->m() <= max_blas_int) |
| 624 |
{ |
| 625 |
// In case we have the BLAS function gemm detected by CMake, we |
| 626 |
// use that algorithm for matrix-matrix multiplication since it |
| 627 |
// provides better performance than the deal.II native function (it |
| 628 |
// uses cache and register blocking in order to access local data). |
| 629 |
// |
| 630 |
// Note that BLAS/LAPACK stores matrix elements column-wise (i.e., all |
| 631 |
// values in one column, then all in the next, etc.), whereas the |
| 632 |
// FullMatrix stores them row-wise. We ignore that difference, and |
| 633 |
// give our row-wise data to BLAS, let BLAS build the product of |
| 634 |
// transpose matrices, and read the result as if it were row-wise |
| 635 |
// again. In other words, we calculate (B^T A)^T, which is A^T B. |
| 636 |
|
| 637 |
const types::blas_int m = static_cast<types::blas_int>(src.n()); |
| 638 |
const types::blas_int n = static_cast<types::blas_int>(this->n()); |
| 639 |
const types::blas_int k = static_cast<types::blas_int>(this->m()); |
| 640 |
const char *trans = "t"; |
| 641 |
const char *notrans = "n"; |
| 642 |
|
| 643 |
const number alpha = 1.; |
| 644 |
const number beta = (adding == true) ? 1. : 0.; |
| 645 |
|
| 646 |
// Use the BLAS function gemm for calculating the matrix-matrix |
| 647 |
// product. |
| 648 |
gemm(notrans, |
| 649 |
trans, |
| 650 |
&m, |
| 651 |
&n, |
| 652 |
&k, |
| 653 |
&alpha, |
| 654 |
&src(0, 0), |
| 655 |
&m, |
| 656 |
this->values.data(), |
| 657 |
&n, |
| 658 |
&beta, |
| 659 |
&dst(0, 0), |
| 660 |
&m); |
| 661 |
|
| 662 |
return; |
| 663 |
} |
| 664 |
|
| 665 |
#endif |
| 666 |
|
| 667 |
const size_type m = n(), n = src.n(), l = this->m(); |
| 668 |
|
| 669 |
// symmetric matrix if the two matrices are the same |
| 670 |
if (PointerComparison::equal(this, &src)) |
| 671 |
for (size_type i = 0; i < m; ++i) |
| 672 |
for (size_type j = i; j < m; ++j) |
| 673 |
{ |
| 674 |
number2 add_value = 0.; |
| 675 |
for (size_type k = 0; k < l; ++k) |
| 676 |
add_value += static_cast<number2>((*this)(k, i)) * |
| 677 |
static_cast<number2>((*this)(k, j)); |
| 678 |
if (adding) |
| 679 |
{ |
| 680 |
dst(i, j) += add_value; |
| 681 |
if (i < j) |
| 682 |
dst(j, i) += add_value; |
| 683 |
} |
| 684 |
else |
| 685 |
dst(i, j) = dst(j, i) = add_value; |
| 686 |
} |
| 687 |
// arrange the loops in a way that we keep write operations low, (writing is |
| 688 |
// usually more costly than reading), even though we need to access the data |
| 689 |
// in src not in a contiguous way. However, we should usually end up in the |
| 690 |
// optimized gemm operation in case the matrix is big, so this shouldn't be |
| 691 |
// too bad. |
| 692 |
else |
| 693 |
for (size_type i = 0; i < m; ++i) |
| 694 |
for (size_type j = 0; j < n; ++j) |
| 695 |
{ |
| 696 |
number2 add_value = adding ? dst(i, j) : 0.; |
| 697 |
for (size_type k = 0; k < l; ++k) |
| 698 |
add_value += static_cast<number2>((*this)(k, i)) * |
| 699 |
static_cast<number2>((src(k, j))); |
| 700 |
dst(i, j) = add_value; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
template <typename number> |
| 707 |
template <typename number2> |
| 708 |
void |
| 709 |
FullMatrix<number>::mTmult(FullMatrix<number2> &dst, |
| 710 |
const FullMatrix<number2> &src, |
| 711 |
const bool adding) const |
| 712 |
{ |
| 713 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 714 |
Assert(n() == src.n(), ExcDimensionMismatch(n(), src.n())); |
| 715 |
Assert(dst.n() == src.m(), ExcDimensionMismatch(dst.n(), src.m())); |
| 716 |
Assert(dst.m() == m(), ExcDimensionMismatch(m(), dst.m())); |
| 717 |
|
| 718 |
// see if we can use BLAS algorithms for this and if the type for 'number' |
| 719 |
// works for us (it is usually not efficient to use BLAS for very small |
| 720 |
// matrices): |
| 721 |
#ifdef DEAL_II_WITH_LAPACK |
| 722 |
const size_type max_blas_int = std::numeric_limits<types::blas_int>::max(); |
| 723 |
if ((std::is_same_v<number, double> || |
| 724 |
std::is_same_v<number, float>)&&std::is_same_v<number, number2>) |
| 725 |
if (this->n() * this->m() * src.m() > 300 && src.m() <= max_blas_int && |
| 726 |
this->n() <= max_blas_int && this->m() <= max_blas_int) |
| 727 |
{ |
| 728 |
// In case we have the BLAS function gemm detected by CMake, we |
| 729 |
// use that algorithm for matrix-matrix multiplication since it |
| 730 |
// provides better performance than the deal.II native function (it |
| 731 |
// uses cache and register blocking in order to access local data). |
| 732 |
// |
| 733 |
// Note that BLAS/LAPACK stores matrix elements column-wise (i.e., all |
| 734 |
// values in one column, then all in the next, etc.), whereas the |
| 735 |
// FullMatrix stores them row-wise. We ignore that difference, and |
| 736 |
// give our row-wise data to BLAS, let BLAS build the product of |
| 737 |
// transpose matrices, and read the result as if it were row-wise |
| 738 |
// again. In other words, we calculate (B A^T)^T, which is AB^T. |
| 739 |
|
| 740 |
const types::blas_int m = static_cast<types::blas_int>(src.m()); |
| 741 |
const types::blas_int n = static_cast<types::blas_int>(this->m()); |
| 742 |
const types::blas_int k = static_cast<types::blas_int>(this->n()); |
| 743 |
const char *notrans = "n"; |
| 744 |
const char *trans = "t"; |
| 745 |
|
| 746 |
const number alpha = 1.; |
| 747 |
const number beta = (adding == true) ? 1. : 0.; |
| 748 |
|
| 749 |
// Use the BLAS function gemm for calculating the matrix-matrix |
| 750 |
// product. |
| 751 |
gemm(trans, |
| 752 |
notrans, |
| 753 |
&m, |
| 754 |
&n, |
| 755 |
&k, |
| 756 |
&alpha, |
| 757 |
&src(0, 0), |
| 758 |
&k, |
| 759 |
this->values.data(), |
| 760 |
&k, |
| 761 |
&beta, |
| 762 |
&dst(0, 0), |
| 763 |
&m); |
| 764 |
|
| 765 |
return; |
| 766 |
} |
| 767 |
|
| 768 |
#endif |
| 769 |
|
| 770 |
const size_type m = this->m(), n = src.m(), l = this->n(); |
| 771 |
|
| 772 |
// symmetric matrix if the two matrices are the same |
| 773 |
if (PointerComparison::equal(this, &src)) |
| 774 |
for (size_type i = 0; i < m; ++i) |
| 775 |
for (size_type j = i; j < m; ++j) |
| 776 |
{ |
| 777 |
number2 add_value = 0.; |
| 778 |
for (size_type k = 0; k < l; ++k) |
| 779 |
add_value += static_cast<number2>((*this)(i, k)) * |
| 780 |
static_cast<number2>((*this)(j, k)); |
| 781 |
if (adding) |
| 782 |
{ |
| 783 |
dst(i, j) += add_value; |
| 784 |
if (i < j) |
| 785 |
dst(j, i) += add_value; |
| 786 |
} |
| 787 |
else |
| 788 |
dst(i, j) = dst(j, i) = add_value; |
| 789 |
} |
| 790 |
else |
| 791 |
// arrange the loops in a way that we keep write operations low, (writing is |
| 792 |
// usually more costly than reading). |
| 793 |
for (size_type i = 0; i < m; ++i) |
| 794 |
for (size_type j = 0; j < n; ++j) |
| 795 |
{ |
| 796 |
number2 add_value = adding ? dst(i, j) : 0.; |
| 797 |
for (size_type k = 0; k < l; ++k) |
| 798 |
add_value += static_cast<number2>((*this)(i, k)) * |
| 799 |
static_cast<number2>(src(j, k)); |
| 800 |
dst(i, j) = add_value; |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
template <typename number> |
| 807 |
template <typename number2> |
| 808 |
void |
| 809 |
FullMatrix<number>::TmTmult(FullMatrix<number2> &dst, |
| 810 |
const FullMatrix<number2> &src, |
| 811 |
const bool adding) const |
| 812 |
{ |
| 813 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 814 |
Assert(m() == src.n(), ExcDimensionMismatch(m(), src.n())); |
| 815 |
Assert(n() == dst.m(), ExcDimensionMismatch(n(), dst.m())); |
| 816 |
Assert(src.m() == dst.n(), ExcDimensionMismatch(src.m(), dst.n())); |
| 817 |
|
| 818 |
|
| 819 |
// see if we can use BLAS algorithms for this and if the type for 'number' |
| 820 |
// works for us (it is usually not efficient to use BLAS for very small |
| 821 |
// matrices): |
| 822 |
#ifdef DEAL_II_WITH_LAPACK |
| 823 |
const size_type max_blas_int = std::numeric_limits<types::blas_int>::max(); |
| 824 |
if ((std::is_same_v<number, double> || |
| 825 |
std::is_same_v<number, float>)&&std::is_same_v<number, number2>) |
| 826 |
if (this->n() * this->m() * src.m() > 300 && src.m() <= max_blas_int && |
| 827 |
this->n() <= max_blas_int && this->m() <= max_blas_int) |
| 828 |
{ |
| 829 |
// In case we have the BLAS function gemm detected by CMake, we |
| 830 |
// use that algorithm for matrix-matrix multiplication since it |
| 831 |
// provides better performance than the deal.II native function (it |
| 832 |
// uses cache and register blocking in order to access local data). |
| 833 |
// |
| 834 |
// Note that BLAS/LAPACK stores matrix elements column-wise (i.e., all |
| 835 |
// values in one column, then all in the next, etc.), whereas the |
| 836 |
// FullMatrix stores them row-wise. We ignore that difference, and |
| 837 |
// give our row-wise data to BLAS, let BLAS build the product of |
| 838 |
// transpose matrices, and read the result as if it were row-wise |
| 839 |
// again. In other words, we calculate (B A)^T, which is A^T B^T. |
| 840 |
|
| 841 |
const types::blas_int m = static_cast<types::blas_int>(src.m()); |
| 842 |
const types::blas_int n = static_cast<types::blas_int>(this->n()); |
| 843 |
const types::blas_int k = static_cast<types::blas_int>(this->m()); |
| 844 |
const char *trans = "t"; |
| 845 |
|
| 846 |
const number alpha = 1.; |
| 847 |
const number beta = (adding == true) ? 1. : 0.; |
| 848 |
|
| 849 |
// Use the BLAS function gemm for calculating the matrix-matrix |
| 850 |
// product. |
| 851 |
gemm(trans, |
| 852 |
trans, |
| 853 |
&m, |
| 854 |
&n, |
| 855 |
&k, |
| 856 |
&alpha, |
| 857 |
&src(0, 0), |
| 858 |
&k, |
| 859 |
this->values.data(), |
| 860 |
&n, |
| 861 |
&beta, |
| 862 |
&dst(0, 0), |
| 863 |
&m); |
| 864 |
|
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
#endif |
| 869 |
|
| 870 |
const size_type m = n(), n = src.m(), l = this->m(); |
| 871 |
|
| 872 |
// arrange the loops in a way that we keep write operations low, (writing is |
| 873 |
// usually more costly than reading), even though we need to access the data |
| 874 |
// in the calling matrix in a non-contiguous way, possibly leading to cache |
| 875 |
// misses. However, we should usually end up in the optimized gemm operation |
| 876 |
// in case the matrix is big, so this shouldn't be too bad. |
| 877 |
for (size_type i = 0; i < m; ++i) |
| 878 |
for (size_type j = 0; j < n; ++j) |
| 879 |
{ |
| 880 |
number2 add_value = adding ? dst(i, j) : 0.; |
| 881 |
for (size_type k = 0; k < l; ++k) |
| 882 |
add_value += static_cast<number2>((*this)(k, i)) * |
| 883 |
static_cast<number2>(src(j, k)); |
| 884 |
dst(i, j) = add_value; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
template <typename number> |
| 890 |
void |
| 891 |
FullMatrix<number>::triple_product(const FullMatrix<number> &A, |
| 892 |
const FullMatrix<number> &B, |
| 893 |
const FullMatrix<number> &D, |
| 894 |
const bool transpose_B, |
| 895 |
const bool transpose_D, |
| 896 |
const number scaling) |
| 897 |
{ |
| 898 |
if (transpose_B) |
| 899 |
{ |
| 900 |
AssertDimension(B.m(), A.m()); |
| 901 |
AssertDimension(B.n(), m()); |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
AssertDimension(B.n(), A.m()); |
| 906 |
AssertDimension(B.m(), m()); |
| 907 |
} |
| 908 |
if (transpose_D) |
| 909 |
{ |
| 910 |
AssertDimension(D.n(), A.n()); |
| 911 |
AssertDimension(D.m(), n()); |
| 912 |
} |
| 913 |
else |
| 914 |
{ |
| 915 |
AssertDimension(D.m(), A.n()); |
| 916 |
AssertDimension(D.n(), n()); |
| 917 |
} |
| 918 |
|
| 919 |
// For all entries of the product |
| 920 |
// AD |
| 921 |
for (size_type i = 0; i < A.m(); ++i) |
| 922 |
for (size_type j = 0; j < n(); ++j) |
| 923 |
{ |
| 924 |
// Compute the entry |
| 925 |
number ADij = 0.; |
| 926 |
if (transpose_D) |
| 927 |
for (size_type k = 0; k < A.n(); ++k) |
| 928 |
ADij += A(i, k) * D(j, k); |
| 929 |
else |
| 930 |
for (size_type k = 0; k < A.n(); ++k) |
| 931 |
ADij += A(i, k) * D(k, j); |
| 932 |
// And add it to this after |
| 933 |
// multiplying with the right |
| 934 |
// factor from B |
| 935 |
if (transpose_B) |
| 936 |
for (size_type k = 0; k < m(); ++k) |
| 937 |
this->operator()(k, j) += scaling * ADij * B(i, k); |
| 938 |
else |
| 939 |
for (size_type k = 0; k < m(); ++k) |
| 940 |
this->operator()(k, j) += scaling * ADij * B(k, i); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
template <typename number> |
| 946 |
void |
| 947 |
FullMatrix<number>::kronecker_product(const FullMatrix<number> &A, |
| 948 |
const FullMatrix<number> &B, |
| 949 |
const bool adding) |
| 950 |
{ |
| 951 |
Assert(!A.empty(), ExcEmptyMatrix()); |
| 952 |
Assert(!B.empty(), ExcEmptyMatrix()); |
| 953 |
|
| 954 |
const size_type m = A.m() * B.m(); |
| 955 |
const size_type n = A.n() * B.n(); |
| 956 |
|
| 957 |
if (adding) |
| 958 |
{ |
| 959 |
AssertDimension(m, this->m()); |
| 960 |
AssertDimension(n, this->n()); |
| 961 |
} |
| 962 |
else |
| 963 |
this->reinit(m, n); |
| 964 |
|
| 965 |
for (size_type i = 0; i < m; ++i) |
| 966 |
for (size_type j = 0; j < n; ++j) |
| 967 |
(*this)(i, j) += A(i / B.m(), j / B.n()) * B(i % B.m(), j % B.n()); |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
template <typename number> |
| 972 |
template <typename number2> |
| 973 |
number2 |
| 974 |
FullMatrix<number>::matrix_norm_square(const Vector<number2> &v) const |
| 975 |
{ |
| 976 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 977 |
|
| 978 |
Assert(m() == v.size(), ExcDimensionMismatch(m(), v.size())); |
| 979 |
Assert(n() == v.size(), ExcDimensionMismatch(n(), v.size())); |
| 980 |
|
| 981 |
number2 sum = 0.; |
| 982 |
const size_type n_rows = m(); |
| 983 |
const number *val_ptr = this->values.data(); |
| 984 |
|
| 985 |
for (size_type row = 0; row < n_rows; ++row) |
| 986 |
{ |
| 987 |
number2 s = 0.; |
| 988 |
const number *const val_end_of_row = val_ptr + n_rows; |
| 989 |
const number2 *v_ptr = v.begin(); |
| 990 |
while (val_ptr != val_end_of_row) |
| 991 |
s += number2(*val_ptr++) * number2(*v_ptr++); |
| 992 |
|
| 993 |
sum += s * numbers::NumberTraits<number2>::conjugate(v(row)); |
| 994 |
} |
| 995 |
|
| 996 |
return sum; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
template <typename number> |
| 1001 |
template <typename number2> |
| 1002 |
number2 |
| 1003 |
FullMatrix<number>::matrix_scalar_product(const Vector<number2> &u, |
| 1004 |
const Vector<number2> &v) const |
| 1005 |
{ |
| 1006 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1007 |
|
| 1008 |
Assert(m() == u.size(), ExcDimensionMismatch(m(), u.size())); |
| 1009 |
Assert(n() == v.size(), ExcDimensionMismatch(n(), v.size())); |
| 1010 |
|
| 1011 |
number2 sum = 0.; |
| 1012 |
const size_type n_rows = m(); |
| 1013 |
const size_type n_cols = n(); |
| 1014 |
const number *val_ptr = this->values.data(); |
| 1015 |
|
| 1016 |
for (size_type row = 0; row < n_rows; ++row) |
| 1017 |
{ |
| 1018 |
number2 s = number2(0.); |
| 1019 |
const number *const val_end_of_row = val_ptr + n_cols; |
| 1020 |
const number2 *v_ptr = v.begin(); |
| 1021 |
while (val_ptr != val_end_of_row) |
| 1022 |
s += number2(*val_ptr++) * number2(*v_ptr++); |
| 1023 |
|
| 1024 |
sum += s * u(row); |
| 1025 |
} |
| 1026 |
|
| 1027 |
return sum; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
template <typename number> |
| 1033 |
void |
| 1034 |
FullMatrix<number>::symmetrize() |
| 1035 |
{ |
| 1036 |
Assert(m() == n(), ExcNotQuadratic()); |
| 1037 |
|
| 1038 |
const size_type N = m(); |
| 1039 |
for (size_type i = 0; i < N; ++i) |
| 1040 |
for (size_type j = i + 1; j < N; ++j) |
| 1041 |
{ |
| 1042 |
const number t = ((*this)(i, j) + (*this)(j, i)) / number(2.); |
| 1043 |
(*this)(i, j) = (*this)(j, i) = t; |
| 1044 |
}; |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
template <typename number> |
| 1049 |
typename FullMatrix<number>::real_type |
| 1050 |
FullMatrix<number>::l1_norm() const |
| 1051 |
{ |
| 1052 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1053 |
|
| 1054 |
real_type sum = 0, max = 0; |
| 1055 |
const size_type n_rows = m(), n_cols = n(); |
| 1056 |
|
| 1057 |
for (size_type col = 0; col < n_cols; ++col) |
| 1058 |
{ |
| 1059 |
sum = 0; |
| 1060 |
for (size_type row = 0; row < n_rows; ++row) |
| 1061 |
sum += std::abs((*this)(row, col)); |
| 1062 |
if (sum > max) |
| 1063 |
max = sum; |
| 1064 |
} |
| 1065 |
return max; |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
template <typename number> |
| 1071 |
typename FullMatrix<number>::real_type |
| 1072 |
FullMatrix<number>::linfty_norm() const |
| 1073 |
{ |
| 1074 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1075 |
|
| 1076 |
real_type sum = 0, max = 0; |
| 1077 |
const size_type n_rows = m(), n_cols = n(); |
| 1078 |
|
| 1079 |
for (size_type row = 0; row < n_rows; ++row) |
| 1080 |
{ |
| 1081 |
sum = 0; |
| 1082 |
for (size_type col = 0; col < n_cols; ++col) |
| 1083 |
sum += std::abs((*this)(row, col)); |
| 1084 |
if (sum > max) |
| 1085 |
max = sum; |
| 1086 |
} |
| 1087 |
return max; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
template <typename number> |
| 1093 |
template <typename number2> |
| 1094 |
void |
| 1095 |
FullMatrix<number>::add(const number a, const FullMatrix<number2> &A) |
| 1096 |
{ |
| 1097 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1098 |
|
| 1099 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 1100 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 1101 |
|
| 1102 |
for (size_type i = 0; i < m(); ++i) |
| 1103 |
for (size_type j = 0; j < n(); ++j) |
| 1104 |
(*this)(i, j) += a * number(A(i, j)); |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
template <typename number> |
| 1109 |
template <typename number2> |
| 1110 |
void |
| 1111 |
FullMatrix<number>::add(const number a, |
| 1112 |
const FullMatrix<number2> &A, |
| 1113 |
const number b, |
| 1114 |
const FullMatrix<number2> &B) |
| 1115 |
{ |
| 1116 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1117 |
|
| 1118 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 1119 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 1120 |
Assert(m() == B.m(), ExcDimensionMismatch(m(), B.m())); |
| 1121 |
Assert(n() == B.n(), ExcDimensionMismatch(n(), B.n())); |
| 1122 |
|
| 1123 |
for (size_type i = 0; i < m(); ++i) |
| 1124 |
for (size_type j = 0; j < n(); ++j) |
| 1125 |
(*this)(i, j) += a * number(A(i, j)) + b * number(B(i, j)); |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
template <typename number> |
| 1131 |
template <typename number2> |
| 1132 |
void |
| 1133 |
FullMatrix<number>::add(const number a, |
| 1134 |
const FullMatrix<number2> &A, |
| 1135 |
const number b, |
| 1136 |
const FullMatrix<number2> &B, |
| 1137 |
const number c, |
| 1138 |
const FullMatrix<number2> &C) |
| 1139 |
{ |
| 1140 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1141 |
|
| 1142 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 1143 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 1144 |
Assert(m() == B.m(), ExcDimensionMismatch(m(), B.m())); |
| 1145 |
Assert(n() == B.n(), ExcDimensionMismatch(n(), B.n())); |
| 1146 |
Assert(m() == C.m(), ExcDimensionMismatch(m(), C.m())); |
| 1147 |
Assert(n() == C.n(), ExcDimensionMismatch(n(), C.n())); |
| 1148 |
|
| 1149 |
|
| 1150 |
for (size_type i = 0; i < m(); ++i) |
| 1151 |
for (size_type j = 0; j < n(); ++j) |
| 1152 |
(*this)(i, j) += |
| 1153 |
a * number(A(i, j)) + b * number(B(i, j)) + c * number(C(i, j)); |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
template <typename number> |
| 1159 |
template <typename number2> |
| 1160 |
void |
| 1161 |
FullMatrix<number>::add(const FullMatrix<number2> &src, |
| 1162 |
const number factor, |
| 1163 |
const size_type dst_offset_i, |
| 1164 |
const size_type dst_offset_j, |
| 1165 |
const size_type src_offset_i, |
| 1166 |
const size_type src_offset_j) |
| 1167 |
{ |
| 1168 |
AssertIndexRange(dst_offset_i, m()); |
| 1169 |
AssertIndexRange(dst_offset_j, n()); |
| 1170 |
AssertIndexRange(src_offset_i, src.m()); |
| 1171 |
AssertIndexRange(src_offset_j, src.n()); |
| 1172 |
|
| 1173 |
// Compute maximal size of copied block |
| 1174 |
const size_type rows = std::min(m() - dst_offset_i, src.m() - src_offset_i); |
| 1175 |
const size_type cols = std::min(n() - dst_offset_j, src.n() - src_offset_j); |
| 1176 |
|
| 1177 |
for (size_type i = 0; i < rows; ++i) |
| 1178 |
for (size_type j = 0; j < cols; ++j) |
| 1179 |
(*this)(dst_offset_i + i, dst_offset_j + j) += |
| 1180 |
factor * number(src(src_offset_i + i, src_offset_j + j)); |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
template <typename number> |
| 1186 |
template <typename number2> |
| 1187 |
void |
| 1188 |
FullMatrix<number>::Tadd(const FullMatrix<number2> &src, |
| 1189 |
const number factor, |
| 1190 |
const size_type dst_offset_i, |
| 1191 |
const size_type dst_offset_j, |
| 1192 |
const size_type src_offset_i, |
| 1193 |
const size_type src_offset_j) |
| 1194 |
{ |
| 1195 |
AssertIndexRange(dst_offset_i, m()); |
| 1196 |
AssertIndexRange(dst_offset_j, n()); |
| 1197 |
AssertIndexRange(src_offset_i, src.n()); |
| 1198 |
AssertIndexRange(src_offset_j, src.m()); |
| 1199 |
|
| 1200 |
// Compute maximal size of copied block |
| 1201 |
const size_type rows = std::min(m() - dst_offset_i, src.n() - src_offset_j); |
| 1202 |
const size_type cols = std::min(n() - dst_offset_j, src.m() - src_offset_i); |
| 1203 |
|
| 1204 |
|
| 1205 |
for (size_type i = 0; i < rows; ++i) |
| 1206 |
for (size_type j = 0; j < cols; ++j) |
| 1207 |
(*this)(dst_offset_i + i, dst_offset_j + j) += |
| 1208 |
factor * number(src(src_offset_i + j, src_offset_j + i)); |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
template <typename number> |
| 1214 |
template <typename number2> |
| 1215 |
void |
| 1216 |
FullMatrix<number>::Tadd(const number a, const FullMatrix<number2> &A) |
| 1217 |
{ |
| 1218 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1219 |
|
| 1220 |
Assert(m() == n(), ExcNotQuadratic()); |
| 1221 |
Assert(m() == A.m(), ExcDimensionMismatch(m(), A.m())); |
| 1222 |
Assert(n() == A.n(), ExcDimensionMismatch(n(), A.n())); |
| 1223 |
|
| 1224 |
for (size_type i = 0; i < n(); ++i) |
| 1225 |
for (size_type j = 0; j < m(); ++j) |
| 1226 |
(*this)(i, j) += a * number(A(j, i)); |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
template <typename number> |
| 1231 |
bool |
| 1232 |
FullMatrix<number>::operator==(const FullMatrix<number> &M) const |
| 1233 |
{ |
| 1234 |
// simply pass down to the base class |
| 1235 |
return Table<2, number>::operator==(M); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
namespace internal |
| 1240 |
{ |
| 1241 |
// LAPACKFullMatrix is not implemented for |
| 1242 |
// complex numbers or long doubles |
| 1243 |
template <typename number, typename = void> |
| 1244 |
struct Determinant |
| 1245 |
{ |
| 1246 |
static number |
| 1247 |
value(const FullMatrix<number> &) |
| 1248 |
{ |
| 1249 |
AssertThrow(false, ExcNotImplemented()); |
| 1250 |
return 0.0; |
| 1251 |
} |
| 1252 |
}; |
| 1253 |
|
| 1254 |
|
| 1255 |
// LAPACKFullMatrix is only implemented for |
| 1256 |
// floats and doubles |
| 1257 |
template <typename number> |
| 1258 |
struct Determinant<number, |
| 1259 |
std::enable_if_t<std::is_same_v<number, float> || |
| 1260 |
std::is_same_v<number, double>>> |
| 1261 |
{ |
| 1262 |
#ifdef DEAL_II_WITH_LAPACK |
| 1263 |
static number |
| 1264 |
value(const FullMatrix<number> &A) |
| 1265 |
{ |
| 1266 |
using s_type = typename LAPACKFullMatrix<number>::size_type; |
| 1267 |
AssertIndexRange(A.m() - 1, std::numeric_limits<s_type>::max()); |
| 1268 |
AssertIndexRange(A.n() - 1, std::numeric_limits<s_type>::max()); |
| 1269 |
LAPACKFullMatrix<number> lp_A(static_cast<s_type>(A.m()), |
| 1270 |
static_cast<s_type>(A.n())); |
| 1271 |
lp_A = A; |
| 1272 |
lp_A.compute_lu_factorization(); |
| 1273 |
return lp_A.determinant(); |
| 1274 |
} |
| 1275 |
#else |
| 1276 |
static number |
| 1277 |
value(const FullMatrix<number> &) |
| 1278 |
{ |
| 1279 |
AssertThrow(false, ExcNeedsLAPACK()); |
| 1280 |
return 0.0; |
| 1281 |
} |
| 1282 |
#endif |
| 1283 |
}; |
| 1284 |
|
| 1285 |
} // namespace internal |
| 1286 |
|
| 1287 |
|
| 1288 |
template <typename number> |
| 1289 |
number |
| 1290 |
FullMatrix<number>::determinant() const |
| 1291 |
{ |
| 1292 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1293 |
|
| 1294 |
Assert(this->n_cols() == this->n_rows(), |
| 1295 |
ExcDimensionMismatch(this->n_cols(), this->n_rows())); |
| 1296 |
|
| 1297 |
switch (this->n_cols()) |
| 1298 |
{ |
| 1299 |
case 1: |
| 1300 |
return (*this)(0, 0); |
| 1301 |
case 2: |
| 1302 |
return (*this)(0, 0) * (*this)(1, 1) - (*this)(1, 0) * (*this)(0, 1); |
| 1303 |
case 3: |
| 1304 |
return ((*this)(0, 0) * (*this)(1, 1) * (*this)(2, 2) - |
| 1305 |
(*this)(0, 0) * (*this)(1, 2) * (*this)(2, 1) - |
| 1306 |
(*this)(1, 0) * (*this)(0, 1) * (*this)(2, 2) + |
| 1307 |
(*this)(1, 0) * (*this)(0, 2) * (*this)(2, 1) + |
| 1308 |
(*this)(2, 0) * (*this)(0, 1) * (*this)(1, 2) - |
| 1309 |
(*this)(2, 0) * (*this)(0, 2) * (*this)(1, 1)); |
| 1310 |
default: |
| 1311 |
return internal::Determinant<number>::value(*this); |
| 1312 |
}; |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
template <typename number> |
| 1318 |
number |
| 1319 |
FullMatrix<number>::trace() const |
| 1320 |
{ |
| 1321 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1322 |
|
| 1323 |
Assert(this->n_cols() == this->n_rows(), |
| 1324 |
ExcDimensionMismatch(this->n_cols(), this->n_rows())); |
| 1325 |
|
| 1326 |
number tr = 0; |
| 1327 |
for (size_type i = 0; i < this->n_rows(); ++i) |
| 1328 |
tr += (*this)(i, i); |
| 1329 |
|
| 1330 |
return tr; |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
template <typename number> |
| 1336 |
typename FullMatrix<number>::real_type |
| 1337 |
FullMatrix<number>::frobenius_norm() const |
| 1338 |
{ |
| 1339 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1340 |
|
| 1341 |
real_type s = 0.; |
| 1342 |
for (size_type i = 0; i < this->n_rows() * this->n_cols(); ++i) |
| 1343 |
s += numbers::NumberTraits<number>::abs_square(this->values[i]); |
| 1344 |
return std::sqrt(s); |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
template <typename number> |
| 1350 |
typename FullMatrix<number>::real_type |
| 1351 |
FullMatrix<number>::relative_symmetry_norm2() const |
| 1352 |
{ |
| 1353 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1354 |
|
| 1355 |
real_type s = 0.; |
| 1356 |
real_type a = 0.; |
| 1357 |
for (size_type i = 0; i < this->n_rows(); ++i) |
| 1358 |
for (size_type j = 0; j < this->n_cols(); ++j) |
| 1359 |
{ |
| 1360 |
const number x_ij = (*this)(i, j); |
| 1361 |
const number x_ji = (*this)(j, i); |
| 1362 |
|
| 1363 |
a += numbers::NumberTraits<number>::abs_square(x_ij - x_ji); |
| 1364 |
s += numbers::NumberTraits<number>::abs_square(x_ij); |
| 1365 |
} |
| 1366 |
|
| 1367 |
if (s != 0.) |
| 1368 |
return std::sqrt(a) / std::sqrt(s); |
| 1369 |
return 0; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
template <typename number> |
| 1375 |
template <typename number2> |
| 1376 |
void |
| 1377 |
FullMatrix<number>::invert(const FullMatrix<number2> &M) |
| 1378 |
{ |
| 1379 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1380 |
|
| 1381 |
Assert(this->n_cols() == this->n_rows(), ExcNotQuadratic()); |
| 1382 |
Assert(this->n_cols() == M.n_cols(), |
| 1383 |
ExcDimensionMismatch(this->n_cols(), M.n_cols())); |
| 1384 |
Assert(this->n_rows() == M.n_rows(), |
| 1385 |
ExcDimensionMismatch(this->n_rows(), M.n_rows())); |
| 1386 |
|
| 1387 |
switch (this->n_cols()) |
| 1388 |
{ |
| 1389 |
case 1: |
| 1390 |
(*this)(0, 0) = number2(1.0) / M(0, 0); |
| 1391 |
return; |
| 1392 |
case 2: |
| 1393 |
{ |
| 1394 |
const number2 M00 = M(0, 0); |
| 1395 |
const number2 M01 = M(0, 1); |
| 1396 |
const number2 M10 = M(1, 0); |
| 1397 |
const number2 M11 = M(1, 1); |
| 1398 |
const number2 t4 = number2(1.0) / (M00 * M11 - M01 * M10); |
| 1399 |
(*this)(0, 0) = M11 * t4; |
| 1400 |
(*this)(0, 1) = -M01 * t4; |
| 1401 |
(*this)(1, 0) = -M10 * t4; |
| 1402 |
(*this)(1, 1) = M00 * t4; |
| 1403 |
return; |
| 1404 |
}; |
| 1405 |
|
| 1406 |
case 3: |
| 1407 |
{ |
| 1408 |
const number2 M00 = M(0, 0); |
| 1409 |
const number2 M01 = M(0, 1); |
| 1410 |
const number2 M02 = M(0, 2); |
| 1411 |
const number2 M10 = M(1, 0); |
| 1412 |
const number2 M11 = M(1, 1); |
| 1413 |
const number2 M12 = M(1, 2); |
| 1414 |
const number2 M20 = M(2, 0); |
| 1415 |
const number2 M21 = M(2, 1); |
| 1416 |
const number2 M22 = M(2, 2); |
| 1417 |
const number2 t00 = M11 * M22 - M12 * M21; |
| 1418 |
const number2 t10 = M12 * M20 - M10 * M22; |
| 1419 |
const number2 t20 = M10 * M21 - M11 * M20; |
| 1420 |
const number2 inv_det = |
| 1421 |
number2(1.0) / (M00 * t00 + M01 * t10 + M02 * t20); |
| 1422 |
(*this)(0, 0) = t00 * inv_det; |
| 1423 |
(*this)(0, 1) = (M02 * M21 - M01 * M22) * inv_det; |
| 1424 |
(*this)(0, 2) = (M01 * M12 - M02 * M11) * inv_det; |
| 1425 |
(*this)(1, 0) = t10 * inv_det; |
| 1426 |
(*this)(1, 1) = (M00 * M22 - M02 * M20) * inv_det; |
| 1427 |
(*this)(1, 2) = (M02 * M10 - M00 * M12) * inv_det; |
| 1428 |
(*this)(2, 0) = t20 * inv_det; |
| 1429 |
(*this)(2, 1) = (M01 * M20 - M00 * M21) * inv_det; |
| 1430 |
(*this)(2, 2) = (M00 * M11 - M01 * M10) * inv_det; |
| 1431 |
return; |
| 1432 |
}; |
| 1433 |
|
| 1434 |
case 4: |
| 1435 |
{ |
| 1436 |
// Initially derived from the following maple script |
| 1437 |
// |
| 1438 |
// with (linalg); |
| 1439 |
// a:=matrix(4,4); |
| 1440 |
// evalm(a); |
| 1441 |
// ai:=inverse(a); |
| 1442 |
// readlib(C); |
| 1443 |
// C(ai,optimized,filename=x4); |
| 1444 |
// |
| 1445 |
// but then combined re-occurring terms via distributive law in an |
| 1446 |
// FMA-friendly format |
| 1447 |
const number2 M00 = M(0, 0); |
| 1448 |
const number2 M01 = M(0, 1); |
| 1449 |
const number2 M02 = M(0, 2); |
| 1450 |
const number2 M03 = M(0, 3); |
| 1451 |
const number2 M10 = M(1, 0); |
| 1452 |
const number2 M11 = M(1, 1); |
| 1453 |
const number2 M12 = M(1, 2); |
| 1454 |
const number2 M13 = M(1, 3); |
| 1455 |
const number2 M20 = M(2, 0); |
| 1456 |
const number2 M21 = M(2, 1); |
| 1457 |
const number2 M22 = M(2, 2); |
| 1458 |
const number2 M23 = M(2, 3); |
| 1459 |
const number2 M30 = M(3, 0); |
| 1460 |
const number2 M31 = M(3, 1); |
| 1461 |
const number2 M32 = M(3, 2); |
| 1462 |
const number2 M33 = M(3, 3); |
| 1463 |
|
| 1464 |
const number2 t14 = M00 * M11 - M10 * M01; |
| 1465 |
const number2 t15 = M22 * M33 - M23 * M32; |
| 1466 |
const number2 t19 = M00 * M21 - M20 * M01; |
| 1467 |
const number2 t20 = M12 * M33 - M13 * M32; |
| 1468 |
const number2 t24 = M00 * M31 - M30 * M01; |
| 1469 |
const number2 t25 = M12 * M23 - M13 * M22; |
| 1470 |
const number2 t32 = M10 * M21 - M20 * M11; |
| 1471 |
const number2 t33 = M02 * M33 - M03 * M32; |
| 1472 |
const number2 t37 = M10 * M31 - M30 * M11; |
| 1473 |
const number2 t38 = M02 * M23 - M03 * M22; |
| 1474 |
const number2 t49 = M20 * M31 - M30 * M21; |
| 1475 |
const number2 t50 = M02 * M13 - M03 * M12; |
| 1476 |
const number2 det = t14 * t15 - t19 * t20 + t24 * t25 + t32 * t33 - |
| 1477 |
t37 * t38 + t49 * t50; |
| 1478 |
const number2 inv_det = number2(1.0) / det; |
| 1479 |
const number2 t81 = M01 * M12 - M02 * M11; |
| 1480 |
const number2 t83 = M01 * M13 - M03 * M11; |
| 1481 |
const number2 t93 = M01 * M22 - M02 * M21; |
| 1482 |
const number2 t95 = M11 * M23 - M13 * M21; |
| 1483 |
const number2 t97 = M01 * M23 - M03 * M21; |
| 1484 |
const number2 t99 = M11 * M22 - M12 * M21; |
| 1485 |
const number2 t101 = M10 * M22 - M20 * M12; |
| 1486 |
const number2 t103 = M10 * M23 - M20 * M13; |
| 1487 |
const number2 t115 = M00 * M22 - M20 * M02; |
| 1488 |
const number2 t117 = M00 * M23 - M20 * M03; |
| 1489 |
const number2 t129 = M00 * M12 - M10 * M02; |
| 1490 |
const number2 t131 = M00 * M13 - M10 * M03; |
| 1491 |
|
| 1492 |
(*this)(0, 0) = (M11 * t15 - M21 * t20 + M31 * t25) * inv_det; |
| 1493 |
(*this)(0, 1) = -(M01 * t15 - M21 * t33 + M31 * t38) * inv_det; |
| 1494 |
(*this)(0, 2) = (M33 * t81 - M32 * t83 + M31 * t50) * inv_det; |
| 1495 |
(*this)(0, 3) = -(M23 * t81 - M22 * t83 + M21 * t50) * inv_det; |
| 1496 |
(*this)(1, 0) = -(M33 * t101 - M32 * t103 + M30 * t25) * inv_det; |
| 1497 |
(*this)(1, 1) = (M33 * t115 - M32 * t117 + M30 * t38) * inv_det; |
| 1498 |
(*this)(1, 2) = -(M33 * t129 - M32 * t131 + M30 * t50) * inv_det; |
| 1499 |
(*this)(1, 3) = (M23 * t129 - M22 * t131 + M20 * t50) * inv_det; |
| 1500 |
(*this)(2, 0) = (M33 * t32 - M31 * t103 + M30 * t95) * inv_det; |
| 1501 |
(*this)(2, 1) = -(M33 * t19 - M31 * t117 + M30 * t97) * inv_det; |
| 1502 |
(*this)(2, 2) = (M33 * t14 - M31 * t131 + M30 * t83) * inv_det; |
| 1503 |
(*this)(2, 3) = -(M23 * t14 - M21 * t131 + M20 * t83) * inv_det; |
| 1504 |
(*this)(3, 0) = -(M32 * t32 - M31 * t101 + M30 * t99) * inv_det; |
| 1505 |
(*this)(3, 1) = (M32 * t19 - M31 * t115 + M30 * t93) * inv_det; |
| 1506 |
(*this)(3, 2) = -(M32 * t14 - M31 * t129 + M30 * t81) * inv_det; |
| 1507 |
(*this)(3, 3) = (M22 * t14 - M21 * t129 + M20 * t81) * inv_det; |
| 1508 |
|
| 1509 |
break; |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
default: |
| 1514 |
// if no inversion is |
| 1515 |
// hardcoded, fall back |
| 1516 |
// to use the |
| 1517 |
// Gauss-Jordan algorithm |
| 1518 |
if (!PointerComparison::equal(&M, this)) |
| 1519 |
*this = M; |
| 1520 |
gauss_jordan(); |
| 1521 |
}; |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
template <typename number> |
| 1526 |
template <typename number2> |
| 1527 |
void |
| 1528 |
FullMatrix<number>::cholesky(const FullMatrix<number2> &A) |
| 1529 |
{ |
| 1530 |
Assert(!A.empty(), ExcEmptyMatrix()); |
| 1531 |
Assert(A.n() == A.m(), ExcNotQuadratic()); |
| 1532 |
// Matrix must be symmetric. |
| 1533 |
Assert(A.relative_symmetry_norm2() < 1.0e-10, |
| 1534 |
ExcMessage("A must be symmetric.")); |
| 1535 |
|
| 1536 |
if (PointerComparison::equal(&A, this)) |
| 1537 |
{ |
| 1538 |
// avoid overwriting source |
| 1539 |
// by destination matrix: |
| 1540 |
const FullMatrix<number> A2 = *this; |
| 1541 |
cholesky(A2); |
| 1542 |
} |
| 1543 |
else |
| 1544 |
{ |
| 1545 |
/* reinit *this to 0 */ |
| 1546 |
this->reinit(A.m(), A.n()); |
| 1547 |
|
| 1548 |
for (size_type i = 0; i < this->n_cols(); ++i) |
| 1549 |
{ |
| 1550 |
double SLik2 = 0.0; |
| 1551 |
for (size_type j = 0; j < i; ++j) |
| 1552 |
{ |
| 1553 |
double SLikLjk = 0.0; |
| 1554 |
for (size_type k = 0; k < j; ++k) |
| 1555 |
{ |
| 1556 |
SLikLjk += (*this)(i, k) * (*this)(j, k); |
| 1557 |
}; |
| 1558 |
(*this)(i, j) = (1. / (*this)(j, j)) * (A(i, j) - SLikLjk); |
| 1559 |
SLik2 += (*this)(i, j) * (*this)(i, j); |
| 1560 |
} |
| 1561 |
AssertThrow(A(i, i) - SLik2 >= 0, ExcMatrixNotPositiveDefinite()); |
| 1562 |
|
| 1563 |
(*this)(i, i) = std::sqrt(A(i, i) - SLik2); |
| 1564 |
} |
| 1565 |
} |
| 1566 |
} |
| 1567 |
|
| 1568 |
|
| 1569 |
template <typename number> |
| 1570 |
template <typename number2> |
| 1571 |
void |
| 1572 |
FullMatrix<number>::outer_product(const Vector<number2> &V, |
| 1573 |
const Vector<number2> &W) |
| 1574 |
{ |
| 1575 |
Assert(V.size() == W.size(), |
| 1576 |
ExcMessage("Vectors V, W must be the same size.")); |
| 1577 |
this->reinit(V.size(), V.size()); |
| 1578 |
|
| 1579 |
for (size_type i = 0; i < this->n(); ++i) |
| 1580 |
{ |
| 1581 |
for (size_type j = 0; j < this->n(); ++j) |
| 1582 |
{ |
| 1583 |
(*this)(i, j) = V(i) * W(j); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
template <typename number> |
| 1590 |
template <typename number2> |
| 1591 |
void |
| 1592 |
FullMatrix<number>::left_invert(const FullMatrix<number2> &A) |
| 1593 |
{ |
| 1594 |
Assert(!A.empty(), ExcEmptyMatrix()); |
| 1595 |
|
| 1596 |
// If the matrix is square, simply do a |
| 1597 |
// standard inversion |
| 1598 |
if (A.m() == A.n()) |
| 1599 |
{ |
| 1600 |
FullMatrix<number2> left_inv(A.n(), A.m()); |
| 1601 |
left_inv.invert(A); |
| 1602 |
*this = std::move(left_inv); |
| 1603 |
return; |
| 1604 |
} |
| 1605 |
|
| 1606 |
Assert(A.m() > A.n(), ExcDimensionMismatch(A.m(), A.n())); |
| 1607 |
Assert(this->m() == A.n(), ExcDimensionMismatch(this->m(), A.n())); |
| 1608 |
Assert(this->n() == A.m(), ExcDimensionMismatch(this->n(), A.m())); |
| 1609 |
|
| 1610 |
FullMatrix<number2> A_t(A.n(), A.m()); |
| 1611 |
FullMatrix<number2> A_t_times_A(A.n(), A.n()); |
| 1612 |
FullMatrix<number2> A_t_times_A_inv(A.n(), A.n()); |
| 1613 |
FullMatrix<number2> left_inv(A.n(), A.m()); |
| 1614 |
|
| 1615 |
A_t.Tadd(A, 1); |
| 1616 |
A_t.mmult(A_t_times_A, A); |
| 1617 |
if (number(A_t_times_A.determinant()) == number(0)) |
| 1618 |
{ |
| 1619 |
Assert(false, ExcSingular()); |
| 1620 |
} |
| 1621 |
else |
| 1622 |
{ |
| 1623 |
A_t_times_A_inv.invert(A_t_times_A); |
| 1624 |
A_t_times_A_inv.mmult(left_inv, A_t); |
| 1625 |
|
| 1626 |
*this = left_inv; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
template <typename number> |
| 1631 |
template <typename number2> |
| 1632 |
void |
| 1633 |
FullMatrix<number>::right_invert(const FullMatrix<number2> &A) |
| 1634 |
{ |
| 1635 |
Assert(!A.empty(), ExcEmptyMatrix()); |
| 1636 |
|
| 1637 |
// If the matrix is square, simply do a |
| 1638 |
// standard inversion |
| 1639 |
if (A.m() == A.n()) |
| 1640 |
{ |
| 1641 |
FullMatrix<number2> right_inv(A.n(), A.m()); |
| 1642 |
right_inv.invert(A); |
| 1643 |
*this = std::move(right_inv); |
| 1644 |
return; |
| 1645 |
} |
| 1646 |
|
| 1647 |
Assert(A.n() > A.m(), ExcDimensionMismatch(A.n(), A.m())); |
| 1648 |
Assert(this->m() == A.n(), ExcDimensionMismatch(this->m(), A.n())); |
| 1649 |
Assert(this->n() == A.m(), ExcDimensionMismatch(this->n(), A.m())); |
| 1650 |
|
| 1651 |
FullMatrix<number> A_t(A.n(), A.m()); |
| 1652 |
FullMatrix<number> A_times_A_t(A.m(), A.m()); |
| 1653 |
FullMatrix<number> A_times_A_t_inv(A.m(), A.m()); |
| 1654 |
FullMatrix<number> right_inv(A.n(), A.m()); |
| 1655 |
|
| 1656 |
A_t.Tadd(A, 1); |
| 1657 |
A.mmult(A_times_A_t, A_t); |
| 1658 |
if (number(A_times_A_t.determinant()) == number(0)) |
| 1659 |
{ |
| 1660 |
Assert(false, ExcSingular()); |
| 1661 |
} |
| 1662 |
else |
| 1663 |
{ |
| 1664 |
A_times_A_t_inv.invert(A_times_A_t); |
| 1665 |
A_t.mmult(right_inv, A_times_A_t_inv); |
| 1666 |
|
| 1667 |
*this = right_inv; |
| 1668 |
} |
| 1669 |
} |
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
template <typename number> |
| 1674 |
template <typename somenumber> |
| 1675 |
void |
| 1676 |
FullMatrix<number>::precondition_Jacobi(Vector<somenumber> &dst, |
| 1677 |
const Vector<somenumber> &src, |
| 1678 |
const number om) const |
| 1679 |
{ |
| 1680 |
Assert(m() == n(), ExcNotQuadratic()); |
| 1681 |
Assert(dst.size() == n(), ExcDimensionMismatch(dst.size(), n())); |
| 1682 |
Assert(src.size() == n(), ExcDimensionMismatch(src.size(), n())); |
| 1683 |
|
| 1684 |
const std::size_t n = src.size(); |
| 1685 |
somenumber *dst_ptr = dst.begin(); |
| 1686 |
const somenumber *src_ptr = src.begin(); |
| 1687 |
|
| 1688 |
for (size_type i = 0; i < n; ++i, ++dst_ptr, ++src_ptr) |
| 1689 |
*dst_ptr = somenumber(om) * *src_ptr / somenumber((*this)(i, i)); |
| 1690 |
} |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
template <typename number> |
| 1695 |
void |
| 1696 |
FullMatrix<number>::print_formatted(std::ostream &out, |
| 1697 |
const unsigned int precision, |
| 1698 |
const bool scientific, |
| 1699 |
const unsigned int width_, |
| 1700 |
const char *zero_string, |
| 1701 |
const double denominator, |
| 1702 |
const double threshold, |
| 1703 |
const char *separator) const |
| 1704 |
{ |
| 1705 |
unsigned int width = width_; |
| 1706 |
|
| 1707 |
Assert((!this->empty()) || (this->n_cols() + this->n_rows() == 0), |
| 1708 |
ExcInternalError()); |
| 1709 |
|
| 1710 |
// set output format, but store old |
| 1711 |
// state |
| 1712 |
std::ios::fmtflags old_flags = out.flags(); |
| 1713 |
std::streamsize old_precision = out.precision(precision); |
| 1714 |
|
| 1715 |
if (scientific) |
| 1716 |
{ |
| 1717 |
out.setf(std::ios::scientific, std::ios::floatfield); |
| 1718 |
if (width == 0u) |
| 1719 |
width = precision + 7; |
| 1720 |
} |
| 1721 |
else |
| 1722 |
{ |
| 1723 |
out.setf(std::ios::fixed, std::ios::floatfield); |
| 1724 |
if (width == 0u) |
| 1725 |
width = precision + 2; |
| 1726 |
} |
| 1727 |
|
| 1728 |
for (size_type i = 0; i < m(); ++i) |
| 1729 |
{ |
| 1730 |
for (size_type j = 0; j < n(); ++j) |
| 1731 |
// we might have complex numbers, so use abs also to check for nan |
| 1732 |
// since there is no isnan on complex numbers |
| 1733 |
if (numbers::is_nan(std::abs((*this)(i, j)))) |
| 1734 |
out << std::setw(width) << (*this)(i, j) << separator; |
| 1735 |
else if (std::abs((*this)(i, j)) > threshold) |
| 1736 |
out << std::setw(width) << (*this)(i, j) * number(denominator) |
| 1737 |
<< separator; |
| 1738 |
else |
| 1739 |
out << std::setw(width) << zero_string << separator; |
| 1740 |
out << std::endl; |
| 1741 |
}; |
| 1742 |
|
| 1743 |
AssertThrow(out.fail() == false, ExcIO()); |
| 1744 |
// reset output format |
| 1745 |
out.flags(old_flags); |
| 1746 |
out.precision(old_precision); |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
template <typename number> |
| 1751 |
void |
| 1752 |
FullMatrix<number>::gauss_jordan() |
| 1753 |
{ |
| 1754 |
Assert(!this->empty(), ExcEmptyMatrix()); |
| 1755 |
Assert(this->n_cols() == this->n_rows(), ExcNotQuadratic()); |
| 1756 |
|
| 1757 |
// see if we can use Lapack algorithms |
| 1758 |
// for this and if the type for 'number' |
| 1759 |
// works for us (it is usually not |
| 1760 |
// efficient to use Lapack for very small |
| 1761 |
// matrices): |
| 1762 |
#ifdef DEAL_II_WITH_LAPACK |
| 1763 |
if (std::is_same_v<number, double> || std::is_same_v<number, float>) |
| 1764 |
if (this->n_cols() > 15 && static_cast<types::blas_int>(this->n_cols()) <= |
| 1765 |
std::numeric_limits<types::blas_int>::max()) |
| 1766 |
{ |
| 1767 |
// In case we have the LAPACK functions |
| 1768 |
// getrf and getri detected by CMake, |
| 1769 |
// we use these algorithms for inversion |
| 1770 |
// since they provide better performance |
| 1771 |
// than the deal.II native functions. |
| 1772 |
// |
| 1773 |
// Note that BLAS/LAPACK stores matrix |
| 1774 |
// elements column-wise (i.e., all values in |
| 1775 |
// one column, then all in the next, etc.), |
| 1776 |
// whereas the FullMatrix stores them |
| 1777 |
// row-wise. |
| 1778 |
// We ignore that difference, and give our |
| 1779 |
// row-wise data to LAPACK, |
| 1780 |
// let LAPACK build the inverse of the |
| 1781 |
// transpose matrix, and read the result as |
| 1782 |
// if it were row-wise again. In other words, |
| 1783 |
// we just got ((A^T)^{-1})^T, which is |
| 1784 |
// A^{-1}. |
| 1785 |
|
| 1786 |
const types::blas_int nn = static_cast<types::blas_int>(this->n()); |
| 1787 |
|
| 1788 |
// workspace for permutations |
| 1789 |
std::vector<types::blas_int> ipiv(nn); |
| 1790 |
types::blas_int info; |
| 1791 |
|
| 1792 |
// Use the LAPACK function getrf for |
| 1793 |
// calculating the LU factorization. |
| 1794 |
getrf(&nn, &nn, this->values.data(), &nn, ipiv.data(), &info); |
| 1795 |
|
| 1796 |
Assert(info >= 0, ExcInternalError()); |
| 1797 |
Assert(info == 0, LACExceptions::ExcSingular()); |
| 1798 |
|
| 1799 |
// scratch array |
| 1800 |
std::vector<number> inv_work(nn); |
| 1801 |
|
| 1802 |
// Use the LAPACK function getri for |
| 1803 |
// calculating the actual inverse using |
| 1804 |
// the LU factorization. |
| 1805 |
getri(&nn, |
| 1806 |
this->values.data(), |
| 1807 |
&nn, |
| 1808 |
ipiv.data(), |
| 1809 |
inv_work.data(), |
| 1810 |
&nn, |
| 1811 |
&info); |
| 1812 |
|
| 1813 |
Assert(info >= 0, ExcInternalError()); |
| 1814 |
Assert(info == 0, LACExceptions::ExcSingular()); |
| 1815 |
|
| 1816 |
return; |
| 1817 |
} |
| 1818 |
|
| 1819 |
#endif |
| 1820 |
|
| 1821 |
// otherwise do it by hand. use the |
| 1822 |
// Gauss-Jordan-Algorithm from |
| 1823 |
// Stoer & Bulirsch I (4th Edition) |
| 1824 |
// p. 153 |
| 1825 |
const size_type N = n(); |
| 1826 |
|
| 1827 |
// first get an estimate of the |
| 1828 |
// size of the elements of this |
| 1829 |
// matrix, for later checks whether |
| 1830 |
// the pivot element is large |
| 1831 |
// enough, or whether we have to |
| 1832 |
// fear that the matrix is not |
| 1833 |
// regular |
| 1834 |
double diagonal_sum = 0; |
| 1835 |
for (size_type i = 0; i < N; ++i) |
| 1836 |
diagonal_sum += std::abs((*this)(i, i)); |
| 1837 |
const double typical_diagonal_element = diagonal_sum / N; |
| 1838 |
|
| 1839 |
// initialize the array that holds |
| 1840 |
// the permutations that we find |
| 1841 |
// during pivot search |
| 1842 |
std::vector<size_type> p(N); |
| 1843 |
for (size_type i = 0; i < N; ++i) |
| 1844 |
p[i] = i; |
| 1845 |
|
| 1846 |
for (size_type j = 0; j < N; ++j) |
| 1847 |
{ |
| 1848 |
// pivot search: search that |
| 1849 |
// part of the line on and |
| 1850 |
// right of the diagonal for |
| 1851 |
// the largest element |
| 1852 |
real_type max = std::abs((*this)(j, j)); |
| 1853 |
size_type r = j; |
| 1854 |
for (size_type i = j + 1; i < N; ++i) |
| 1855 |
{ |
| 1856 |
if (std::abs((*this)(i, j)) > max) |
| 1857 |
{ |
| 1858 |
max = std::abs((*this)(i, j)); |
| 1859 |
r = i; |
| 1860 |
} |
| 1861 |
} |
| 1862 |
// check whether the pivot is |
| 1863 |
// too small |
| 1864 |
Assert(max > 1.e-16 * typical_diagonal_element, ExcNotRegular(max)); |
| 1865 |
|
| 1866 |
// row interchange |
| 1867 |
if (r > j) |
| 1868 |
{ |
| 1869 |
for (size_type k = 0; k < N; ++k) |
| 1870 |
std::swap((*this)(j, k), (*this)(r, k)); |
| 1871 |
|
| 1872 |
std::swap(p[j], p[r]); |
| 1873 |
} |
| 1874 |
|
| 1875 |
// transformation |
| 1876 |
const number hr = number(1.) / (*this)(j, j); |
| 1877 |
(*this)(j, j) = hr; |
| 1878 |
for (size_type k = 0; k < N; ++k) |
| 1879 |
{ |
| 1880 |
if (k == j) |
| 1881 |
continue; |
| 1882 |
for (size_type i = 0; i < N; ++i) |
| 1883 |
{ |
| 1884 |
if (i == j) |
| 1885 |
continue; |
| 1886 |
(*this)(i, k) -= (*this)(i, j) * (*this)(j, k) * hr; |
| 1887 |
} |
| 1888 |
} |
| 1889 |
for (size_type i = 0; i < N; ++i) |
| 1890 |
{ |
| 1891 |
(*this)(i, j) *= hr; |
| 1892 |
(*this)(j, i) *= -hr; |
| 1893 |
} |
| 1894 |
(*this)(j, j) = hr; |
| 1895 |
} |
| 1896 |
// column interchange |
| 1897 |
std::vector<number> hv(N); |
| 1898 |
for (size_type i = 0; i < N; ++i) |
| 1899 |
{ |
| 1900 |
for (size_type k = 0; k < N; ++k) |
| 1901 |
hv[p[k]] = (*this)(i, k); |
| 1902 |
for (size_type k = 0; k < N; ++k) |
| 1903 |
(*this)(i, k) = hv[k]; |
| 1904 |
} |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
template <typename number> |
| 1910 |
std::size_t |
| 1911 |
FullMatrix<number>::memory_consumption() const |
| 1912 |
{ |
| 1913 |
return (sizeof(*this) - sizeof(Table<2, number>) + |
| 1914 |
Table<2, number>::memory_consumption()); |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
DEAL_II_NAMESPACE_CLOSE |
| 1919 |
|
| 1920 |
#endif |