tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
eig_d.cc
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <tensor/tensor.h>
21 #include <tensor/tensor_lapack.h>
22 #include <tensor/linalg.h>
23 
24 namespace linalg {
25 
26  using tensor::RTensor;
27  using tensor::CTensor;
28  using namespace lapack;
29 
44  const CTensor
45  eig(const RTensor &A, CTensor *R, CTensor *L)
46  {
47  assert(A.rows() > 0);
48  assert(A.rank() == 2);
49  assert(A.rows() == A.columns());
50 
51  char jobvl[2] = "N";
52  char jobvr[2] = "N";
53  integer lda, ldvl, ldvr, lwork, info;
54  double *vl, *vr, *wr, *wi;
55  RTensor aux(A);
56  double *a = tensor_pointer(aux);
57  integer n = A.rows();
58  RTensor *realL, *realR;
59 
60  if ((size_t)n != A.columns()) {
61  std::cerr << "Routine eig() can only compute eigenvalues of square matrices, and you\n"
62  << "have passed a matrix that is " << A.rows() << " by " << A.columns();
63  abort();
64  }
65 
66  if (L) {
67  realL = new RTensor(n,n);
68  vl = tensor_pointer(*realL);
69  jobvl[0] = 'V';
70  } else {
71  realL = NULL;
72  vl = NULL;
73  }
74  if (R) {
75  realR = new RTensor(n, n);
76  vr = tensor_pointer(*realR);
77  jobvr[0] = 'V';
78  } else {
79  realR = NULL;
80  vr = NULL;
81  }
82  ldvl = ldvr = n;
83  lda = n;
84  RTensor real(n), imag(n);
85  wr = tensor_pointer(real);
86  wi = tensor_pointer(imag);
87 
88 #ifdef TENSOR_USE_ACML
89  dgeev(*jobvl, *jobvr, n, a, lda, wr, wi, vl, ldvl, vr,
90  ldvr, &info);
91 #else
92  lwork = -1;
93  double work0[1];
94  F77NAME(dgeev)(jobvl, jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr, work0, &lwork,
95  &info);
96  lwork = (int)work0[0];
97  double *work = new double[lwork];
98  F77NAME(dgeev)(jobvl, jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr, work, &lwork,
99  &info);
100  delete[] work;
101 #endif
102 
103  CTensor output(to_complex(real));
104  if (L) *L = to_complex(*realL);
105  if (R) *R = to_complex(*realR);
106  for (size_t i = 0; i < (size_t)n; i++) {
107  if (imag[i] != 0) {
108  // Complex eigenvalues and eigenvectors. The i-th elements have
109  // the real part, the i+1-th, the imaginary.
110  output.at(i) = tensor::to_complex(real[i],imag[i]);
111  output.at(i+1) = tensor::to_complex(real[i],-imag[i]);
112  if (realL) {
113  for (size_t j = 0; j < (size_t)n; j++) {
114  double re = (*realL)(j,i);
115  double im = (*realL)(j,i+1);
116  (*L).at(j,i) = tensor::to_complex(re,im);
117  (*L).at(j,i+1) = tensor::to_complex(re,-im);
118  }
119  }
120  if (realR) {
121  for (size_t j = 0; j < (size_t)n; j++) {
122  double re = (*realR)(j,i);
123  double im = (*realR)(j,i+1);
124  (*R).at(j,i) = tensor::to_complex(re,im);
125  (*R).at(j,i+1) = tensor::to_complex(re,-im);
126  }
127  }
128  i++;
129  }
130  }
131  delete realL;
132  delete realR;
133  return output;
134  }
135 
136 } // namespace linalg
int rank() const
Number of Tensor indices.
Definition: tensor.h:119
const CTensor eig(const RTensor &A, CTensor *R=0, CTensor *L=0)
Eigenvalue decomposition of a real matrix.
Definition: eig_d.cc:45
index columns() const
Query the size of 2nd index.
Definition: tensor.h:137
index rows() const
Query then size of 1st index.
Definition: tensor.h:139
Real Tensor with elements of type "double".
Definition: tensor.h:349
Complex Tensor with elements of type "cdouble".
Definition: tensor.h:435
elt_t & at(index i)
Return a mutable reference to an element of a 1D Tensor.