tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
eigs_sym_sp_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 <linalg.h>
21 #include <arpack.h>
22 
23 RTensor
24 eigs_sym(const CSparse &A, RArpack::EigType t, size_t neig, CTensor *eigenvectors,
25  const CTensor::elt_t *initial_guess)
26 {
27  size_t n = A.columns();
28 #ifdef COMPLEX
29  size_t ndouble = 2*n;
30 #else
31  size_t ndouble = n;
32 #endif
33 
34  if (n <= 10) {
35  return eigs_sym(full(A), t, neig, eigenvectors, initial_guess);
36  }
37 
38  if (A.rows() != n) {
39  std::cerr << "In eigs(): Can only compute eigenvalues of square matrices.";
40  myabort();
41  }
42 
43  RArpack data(ndouble, t, neig);
44 
45  if (initial_guess)
46  data.set_start_vector((const double *)initial_guess);
47 
48  size_t bytes = ndouble * sizeof(double);
49  while (data.update() < RArpack::Finished) {
50  CTensor x(n, (CTensor::elt_t *)data.get_x_vector());
51  CTensor y = mmult(A, x);
52  memcpy(data.get_y_vector(), (const double *)x.pointer(), bytes);
53  }
54  if (data.get_status() == RArpack::Finished) {
55  if (eigenvectors) {
56  *eigenvectors = CTensor(n, neig);
57  }
58  return data.get_data((double *)eigenvectors->pointer());
59  } else {
60  std::cerr << data.error_message() << '\n';
61  myabort();
62  }
63  return RTensor();
64 }
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.