tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
svd_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 
44  RTensor
45  svd(CTensor A, CTensor *U, CTensor *VT, bool economic)
46  {
47  /*
48  if (accurate_svd) {
49  return block_svd(A, U, VT, economic);
50  }
51  */
52  assert(A.rows() > 0);
53  assert(A.columns() > 0);
54  assert(A.rank() == 2);
55 
56  integer m = A.rows();
57  integer n = A.columns();
58  integer k = std::min(m, n);
59  integer lwork, ldu, lda, ldv, info;
60  RTensor output(k);
61  cdouble *work, *u, *v, *a = tensor_pointer(A), foo;
62  double *rwork, *s = tensor_pointer(output);
63  char jobv[1], jobu[1];
64 
65  if (U) {
66  *U = CTensor(m, economic? k : m);
67  u = tensor_pointer(*U);
68  jobu[0] = economic? 'S' : 'A';
69  ldu = m;
70  } else {
71  jobu[0] = 'N';
72  u = &foo;
73  ldu = 1;
74  }
75  if (VT) {
76  (*VT) = CTensor(economic? k : n, n);
77  v = tensor_pointer(*VT);
78  jobv[0] = economic? 'S' : 'A';
79  ldv = economic? k : n;
80  } else {
81  jobv[0] = 'N';
82  v = &foo;
83  ldv = 1;
84  }
85  lda = m;
86 #ifdef TENSOR_USE_ACML
87  zgesvd(*jobu, *jobv, m, n, a, m, s, u, ldu, v, ldv, &info);
88 #else
89  lwork = -1;
90  work = &foo;
91  rwork = (double *)&foo;
92  F77NAME(zgesvd)(jobu, jobv, &m, &n, a, &m, s, u, &ldu, v, &ldv,
93  work, &lwork, rwork, &info);
94  lwork = lapack::real(work[0]);
95  work = new cdouble[lwork];
96  rwork = new double[5 * k];
97  F77NAME(zgesvd)(jobu, jobv, &m, &n, a, &m, s, u, &ldu, v, &ldv,
98  work, &lwork, rwork, &info);
99  delete[] work;
100  delete[] rwork;
101 #endif
102  return output;
103  }
104 
105 
106 } // 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
Complex Tensor with elements of type "cdouble".
Definition: tensor.h:435
RTensor svd(RTensor A, RTensor *pU=0, RTensor *pVT=0, bool economic=0)
Singular value decomposition of a real matrix.
Definition: svd_d.cc:50