tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
eigs_symz.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 // MPS library
20 //
21 // (c) 2004 Juan Jose Garcia Ripoll
22 //
23 //----------------------------------------------------------------------
24 // ARPACK DRIVER FOR SYMMETRIC REAL EIGENVALUE PROBLEMS
25 //
26 
27 #include <linalg.h>
28 #include <arpack.h>
29 #include <utils.h>
30 #include <sys/blas.h>
31 
32 #undef COMPLEX
33 
34 RTensor
35 eigs_sym(const CTensor &A, RArpack::EigType t, size_t neig, CTensor *eigenvectors,
36  const CTensor::elt_t *initial_guess)
37 {
38  size_t n = A.columns();
39 
40  if ((A.rank() != 2) || (A.rows() != n)) {
41  std::cerr << "In eigs(): Can only compute eigenvalues of square matrices.";
42  myabort();
43  }
44 
45  if (neig > n || neig == 0) {
46  std::cerr << "In eigs(): Can only compute up to " << n << " eigenvalues\n"
47  << "in a matrix that has " << n << " times " << n << " elements.";
48  myabort();
49  }
50 
51  if (n <= 4) {
52  RTensor values = eig_sym(A, eigenvectors);
53  UIVector ndx = RArpack::sort_values(values, t)(Range(0, neig-1));
54  if (eigenvectors) {
55  *eigenvectors = (*eigenvectors)(Range(), Range(ndx));
56  }
57  return re_part(values(Range(ndx)));
58  }
59 
60  RArpack data(2*n, t, neig);
61 
62  if (initial_guess)
63  data.set_start_vector((double *)initial_guess);
64 
65  while (data.update() < RArpack::Finished) {
66  cdouble *x = (CTensor::elt_t*)data.get_x_vector();
67  cdouble *y = (CTensor::elt_t*)data.get_y_vector();
68  gemv('N', n, n, CTensor::elt_one(), A.pointer(), n, x, 1,
69  CTensor::elt_zero(), y, 1);
70  }
71  if (data.get_status() == RArpack::Finished) {
72  if (eigenvectors) {
73  *eigenvectors = CTensor(n, neig);
74  }
75  return data.get_data(eigenvectors? (double*)eigenvectors->pointer() : NULL);
76  } else {
77  std::cerr << data.error_message() << '\n';
78  myabort();
79  }
80  return RTensor();
81 }
82 
RTensor eig_sym(const RTensor &A, RTensor *pR=0)
Eigenvalue decomposition of a real matrix.
Definition: eig_sym_d.cc:44
RTensor eigs_sym(const RTensor &A, int eig_type, size_t neig, RTensor *vectors=NULL, const double *initial_guess=NULL)
Find out a few eigenvalues and eigenvectors of a symmetric real matrix.