tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
eig_z.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 
46  const CTensor
47  eig(const CTensor &A, CTensor *R, CTensor *L)
48  {
49  assert(A.rows() > 0);
50  assert(A.rank() == 2);
51  assert(A.rows() == A.columns());
52 
53  char jobvl[2] = "N";
54  char jobvr[2] = "N";
55  integer lda, ldvl, ldvr, lwork, info;
56  cdouble *vl, *vr, *w;
57  double *rwork;
58  CTensor aux(A);
59  cdouble *a = tensor_pointer(aux);
60  integer n = A.rows();
61 
62  if ((size_t)n != A.columns()) {
63  std::cerr << "Routine eig() can only compute eigenvalues of square matrices, and you\n"
64  << "have passed a matrix that is " << A.rows() << " by " << A.columns();
65  abort();
66  }
67 
68  if (L) {
69  (*L) = CTensor(n,n);
70  vl = tensor_pointer(*L);
71  jobvl[0] = 'V';
72  } else {
73  vl = NULL;
74  }
75  if (R) {
76  (*R) = CTensor(n,n);
77  vr = tensor_pointer(*R);
78  jobvr[0] = 'V';
79  } else {
80  vr = NULL;
81  }
82 
83  ldvl = ldvr = n;
84  lda = n;
85  CTensor output(n);
86  w = tensor_pointer(output);
87 #ifdef TENSOR_USE_ACML
88  zgeev(*jobvl, *jobvr, n, a, lda, w, vl, ldvl, vr, ldvr, &info);
89 #else
90  cdouble work0[1];
91  lwork = -1;
92  rwork = new double[2*n];
93  F77NAME(zgeev)(jobvl, jobvr, &n, a, &lda, w, vl, &ldvl, vr, &ldvr, work0, &lwork,
94  rwork, &info);
95  lwork = lapack::real(work0[0]);
96 
97  cdouble *work = new cdouble[lwork];
98  F77NAME(zgeev)(jobvl, jobvr, &n, a, &lda, w, vl, &ldvl, vr, &ldvr, work, &lwork,
99  rwork, &info);
100  delete[] rwork;
101  delete[] work;
102 #endif
103  return output;
104  }
105 
106 } // 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