irlba
A C++ library for IRLBA
Loading...
Searching...
No Matches
centered.hpp
Go to the documentation of this file.
1#ifndef IRLBA_MATRIX_CENTERED_HPP
2#define IRLBA_MATRIX_CENTERED_HPP
3
4#include <memory>
5
6#include "../utils.hpp"
7#include "interface.hpp"
8
9#include "Eigen/Dense"
10
16namespace irlba {
17
21template<class EigenVector_, class Matrix_, class Center_>
22class CenteredWorkspace final : public Workspace<EigenVector_> {
23public:
24 CenteredWorkspace(const Matrix_& matrix, const Center_& center) :
25 my_work(matrix.new_known_workspace()),
26 my_center(center)
27 {}
28
29private:
30 I<decltype(std::declval<Matrix_>().new_known_workspace())> my_work;
31 const Center_& my_center;
32
33public:
34 void multiply(const EigenVector_& right, EigenVector_& out) {
35 my_work->multiply(right, out);
36 const auto beta = right.dot(my_center);
37 for (auto& o : out) {
38 o -= beta;
39 }
40 }
41};
42
43template<class EigenVector_, class Matrix_, class Center_>
44class CenteredAdjointWorkspace final : public AdjointWorkspace<EigenVector_> {
45public:
46 CenteredAdjointWorkspace(const Matrix_& matrix, const Center_& center) :
47 my_work(matrix.new_known_adjoint_workspace()),
48 my_center(center)
49 {}
50
51private:
52 I<decltype(std::declval<Matrix_>().new_known_adjoint_workspace())> my_work;
53 const Center_& my_center;
54
55public:
56 void multiply(const EigenVector_& right, EigenVector_& out) {
57 my_work->multiply(right, out);
58 const auto beta = right.sum();
59 out -= beta * my_center;
60 }
61};
62
63template<class EigenMatrix_, class Matrix_, class Center_>
64class CenteredRealizeWorkspace final : public RealizeWorkspace<EigenMatrix_> {
65public:
66 CenteredRealizeWorkspace(const Matrix_& matrix, const Center_& center) :
67 my_work(matrix.new_known_realize_workspace()),
68 my_center(center)
69 {}
70
71private:
72 I<decltype(std::declval<Matrix_>().new_known_realize_workspace())> my_work;
73 const Center_& my_center;
74
75public:
76 const EigenMatrix_& realize(EigenMatrix_& buffer) {
77 my_work->realize_copy(buffer);
78 buffer.array().rowwise() -= my_center.adjoint().array();
79 return buffer;
80 }
81};
100template<
101 class EigenVector_,
102 class EigenMatrix_,
103 class MatrixPointer_ = const Matrix<EigenVector_, EigenMatrix_>*,
104 class CenterPointer_ = const EigenVector_*
105>
106class CenteredMatrix final : public Matrix<EigenVector_, EigenMatrix_> {
107public:
113 CenteredMatrix(MatrixPointer_ matrix, CenterPointer_ center) :
114 my_matrix(std::move(matrix)),
115 my_center(std::move(center))
116 {}
117
118private:
119 MatrixPointer_ my_matrix;
120 CenterPointer_ my_center;
121
122public:
123 Eigen::Index rows() const {
124 return my_matrix->rows();
125 }
126
127 Eigen::Index cols() const {
128 return my_matrix->cols();
129 }
130
131public:
132 std::unique_ptr<Workspace<EigenVector_> > new_workspace() const {
133 return new_known_workspace();
134 }
135
136 std::unique_ptr<AdjointWorkspace<EigenVector_> > new_adjoint_workspace() const {
138 }
139
140 std::unique_ptr<RealizeWorkspace<EigenMatrix_> > new_realize_workspace() const {
142 }
143
144public:
148 auto new_known_workspace() const {
149 return std::make_unique<CenteredWorkspace<EigenVector_, I<decltype(*my_matrix)>, I<decltype(*my_center)> > >(*my_matrix, *my_center);
150 }
151
156 return std::make_unique<CenteredAdjointWorkspace<EigenVector_, I<decltype(*my_matrix)>, I<decltype(*my_center)> > >(*my_matrix, *my_center);
157 }
158
163 return std::make_unique<CenteredRealizeWorkspace<EigenMatrix_, I<decltype(*my_matrix)>, I<decltype(*my_center)> > >(*my_matrix, *my_center);
164 }
165};
166
167}
168
169#endif
Deferred centering of a matrix.
Definition centered.hpp:106
auto new_known_workspace() const
Definition centered.hpp:148
Eigen::Index cols() const
Definition centered.hpp:127
auto new_known_adjoint_workspace() const
Definition centered.hpp:155
auto new_known_realize_workspace() const
Definition centered.hpp:162
Eigen::Index rows() const
Definition centered.hpp:123
std::unique_ptr< Workspace< EigenVector_ > > new_workspace() const
Definition centered.hpp:132
CenteredMatrix(MatrixPointer_ matrix, CenterPointer_ center)
Definition centered.hpp:113
std::unique_ptr< RealizeWorkspace< EigenMatrix_ > > new_realize_workspace() const
Definition centered.hpp:140
std::unique_ptr< AdjointWorkspace< EigenVector_ > > new_adjoint_workspace() const
Definition centered.hpp:136
Interface for a matrix to use in compute().
Definition interface.hpp:142
Interfaces for matrix inputs.