| 1 |
// |
| 2 |
// ZQUATEV: Diagonalization of quaternionic matrices |
| 3 |
// File : supermat.h |
| 4 |
// Copyright (c) 2016, 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 |
|
| 33 |
#ifndef __SUPERMATRIX_H |
| 34 |
#define __SUPERMATRIX_H |
| 35 |
|
| 36 |
#include "f77.h" |
| 37 |
#include <cassert> |
| 38 |
#include <complex> |
| 39 |
#include <iostream> |
| 40 |
#include <iomanip> |
| 41 |
#include <array> |
| 42 |
#include <algorithm> |
| 43 |
|
| 44 |
namespace ts { |
| 45 |
namespace impl { |
| 46 |
|
| 47 |
template <size_t NB, size_t MB> |
| 48 |
class SuperMatrix { |
| 49 |
protected: |
| 50 |
// raw pointer. Efficiency is everything! |
| 51 |
std::complex<double>* data_; |
| 52 |
|
| 53 |
// size information |
| 54 |
const int nmax_; |
| 55 |
const int mmax_; |
| 56 |
int area() const { return nmax_*mmax_; } |
| 57 |
|
| 58 |
// size pointer |
| 59 |
std::array<int, NB> nptr_; |
| 60 |
std::array<int, MB> mptr_; |
| 61 |
|
| 62 |
public: |
| 63 |
SuperMatrix(std::complex<double>* d, const int nm, const int mm, const int nst = 1, const int mst = 1, const bool init = true, const bool last_is_one = false) |
| 64 |
: data_(d), nmax_(nm), mmax_(mm) { |
| 65 |
std::fill(nptr_.begin(), nptr_.end(), nst); |
| 66 |
std::fill(mptr_.begin(), mptr_.end(), mst); |
| 67 |
if (last_is_one) |
| 68 |
mptr_[mst-1] = 1; |
| 69 |
if (init) |
| 70 |
std::fill_n(data_, (!last_is_one ? nmax_*mmax_*NB*MB : (nmax_*mmax_*NB*(MB-1) + nmax_*NB)), 0.0); |
| 71 |
} |
| 72 |
|
| 73 |
SuperMatrix(std::complex<double>* d, const int nm, const int mm, const std::array<int, NB>& nptr, const std::array<int, MB>& mptr) |
| 74 |
: data_(d), nmax_(nm), mmax_(mm), nptr_(nptr), mptr_(mptr) { |
| 75 |
} |
| 76 |
|
| 77 |
SuperMatrix(std::complex<double>* d, const SuperMatrix<NB,MB>& o) |
| 78 |
: data_(d), nmax_(o.nmax_), mmax_(o.mmax_), nptr_(o.nptr_), mptr_(o.mptr_) { |
| 79 |
for (int m = 0; m != MB; ++m) |
| 80 |
for (int n = 0; n != NB; ++n) |
| 81 |
for (int j = 0; j != mptr_[m]; ++j) |
| 82 |
std::copy_n(o.block(n,m)+j*nmax_, nptr_[n], block(n,m)+j*nmax_); |
| 83 |
} |
| 84 |
|
| 85 |
template<int iblock, int mb = 1, class = typename std::enable_if<(iblock+mb-1 < MB)>::type> |
| 86 |
SuperMatrix<NB,mb> slice() { |
| 87 |
std::array<int, mb> mp; |
| 88 |
std::copy_n(mptr_.begin()+iblock, mb, mp.begin()); |
| 89 |
return SuperMatrix<NB,mb>(block(0,iblock), nmax_, mmax_, nptr_, mp); |
| 90 |
} |
| 91 |
|
| 92 |
template<int nb, class = typename std::enable_if<(nb < NB)>::type> |
| 93 |
SuperMatrix<nb,MB> trunc() { |
| 94 |
static_assert(MB == 1, "only defined for MB = 1"); |
| 95 |
std::array<int, nb> np; |
| 96 |
std::copy_n(nptr_.begin(), nb, np.begin()); |
| 97 |
return SuperMatrix<nb,MB>(block(0,0), nmax_, mmax_, np, mptr_); |
| 98 |
} |
| 99 |
|
| 100 |
SuperMatrix<NB,MB> shift(const int n) { |
| 101 |
std::array<int, NB> np; |
| 102 |
for (int i = 0; i != NB; ++i) { |
| 103 |
assert(nptr_[i]-n > 0); |
| 104 |
np[i] = nptr_[i]-n; |
| 105 |
} |
| 106 |
return SuperMatrix<NB,MB>(data_+n, nmax_, mmax_, np, mptr_); |
| 107 |
} |
| 108 |
|
| 109 |
template<size_t I, size_t J, class = typename std::enable_if<(I < NB and J < MB)>::type> |
| 110 |
std::complex<double>& data(const int n, const int m) { return *(block(I, J) + n + nmax_*m); } |
| 111 |
template<size_t I, size_t J, class = typename std::enable_if<(I < NB and J < MB)>::type> |
| 112 |
std::complex<double>* ptr(const int n, const int m) { return block(I, J) + n + nmax_*m; } |
| 113 |
|
| 114 |
std::complex<double>* block(const int i, const int j) { return data_+area()*(i+NB*j); } |
| 115 |
const std::complex<double>* block(const int i, const int j) const { return data_+area()*(i+NB*j); } |
| 116 |
|
| 117 |
void conj() { |
| 118 |
for (int m = 0; m != MB; ++m) |
| 119 |
for (int n = 0; n != NB; ++n) |
| 120 |
for (int j = 0; j != mptr_[m]; ++j) |
| 121 |
conj_n(block(n,m)+j*nmax_, nptr_[n]); |
| 122 |
} |
| 123 |
|
| 124 |
void scale(const std::complex<double>& a) { |
| 125 |
for (int m = 0; m != MB; ++m) |
| 126 |
for (int n = 0; n != NB; ++n) |
| 127 |
for (int j = 0; j != mptr_[m]; ++j) |
| 128 |
zscal_(nptr_[n], a, block(n,m)+j*nmax_, 1); |
| 129 |
} |
| 130 |
|
| 131 |
template<size_t iblock, class = typename std::enable_if<(iblock < MB)>::type> |
| 132 |
void append_column(const std::complex<double>* d, const int ld, const int off = 0) { |
| 133 |
assert(mptr_[iblock]+1 <= mmax_); |
| 134 |
for (int nblock = 0; nblock != NB; ++nblock) |
| 135 |
std::copy_n(d+ld*nblock, nptr_[nblock]-off, block(nblock, iblock)+mptr_[iblock]*nmax_+off); |
| 136 |
mptr_[iblock]++; |
| 137 |
} |
| 138 |
|
| 139 |
template<size_t iblock, class = typename std::enable_if<(iblock < MB)>::type> |
| 140 |
void append_column(const SuperMatrix<NB,1>& d) { |
| 141 |
append_column<iblock>(d.block(0,0), d.nmax()); |
| 142 |
} |
| 143 |
|
| 144 |
template<size_t iblock, size_t nblock = 0, class = typename std::enable_if<(iblock < MB)>::type> |
| 145 |
void append_column(const int off = 0, const std::complex<double>& a = 0.0) { |
| 146 |
assert(mptr_[iblock]+1 <= nmax_); |
| 147 |
*(block(nblock, iblock)+off+nmax_*mptr_[iblock]) = a; |
| 148 |
mptr_[iblock]++; |
| 149 |
} |
| 150 |
|
| 151 |
template<size_t iblock, class = typename std::enable_if<(iblock < MB)>::type> |
| 152 |
void add_lastcolumn(const std::complex<double>* d, const int ld, const int off = 0, const std::complex<double>& a = 1.0) { |
| 153 |
for (int nblock = 0; nblock != NB; ++nblock) |
| 154 |
zaxpy_(nptr_[nblock]-off, a, d+ld*nblock, 1, block(nblock, iblock)+(mptr_[iblock]-1)*nmax_+off, 1); |
| 155 |
} |
| 156 |
|
| 157 |
template<size_t iblock, class = typename std::enable_if<(iblock < MB)>::type> |
| 158 |
void add_lastcolumn(const SuperMatrix<NB,1>& d, const std::complex<double>& a = 1.0) { |
| 159 |
assert(d.mptr(0) == 1); |
| 160 |
add_lastcolumn<iblock>(d.block(0,0), d.nmax(), 0, a); |
| 161 |
} |
| 162 |
|
| 163 |
template<size_t iblock, class = typename std::enable_if<(iblock < MB)>::type> |
| 164 |
void write_lastcolumn(std::complex<double>* d, const int ld, const int off = 0) { |
| 165 |
for (int nblock = 0; nblock != NB; ++nblock) |
| 166 |
std::copy_n(d+ld*nblock, nptr_[nblock]-off, block(nblock, iblock)+(mptr_[iblock]-1)*nmax_+off); |
| 167 |
} |
| 168 |
|
| 169 |
template<size_t iblock, class = typename std::enable_if<(iblock < NB)>::type> |
| 170 |
void append_row(std::complex<double>* d, const int ld) { |
| 171 |
assert(nptr_[iblock]+1 <= nmax_); |
| 172 |
for (int mblock = 0; mblock != NB; ++mblock) |
| 173 |
for (int m = 0; m != mptr_[mblock]; ++m) |
| 174 |
*(block(iblock, mblock)+m*nmax_+nptr_[iblock]) = *(d+m+ld*mblock); |
| 175 |
nptr_[iblock]++; |
| 176 |
} |
| 177 |
|
| 178 |
template<size_t iblock, size_t mblock = 0, class = typename std::enable_if<(iblock < NB)>::type> |
| 179 |
void append_row(const int off = 0, const std::complex<double>& a = 0.0) { |
| 180 |
assert(nptr_[iblock]+1 <= nmax_); |
| 181 |
*(block(iblock, mblock)+off*nmax_+nptr_[iblock]) = a; |
| 182 |
nptr_[iblock]++; |
| 183 |
} |
| 184 |
|
| 185 |
template<size_t nblock> |
| 186 |
void cut_row(const int off, SuperMatrix<MB,1>& result) const { |
| 187 |
for (int i = 0; i != MB; ++i) { |
| 188 |
std::complex<double>* out = result.block(i,0); |
| 189 |
result.nptr(i) = mptr(i); |
| 190 |
for (int m = 0; m != mptr_[i]; ++m) |
| 191 |
*out++ = std::conj(*(block(nblock, i)+m*nmax_+off)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
int& nptr(const int i) { return nptr_[i]; } |
| 196 |
int& mptr(const int i) { return mptr_[i]; } |
| 197 |
const int& nptr(const int i) const { return nptr_[i]; } |
| 198 |
const int& mptr(const int i) const { return mptr_[i]; } |
| 199 |
int nmax() const { return nmax_; } |
| 200 |
int mmax() const { return mmax_; } |
| 201 |
|
| 202 |
void reset() { |
| 203 |
std::fill(nptr_.begin(), nptr_.end(), 1); |
| 204 |
std::fill(mptr_.begin(), mptr_.end(), 1); |
| 205 |
std::fill_n(data_, nmax_*mmax_*NB*MB, 0.0); |
| 206 |
} |
| 207 |
|
| 208 |
void print() { |
| 209 |
std::cout << std::setprecision(4); |
| 210 |
for (int n = 0; n != NB; ++n) |
| 211 |
for (int m = 0; m != MB; ++m) { |
| 212 |
std::cout << n << " " << m << ":" << std::endl; |
| 213 |
for (int i = 0; i != nptr_[n]; ++i) { |
| 214 |
for (int j = 0; j != mptr_[m]; ++j) |
| 215 |
std::cout << *(block(n, m)+i+nmax_*j); |
| 216 |
std::cout << std::endl; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
namespace { |
| 224 |
|
| 225 |
enum Trans { _N, _T, _C}; |
| 226 |
|
| 227 |
template<Trans TA, Trans TB, size_t N, size_t M, size_t K, size_t L, size_t X, size_t Y> |
| 228 |
void contract(std::complex<double> a, const SuperMatrix<N,M>& A, const SuperMatrix<K,L>& B, SuperMatrix<X,Y>& C) { |
| 229 |
const constexpr char* c1 = TA == _N ? "N" : (TA == _T ? "T" : "C"); |
| 230 |
const constexpr char* c2 = TB == _N ? "N" : (TB == _T ? "T" : "C"); |
| 231 |
const constexpr bool transA = !(TA == _N); |
| 232 |
const constexpr bool transB = !(TB == _N); |
| 233 |
const constexpr int loopblock = transA ? N : M; |
| 234 |
static_assert((transA ? M : N) == X, "A dim wrong"); |
| 235 |
static_assert((transB ? K : L) == Y, "B dim wrong"); |
| 236 |
static_assert((transA ? N : M) == (transB ? L : K), "AB dim wrong"); |
| 237 |
for (int y = 0; y != Y; ++y) |
| 238 |
for (int x = 0; x != X; ++x) { |
| 239 |
for (int l = 0; l != loopblock; ++l) { |
| 240 |
assert((transA ? A.nptr(l) : A.mptr(l)) == (transB ? B.mptr(l) : B.nptr(l))); |
| 241 |
zgemm3m_(c1, c2, (transA ? A.mptr(x) : A.nptr(x)), (transB ? B.nptr(y) : B.mptr(y)), (transA ? A.nptr(l) : A.mptr(l)), |
| 242 |
a, (transA ? A.block(l, x) : A.block(x, l)), A.nmax(), (transB ? B.block(y, l) : B.block(l, y)), B.nmax(), |
| 243 |
1.0, C.block(x,y), C.nmax()); |
| 244 |
} |
| 245 |
C.nptr(x) = (transA ? A.mptr(x) : A.nptr(x)); |
| 246 |
C.mptr(y) = (transB ? B.nptr(y) : B.mptr(y)); |
| 247 |
assert(C.nptr(x) <= C.nmax()); |
| 248 |
assert(C.mptr(y) <= C.mmax()); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
template<Trans TA, size_t N, size_t M, size_t K, size_t L, size_t X, size_t Y> |
| 253 |
void contract(std::complex<double> a, const SuperMatrix<N,M>& A, const SuperMatrix<K,L>& B, SuperMatrix<X,Y>& C) { |
| 254 |
const constexpr char* c1 = TA == _N ? "N" : (TA == _T ? "T" : "C"); |
| 255 |
const constexpr bool transA = !(TA == _N); |
| 256 |
const constexpr int loopblock = transA ? N : M; |
| 257 |
static_assert((transA ? M : N) == X, "A dim wrong"); |
| 258 |
static_assert(L == Y, "B dim wrong"); |
| 259 |
static_assert((transA ? N : M) == K, "AB dim wrong"); |
| 260 |
assert(B.mmax() == 1 && C.mmax() == 1); |
| 261 |
for (int y = 0; y != Y; ++y) |
| 262 |
for (int x = 0; x != X; ++x) { |
| 263 |
for (int l = 0; l != loopblock; ++l) { |
| 264 |
assert((transA ? A.nptr(l) : A.mptr(l)) == B.nptr(l)); |
| 265 |
zgemv_(c1, A.nptr(transA ? l : x) , A.mptr(transA ? x : l), a, (transA ? A.block(l, x) : A.block(x, l)), A.nmax(), B.block(l,y), 1, 1.0, C.block(x,y), 1); |
| 266 |
} |
| 267 |
C.nptr(x) = (transA ? A.mptr(x) : A.nptr(x)); |
| 268 |
C.mptr(y) = B.mptr(y); |
| 269 |
assert(C.nptr(x) <= C.nmax()); |
| 270 |
assert(C.mptr(y) <= C.mmax()); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
template<Trans TA, size_t N, size_t M, size_t K, size_t L, size_t X, size_t Y> |
| 276 |
void contract_tr(std::complex<double> a, const SuperMatrix<N,M>& A, const SuperMatrix<K,L>& B, SuperMatrix<X,Y>& C, std::complex<double>* work = nullptr) { |
| 277 |
#if 1 |
| 278 |
const constexpr char* c1 = TA == _N ? "N" : (TA == _T ? "T" : "C"); |
| 279 |
const constexpr bool transA = !(TA == _N); |
| 280 |
const constexpr int loopblock = transA ? N : M; |
| 281 |
static_assert((transA ? M : N) == X, "A dim wrong"); |
| 282 |
static_assert(L == Y, "B dim wrong"); |
| 283 |
static_assert((transA ? N : M) == K, "AB dim wrong"); |
| 284 |
assert(B.mmax() == 1 && C.mmax() == 1); |
| 285 |
assert(loopblock == 1 || work); |
| 286 |
for (int y = 0; y != Y; ++y) |
| 287 |
for (int x = 0; x != X; ++x) { |
| 288 |
for (int l = 0; l != loopblock; ++l) { |
| 289 |
assert((transA ? A.nptr(l) : A.mptr(l)) == B.nptr(l)); |
| 290 |
if (A.nptr(transA ? l : x) == A.mptr(transA ? x : l)) { |
| 291 |
std::copy_n(B.block(l,y), B.nptr(l), work); |
| 292 |
ztrmv_("U", c1, "N", B.nptr(l), (transA ? A.block(l, x) : A.block(x, l)), A.nmax(), work, 1); |
| 293 |
zaxpy_(B.nptr(l), a, work, 1, C.block(x,y), 1); |
| 294 |
} else { |
| 295 |
zgemv_(c1, A.nptr(transA ? l : x) , A.mptr(transA ? x : l), a, (transA ? A.block(l, x) : A.block(x, l)), A.nmax(), B.block(l,y), 1, 1.0, C.block(x,y), 1); |
| 296 |
} |
| 297 |
} |
| 298 |
C.nptr(x) = (transA ? A.mptr(x) : A.nptr(x)); |
| 299 |
C.mptr(y) = B.mptr(y); |
| 300 |
assert(C.nptr(x) <= C.nmax()); |
| 301 |
assert(C.mptr(y) <= C.mmax()); |
| 302 |
} |
| 303 |
#else |
| 304 |
contract<TA>(a, A, B, C); |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
}} |
| 310 |
|
| 311 |
#endif |