summaryrefslogtreecommitdiffstats
path: root/src/armadillo/include/armadillo_bits/newarp_UpperHessenbergEigen_meat.hpp
blob: a75820594b3b331d94db7cab8a1b3012f2800ef3 (plain)
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
// SPDX-License-Identifier: Apache-2.0
// 
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------


namespace newarp
{


template<typename eT>
inline
UpperHessenbergEigen<eT>::UpperHessenbergEigen()
  : n_rows(0)
  , computed(false)
  {
  arma_extra_debug_sigprint();
  }



template<typename eT>
inline
UpperHessenbergEigen<eT>::UpperHessenbergEigen(const Mat<eT>& mat_obj)
  : n_rows(mat_obj.n_rows)
  , computed(false)
  {
  arma_extra_debug_sigprint();
  
  compute(mat_obj);
  }



template<typename eT>
inline
void
UpperHessenbergEigen<eT>::compute(const Mat<eT>& mat_obj)
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( (mat_obj.is_square() == false), "newarp::UpperHessenbergEigen::compute(): matrix must be square" );
  
  n_rows = mat_obj.n_rows;
  
  mat_Z.set_size(n_rows, n_rows);
  mat_T.set_size(n_rows, n_rows);
  evals.set_size(n_rows);
  
  mat_Z.eye();
  mat_T = mat_obj;
  
  blas_int want_T = blas_int(1);
  blas_int want_Z = blas_int(1);
  
  blas_int n    = blas_int(n_rows);
  blas_int ilo  = blas_int(1);
  blas_int ihi  = blas_int(n_rows);
  blas_int iloz = blas_int(1);
  blas_int ihiz = blas_int(n_rows);
  
  blas_int info = blas_int(0);
  
  podarray<eT> wr(n_rows);
  podarray<eT> wi(n_rows);
  
  arma_extra_debug_print("lapack::lahqr()");
  lapack::lahqr(&want_T, &want_Z, &n, &ilo, &ihi, mat_T.memptr(), &n, wr.memptr(), wi.memptr(), &iloz, &ihiz, mat_Z.memptr(), &n, &info);
  
  if(info != 0)  { arma_stop_runtime_error("lapack::lahqr(): failed to compute all eigenvalues"); return; }
  
  for(uword i=0; i < n_rows; i++)  { evals(i) = std::complex<eT>(wr[i], wi[i]); }
  
  char     side   = 'R';
  char     howmny = 'B';
  blas_int m      = blas_int(0);
  
  podarray<eT> work(3*n);
  
  arma_extra_debug_print("lapack::trevc()");
  lapack::trevc(&side, &howmny, (blas_int*) NULL, &n, mat_T.memptr(), &n, (eT*) NULL, &n, mat_Z.memptr(), &n, &n, &m, work.memptr(), &info);
  
  if(info != 0)  { arma_stop_runtime_error("lapack::trevc(): illegal value"); return; }
  
  computed = true;
  }



template<typename eT>
inline
Col< std::complex<eT> >
UpperHessenbergEigen<eT>::eigenvalues()
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( (computed == false), "newarp::UpperHessenbergEigen::eigenvalues(): need to call compute() first" );
  
  return evals;
  }



template<typename eT>
inline
Mat< std::complex<eT> >
UpperHessenbergEigen<eT>::eigenvectors()
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( (computed == false), "newarp::UpperHessenbergEigen::eigenvectors(): need to call compute() first" );
  
  // Lapack will set the imaginary parts of real eigenvalues to be exact zero
  Mat< std::complex<eT> > evecs(n_rows, n_rows, arma_zeros_indicator());
  
  std::complex<eT>* col_ptr = evecs.memptr();
  
  for(uword i=0; i < n_rows; i++)
    {
    if(cx_attrib::is_real(evals(i), eT(0)))
      {
      // for real eigenvector, normalise and copy
      const eT z_norm = norm(mat_Z.col(i));
      
      for(uword j=0; j < n_rows; j++)
        {
        col_ptr[j] = std::complex<eT>(mat_Z(j, i) / z_norm, eT(0));
        }
      
      col_ptr += n_rows;
      }
    else
      {
      // complex eigenvectors are stored in consecutive columns
      const eT r2 = dot(mat_Z.col(i  ), mat_Z.col(i  ));
      const eT i2 = dot(mat_Z.col(i+1), mat_Z.col(i+1));
      
      const eT  z_norm = std::sqrt(r2 + i2);
      const eT* z_ptr  = mat_Z.colptr(i);
      
      for(uword j=0; j < n_rows; j++)
        {
        col_ptr[j         ] = std::complex<eT>(z_ptr[j] / z_norm, z_ptr[j + n_rows] / z_norm);
        col_ptr[j + n_rows] = std::conj(col_ptr[j]);
        }
      
      i++;
      col_ptr += 2 * n_rows;
      }
    }
  
  return evecs;
  }


}  // namespace newarp