tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
solve_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 
38  const RTensor
39  solve(const RTensor &A, const RTensor &B) {
40  integer n = A.rows();
41  integer lda = n;
42  integer ldb = B.dimension(0);
43  integer nrhs;
44 
45  // Currently, we only solve square systems
46  if ((size_t)n != A.columns()) {
47  std::cerr << "Routine solve() can only operate on square systems of equations, i.e\n"
48  << "when the number of unknowns is equal to the number of equations.\n"
49  << "However, you have passed a matrix of size " << A.columns() << " by " << A.rows();
50  abort();
51  }
52  // The size of B has to be compatible with that of A
53  if (n != ldb) {
54  std::cerr << "In solve(A,B), the number of equations does not match the number of right\n"
55  << "hand members. That is, while matrix A has " << n << " columns, the vector\n"
56  << "B has " << ldb << " elements.";
57  abort();
58  }
59 
60  // The matrix that we pass to LAPACK is overwritten with the solution X
61  RTensor output(B);
62  RTensor::elt_t *b = tensor_pointer(output);
63 
64  // Since B may be a tensor, we compute how many effective
65  // right-hand-sides (nrhs) there are.
66  nrhs = B.size() / ldb;
67 
68  // The matrix that we pass to LAPACK is modified
69  RTensor aux(A);
70  RTensor::elt_t *a = tensor_pointer(aux);
71 
72  integer *ipiv = new integer[n];
73  integer info;
74 #ifdef TENSOR_USE_ACML
75  dgesv(n, nrhs, a, lda, ipiv, b, ldb, &info);
76 #else
77  F77NAME(dgesv)(&n, &nrhs, a, &lda, ipiv, b, &ldb, &info);
78 #endif
79  delete[] ipiv;
80 
81  if (info) {
82  std::cerr <<
83  "In solve()\n"
84  "The matrix of the system of equations is singular and"
85  " thus the problem cannot be\n"
86  "solved with the standard resolutor.\n";
87  abort();
88  }
89 
90  return output;
91  }
92 
93 }
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
index size() const
Returns total number of elements in Tensor.
Definition: tensor.h:114
index dimension(int which) const
Length of a given Tensor index.
const RTensor solve(const RTensor &A, const RTensor &B)
Solve a real linear system of equations by Gauss-Seidel method.
Definition: solve_d.cc:39