DP3
SVDSolver.h
Go to the documentation of this file.
1 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #ifndef SVD_SOLVER_H
5 #define SVD_SOLVER_H
6 
7 #include <complex>
8 #include <vector>
9 #include <cmath>
10 
11 #include "LLSSolver.h"
12 
13 namespace dp3 {
14 namespace ddecal {
15 
16 using complex = std::complex<float>;
17 
18 /* CGELSD prototype */
19 extern "C" void cgelss_(int* m, int* n, int* nrhs, complex* a, int* lda,
20  complex* b, int* ldb, float* s, float* rcond, int* rank,
21  complex* work, int* lwork, float* rwork, int* info);
22 
23 class SVDSolver final : public LLSSolver {
24  public:
25  SVDSolver(int m, int n, int nrhs) : LLSSolver(m, n, nrhs) {}
26 
35  bool Solve(complex* a, complex* b) override {
36  int info;
37  int ldb = std::max(m_, n_);
38  std::vector<float> s;
39  s.resize(std::min(n_, m_));
40  float rcond = 0.0;
41  int rank;
42  std::vector<float> rwork(5 * std::min(m_, n_));
43 
44  if (work_.empty()) {
45  int lwork = -1;
46  complex wkopt;
47  cgelss_(&m_, &n_, &nrhs_, a, &m_, b, &ldb, s.data(), &rcond, &rank,
48  &wkopt, &lwork, rwork.data(), &info);
49  work_.resize((int)wkopt.real());
50  }
51  int lwork = work_.size();
52  cgelss_(&m_, &n_, &nrhs_, a, &m_, b, &ldb, s.data(), &rcond, &rank,
53  work_.data(), &lwork, rwork.data(), &info);
55  return info == 0;
56  }
57 
58  private:
59  std::vector<complex> work_;
60 };
61 
62 } // namespace ddecal
63 } // namespace dp3
64 
65 #endif
Definition: LLSSolver.h:25
int m_
Definition: LLSSolver.h:44
int n_
Definition: LLSSolver.h:45
int nrhs_
Definition: LLSSolver.h:46
std::complex< float > complex
Definition: LLSSolver.h:27
Definition: SVDSolver.h:23
SVDSolver(int m, int n, int nrhs)
Definition: SVDSolver.h:25
bool Solve(complex *a, complex *b) override
Definition: SVDSolver.h:35
std::complex< float > complex
Definition: QRSolver.h:16
void cgelss_(int *m, int *n, int *nrhs, complex *a, int *lda, complex *b, int *ldb, float *s, float *rcond, int *rank, complex *work, int *lwork, float *rwork, int *info)
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53