1 /*
2 * Copyright (c) 2011, Michael Lehn
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3) Neither the name of the FLENS development group nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* Based on
34 *
35 SUBROUTINE DLAQR1( N, H, LDH, SR1, SI1, SR2, SI2, V )
36 *
37 * -- LAPACK auxiliary routine (version 3.2) --
38 * Univ. of Tennessee, Univ. of California Berkeley,
39 * Univ. of Colorado Denver and NAG Ltd..
40 * November 2006
41 */
42
43 #ifndef FLENS_LAPACK_EIG_LAQR1_TCC
44 #define FLENS_LAPACK_EIG_LAQR1_TCC 1
45
46 #include <flens/blas/blas.h>
47 #include <flens/lapack/lapack.h>
48
49 namespace flens { namespace lapack {
50
51 //== generic lapack implementation =============================================
52
53 template <typename MH, typename T, typename VV>
54 void
55 laqr1_generic(GeMatrix<MH> &H,
56 const T &sr1,
57 const T &si1,
58 const T &sr2,
59 const T &si2,
60 DenseVector<VV> &v)
61 {
62 using std::abs;
63
64 typedef typename GeMatrix<MH>::IndexType IndexType;
65
66 const IndexType n = H.numRows();
67 const T Zero(0);
68
69 if (n==2) {
70 const T s = abs(H(1,1)-sr2) + abs(si2) + abs(H(2,1));
71 if (s==Zero) {
72 v(1) = Zero;
73 v(2) = Zero;
74 } else {
75 const T H21s = H(2,1)/s;
76 v(1) = H21s*H(1,2) + (H(1,1)-sr1)*((H(1,1)-sr2)/s) - si1*(si2/s);
77 v(2) = H21s*(H(1,1) + H(2,2)-sr1-sr2);
78 }
79 } else {
80 const T s = abs(H(1,1)-sr2) + abs(si2) + abs(H(2,1)) + abs(H(3,1));
81 if (s==Zero) {
82 v(1) = Zero;
83 v(2) = Zero;
84 v(3) = Zero;
85 } else {
86 const T H21s = H(2,1) / s;
87 const T H31s = H(3,1) / s;
88 v(1) = (H(1,1)-sr1)*((H(1,1)-sr2)/s) - si1*(si2/s)
89 + H(1,2)*H21s + H(1,3)*H31s;
90 v(2) = H21s*(H(1,1) + H(2,2)-sr1-sr2) + H(2,3 )*H31s;
91 v(3) = H31s*(H(1,1) + H(3,3)-sr1-sr2) + H21s*H(3,2);
92 }
93 }
94 }
95
96 //== interface for native lapack ===============================================
97
98 #ifdef CHECK_CXXLAPACK
99
100 template <typename MH, typename T, typename VV>
101 void
102 laqr1_native(GeMatrix<MH> &H,
103 const T &sr1,
104 const T &si1,
105 const T &sr2,
106 const T &si2,
107 DenseVector<VV> &v)
108 {
109 const INTEGER N = H.numRows();
110 const INTEGER LDH = H.leadingDimension();
111
112 if (IsSame<T,DOUBLE>::value) {
113 LAPACK_IMPL(dlaqr1)(&N,
114 H.data(),
115 &LDH,
116 &sr1,
117 &si1,
118 &sr2,
119 &si2,
120 v.data());
121 } else {
122 ASSERT(0);
123 }
124 }
125
126 #endif // CHECK_CXXLAPACK
127
128 //== public interface ==========================================================
129
130 template <typename MH, typename T, typename VV>
131 void
132 laqr1(GeMatrix<MH> &H,
133 const T &sr1,
134 const T &si1,
135 const T &sr2,
136 const T &si2,
137 DenseVector<VV> &v)
138 {
139 LAPACK_DEBUG_OUT("laqr1");
140
141 //
142 // Test the input parameters
143 //
144 # ifndef NDEBUG
145 typedef typename GeMatrix<MH>::IndexType IndexType;
146
147 ASSERT(H.firstRow()==1);
148 ASSERT(H.firstCol()==1);
149 ASSERT(H.numRows()==H.numCols());
150
151 const IndexType n = H.numRows();
152
153 ASSERT(n==2 || n==3);
154
155 ASSERT(v.length()==n);
156 # endif
157
158 //
159 // Make copies of output arguments
160 //
161 # ifdef CHECK_CXXLAPACK
162 typename GeMatrix<MH>::NoView H_org = H;
163 typename DenseVector<VV>::NoView v_org = v;
164 # endif
165
166 //
167 // Call implementation
168 //
169 laqr1_generic(H, sr1, si1, sr2, si2, v);
170
171 # ifdef CHECK_CXXLAPACK
172 //
173 // Make copies of results computed by the generic implementation
174 //
175 typename GeMatrix<MH>::NoView H_generic = H;
176 typename DenseVector<VV>::NoView v_generic = v;
177 //
178 // restore output arguments
179 //
180 H = H_org;
181 v = v_org;
182 //
183 // Compare results
184 //
185 laqr1_native(H, sr1, si1, sr2, si2, v);
186
187 bool failed = false;
188 if (! isIdentical(H_generic, H, "H_generic", "H")) {
189 std::cerr << "CXXLAPACK: H_generic = " << H_generic << std::endl;
190 std::cerr << "F77LAPACK: H = " << H << std::endl;
191 failed = true;
192 }
193
194 if (! isIdentical(v_generic, v, "v_generic", "v")) {
195 std::cerr << "CXXLAPACK: v_generic = " << v_generic << std::endl;
196 std::cerr << "F77LAPACK: v = " << v << std::endl;
197 failed = true;
198 }
199
200 if (failed) {
201 std::cerr << "error in: laqr1.tcc" << std::endl;
202 ASSERT(0);
203 } else {
204 // std::cerr << "passed: laqr1.tcc" << std::endl;
205 }
206 # endif
207 }
208
209 //-- forwarding ----------------------------------------------------------------
210
211 template <typename MH, typename T, typename VV>
212 void
213 laqr1(MH &&H,
214 const T &sr1,
215 const T &si1,
216 const T &sr2,
217 const T &si2,
218 VV &&v)
219 {
220 CHECKPOINT_ENTER;
221 laqr1(H, sr1, si1, sr2, si2, v);
222 CHECKPOINT_LEAVE;
223 }
224
225 } } // namespace lapack, flens
226
227 #endif // FLENS_LAPACK_EIG_LAQR1_TCC
2 * Copyright (c) 2011, Michael Lehn
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3) Neither the name of the FLENS development group nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* Based on
34 *
35 SUBROUTINE DLAQR1( N, H, LDH, SR1, SI1, SR2, SI2, V )
36 *
37 * -- LAPACK auxiliary routine (version 3.2) --
38 * Univ. of Tennessee, Univ. of California Berkeley,
39 * Univ. of Colorado Denver and NAG Ltd..
40 * November 2006
41 */
42
43 #ifndef FLENS_LAPACK_EIG_LAQR1_TCC
44 #define FLENS_LAPACK_EIG_LAQR1_TCC 1
45
46 #include <flens/blas/blas.h>
47 #include <flens/lapack/lapack.h>
48
49 namespace flens { namespace lapack {
50
51 //== generic lapack implementation =============================================
52
53 template <typename MH, typename T, typename VV>
54 void
55 laqr1_generic(GeMatrix<MH> &H,
56 const T &sr1,
57 const T &si1,
58 const T &sr2,
59 const T &si2,
60 DenseVector<VV> &v)
61 {
62 using std::abs;
63
64 typedef typename GeMatrix<MH>::IndexType IndexType;
65
66 const IndexType n = H.numRows();
67 const T Zero(0);
68
69 if (n==2) {
70 const T s = abs(H(1,1)-sr2) + abs(si2) + abs(H(2,1));
71 if (s==Zero) {
72 v(1) = Zero;
73 v(2) = Zero;
74 } else {
75 const T H21s = H(2,1)/s;
76 v(1) = H21s*H(1,2) + (H(1,1)-sr1)*((H(1,1)-sr2)/s) - si1*(si2/s);
77 v(2) = H21s*(H(1,1) + H(2,2)-sr1-sr2);
78 }
79 } else {
80 const T s = abs(H(1,1)-sr2) + abs(si2) + abs(H(2,1)) + abs(H(3,1));
81 if (s==Zero) {
82 v(1) = Zero;
83 v(2) = Zero;
84 v(3) = Zero;
85 } else {
86 const T H21s = H(2,1) / s;
87 const T H31s = H(3,1) / s;
88 v(1) = (H(1,1)-sr1)*((H(1,1)-sr2)/s) - si1*(si2/s)
89 + H(1,2)*H21s + H(1,3)*H31s;
90 v(2) = H21s*(H(1,1) + H(2,2)-sr1-sr2) + H(2,3 )*H31s;
91 v(3) = H31s*(H(1,1) + H(3,3)-sr1-sr2) + H21s*H(3,2);
92 }
93 }
94 }
95
96 //== interface for native lapack ===============================================
97
98 #ifdef CHECK_CXXLAPACK
99
100 template <typename MH, typename T, typename VV>
101 void
102 laqr1_native(GeMatrix<MH> &H,
103 const T &sr1,
104 const T &si1,
105 const T &sr2,
106 const T &si2,
107 DenseVector<VV> &v)
108 {
109 const INTEGER N = H.numRows();
110 const INTEGER LDH = H.leadingDimension();
111
112 if (IsSame<T,DOUBLE>::value) {
113 LAPACK_IMPL(dlaqr1)(&N,
114 H.data(),
115 &LDH,
116 &sr1,
117 &si1,
118 &sr2,
119 &si2,
120 v.data());
121 } else {
122 ASSERT(0);
123 }
124 }
125
126 #endif // CHECK_CXXLAPACK
127
128 //== public interface ==========================================================
129
130 template <typename MH, typename T, typename VV>
131 void
132 laqr1(GeMatrix<MH> &H,
133 const T &sr1,
134 const T &si1,
135 const T &sr2,
136 const T &si2,
137 DenseVector<VV> &v)
138 {
139 LAPACK_DEBUG_OUT("laqr1");
140
141 //
142 // Test the input parameters
143 //
144 # ifndef NDEBUG
145 typedef typename GeMatrix<MH>::IndexType IndexType;
146
147 ASSERT(H.firstRow()==1);
148 ASSERT(H.firstCol()==1);
149 ASSERT(H.numRows()==H.numCols());
150
151 const IndexType n = H.numRows();
152
153 ASSERT(n==2 || n==3);
154
155 ASSERT(v.length()==n);
156 # endif
157
158 //
159 // Make copies of output arguments
160 //
161 # ifdef CHECK_CXXLAPACK
162 typename GeMatrix<MH>::NoView H_org = H;
163 typename DenseVector<VV>::NoView v_org = v;
164 # endif
165
166 //
167 // Call implementation
168 //
169 laqr1_generic(H, sr1, si1, sr2, si2, v);
170
171 # ifdef CHECK_CXXLAPACK
172 //
173 // Make copies of results computed by the generic implementation
174 //
175 typename GeMatrix<MH>::NoView H_generic = H;
176 typename DenseVector<VV>::NoView v_generic = v;
177 //
178 // restore output arguments
179 //
180 H = H_org;
181 v = v_org;
182 //
183 // Compare results
184 //
185 laqr1_native(H, sr1, si1, sr2, si2, v);
186
187 bool failed = false;
188 if (! isIdentical(H_generic, H, "H_generic", "H")) {
189 std::cerr << "CXXLAPACK: H_generic = " << H_generic << std::endl;
190 std::cerr << "F77LAPACK: H = " << H << std::endl;
191 failed = true;
192 }
193
194 if (! isIdentical(v_generic, v, "v_generic", "v")) {
195 std::cerr << "CXXLAPACK: v_generic = " << v_generic << std::endl;
196 std::cerr << "F77LAPACK: v = " << v << std::endl;
197 failed = true;
198 }
199
200 if (failed) {
201 std::cerr << "error in: laqr1.tcc" << std::endl;
202 ASSERT(0);
203 } else {
204 // std::cerr << "passed: laqr1.tcc" << std::endl;
205 }
206 # endif
207 }
208
209 //-- forwarding ----------------------------------------------------------------
210
211 template <typename MH, typename T, typename VV>
212 void
213 laqr1(MH &&H,
214 const T &sr1,
215 const T &si1,
216 const T &sr2,
217 const T &si2,
218 VV &&v)
219 {
220 CHECKPOINT_ENTER;
221 laqr1(H, sr1, si1, sr2, si2, v);
222 CHECKPOINT_LEAVE;
223 }
224
225 } } // namespace lapack, flens
226
227 #endif // FLENS_LAPACK_EIG_LAQR1_TCC