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