//
// ZQUATEV: Diagonalization of quaternionic matrices
// File   : zquatev.h
// Copyright (c) 2013, Toru Shiozaki (shiozaki@northwestern.edu)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those
// of the authors and should not be interpreted as representing official policies,
// either expressed or implied, of the FreeBSD Project.
//

#ifndef __TS_ZQUATEV_H
#define __TS_ZQUATEV_H

#include <memory>
#include <complex>

namespace ts {

  // Diagonalize a quaternion matrix:
  //
  // (  A  -B* )
  // (  B   A* )
  //
  // where A is Hermitian and B is anti-Hermitian. Only the left half of the matrix
  // will be referenced as an input.
  //
  // This matrix has doubly-degenerate eigenvalues, and there is a set of
  // eivenvectors that has the same symmetry property:
  //
  // (  U  -V* )
  // (  V   U* )
  //
  extern int zquatev(const int n2, std::complex<double>* const D, const int nld2, double* const eig);

  // internal functions
  namespace impl {
    extern void panel_update(const int current_size, const int block_size,
                             std::complex<double>* const D0, std::complex<double>* const D1,
                             std::complex<double>* const Q0, std::complex<double>* const Q1,
                             const int ld, const int original_size, std::complex<double>* const work);
    extern void unblocked_update(const int current_size,
                                 std::complex<double>* const D0, std::complex<double>* const D1,
                                 std::complex<double>* const Q0, std::complex<double>* const Q1,
                                 const int ld, const int original_size, std::complex<double>* const work);

    extern void transpose     (const int, const int, const std::complex<double>*, const int, std::complex<double>*, const int);
    extern void transpose_conj(const int, const int, const std::complex<double>*, const int, std::complex<double>*, const int);

    namespace {
      void conj_n(std::complex<double>* p, const size_t n) {
        for (size_t i = 0; i != n; ++i)
          p[i] = std::conj(p[i]);
      }
    }
  }
}

#endif
