| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Teuchos: Common Tools Package |
| 4 |
// |
| 5 |
// Copyright 2004 NTESS and the Teuchos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef _TEUCHOS_BLAS_UQ_PCE_HPP_ |
| 11 |
#define _TEUCHOS_BLAS_UQ_PCE_HPP_ |
| 12 |
|
| 13 |
#include "Teuchos_BLAS.hpp" |
| 14 |
#include "Sacado_UQ_PCE.hpp" |
| 15 |
|
| 16 |
// Specialize some things used in the default BLAS implementation that |
| 17 |
// don't seem correct for UQ::PCE scalar type |
| 18 |
namespace Teuchos { |
| 19 |
|
| 20 |
namespace details { |
| 21 |
|
| 22 |
template<typename Storage> |
| 23 |
class GivensRotator<Sacado::UQ::PCE<Storage>, false> { |
| 24 |
public: |
| 25 |
typedef Sacado::UQ::PCE<Storage> ScalarType; |
| 26 |
typedef ScalarType c_type; |
| 27 |
|
| 28 |
void |
| 29 |
ROTG (ScalarType* da, |
| 30 |
ScalarType* db, |
| 31 |
ScalarType* c, |
| 32 |
ScalarType* s) const { |
| 33 |
|
| 34 |
typedef ScalarTraits<ScalarType> STS; |
| 35 |
|
| 36 |
// This is a straightforward translation into C++ of the |
| 37 |
// reference BLAS' implementation of DROTG. You can get |
| 38 |
// the Fortran 77 source code of DROTG here: |
| 39 |
// |
| 40 |
// http://www.netlib.org/blas/drotg.f |
| 41 |
// |
| 42 |
// I used the following rules to translate Fortran types and |
| 43 |
// intrinsic functions into C++: |
| 44 |
// |
| 45 |
// DOUBLE PRECISION -> ScalarType |
| 46 |
// DABS -> STS::magnitude |
| 47 |
// DSQRT -> STM::squareroot |
| 48 |
// DSIGN -> SIGN (see below) |
| 49 |
// |
| 50 |
// DSIGN(x,y) (the old DOUBLE PRECISION type-specific form of |
| 51 |
// the Fortran type-generic SIGN intrinsic) required special |
| 52 |
// translation, which we did in a separate utility function in |
| 53 |
// the specializaton of GivensRotator for real arithmetic. |
| 54 |
// (ROTG for complex arithmetic doesn't require this function.) |
| 55 |
// C99 provides a copysign() math library function, but we are |
| 56 |
// not able to rely on the existence of C99 functions here. |
| 57 |
ScalarType r, roe, scale, z; |
| 58 |
|
| 59 |
roe = *db; |
| 60 |
if (STS::magnitude (*da) > STS::magnitude (*db)) { |
| 61 |
roe = *da; |
| 62 |
} |
| 63 |
scale = STS::magnitude (*da) + STS::magnitude (*db); |
| 64 |
if (scale == STS::zero()) { |
| 65 |
*c = STS::one(); |
| 66 |
*s = STS::zero(); |
| 67 |
r = STS::zero(); |
| 68 |
z = STS::zero(); |
| 69 |
} else { |
| 70 |
// I introduced temporaries into the translated BLAS code in |
| 71 |
// order to make the expression easier to read and also save |
| 72 |
// a few floating-point operations. |
| 73 |
const ScalarType da_scaled = *da / scale; |
| 74 |
const ScalarType db_scaled = *db / scale; |
| 75 |
r = scale * STS::squareroot (da_scaled*da_scaled + db_scaled*db_scaled); |
| 76 |
r = SIGN (STS::one(), roe) * r; |
| 77 |
*c = *da / r; |
| 78 |
*s = *db / r; |
| 79 |
z = STS::one(); |
| 80 |
if (STS::magnitude (*da) > STS::magnitude (*db)) { |
| 81 |
z = *s; |
| 82 |
} |
| 83 |
if (STS::magnitude (*db) >= STS::magnitude (*da) && *c != STS::zero()) { |
| 84 |
z = STS::one() / *c; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
*da = r; |
| 89 |
*db = z; |
| 90 |
} |
| 91 |
|
| 92 |
private: |
| 93 |
|
| 94 |
/// Return ABS(x) if y > 0 or y is +0, else -ABS(x) (if y is -0 or < 0). |
| 95 |
ScalarType SIGN (ScalarType x, ScalarType y) const { |
| 96 |
typedef typename ScalarType::value_type value_type; |
| 97 |
typedef typename ScalarType::ordinal_type ordinal_type; |
| 98 |
|
| 99 |
GivensRotator<value_type> value_rotator; |
| 100 |
const ordinal_type sz = x.size() > y.size() ? x.size() : y.size(); |
| 101 |
ScalarType z(sz, 0.0); |
| 102 |
for (ordinal_type i=0; i<sz; ++i) |
| 103 |
z.fastAccessCoeff(i) = value_rotator.SIGN(x.coeff(i), y.coeff(i)); |
| 104 |
return z; |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
} // namespace details |
| 109 |
|
| 110 |
} // namespace Teuchos |
| 111 |
|
| 112 |
#endif // _TEUCHOS_BLAS_UQ_PCE_HPP_ |