File: rotg.hh

package info (click to toggle)
blaspp 2024.10.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,636 kB
  • sloc: cpp: 29,332; ansic: 8,448; python: 2,192; xml: 182; perl: 101; makefile: 53; sh: 7
file content (239 lines) | stat: -rw-r--r-- 6,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.

#ifndef BLAS_ROTG_HH
#define BLAS_ROTG_HH

#include "blas/util.hh"

#include <limits>

namespace blas {

// =============================================================================
/// Construct plane rotation that eliminates b, such that:
/// \[
///       \begin{bmatrix} r     \\ 0      \end{bmatrix}
///     = \begin{bmatrix} c & s \\ -s & c \end{bmatrix}
///       \begin{bmatrix} a     \\ b      \end{bmatrix}.
/// \]
///
/// @see rot to apply the rotation.
///
/// Generic implementation for arbitrary data types.
///
/// @param[in, out] a
///     On entry, scalar a. On exit, set to r.
///
/// @param[in, out] b
///     On entry, scalar b. On exit, set to s, 1/c, or 0.
///
/// @param[out] c
///     Cosine of rotation; real.
///
/// @param[out] s
///     Sine of rotation; real.
///
/// __Further details__
///
/// Anderson E (2017) Algorithm 978: Safe scaling in the level 1 BLAS.
/// ACM Trans Math Softw 44:. https://doi.org/10.1145/3061665
///
/// @ingroup rotg

template <typename TA, typename TB>
void rotg(
    TA *a,
    TB *b,
    blas::real_type<TA, TB> *c,
    blas::real_type<TA, TB> *s )
{
    typedef real_type<TA, TB> real_t;
    using std::abs;

    // Constants
    const real_t one  = 1;
    const real_t zero = 0;

    // Scaling constants
    const real_t safmin = safe_min<real_t>();
    const real_t safmax = safe_max<real_t>();

    // Norms
    const real_t anorm = abs(*a);
    const real_t bnorm = abs(*b);

    // quick return
    if (bnorm == zero) {
        *c = one;
        *s = zero;
        *b = TB( 0.0 );
    }
    else if (anorm == zero) {
        *c = zero;
        *s = one;
        *a = *b;
        *b = TB( 1.0 );
    }
    else {
        real_t scl = min( safmax, max(safmin, anorm, bnorm) );
        real_t sigma = (anorm > bnorm)
            ? sgn(*a)
            : sgn(*b);
        real_t r = sigma * scl * sqrt( (*a/scl) * (*a/scl) + (*b/scl) * (*b/scl) );
        *c = *a / r;
        *s = *b / r;
        *a = r;
        if (anorm > bnorm)
            *b = *s;
        else if (*c != zero)
            *b = one / *c;
        else
            *b = one;
    }
}

// =============================================================================
/// Construct plane rotation that eliminates b, such that:
/// \[
///       \begin{bmatrix} r     \\ 0      \end{bmatrix}
///     = \begin{bmatrix} c & s \\ -conjg(s) & c \end{bmatrix}
///       \begin{bmatrix} a     \\ b      \end{bmatrix}.
/// \]
///
/// @see rot to apply the rotation.
///
/// Generic implementation for arbitrary data types.
///
/// @param[in, out] a
///     On entry, scalar a. On exit, set to r.
///
/// @param[in, out] b
///     On entry, scalar b. On exit, set to s, 1/c, or 0.
///
/// @param[out] c
///     Cosine of rotation; real.
///
/// @param[out] s
///     Sine of rotation; complex.
///
/// __Further details__
///
/// Anderson E (2017) Algorithm 978: Safe scaling in the level 1 BLAS.
/// ACM Trans Math Softw 44:. https://doi.org/10.1145/3061665
///
/// @ingroup rotg

template <typename TA, typename TB>
void rotg(
    std::complex<TA> *a,
    std::complex<TB> *b,
    blas::real_type<TA, TB>   *c,
    blas::complex_type<TA, TB> *s )
{
    typedef real_type<TA, TB> real_t;
    typedef complex_type<TA, TB> scalar_t;
    using std::abs;

    #define BLAS_ABSSQ(t_) real(t_)*real(t_) + imag(t_)*imag(t_)

    // Constants
    const real_t r_one = 1;
    const real_t r_zero = 0;
    const scalar_t zero = 0;
    const std::complex<TA> zero_ta = 0;
    const std::complex<TB> zero_tb = 0;

    // Scaling constants
    const real_t safmin = safe_min<real_t>();
    const real_t safmax = safe_max<real_t>();
    const real_t rtmin = root_min<real_t>();
    const real_t rtmax = root_max<real_t>();

    // quick return
    if (*b == zero_tb) {
        *c = r_one;
        *s = zero;
        return;
    }

    if (*a == zero_ta) {
        *c = r_zero;
        real_t g1 = max( abs(real(*b)), abs(imag(*b)) );
        if (g1 > rtmin && g1 < rtmax) {
            // Use unscaled algorithm
            real_t g2 = BLAS_ABSSQ( *b );
            real_t d = sqrt( g2 );
            *s = conj( *b ) / d;
            *a = d;
        }
        else {
            // Use scaled algorithm
            real_t u = min( safmax, max( safmin, g1 ) );
            real_t uu = r_one / u;
            scalar_t gs = (*b)*uu;
            real_t g2 = BLAS_ABSSQ( gs );
            real_t d = sqrt( g2 );
            *s = conj( gs ) / d;
            *a = d*u;
        }
    }
    else {
        real_t f1 = max( abs(real(*a)), abs(imag(*a)) );
        real_t g1 = max( abs(real(*b)), abs(imag(*b)) );
        if (f1 > rtmin && f1 < rtmax
            && g1 > rtmin && g1 < rtmax) {
            // Use unscaled algorithm
            real_t f2 = BLAS_ABSSQ( *a );
            real_t g2 = BLAS_ABSSQ( *b );
            real_t h2 = f2 + g2;
            real_t d = ( f2 > rtmin && h2 < rtmax )
                       ? sqrt( f2*h2 )
                       : sqrt( f2 )*sqrt( h2 );
            real_t p = r_one / d;
            *c  = f2*p;
            *s  = conj( *b )*( (*a)*p );
            *a *= h2*p;
        }
        else {
            // Use scaled algorithm
            real_t u = min( safmax, max( safmin, f1, g1 ) );
            real_t uu = r_one / u;
            scalar_t gs = (*b)*uu;
            real_t g2 = BLAS_ABSSQ( gs );
            real_t f2, h2, w;
            scalar_t fs;
            if (f1*uu < rtmin) {
                // a is not well-scaled when scaled by g1.
                real_t v = min( safmax, max( safmin, f1 ) );
                real_t vv = r_one / v;
                w = v * uu;
                fs = (*a)*vv;
                f2 = BLAS_ABSSQ( fs );
                h2 = f2*w*w + g2;
            }
            else {
                // Otherwise use the same scaling for a and b.
                w = r_one;
                fs = (*a)*uu;
                f2 = BLAS_ABSSQ( fs );
                h2 = f2 + g2;
            }
            real_t d = ( f2 > rtmin && h2 < rtmax )
                       ? sqrt( f2*h2 )
                       : sqrt( f2 )*sqrt( h2 );
            real_t p = r_one / d;
            *c = ( f2*p )*w;
            *s = conj( gs )*( fs*p );
            *a = ( fs*( h2*p ) )*u;
        }
    }

    #undef BLAS_ABSSQ
}

}  // namespace blas

#endif        //  #ifndef BLAS_ROTG_HH