fml  0.1-0
Fused Matrix Library
linalg.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_CPU_FUTURE_LINALG_H
6 #define FML_CPU_FUTURE_LINALG_H
7 #pragma once
8 
9 
10 #include "../linalg.hh"
11 
12 
13 namespace linalg
14 {
15  template <typename REAL>
16  void rsvd(const int k, const int q, cpumat<REAL> &x, cpuvec<REAL> &s, cpumat<REAL> &u, cpumat<REAL> &vt)
17  {
18  const len_t m = x.nrows();
19  const len_t n = x.ncols();
20 
21  cpumat<REAL> omega(n, 2*k);
22  omega.fill_runif();
23 
24  cpumat<REAL> Y(m, 2*k);
25  cpumat<REAL> QY(m, 2*k);
26  cpumat<REAL> Z(n, 2*k);
27  cpumat<REAL> QZ(n, 2*k);
28 
29  cpuvec<REAL> qraux;
30  cpuvec<REAL> work;
31 
32  cpumat<REAL> B(2*k, n);
33 
34  // Stage A
35  matmult(false, false, (REAL)1.0, x, omega, Y);
36  qr_internals(false, Y, qraux, work);
37  qr_Q(Y, qraux, QY, work);
38 
39  for (int i=1; i<q; i++)
40  {
41  matmult(true, false, (REAL)1.0, x, QY, Z);
42  qr_internals(false, Z, qraux, work);
43  qr_Q(Z, qraux, QZ, work);
44 
45  matmult(false, false, (REAL)1.0, x, QZ, Y);
46  qr_internals(false, Y, qraux, work);
47  qr_Q(Y, qraux, QY, work);
48  }
49 
50  // Stage B
51  matmult(true, false, (REAL)1.0, QY, x, B);
52 
53  cpumat<REAL> uB;
54  svd(B, s, uB, vt);
55 
56  s.resize(k);
57 
58  matmult(false, false, (REAL)1.0, QY, uB, u);
59  u.resize(u.nrows(), k);
60  }
61 }
62 
63 
64 #endif
fml::linalg::matmult
cpumat< REAL > matmult(const bool transx, const bool transy, const REAL alpha, const cpumat< REAL > &x, const cpumat< REAL > &y)
Returns alpha*op(x)*op(y) where op(A) is A or A^T.
Definition: linalg_blas.hh:126
fml::linalg::svd
void svd(cpumat< REAL > &x, cpuvec< REAL > &s)
Computes the singular value decomposition.
Definition: linalg_factorizations.hh:146
fml::linalg::qr_Q
void qr_Q(const cpumat< REAL > &QR, const cpuvec< REAL > &qraux, cpumat< REAL > &Q, cpuvec< REAL > &work)
Recover the Q matrix from a QR decomposition.
Definition: linalg_factorizations.hh:431