| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2019 - 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_qr_h |
| 16 |
#define dealii_qr_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/exceptions.h> |
| 21 |
|
| 22 |
#include <deal.II/lac/lapack_full_matrix.h> |
| 23 |
#include <deal.II/lac/utilities.h> |
| 24 |
|
| 25 |
#include <boost/signals2/signal.hpp> |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
|
| 29 |
DEAL_II_NAMESPACE_OPEN |
| 30 |
|
| 31 |
/** |
| 32 |
* A base class for thin QR implementations. |
| 33 |
* |
| 34 |
* This class and classes derived from it are meant to build $Q$ and $R$ |
| 35 |
* matrices one row/column at a time, i.e., by growing $R$ matrix from an empty |
| 36 |
* $0\times 0$ matrix to $N\times N$, where $N$ is the number of added column |
| 37 |
* vectors. |
| 38 |
* |
| 39 |
* As a consequence, matrices which have the same number of rows as each vector |
| 40 |
* (i.e. $Q$ matrix) is stored as a collection of vectors of `VectorType`. |
| 41 |
*/ |
| 42 |
template <typename VectorType> |
| 43 |
class BaseQR |
| 44 |
{ |
| 45 |
/** |
| 46 |
* Number type for R matrix. |
| 47 |
*/ |
| 48 |
using Number = typename VectorType::value_type; |
| 49 |
|
| 50 |
protected: |
| 51 |
/** |
| 52 |
* Default private constructor. |
| 53 |
*/ |
| 54 |
BaseQR(); |
| 55 |
|
| 56 |
public: |
| 57 |
/** |
| 58 |
* Destructor. |
| 59 |
*/ |
| 60 |
virtual ~BaseQR() = default; |
| 61 |
|
| 62 |
/** |
| 63 |
* Append @p column to the QR factorization. |
| 64 |
* Returns <code>true</code> if the result is successful, i.e. |
| 65 |
* the columns are linearly independent. Otherwise the @p column |
| 66 |
* is rejected and the return value is <code>false</code>. |
| 67 |
*/ |
| 68 |
virtual bool |
| 69 |
append_column(const VectorType &column) = 0; |
| 70 |
|
| 71 |
/** |
| 72 |
* Remove a column @p k and update QR factorization. |
| 73 |
*/ |
| 74 |
virtual void |
| 75 |
remove_column(const unsigned int k = 0) = 0; |
| 76 |
|
| 77 |
/** |
| 78 |
* Return size of the subspace. |
| 79 |
*/ |
| 80 |
unsigned int |
| 81 |
size() const; |
| 82 |
|
| 83 |
/** |
| 84 |
* Return the current upper triangular matrix R. |
| 85 |
*/ |
| 86 |
const LAPACKFullMatrix<Number> & |
| 87 |
get_R() const; |
| 88 |
|
| 89 |
/** |
| 90 |
* Solve $Rx=y$. Vectors @p x and @p y should be consistent |
| 91 |
* with the current size of the subspace. |
| 92 |
* If @p transpose is <code>true</code>, $R^Tx=y$ is solved instead. |
| 93 |
*/ |
| 94 |
void |
| 95 |
solve(Vector<Number> &x, |
| 96 |
const Vector<Number> &y, |
| 97 |
const bool transpose = false) const; |
| 98 |
|
| 99 |
/** |
| 100 |
* Set $y = Qx$. The size of $x$ should be consistent with the |
| 101 |
* size of the R matrix. |
| 102 |
*/ |
| 103 |
virtual void |
| 104 |
multiply_with_Q(VectorType &y, const Vector<Number> &x) const = 0; |
| 105 |
|
| 106 |
/** |
| 107 |
* Set $y = Q^Tx$. The size of $x$ should be consistent with the |
| 108 |
* size of column vectors. |
| 109 |
*/ |
| 110 |
virtual void |
| 111 |
multiply_with_QT(Vector<Number> &y, const VectorType &x) const = 0; |
| 112 |
|
| 113 |
/** |
| 114 |
* Set $y = QRx$. The size of $x$ should be consistent with the |
| 115 |
* size of the R matrix. |
| 116 |
*/ |
| 117 |
virtual void |
| 118 |
multiply_with_A(VectorType &y, const Vector<Number> &x) const = 0; |
| 119 |
|
| 120 |
/** |
| 121 |
* Set $y = R^T Q^Tx$. The size of $x$ should be consistent with the |
| 122 |
* size of column vectors. |
| 123 |
*/ |
| 124 |
virtual void |
| 125 |
multiply_with_AT(Vector<Number> &y, const VectorType &x) const = 0; |
| 126 |
|
| 127 |
/** |
| 128 |
* Connect a slot to retrieve a notification when the Givens rotations |
| 129 |
* are performed. |
| 130 |
* |
| 131 |
* The function takes two indices, @p i and @p j, describing the plane of |
| 132 |
* rotation, and a triplet of numbers @p csr (cosine, sine and radius, see |
| 133 |
* Utilities::LinearAlgebra::givens_rotation()) which represents the rotation |
| 134 |
* matrix. |
| 135 |
*/ |
| 136 |
boost::signals2::connection |
| 137 |
connect_givens_slot( |
| 138 |
const std::function<void(const unsigned int i, |
| 139 |
const unsigned int j, |
| 140 |
const std::array<Number, 3> &csr)> &slot); |
| 141 |
|
| 142 |
protected: |
| 143 |
/** |
| 144 |
* Compute $y=Hx$ where $H$ is the matrix formed by the column vectors stored |
| 145 |
* by this object. |
| 146 |
*/ |
| 147 |
void |
| 148 |
multiply_with_cols(VectorType &y, const Vector<Number> &x) const; |
| 149 |
|
| 150 |
/** |
| 151 |
* Multiply with transpose columns stored in the object. |
| 152 |
*/ |
| 153 |
void |
| 154 |
multiply_with_colsT(Vector<Number> &y, const VectorType &x) const; |
| 155 |
|
| 156 |
/** |
| 157 |
* A vector of unique pointers to store columns. |
| 158 |
*/ |
| 159 |
std::vector<std::unique_ptr<VectorType>> columns; |
| 160 |
|
| 161 |
/** |
| 162 |
* Matrix to store R. |
| 163 |
*/ |
| 164 |
LAPACKFullMatrix<Number> R; |
| 165 |
|
| 166 |
/** |
| 167 |
* Current size (number of columns in Q). |
| 168 |
*/ |
| 169 |
unsigned int current_size; |
| 170 |
|
| 171 |
/** |
| 172 |
* Signal used to retrieve a notification |
| 173 |
* when Givens rotations are performed in the `(i,j)`-plane. |
| 174 |
*/ |
| 175 |
boost::signals2::signal<void(const unsigned int i, |
| 176 |
const unsigned int j, |
| 177 |
const std::array<Number, 3> &)> |
| 178 |
givens_signal; |
| 179 |
}; |
| 180 |
|
| 181 |
// clang-format off |
| 182 |
/** |
| 183 |
* A class to compute and store the QR factorization of a matrix represented by a set of column vectors. |
| 184 |
* |
| 185 |
* The class is design to update a given (possibly empty) QR factorization |
| 186 |
* of a matrix $A$ (constructed incrementally by providing its columns) |
| 187 |
* due to the addition of a new column vector to $A$. This is equivalent to |
| 188 |
* constructing an orthonormal basis by the Gram-Schmidt procedure. |
| 189 |
* The class also provides update functionality when the first column |
| 190 |
* is removed. |
| 191 |
* |
| 192 |
* The `VectorType` template argument may either be a parallel and serial vector, and only need |
| 193 |
* to have basic operations such as additions, scalar product, etc. |
| 194 |
* It also needs to have a copy-constructor. |
| 195 |
* |
| 196 |
* See sections 6.5.2-6.5.3 on pp. 335-338 in @cite Golub2013 as well as |
| 197 |
* @cite Daniel1976 and @cite Reichel1990. |
| 198 |
*/ |
| 199 |
// clang-format on |
| 200 |
template <typename VectorType> |
| 201 |
class QR : public BaseQR<VectorType> |
| 202 |
{ |
| 203 |
public: |
| 204 |
/** |
| 205 |
* Number type for R matrix. |
| 206 |
*/ |
| 207 |
using Number = typename VectorType::value_type; |
| 208 |
|
| 209 |
/** |
| 210 |
* Default constructor. |
| 211 |
*/ |
| 212 |
QR(); |
| 213 |
|
| 214 |
/** |
| 215 |
* Destructor. |
| 216 |
*/ |
| 217 |
virtual ~QR() = default; |
| 218 |
|
| 219 |
/** |
| 220 |
* @copydoc BaseQR::append_column |
| 221 |
* |
| 222 |
* @note Currently this function always returns <code>true</code>. |
| 223 |
*/ |
| 224 |
virtual bool |
| 225 |
append_column(const VectorType &column) override; |
| 226 |
|
| 227 |
/** |
| 228 |
* Remove first column and update QR factorization. |
| 229 |
* |
| 230 |
* Starting from the given QR decomposition |
| 231 |
* $QR= A = [a_1\,\dots a_n], \quad a_i \in {\mathbb R}^m$ |
| 232 |
* we aim at computing factorization of |
| 233 |
* $\tilde Q \tilde R= \tilde A = [a_2\,\dots a_n], \quad a_i \in {\mathbb |
| 234 |
* R}^m$. |
| 235 |
* |
| 236 |
* The standard approach is to partition $R$ as |
| 237 |
* \f[ |
| 238 |
* R = |
| 239 |
* \begin{bmatrix} |
| 240 |
* r_{11} & w^T \\ |
| 241 |
* 0 & R_{33} |
| 242 |
* \end{bmatrix} |
| 243 |
* \f] |
| 244 |
* It then follows that |
| 245 |
* \f[ |
| 246 |
* Q^T \tilde A = |
| 247 |
* \begin{bmatrix} |
| 248 |
* 0 & w^T \\ |
| 249 |
* 0 & R_{33} |
| 250 |
* \end{bmatrix} |
| 251 |
* \f] |
| 252 |
* is upper Hessenberg where unwanted sub-diagonal elements can be |
| 253 |
* zeroed by a sequence of Givens rotations. |
| 254 |
* |
| 255 |
* Note that $\tilde R^T \tilde R = \tilde A^T \tilde A$, |
| 256 |
* where the RHS is included in $A^T A = R^T R$. Therefore |
| 257 |
* $\tilde R$ can be obtained by Cholesky decomposition. |
| 258 |
*/ |
| 259 |
virtual void |
| 260 |
remove_column(const unsigned int k = 0) override; |
| 261 |
|
| 262 |
virtual void |
| 263 |
multiply_with_Q(VectorType &y, const Vector<Number> &x) const override; |
| 264 |
|
| 265 |
virtual void |
| 266 |
multiply_with_QT(Vector<Number> &y, const VectorType &x) const override; |
| 267 |
|
| 268 |
virtual void |
| 269 |
multiply_with_A(VectorType &y, const Vector<Number> &x) const override; |
| 270 |
|
| 271 |
virtual void |
| 272 |
multiply_with_AT(Vector<Number> &y, const VectorType &x) const override; |
| 273 |
|
| 274 |
private: |
| 275 |
/** |
| 276 |
* Apply givens rotation in the `(i,j)`-plane to @p Q and @p R so that |
| 277 |
* <code>R(k,k)</code> is zeroed. |
| 278 |
* |
| 279 |
* See Chapter 5.1.9 of Golub 2013, Matrix computations. |
| 280 |
*/ |
| 281 |
void |
| 282 |
apply_givens_rotation(const unsigned int i, const unsigned int k); |
| 283 |
|
| 284 |
/** |
| 285 |
* Temporary vector needed to do Givens rotation of Q. |
| 286 |
*/ |
| 287 |
VectorType tmp; |
| 288 |
}; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* A class to obtain the triangular $R$ matrix of the $A=QR$ factorization |
| 294 |
* together with the matrix $A$ itself. The orthonormal matrix $Q$ is not stored |
| 295 |
* explicitly, the name of the class. |
| 296 |
* The multiplication with $Q$ can be represented as $Q=A R^{-1}$, whereas the |
| 297 |
* multiplication with $Q^T$ is given by $Q^T=R^{-T}A^T$. |
| 298 |
* |
| 299 |
* The class is designed to update a given (possibly empty) QR factorization |
| 300 |
* due to the addition of a new column vector. This is equivalent to |
| 301 |
* constructing an orthonormal basis by the Gram-Schmidt procedure. |
| 302 |
* The class also provides update functionality when the column |
| 303 |
* is removed. |
| 304 |
* |
| 305 |
* The `VectorType` template argument may either be a parallel and serial |
| 306 |
* vector, and only need to have basic operations such as additions, scalar |
| 307 |
* product, etc. It also needs to have a copy-constructor. |
| 308 |
*/ |
| 309 |
template <typename VectorType> |
| 310 |
class ImplicitQR : public BaseQR<VectorType> |
| 311 |
{ |
| 312 |
public: |
| 313 |
/** |
| 314 |
* Number type for R matrix. |
| 315 |
*/ |
| 316 |
using Number = typename VectorType::value_type; |
| 317 |
|
| 318 |
/** |
| 319 |
* Default constructor. |
| 320 |
*/ |
| 321 |
ImplicitQR(); |
| 322 |
|
| 323 |
/** |
| 324 |
* Destructor. |
| 325 |
*/ |
| 326 |
virtual ~ImplicitQR() = default; |
| 327 |
|
| 328 |
virtual bool |
| 329 |
append_column(const VectorType &column); |
| 330 |
|
| 331 |
/** |
| 332 |
* Remove column and update QR factorization. |
| 333 |
* |
| 334 |
* Starting from the given QR decomposition |
| 335 |
* $QR= A = [a_1\,\dots a_n], \quad a_i \in R^m$ |
| 336 |
* we aim at computing factorization of |
| 337 |
* $\tilde Q \tilde R= \tilde A = [a_2\,\dots a_n], \quad a_i \in R^m$. |
| 338 |
* |
| 339 |
* Note that $\tilde R^T \tilde R = \tilde A^T \tilde A$, |
| 340 |
* where the RHS is included in $A^T A = R^T R$. Therefore |
| 341 |
* $\tilde R$ can be obtained by Cholesky decomposition. |
| 342 |
*/ |
| 343 |
virtual void |
| 344 |
remove_column(const unsigned int k = 0); |
| 345 |
|
| 346 |
virtual void |
| 347 |
multiply_with_Q(VectorType &y, const Vector<Number> &x) const; |
| 348 |
|
| 349 |
virtual void |
| 350 |
multiply_with_QT(Vector<Number> &y, const VectorType &x) const; |
| 351 |
|
| 352 |
virtual void |
| 353 |
multiply_with_A(VectorType &y, const Vector<Number> &x) const; |
| 354 |
|
| 355 |
virtual void |
| 356 |
multiply_with_AT(Vector<Number> &y, const VectorType &x) const; |
| 357 |
|
| 358 |
/** |
| 359 |
* Connect a slot to implement a custom check of linear dependency |
| 360 |
* during addition of a column. |
| 361 |
* |
| 362 |
* Here, @p u is the last column of the to-be R matrix, @p rho |
| 363 |
* is its diagonal and @p col_norm_sqr is the square of the $l2$ norm of the column. |
| 364 |
* The function should return <code>true</code> if the new column is |
| 365 |
* linearly independent. |
| 366 |
*/ |
| 367 |
boost::signals2::connection |
| 368 |
connect_append_column_slot( |
| 369 |
const std::function<bool(const Vector<Number> &u, |
| 370 |
const Number &rho2, |
| 371 |
const Number &col_norm_sqr)> &slot); |
| 372 |
|
| 373 |
private: |
| 374 |
/** |
| 375 |
* Apply givens rotation in the `(i,k)`-plane to zero out $R(k,k)$. |
| 376 |
*/ |
| 377 |
void |
| 378 |
apply_givens_rotation(const unsigned int i, const unsigned int k); |
| 379 |
|
| 380 |
/** |
| 381 |
* Signal used to decide if the new column is linear dependent. |
| 382 |
* |
| 383 |
* Here, @p u is the last column of the to-be R matrix, @p rho |
| 384 |
* is its diagonal and @p col_norm_sqr is the square of the $l2$ norm of the column. |
| 385 |
* The function should return <code>true</code> if the new column is |
| 386 |
* linearly independent. |
| 387 |
*/ |
| 388 |
boost::signals2::signal<bool(const Vector<Number> &u, |
| 389 |
const Number &rho, |
| 390 |
const Number &col_norm_sqr)> |
| 391 |
column_signal; |
| 392 |
}; |
| 393 |
|
| 394 |
// ------------------- inline and template functions ---------------- |
| 395 |
#ifndef DOXYGEN |
| 396 |
|
| 397 |
namespace internal |
| 398 |
{ |
| 399 |
namespace QRImplementation |
| 400 |
{ |
| 401 |
// We want to avoid including our own LAPACK wrapper header in any external |
| 402 |
// headers to avoid possible conflicts with other packages that may define |
| 403 |
// their own such header. At the same time we want to be able to call some |
| 404 |
// LAPACK functions from the template functions below. To resolve both |
| 405 |
// problems define some extra wrappers here that can be in the header: |
| 406 |
template <typename Number> |
| 407 |
void |
| 408 |
call_trmv(const char uplo, |
| 409 |
const char trans, |
| 410 |
const char diag, |
| 411 |
const types::blas_int n, |
| 412 |
const Number *a, |
| 413 |
const types::blas_int lda, |
| 414 |
Number *x, |
| 415 |
const types::blas_int incx); |
| 416 |
|
| 417 |
template <typename Number> |
| 418 |
void |
| 419 |
call_trtrs(const char uplo, |
| 420 |
const char trans, |
| 421 |
const char diag, |
| 422 |
const types::blas_int n, |
| 423 |
const types::blas_int nrhs, |
| 424 |
const Number *a, |
| 425 |
const types::blas_int lda, |
| 426 |
Number *b, |
| 427 |
const types::blas_int ldb, |
| 428 |
types::blas_int *info); |
| 429 |
} // namespace QRImplementation |
| 430 |
} // namespace internal |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
template <typename VectorType> |
| 435 |
BaseQR<VectorType>::BaseQR() |
| 436 |
: current_size(0) |
| 437 |
{ |
| 438 |
R.set_property(LAPACKSupport::upper_triangular); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
template <typename VectorType> |
| 444 |
unsigned int |
| 445 |
BaseQR<VectorType>::size() const |
| 446 |
{ |
| 447 |
return current_size; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
template <typename VectorType> |
| 453 |
const LAPACKFullMatrix<typename BaseQR<VectorType>::Number> & |
| 454 |
BaseQR<VectorType>::get_R() const |
| 455 |
{ |
| 456 |
return R; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
template <typename VectorType> |
| 462 |
void |
| 463 |
BaseQR<VectorType>::solve(Vector<Number> &x, |
| 464 |
const Vector<Number> &y, |
| 465 |
const bool transpose) const |
| 466 |
{ |
| 467 |
Assert(x.size() == this->current_size, |
| 468 |
ExcDimensionMismatch(x.size(), this->current_size)); |
| 469 |
Assert(y.size() == this->current_size, |
| 470 |
ExcDimensionMismatch(y.size(), this->current_size)); |
| 471 |
|
| 472 |
// copy if the two vectors are not the same |
| 473 |
if (&x != &y) |
| 474 |
x = y; |
| 475 |
|
| 476 |
const int lda = this->current_size; |
| 477 |
const int ldb = this->current_size; |
| 478 |
const int N = this->current_size; |
| 479 |
const int n_rhs = 1; |
| 480 |
int info = 0; |
| 481 |
internal::QRImplementation::call_trtrs('U', |
| 482 |
transpose ? 'T' : 'N', |
| 483 |
'N', |
| 484 |
N, |
| 485 |
n_rhs, |
| 486 |
&this->R(0, 0), |
| 487 |
lda, |
| 488 |
&x(0), |
| 489 |
ldb, |
| 490 |
&info); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
template <typename VectorType> |
| 496 |
void |
| 497 |
BaseQR<VectorType>::multiply_with_cols(VectorType &y, |
| 498 |
const Vector<Number> &x) const |
| 499 |
{ |
| 500 |
Assert(x.size() == this->current_size, |
| 501 |
ExcDimensionMismatch(x.size(), this->current_size)); |
| 502 |
|
| 503 |
y = 0.; |
| 504 |
for (unsigned int j = 0; j < this->current_size; ++j) |
| 505 |
y.add(x[j], *this->columns[j]); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
template <typename VectorType> |
| 511 |
void |
| 512 |
BaseQR<VectorType>::multiply_with_colsT(Vector<Number> &y, |
| 513 |
const VectorType &x) const |
| 514 |
{ |
| 515 |
Assert(y.size() == this->current_size, |
| 516 |
ExcDimensionMismatch(y.size(), this->current_size)); |
| 517 |
|
| 518 |
for (unsigned int j = 0; j < this->current_size; ++j) |
| 519 |
y[j] = (*this->columns[j]) * x; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename VectorType> |
| 525 |
boost::signals2::connection |
| 526 |
BaseQR<VectorType>::connect_givens_slot( |
| 527 |
const std::function<void(const unsigned int i, |
| 528 |
const unsigned int j, |
| 529 |
const std::array<Number, 3> &)> &slot) |
| 530 |
{ |
| 531 |
return givens_signal.connect(slot); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
template <typename VectorType> |
| 537 |
boost::signals2::connection |
| 538 |
ImplicitQR<VectorType>::connect_append_column_slot( |
| 539 |
const std::function<bool(const Vector<Number> &u, |
| 540 |
const Number &rho, |
| 541 |
const Number &col_norm_sqr)> &slot) |
| 542 |
{ |
| 543 |
return column_signal.connect(slot); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
template <typename VectorType> |
| 549 |
ImplicitQR<VectorType>::ImplicitQR() |
| 550 |
: BaseQR<VectorType>() |
| 551 |
{} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
template <typename VectorType> |
| 556 |
bool |
| 557 |
ImplicitQR<VectorType>::append_column(const VectorType &column) |
| 558 |
{ |
| 559 |
if (this->current_size == 0) |
| 560 |
{ |
| 561 |
this->R.grow_or_shrink(this->current_size + 1); |
| 562 |
this->columns.push_back(std::make_unique<VectorType>(column)); |
| 563 |
this->R(0, 0) = column.l2_norm(); |
| 564 |
++this->current_size; |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
// first get scalar products with A^T |
| 569 |
Vector<Number> u(this->current_size); |
| 570 |
this->multiply_with_AT(u, column); |
| 571 |
|
| 572 |
// now solve R^T x = (A^T * column) |
| 573 |
const int lda = this->current_size; |
| 574 |
const int ldb = this->current_size; |
| 575 |
const int N = this->current_size; |
| 576 |
const int n_rhs = 1; |
| 577 |
int info = 0; |
| 578 |
internal::QRImplementation::call_trtrs( |
| 579 |
'U', 'T', 'N', N, n_rhs, &this->R(0, 0), lda, &u(0), ldb, &info); |
| 580 |
|
| 581 |
// finally get the diagonal element: |
| 582 |
// rho2 = |column|^2 - |u|^2 |
| 583 |
const Number column_norm_sqr = column.norm_sqr(); |
| 584 |
const Number rho2 = column_norm_sqr - u.norm_sqr(); |
| 585 |
const bool linearly_independent = |
| 586 |
column_signal.empty() ? rho2 > 0 : |
| 587 |
column_signal(u, rho2, column_norm_sqr).get(); |
| 588 |
|
| 589 |
// bail out if it turns out to be linearly dependent |
| 590 |
if (!linearly_independent) |
| 591 |
return false; |
| 592 |
|
| 593 |
// at this point we update is successful and we can enlarge R |
| 594 |
// and store the column: |
| 595 |
this->columns.push_back(std::make_unique<VectorType>(column)); |
| 596 |
this->R.grow_or_shrink(this->current_size + 1); |
| 597 |
this->R(this->current_size, this->current_size) = std::sqrt(rho2); |
| 598 |
for (unsigned int i = 0; i < this->current_size; ++i) |
| 599 |
this->R(i, this->current_size) = u(i); |
| 600 |
|
| 601 |
this->current_size++; |
| 602 |
} |
| 603 |
|
| 604 |
return true; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
template <typename VectorType> |
| 610 |
void |
| 611 |
ImplicitQR<VectorType>::apply_givens_rotation(const unsigned int i, |
| 612 |
const unsigned int k) |
| 613 |
{ |
| 614 |
AssertIndexRange(i, k); |
| 615 |
AssertIndexRange(k, this->current_size); |
| 616 |
const std::array<Number, 3> csr = |
| 617 |
dealii::Utilities::LinearAlgebra::givens_rotation<Number>(this->R(i, k), |
| 618 |
this->R(k, k)); |
| 619 |
|
| 620 |
// first, set k'th column: |
| 621 |
this->R(i, k) = csr[2]; |
| 622 |
this->R(k, k) = 0.; |
| 623 |
// now do the rest: |
| 624 |
for (unsigned int j = 0; j < this->R.n(); ++j) |
| 625 |
if (j != k) |
| 626 |
{ |
| 627 |
const Number t = this->R(i, j); |
| 628 |
this->R(i, j) = csr[0] * this->R(i, j) + csr[1] * this->R(k, j); |
| 629 |
this->R(k, j) = -csr[1] * t + csr[0] * this->R(k, j); |
| 630 |
} |
| 631 |
|
| 632 |
if (!this->givens_signal.empty()) |
| 633 |
this->givens_signal(i, k, csr); |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
template <typename VectorType> |
| 639 |
void |
| 640 |
ImplicitQR<VectorType>::remove_column(const unsigned int k) |
| 641 |
{ |
| 642 |
// before actually removing a column from Q and resizing R, |
| 643 |
// apply givens rotations to bring H into upper triangular form: |
| 644 |
for (unsigned int j = k + 1; j < this->R.n(); ++j) |
| 645 |
{ |
| 646 |
const unsigned int i = j - 1; |
| 647 |
apply_givens_rotation(i, j); |
| 648 |
} |
| 649 |
|
| 650 |
// remove last row and k-th column |
| 651 |
--this->current_size; |
| 652 |
this->R.remove_row_and_column(this->current_size, k); |
| 653 |
|
| 654 |
// Finally remove the column from A |
| 655 |
this->columns.erase(this->columns.begin() + k); |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
template <typename VectorType> |
| 661 |
void |
| 662 |
ImplicitQR<VectorType>::multiply_with_Q(VectorType &y, |
| 663 |
const Vector<Number> &x) const |
| 664 |
{ |
| 665 |
// A = QR |
| 666 |
// A R^{-1} = Q |
| 667 |
Vector<Number> x1 = x; |
| 668 |
BaseQR<VectorType>::solve(x1, x1, false); |
| 669 |
multiply_with_A(y, x1); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
template <typename VectorType> |
| 675 |
void |
| 676 |
ImplicitQR<VectorType>::multiply_with_QT(Vector<Number> &y, |
| 677 |
const VectorType &x) const |
| 678 |
{ |
| 679 |
// A = QR |
| 680 |
// A^T = R^T Q^T |
| 681 |
// {R^T}^{-1} A^T = Q^T |
| 682 |
multiply_with_AT(y, x); |
| 683 |
BaseQR<VectorType>::solve(y, y, true); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
template <typename VectorType> |
| 689 |
void |
| 690 |
ImplicitQR<VectorType>::multiply_with_A(VectorType &y, |
| 691 |
const Vector<Number> &x) const |
| 692 |
{ |
| 693 |
BaseQR<VectorType>::multiply_with_cols(y, x); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
template <typename VectorType> |
| 699 |
void |
| 700 |
ImplicitQR<VectorType>::multiply_with_AT(Vector<Number> &y, |
| 701 |
const VectorType &x) const |
| 702 |
{ |
| 703 |
BaseQR<VectorType>::multiply_with_colsT(y, x); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
template <typename VectorType> |
| 709 |
QR<VectorType>::QR() |
| 710 |
: BaseQR<VectorType>() |
| 711 |
{} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
template <typename VectorType> |
| 716 |
bool |
| 717 |
QR<VectorType>::append_column(const VectorType &column) |
| 718 |
{ |
| 719 |
// resize R: |
| 720 |
this->R.grow_or_shrink(this->current_size + 1); |
| 721 |
this->columns.push_back(std::make_unique<VectorType>(column)); |
| 722 |
|
| 723 |
// now a Gram-Schmidt part: orthonormalize the new column |
| 724 |
// against everything we have so far: |
| 725 |
auto &last_col = *this->columns.back(); |
| 726 |
for (unsigned int i = 0; i < this->current_size; ++i) |
| 727 |
{ |
| 728 |
const auto &i_col = *this->columns[i]; |
| 729 |
this->R(i, this->current_size) = i_col * last_col; |
| 730 |
last_col.add(-this->R(i, this->current_size), i_col); |
| 731 |
} |
| 732 |
|
| 733 |
this->R(this->current_size, this->current_size) = last_col.l2_norm(); |
| 734 |
|
| 735 |
Assert(this->R(this->current_size, this->current_size) > 0., |
| 736 |
ExcDivideByZero()); |
| 737 |
last_col *= 1. / this->R(this->current_size, this->current_size); |
| 738 |
|
| 739 |
++this->current_size; |
| 740 |
return true; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
template <typename VectorType> |
| 746 |
void |
| 747 |
QR<VectorType>::apply_givens_rotation(const unsigned int i, |
| 748 |
const unsigned int k) |
| 749 |
{ |
| 750 |
AssertIndexRange(i, k); |
| 751 |
AssertIndexRange(k, this->current_size); |
| 752 |
const std::array<Number, 3> csr = |
| 753 |
dealii::Utilities::LinearAlgebra::givens_rotation<Number>(this->R(i, k), |
| 754 |
this->R(k, k)); |
| 755 |
|
| 756 |
// first, set k'th column: |
| 757 |
this->R(i, k) = csr[2]; |
| 758 |
this->R(k, k) = 0.; |
| 759 |
// now do the rest: |
| 760 |
for (unsigned int j = 0; j < this->R.n(); ++j) |
| 761 |
if (j != k) |
| 762 |
{ |
| 763 |
const Number t = this->R(i, j); |
| 764 |
this->R(i, j) = csr[0] * this->R(i, j) + csr[1] * this->R(k, j); |
| 765 |
this->R(k, j) = -csr[1] * t + csr[0] * this->R(k, j); |
| 766 |
} |
| 767 |
|
| 768 |
// now adjust i,k columns due to multiplication with the |
| 769 |
// transpose Givens matrix from right: |
| 770 |
auto &col_i = *this->columns[i]; |
| 771 |
auto &col_k = *this->columns[k]; |
| 772 |
// save column i: |
| 773 |
tmp = col_i; |
| 774 |
col_i.sadd(csr[0], csr[1], col_k); |
| 775 |
col_k.sadd(csr[0], -csr[1], tmp); |
| 776 |
|
| 777 |
if (!this->givens_signal.empty()) |
| 778 |
this->givens_signal(i, k, csr); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
template <typename VectorType> |
| 784 |
void |
| 785 |
QR<VectorType>::remove_column(const unsigned int k) |
| 786 |
{ |
| 787 |
AssertIndexRange(k, this->current_size); |
| 788 |
Assert(this->current_size > 0, |
| 789 |
ExcMessage("Can not remove a column if QR is empty")); |
| 790 |
// apply a sequence of Givens rotations |
| 791 |
// see section 6.5 "Updating matrix factorizations" in Golub 2013, Matrix |
| 792 |
// computations |
| 793 |
|
| 794 |
// So we want to have QR for \tilde A \in R^{m*(n-1)} |
| 795 |
// if we remove the column k, we end up with upper Hessenberg matrix |
| 796 |
// x x x x x |
| 797 |
// x x x x |
| 798 |
// H = x x x |
| 799 |
// x x x |
| 800 |
// x x |
| 801 |
// x |
| 802 |
// where k = 2 (3rd column), m = 7, n = 6 |
| 803 |
// |
| 804 |
// before actually removing a column from Q and resizing R, |
| 805 |
// apply givens rotations to bring H into upper triangular form: |
| 806 |
for (unsigned int j = k + 1; j < this->R.n(); ++j) |
| 807 |
{ |
| 808 |
const unsigned int i = j - 1; |
| 809 |
apply_givens_rotation(i, j); |
| 810 |
} |
| 811 |
|
| 812 |
// now we can throw away the column from Q and adjust R |
| 813 |
// since we do thin-QR, after Givens rotations we need to throw |
| 814 |
// away the last column: |
| 815 |
const unsigned int size_minus_1 = this->columns.size() - 1; |
| 816 |
this->columns.erase(this->columns.begin() + size_minus_1); |
| 817 |
|
| 818 |
// remove last row and k-th column |
| 819 |
--this->current_size; |
| 820 |
this->R.remove_row_and_column(this->current_size, k); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
template <typename VectorType> |
| 826 |
void |
| 827 |
QR<VectorType>::multiply_with_Q(VectorType &y, const Vector<Number> &x) const |
| 828 |
{ |
| 829 |
BaseQR<VectorType>::multiply_with_cols(y, x); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
template <typename VectorType> |
| 835 |
void |
| 836 |
QR<VectorType>::multiply_with_QT(Vector<Number> &y, const VectorType &x) const |
| 837 |
{ |
| 838 |
BaseQR<VectorType>::multiply_with_colsT(y, x); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
template <typename VectorType> |
| 844 |
void |
| 845 |
QR<VectorType>::multiply_with_A(VectorType &y, const Vector<Number> &x) const |
| 846 |
{ |
| 847 |
Vector<Number> x1 = x; |
| 848 |
const int N = this->current_size; |
| 849 |
const int lda = N; |
| 850 |
const int incx = 1; |
| 851 |
internal::QRImplementation::call_trmv( |
| 852 |
'U', 'N', 'N', N, &this->R(0, 0), lda, &x1[0], incx); |
| 853 |
|
| 854 |
multiply_with_Q(y, x1); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
template <typename VectorType> |
| 860 |
void |
| 861 |
QR<VectorType>::multiply_with_AT(Vector<Number> &y, const VectorType &x) const |
| 862 |
{ |
| 863 |
multiply_with_QT(y, x); |
| 864 |
|
| 865 |
const int N = this->current_size; |
| 866 |
const int lda = N; |
| 867 |
const int incx = 1; |
| 868 |
internal::QRImplementation::call_trmv( |
| 869 |
'U', 'T', 'N', N, &this->R(0, 0), lda, &y[0], incx); |
| 870 |
} |
| 871 |
|
| 872 |
#endif // no DOXYGEN |
| 873 |
|
| 874 |
DEAL_II_NAMESPACE_CLOSE |
| 875 |
|
| 876 |
#endif |