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