VDVEigDecomposition.hh 2.89 KB
Newer Older
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
/*
 * Copyright (C) 2010 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *  Calculates vector of Eigen values D and matrix of Eigen vectors V
 *  of input matrix m
 */

#if !defined(VDVEigDec_INCLUDE)
#define VDVEigDec_INCLUDE

#include <cstdlib>
#include "Vector.hh"
#include "Matrix.hh"
#include <dynlapack.h>

class VDVEigDecomposition
{
  lapack_int lda, n;
36
  lapack_int lwork, info;
37
38
39
40
41
42
43
44
45
46
  double *work;
  bool converged;
  Matrix V;
  Vector D;
public:

public:
  class VDVEigException
  {
  public:
47
    const lapack_int info;
48
    std::string message;
49
    VDVEigException(lapack_int info_arg, std::string message_arg) :
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
      info(info_arg), message(message_arg) {
    };
  };

/**
 *  This constructor only creates optimal workspace using
 *  input matrix m
 */
  VDVEigDecomposition(const Matrix &m) throw(VDVEigException);

/**
 *  This constructoro only crates workspace using the size of
 *  the input matrix m
 */
  VDVEigDecomposition(size_t n) throw(VDVEigException);

  virtual ~VDVEigDecomposition()
  {
68
    delete[] work;
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
  };
  template <class Mat>
  void calculate(const Mat &H) throw(VDVEigException);
  // get eigenvalues
  Vector &
  getD()
  {
    return D;
  };
  // get eigen vector
  Matrix &
  getV()
  {
    return V;
  };
  // check if converged
  bool
  hasConverged()
  {
    return converged;
  };
};

template <class Mat>
void
VDVEigDecomposition::calculate(const Mat &m) throw(VDVEigException)
{
  info = 0;
  if (m.getRows() != m.getCols())
    throw(VDVEigException(info, "Matrix is not square in VDVEigDecomposition calculate"));

  if (m.getCols() != (size_t) n  || m.getLd() != (size_t) lda)
    throw(VDVEigException(info, "Matrix not matching VDVEigDecomposition class"));
102
103

  double tmpwork;
104
  lapack_int tmplwork = -1;
105
106
  V = m;
  dsyev("V", "U", &n, V.getData(), &lda, D.getData(), &tmpwork, &tmplwork, &info);
107
  if (lwork < (lapack_int) tmpwork)
108
    {
109
      lwork = (lapack_int) tmpwork;
110
111
112
      delete[] work;
      work = new double[lwork];
    }
113
114
115
116
117
118
119
120
121
122
123
124
  dsyev("V", "U", &n, V.getData(), &lda, D.getData(), work, &lwork, &info);

  if (info < 0)
    throw(VDVEigException(info, "Internal error in VDVEigDecomposition calculation"));
  converged = true;
  if (info)
    converged = false;
}

std::ostream &operator<<(std::ostream &out, const VDVEigDecomposition::VDVEigException &e);

#endif