forked from qsimulate-open/zquatev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzquartev.cc
188 lines (150 loc) · 6.62 KB
/
zquartev.cc
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
//
// Filename: zquartev.cc
// Copyright (C) 2013 Toru Shiozaki
//
// Author: Toru Shiozaki <[email protected]>
// Maintainer: TS
//
// You can redistribute this program and/or modify
// it under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
#include "zquartev.h"
#include "f77.h"
#include <cassert>
#include <algorithm>
using namespace std;
namespace ts {
// some local functions..
static auto givens = [](const complex<double> a, const complex<double> b) {
const double absa = abs(a);
const double c = absa / sqrt(absa*absa + norm(b));
const complex<double> s = absa == 0.0 ? 1.0 : (a / absa * conj(b) / sqrt(absa*absa + norm(b)));
return make_pair(c, s);
};
static auto householder = [](const complex<double>* const hin, complex<double>* const out, const int len) {
const double norm = sqrt(real(zdotc_(len, hin, 1, hin, 1)));
const double sign = abs(real(hin[0])) == 0.0 ? 0.0 : real(hin[0])/abs(real(hin[0]));
out[0] = hin[0] + sign*norm;
for (int i = 1; i < len; ++i) out[i] = hin[i];
return conj(1.0 / (conj(out[0]) * (sign*norm)));
};
// implementation...
void zquatev(const int n2, complex<double>* const D, double* const eig) {
assert(n2 % 2 == 0);
const int n = n2/2;
// rearrange data
complex<double>* const D0 = D;
complex<double>* const D1 = D + n*n;
copy_n(D, n2*n, D+n2*n);
for (int i = 0; i != n; ++i) {
copy_n(D+n2*n+i*n2, n, D0+i*n);
copy_n(D+n2*n+i*n2+n, n, D1+i*n);
}
// identity matrix of n2 dimension
complex<double>* const Q0 = D + n2*n;
complex<double>* const Q1 = D + n2*n + n*n;
fill_n(Q0, n*n, 0.0);
fill_n(Q1, n*n, 0.0);
for (int i = 0; i != n; ++i) Q0[i+n*i] = 1.0;
unique_ptr<complex<double>[]> buf(new complex<double>[n2]);
unique_ptr<complex<double>[]> hout(new complex<double>[n2]);
unique_ptr<complex<double>[]> choutf(new complex<double>[n2]);
// Reference - arXiv:1203.6151v4
for (int k = 0; k != n-1; ++k) {
const int len = n-k-1;
if (len > 1) {
complex<double>* const hin = D1+n*k+k+1;
complex<double> tau = householder(hin, hout.get(), len);
for (int i = 0; i != len; ++i) choutf[i] = conj(hout[i]);
// 00-1
zgemv_("T", len, len+1, 1.0, D0+k+1+(k)*n, n, hout.get(), 1, 0.0, buf.get(), 1);
zgeru_(len, len+1, -conj(tau), choutf.get(), 1, buf.get(), 1, D0+k+1+(k)*n, n);
// 00-2
zgemv_("N", len+1, len, 1.0, D0+(k+1)*n+(k), n, choutf.get(), 1, 0.0, buf.get(), 1);
zgeru_(len+1, len, -tau, buf.get(), 1, hout.get(), 1, D0+(k+1)*n+(k), n);
// 10-1
zgemv_("T", len, len+1, -1.0, D1+k+1+(k)*n, n, choutf.get(), 1, 0.0, buf.get(), 1);
zgeru_(len, len+1, tau, hout.get(), 1, buf.get(), 1, D1+k+1+(k)*n, n);
// 10-2
zgemv_("N", len+1, len, 1.0, D1+k+(k+1)*n, n, choutf.get(), 1, 0.0, buf.get(), 1);
zgeru_(len+1, len, -tau, buf.get(), 1, hout.get(), 1, D1+(k+1)*n+(k), n);
// 00-2
zgemv_("N", n, len, 1.0, Q0+(k+1)*n, n, choutf.get(), 1, 0.0, buf.get(), 1);
zgeru_(n, len, -tau, buf.get(), 1, hout.get(), 1, Q0+(k+1)*n, n);
// 10-2
zgemv_("N", n, len, 1.0, Q1+(k+1)*n, n, choutf.get(), 1, 0.0, buf.get(), 1);
zgeru_(n, len, -tau, buf.get(), 1, hout.get(), 1, Q1+(k+1)*n, n);
}
// symplectic Givens rotation to clear out D(k+n, k)
pair<double,complex<double>> gr = givens(D0[k+1+k*n], D1[k+1+k*n]);
zrot_(len+1, D0+k+1+k*n, n, D1+k+1+k*n, n, gr.first, gr.second);
for (int i = 0; i != len+1; ++i)
D1[(k+1)*n+k+i] = -conj(D1[(k+1)*n+k+i]);
zrot_(len+1, D0+(k+1)*n+k, 1, D1+(k+1)*n+k, 1, gr.first, conj(gr.second));
for (int i = 0; i != len+1; ++i)
D1[(k+1)*n+k+i] = -conj(D1[(k+1)*n+k+i]);
for (int i = 0; i != n; ++i)
Q1[(k+1)*n+i] = -conj(Q1[(k+1)*n+i]);
zrot_(n, Q0+(k+1)*n, 1, Q1+(k+1)*n, 1, gr.first, conj(gr.second));
for (int i = 0; i != n; ++i)
Q1[(k+1)*n+i] = -conj(Q1[(k+1)*n+i]);
// Householder to fix top half in column k
if (len > 1) {
complex<double>* const hin = D0+n*k+k+1;
complex<double> tau = householder(hin, hout.get(), len);
for (int i = 0; i != len; ++i) choutf[i] = conj(hout[i]);
// 00-1
zgemv_("C", len, len+1, 1.0, D0+k+1+(k)*n, n, hout.get(), 1, 0.0, buf.get(), 1);
zgerc_(len, len+1, -tau, hout.get(), 1, buf.get(), 1, D0+k+1+(k)*n, n);
// 00-2
zgemv_("N", len+1, len, 1.0, D0+(k+1)*n+(k), n, hout.get(), 1, 0.0, buf.get(), 1);
zgerc_(len+1, len, -conj(tau), buf.get(), 1, hout.get(), 1, D0+(k+1)*n+(k), n);
// 01-1
zgemv_("T", len, len+1, 1.0, D1+k+1+(k)*n, n, hout.get(), 1, 0.0, buf.get(), 1);
zgeru_(len, len+1, -conj(tau), choutf.get(), 1, buf.get(), 1, D1+k+1+(k)*n, n);
// 01-2
zgemv_("N", len+1, len, -1.0, D1+(k+1)*n+(k), n, hout.get(), 1, 0.0, buf.get(), 1);
zgerc_(len+1, len, conj(tau), buf.get(), 1, hout.get(), 1, D1+(k+1)*n+(k), n);
// 00-2
zgemv_("N", n, len, 1.0, Q0+(k+1)*n, n, hout.get(), 1, 0.0, buf.get(), 1);
zgerc_(n, len, -conj(tau), buf.get(), 1, hout.get(), 1, Q0+(k+1)*n, n);
// 01-2
zgemv_("N", n, len, -1.0, Q1+(k+1)*n, n, hout.get(), 1, 0.0, buf.get(), 1);
zgerc_(n, len, conj(tau), buf.get(), 1, hout.get(), 1, Q1+(k+1)*n, n);
}
}
// diagonalize this tri-diagonal matrix (this step is much cheaper than
// the Householder transformation above).
unique_ptr<complex<double>[]> Cmat(new complex<double>[n*n]);
unique_ptr<complex<double>[]> Work(new complex<double>[n]);
int info;
unique_ptr<double[]> rwork(new double[n*3]);
for (int i = 0; i != n; ++i)
for (int j = 0; j <= i; ++j)
D0[i-j+j*n] = D0[i+j*n];
zhbev_("V", "L", n, 1, D0, n, eig, Cmat.get(), n, Work.get(), rwork.get(), info);
if (info) throw runtime_error("zhbev failed in quaternion diagonalization");
// form the coefficient matrix in D
#ifdef MKL
zgemm3m_("N", "N", n, n, n, 1.0, Q0, n, Cmat.get(), n, 0.0, D, n2);
zgemm3m_("N", "N", n, n, n, 1.0, Q1, n, Cmat.get(), n, 0.0, D+n, n2);
#else
zgemm_("N", "N", n, n, n, 1.0, Q0, n, Cmat.get(), n, 0.0, D, n2);
zgemm_("N", "N", n, n, n, 1.0, Q1, n, Cmat.get(), n, 0.0, D+n, n2);
#endif
// eigen vectors using symmetry
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
D[j+n2*(i+n)] = -conj(D[j+n+n2*i]);
D[j+n+n2*(i+n)] = conj(D[j+n2*i]);
}
}
}
}