| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef BELOS_TPETRA_UPDATENEWTON_HPP |
| 11 |
#define BELOS_TPETRA_UPDATENEWTON_HPP |
| 12 |
|
| 13 |
#include "Teuchos_ScalarTraits.hpp" |
| 14 |
#include "Teuchos_SerialDenseMatrix.hpp" |
| 15 |
#include <complex> |
| 16 |
|
| 17 |
namespace BelosTpetra { |
| 18 |
namespace Impl { |
| 19 |
|
| 20 |
template<class SC, |
| 21 |
class MV, |
| 22 |
const bool isComplex = Teuchos::ScalarTraits<SC>::isComplex> |
| 23 |
struct UpdateNewton {}; |
| 24 |
|
| 25 |
template<class SC, class MV> |
| 26 |
struct UpdateNewton<SC, MV, true> { |
| 27 |
using STS = Teuchos::ScalarTraits<SC>; |
| 28 |
using real_type = typename STS::magnitudeType; |
| 29 |
using complex_type = std::complex<real_type>; |
| 30 |
using dense_matrix_type = Teuchos::SerialDenseMatrix<int, SC>; |
| 31 |
|
| 32 |
//! Update the vector to convert a Monomial basis to a Newton basis. |
| 33 |
static void |
| 34 |
updateNewtonV (const int iter, |
| 35 |
MV& V, |
| 36 |
const complex_type& theta) |
| 37 |
{ |
| 38 |
const SC one = STS::one (); |
| 39 |
auto Z = V.getVectorNonConst (iter); |
| 40 |
auto W = V.getVectorNonConst (iter+1); |
| 41 |
|
| 42 |
W->update (-theta, *Z, one); |
| 43 |
} |
| 44 |
|
| 45 |
/// \brief Update the change-of-basis matrix to convert a Monomial |
| 46 |
/// basis to a Newton basis. |
| 47 |
static void |
| 48 |
updateNewtonH (const int iter, |
| 49 |
dense_matrix_type& H, |
| 50 |
const complex_type& theta) |
| 51 |
{ |
| 52 |
H(iter, iter) += theta; |
| 53 |
} |
| 54 |
|
| 55 |
static SC |
| 56 |
updateNewtonH (const int i, |
| 57 |
const int j, |
| 58 |
dense_matrix_type& R, |
| 59 |
const complex_type& theta) |
| 60 |
{ |
| 61 |
return theta * R(i, j); |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
template<class SC, class MV> |
| 66 |
struct UpdateNewton<SC, MV, false> { |
| 67 |
using STS = Teuchos::ScalarTraits<SC>; |
| 68 |
using real_type = typename STS::magnitudeType; |
| 69 |
using STM = Teuchos::ScalarTraits<real_type>; |
| 70 |
using complex_type = std::complex<real_type>; |
| 71 |
using dense_matrix_type = Teuchos::SerialDenseMatrix<int, SC>; |
| 72 |
|
| 73 |
static void |
| 74 |
updateNewtonV (const int iter, |
| 75 |
MV& V, |
| 76 |
const complex_type& theta) |
| 77 |
{ |
| 78 |
const SC one = STS::one (); |
| 79 |
auto Z = V.getVectorNonConst (iter); |
| 80 |
auto W = V.getVectorNonConst (iter+1); |
| 81 |
|
| 82 |
W->update (-theta.real(), *Z, one); |
| 83 |
if (theta.imag() != STM::zero()) { |
| 84 |
if (iter > 0) { |
| 85 |
auto Zp = V.getVectorNonConst (iter-1); |
| 86 |
W->update (theta.imag()*theta.imag(), *Zp, one); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
updateNewtonH (const int iter, |
| 93 |
dense_matrix_type& H, |
| 94 |
const complex_type& theta) |
| 95 |
{ |
| 96 |
H(iter, iter) += theta.real(); |
| 97 |
if (theta.imag() != 0.0) { |
| 98 |
if (iter > 0) { |
| 99 |
H(iter, iter-1) -= theta.imag()*theta.imag(); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static SC |
| 105 |
updateNewtonH (const int i, |
| 106 |
const int j, |
| 107 |
dense_matrix_type& R, |
| 108 |
const complex_type& theta) |
| 109 |
{ |
| 110 |
real_type ret = theta.real() * R(i, j); |
| 111 |
if (theta.imag() != 0.0 && j > 0) { |
| 112 |
ret -= (theta.imag()*theta.imag()) * R(i, j-1); |
| 113 |
} |
| 114 |
return ret; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
} // namespace Impl |
| 119 |
} // namespace BelosTpetra |
| 120 |
|
| 121 |
#endif // BELOS_TPETRA_UPDATENEWTON_HPP |