Debian Codegraph @

src/util/math/zquatev/zquatev.cc

raw
1 //
2 // ZQUATEV: Diagonalization of quaternionic matrices
3 // File : zquatev.cc
4 // Copyright (c) 2013, Toru Shiozaki ([email protected])
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright notice, this
11 // list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 //
27 // The views and conclusions contained in the software and documentation are those
28 // of the authors and should not be interpreted as representing official policies,
29 // either expressed or implied, of the FreeBSD Project.
30 //
31
32 #include "zquatev.h"
33 #include "f77.h"
34 #include <cassert>
35 #include <algorithm>
36
37 using namespace std;
38 using namespace ts::impl;
39
40 namespace ts {
41
42 int zquatev(const int n2, complex<double>* const D, const int ld2, double* const eig) {
43 assert(n2 % 2 == 0);
44 const int n = n2/2;
45 const int ld = ld2/2;
46
47 // rearrange data
48 complex<double>* const D0 = D;
49 complex<double>* const D1 = D + n*ld;
50 copy_n(D, ld2*n, D+ld2*n);
51 for (int i = 0; i != n; ++i) {
52 copy_n(D+ld2*n+i*ld2, n, D0+i*ld);
53 copy_n(D+ld2*n+i*ld2+n, n, D1+i*ld);
54 }
55
56 // identity matrix of n2 dimension
57 complex<double>* const Q0 = D + ld2*n;
58 complex<double>* const Q1 = D + ld2*n + ld*n;
59 fill_n(Q0, ld*n, 0.0);
60 fill_n(Q1, ld*n, 0.0);
61 for (int i = 0; i != n; ++i) Q0[i+ld*i] = 1.0;
62
63 // TODO allocation will move out
64 const int nb = 20;
65 const size_t alloc_size = nb*5 + nb*nb*15 + (max(4,nb)+10*nb+3)*n;
66 unique_ptr<complex<double>[]> tmp_mem(new complex<double>[alloc_size]);
67
68 for (int p = 0; p < n; p += nb)
69 if (n-p > nb+1)
70 panel_update(n-p, nb, D0+p*ld+p, D1+p*ld+p, Q0+p*ld, Q1+p*ld, ld, n, tmp_mem.get());
71 else
72 unblocked_update(n-p, D0+p*ld+p, D1+p*ld+p, Q0+p*ld, Q1+p*ld, ld, n, tmp_mem.get());
73
74 // diagonalize this tri-diagonal matrix (this step is much cheaper than
75 // the Householder transformation above).
76 auto work1 = tmp_mem.get();
77 auto work2 = work1 + n;
78 auto work3 = work2 + n;
79 for (int i = 0; i != n; ++i) {
80 work3[2*i] = D0[i+i*ld];
81 work3[2*i+1] = D0[i+i*ld+1];
82 }
83 int info;
84 zhbev_("V", "L", n, 1, work3, 2, eig, D0+ld, ld2, work1, reinterpret_cast<double*>(work2), info);
85
86 // form the coefficient matrix in D
87 zgemm3m_("N", "N", n, n, n, 1.0, Q0, ld, D+ld, ld2, 0.0, D, ld2);
88 for (int i = 0; i != n; ++i)
89 copy_n(D+ld+i*ld2, n, Q0+i*ld);
90 zgemm3m_("N", "N", n, n, n, 1.0, Q1, ld, Q0, n, 0.0, D+ld, ld2);
91
92 // eigen vectors using symmetry
93 for (int i = 0; i != n; ++i) {
94 for (int j = 0; j != n; ++j) {
95 D[j+ld2*(i+n)] = -conj(D[j+ld+ld2*i]);
96 D[j+ld+ld2*(i+n)] = conj(D[j+ld2*i]);
97 }
98 }
99 return info;
100 }
101
102 }