irlba_tatami
tatami wrappers for IRLBA
Loading...
Searching...
No Matches
normal.hpp
Go to the documentation of this file.
1#ifndef IRLBA_TATAMI_NORMAL_HPP
2#define IRLBA_TATAMI_NORMAL_HPP
3
4#include "tatami/tatami.hpp"
5#include "tatami_mult/tatami_mult.hpp"
6#include "irlba/irlba.hpp"
7#include "sanisizer/sanisizer.hpp"
8
9#include <memory>
10#include <type_traits>
11
17namespace irlba_tatami {
18
22template<class EigenVector_, typename TValue_, typename TIndex_, class TMatrix_ = tatami::Matrix<TValue_, TIndex_> >
23class NormalWorkspace final : public irlba::Workspace<EigenVector_> {
24public:
25 NormalWorkspace(const TMatrix_& mat, int num_threads) : my_mat(mat) {
26 opt.num_threads = num_threads;
27 }
28
29private:
30 const TMatrix_& my_mat;
31 tatami_mult::Options opt;
32
33public:
34 void multiply(const EigenVector_& right, EigenVector_& out) {
35 tatami_mult::multiply(my_mat, right.data(), out.data(), opt);
36 }
37};
38
39template<class EigenVector_, typename TValue_, typename TIndex_, class TMatrix_ = tatami::Matrix<TValue_, TIndex_> >
40class NormalAdjointWorkspace final : public irlba::AdjointWorkspace<EigenVector_> {
41public:
42 NormalAdjointWorkspace(const TMatrix_& mat, int num_threads) : my_mat(mat) {
43 opt.num_threads = num_threads;
44 }
45
46private:
47 const TMatrix_& my_mat;
48 tatami_mult::Options opt;
49
50public:
51 void multiply(const EigenVector_& right, EigenVector_& out) {
52 tatami_mult::multiply(right.data(), my_mat, out.data(), opt);
53 }
54};
55
56template<class EigenMatrix_, typename TValue_, typename TIndex_, class TMatrix_ = tatami::Matrix<TValue_, TIndex_> >
57class NormalRealizeWorkspace final : public irlba::RealizeWorkspace<EigenMatrix_> {
58public:
59 NormalRealizeWorkspace(const TMatrix_& mat, int num_threads) : my_mat(mat), my_num_threads(num_threads) {}
60
61private:
62 const TMatrix_& my_mat;
63 int my_num_threads;
64
65public:
66 const EigenMatrix_& realize(EigenMatrix_& buffer) {
67 // Copying into a transposed matrix, hence the switch of the ncol/nrow order.
68 // Both values can be cast to Eigen::Index, as we checked this in the Normal constructor.
69 buffer.resize(my_mat.nrow(), my_mat.ncol());
70
72 my_mat,
73 buffer.IsRowMajor,
74 buffer.data(),
75 [&]{
76 tatami::ConvertToDenseOptions opt;
77 opt.num_threads = my_num_threads;
78 return opt;
79 }()
80 );
81
82 return buffer;
83 }
84};
103template<class EigenVector_, class EigenMatrix_, typename TValue_, typename TIndex_, class TMatrixPointer_ = std::shared_ptr<const tatami::Matrix<TValue_, TIndex_> > >
104class Normal final : public irlba::Matrix<EigenVector_, EigenMatrix_> {
105public:
111 Normal(TMatrixPointer_ mat, int num_threads) : my_mat(std::move(mat)), my_num_threads(num_threads) {
112 // Check that these casts are safe in rows(), cols() below.
113 sanisizer::cast<Eigen::Index>(tatami::attest_for_Index(my_mat->nrow()));
114 sanisizer::cast<Eigen::Index>(tatami::attest_for_Index(my_mat->ncol()));
115 }
116
117public:
118 Eigen::Index rows() const {
119 return my_mat->nrow();
120 }
121
122 Eigen::Index cols() const {
123 return my_mat->ncol();
124 }
125
126private:
127 TMatrixPointer_ my_mat;
128 int my_num_threads;
129
130 typedef std::remove_cv_t<std::remove_reference_t<decltype(*my_mat)> > TMatrix;
131
132public:
133 std::unique_ptr<irlba::Workspace<EigenVector_> > new_workspace() const {
134 return new_known_workspace();
135 }
136
137 std::unique_ptr<irlba::AdjointWorkspace<EigenVector_> > new_adjoint_workspace() const {
139 }
140
141 std::unique_ptr<irlba::RealizeWorkspace<EigenMatrix_> > new_realize_workspace() const {
143 }
144
145public:
149 auto new_known_workspace() const {
150 return std::make_unique<NormalWorkspace<EigenVector_, TValue_, TIndex_, TMatrix> >(*my_mat, my_num_threads);
151 }
152
157 return std::make_unique<NormalAdjointWorkspace<EigenVector_, TValue_, TIndex_, TMatrix> >(*my_mat, my_num_threads);
158 }
159
164 return std::make_unique<NormalRealizeWorkspace<EigenMatrix_, TValue_, TIndex_, TMatrix> >(*my_mat, my_num_threads);
165 }
166};
167
168}
169
170#endif
Wrap a tatami matrix for irlba.
Definition normal.hpp:104
auto new_known_adjoint_workspace() const
Definition normal.hpp:156
Normal(TMatrixPointer_ mat, int num_threads)
Definition normal.hpp:111
auto new_known_realize_workspace() const
Definition normal.hpp:163
auto new_known_workspace() const
Definition normal.hpp:149
Wrap tatami for irlba.
void convert_to_dense(const Matrix< InputValue_, InputIndex_ > &matrix, const bool row_major, StoredValue_ *const store, const ConvertToDenseOptions &options)