tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
eig_sym_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 namespace lapack;
27 
43  RTensor
44  eig_sym(const CTensor &A, CTensor *V)
45  {
46  assert(A.rows() > 0);
47  assert(A.rank() == 2);
48  assert(A.rows() == A.columns());
49 
50  //if (accurate_svd)
51  // return block_eig_sym(A, V);
52  integer n = A.rows();
53  if ((size_t)n != A.columns()) {
54  std::cerr << "Routine eig() can only compute eigenvalues of square matrices, and you\n"
55  << "have passed a matrix that is " << A.rows() << " by " << A.columns();
56  abort();
57  }
58 
59  CTensor aux(A);
60  cdouble *a = tensor_pointer(aux);
61  integer lda = n, info[1];
62  char jobz[2] = { (V == 0)? 'N' : 'V', 0 };
63  char uplo[2] = { 'U', 0 };
64  RTensor output(n);
65  double *w = tensor_pointer(output);
66  RTensor rwork(3*n);
67 
68 #ifdef TENSOR_USE_ACML
69  zheev(*jobz, *uplo, n, a, lda, w, info);
70 #else
71  integer lwork = -1;
72  CTensor work(1);
73  F77NAME(zheev)(jobz, uplo, &n, a, &lda, w, tensor_pointer(work),
74  &lwork, tensor_pointer(rwork), info);
75  lwork = (int)tensor::real(work[0]);
76 
77  work = CTensor(lwork);
78  F77NAME(zheev)(jobz, uplo, &n, a, &lda, w, tensor_pointer(work),
79  &lwork, tensor_pointer(rwork), info);
80 #endif
81 
82  if (V) *V = aux;
83  return output;
84  }
85 
86 } // namespace linalg
int rank() const
Number of Tensor indices.
Definition: tensor.h:119
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
RTensor eig_sym(const RTensor &A, RTensor *pR=0)
Eigenvalue decomposition of a real matrix.
Definition: eig_sym_d.cc:44
Complex Tensor with elements of type "cdouble".
Definition: tensor.h:435