File: rotmg.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-- 5,836 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_ROTMG_HH
#define BLAS_ROTMG_HH

#include "blas/util.hh"

#include <limits>

namespace blas {

// =============================================================================
/// Construct modified (fast) plane rotation, H, that eliminates b, such that
/// \[
///       \begin{bmatrix} z \\ 0 \end{bmatrix}
///     = H
///       \begin{bmatrix} \sqrt{d_1} & 0 \\ 0 & \sqrt{d_2} \end{bmatrix}
///       \begin{bmatrix} a \\ b \end{bmatrix}.
/// \]
///
/// @see rotm to apply the rotation.
///
/// With modified plane rotations, vectors u and v are held in factored form as
/// \[
///     \begin{bmatrix} u^T \\ v^T \end{bmatrix} =
///     \begin{bmatrix} \sqrt{d_1} & 0 \\ 0 & \sqrt{d_2} \end{bmatrix}
///     \begin{bmatrix} x^T \\ y^T \end{bmatrix}.
/// \]
///
/// Application of H to vectors x and y requires 4n flops (2n mul, 2n add)
/// instead of 6n flops (4n mul, 2n add) as in standard plane rotations.
///
/// Let param = [ flag, $h_{11}, h_{21}, h_{12}, h_{22}$ ].
/// Then H has one of the following forms:
///
/// - For flag = -1,
///     \[
///         H = \begin{bmatrix}
///             h_{11}  &  h_{12}
///         \\  h_{21}  &  h_{22}
///         \end{bmatrix}
///     \]
///
/// - For flag = 0,
///     \[
///         H = \begin{bmatrix}
///             1       &  h_{12}
///         \\  h_{21}  &  1
///         \end{bmatrix}
///     \]
///
/// - For flag = 1,
///     \[
///         H = \begin{bmatrix}
///             h_{11}  &  1
///         \\  -1      &  h_{22}
///         \end{bmatrix}
///     \]
///
/// - For flag = -2,
///     \[
///         H = \begin{bmatrix}
///             1  &  0
///         \\  0  &  1
///         \end{bmatrix}
///     \]
///
/// Generic implementation for arbitrary data types.
///
/// @param[in, out] d1
///     sqrt(d1) is scaling factor for vector x.
///
/// @param[in, out] d2
///     sqrt(d2) is scaling factor for vector y.
///
/// @param[in, out] a
///     On entry, scalar a. On exit, set to z.
///
/// @param[in] b
///     On entry, scalar b.
///
/// @param[out] param
///     Array of length 5 giving parameters of modified plane rotation,
///     as described above.
///
/// __Further details__
///
/// Hammarling, Sven. A note on modifications to the Givens plane rotation.
/// IMA Journal of Applied Mathematics, 13:215-218, 1974.
/// http://dx.doi.org/10.1093/imamat/13.2.215
/// (Note the notation swaps u <=> x, v <=> y, d_i -> l_i.)
///
/// @ingroup rotmg

template <typename T>
void rotmg(
    T *d1,
    T *d2,
    T *a,
    T  b,
    T  param[5] )
{
    using std::abs;

    // Constants
    const T zero = 0;
    const T one = 1;
    const T gam = 4096;
    const T gamsq = gam*gam;
    const T rgamsq = one/gamsq;

    T& x1 = *a;
    T& y1 = b;

    T h11 = zero;
    T h12 = zero;
    T h21 = zero;
    T h22 = zero;

    if (*d1 < zero) {
        param[0] = -1;
        *d1 = zero;
        *d2 = zero;
        x1 = zero;
    }
    else {
        T p2 = (*d2)*y1;
        if (p2 == zero) {
            param[0] = -2;
            return;
        }

        T p1 = (*d1)*x1;
        T q2 = p2*y1;
        T q1 = p1*x1;

        if (abs(q1) > abs(q2)) {
            param[0] = zero;
            h21 = -y1/x1;
            h12 = p2/p1;
            T u = one - h12*h21;
            if (u > zero) {
                *d1 /= u;
                *d2 /= u;
                x1 *= u;
            }
        }
        else if (q2 < zero) {
            param[0] = -1;
            *d1 = zero;
            *d2 = zero;
            x1 = zero;
        }
        else {
            param[0] = 1;
            h11 = p1/p2;
            h22 = x1/y1;
            T u = one + h11*h22;
            T stemp = *d2/u;
            *d2 = *d1/u;
            *d1 = stemp;
            x1 = y1*u;
        }

        if (*d1 != zero) {
            while ((*d1 <= rgamsq) || (*d1 >= gamsq)) {
                if (param[0] == 0) {
                    h11 = one;
                    h22 = one;
                    param[0] = -1;
                }
                else {
                    h21 = -one;
                    h12 = one;
                    param[0] = -1;
                }
                if (*d1 <= rgamsq) {
                    *d1 *= gam*gam;
                    x1 /= gam;
                    h11 /= gam;
                    h12 /= gam;
                }
                else {
                    *d1 /= gam*gam;
                    x1 *= gam;
                    h11 *= gam;
                    h12 *= gam;
                }
            }
        }

        if (*d2 != zero) {
            while ((abs(*d2) <= rgamsq) || (abs(*d2) >= gamsq)) {
                if (param[0] == 0) {
                    h11=one;
                    h22=one;
                    param[0]=-1;
                }
                else {
                    h21=-one;
                    h12=one;
                    param[0]=-1;
                }
                if (abs(*d2) <= rgamsq) {
                    *d2 *= gam*gam;
                    h21 /= gam;
                    h22 /= gam;
                }
                else {
                    *d2 /= gam*gam;
                    h21 *= gam;
                    h22 *= gam;
                }
            }
        }
    }

    if (param[0] < 0) {
        param[1] = h11;
        param[2] = h21;
        param[3] = h12;
        param[4] = h22;
    }
    else if (param[0] == 0) {
        param[2] = h21;
        param[3] = h12;
    }
    else {
        param[1] = h11;
        param[4] = h22;
    }
}

}  // namespace blas

#endif        //  #ifndef BLAS_ROTMG_HH