MUQ  0.4.3
LinearOperatorWrapper.cpp
Go to the documentation of this file.
1 #include "AllClassWrappers.h"
2 
4 
11 //#include "MUQ/Modeling/LinearAlgebra/SparseLinearOperator.h"
24 
26 
27 #include <pybind11/pybind11.h>
28 #include <pybind11/stl.h>
29 #include <pybind11/eigen.h>
30 #include <pybind11/iostream.h>
31 
32 #include <string>
33 
34 #include <functional>
35 #include <vector>
36 
37 using namespace muq::Modeling::PythonBindings;
38 using namespace muq::Modeling;
39 using namespace muq::Utilities;
40 
41 namespace py = pybind11;
42 
43 
45 {
46 
47  py::class_<LinearOperator, ModPiece, WorkPiece, std::shared_ptr<LinearOperator>> lo(m, "LinearOperator");
48  lo
49  .def("rows", &LinearOperator::rows)
50  .def("cols", &LinearOperator::cols)
51  .def("GetMatrix", &LinearOperator::GetMatrix);
52 
53  py::class_<AffineOperator, ModPiece, WorkPiece, std::shared_ptr<AffineOperator>>(m, "AffineOperator")
54  .def(py::init([](Eigen::MatrixXd const& Ain, Eigen::VectorXd const& bIn) {
55  return std::make_shared<AffineOperator>(Ain, bIn);;
56  }))
57  .def(py::init<std::shared_ptr<LinearOperator>,Eigen::VectorXd>())
58  .def("Linear", &AffineOperator::Linear)
59  .def("Offset", &AffineOperator::Offset)
60  .def("rows", &AffineOperator::rows)
61  .def("cols", &AffineOperator::cols);
62 
63  py::class_<EigenLinearOperator<Eigen::MatrixXd>, LinearOperator, ModPiece, WorkPiece, std::shared_ptr<EigenLinearOperator<Eigen::MatrixXd>>> elo(m, "DenseLinearOperator");
64  elo
65  .def(py::init<Eigen::MatrixXd>())
66  .def("Apply", &EigenLinearOperator<Eigen::MatrixXd>::Apply)
67  .def("ApplyTranspose", &EigenLinearOperator<Eigen::MatrixXd>::ApplyTranspose);
68 
69  py::class_<IdentityOperator, LinearOperator, ModPiece, WorkPiece, std::shared_ptr<IdentityOperator>> io(m, "IdentityOperator");
70  io
71  .def(py::init<unsigned int>())
72  .def("Apply", &IdentityOperator::Apply)
73  .def("ApplyTranspose", &IdentityOperator::ApplyTranspose);
74 
75  /*
76  py::class_<SparseLinearOperator, LinearOperator, std::shared_ptr<SparseLinearOperator>> slo(m, "SparseLinearOperator");
77  slo
78  .def(py::init<Eigen::SparseMatrix const&>())
79  .def("Apply", (Eigen::MatrixXd (SparseLinearOperator::*)(Eigen::Ref<Eigen::MatrixXd> const&)) &SparseLinearOperator::Apply)
80  .def("Apply", (void (SparseLinearOperator::*)(Eigen::Ref<Eigen::MatrixXd> const&, Eigen::Ref<Eigen::MatrixXd>)) &SparseLinearOperator::Apply)
81  .def("ApplyTranspose", (Eigen::MatrixXd (SparseLinearOperator::*)(Eigen::Ref<Eigen::MatrixXd> const&)) &SparseLinearOperator::ApplyTranspose)
82  .def("ApplyTranspose", (void (SparseLinearOperator::*)(Eigen::Ref<Eigen::MatrixXd> const&, Eigen::Ref<Eigen::MatrixXd>)) &SparseLinearOperator::ApplyTranspose);
83  */
84 
85  py::class_<BlockDiagonalOperator, LinearOperator, std::shared_ptr<BlockDiagonalOperator>> bdOp(m, "BlockDiagonalOperator");
86  bdOp
87  .def(py::init<std::vector<std::shared_ptr<LinearOperator>> const&>())
88  .def("Apply", &BlockDiagonalOperator::Apply)
89  .def("ApplyTranspose", &BlockDiagonalOperator::ApplyTranspose)
90  .def("GetMatrix", &BlockDiagonalOperator::GetMatrix)
91  .def("GetBlock", &BlockDiagonalOperator::GetBlock)
92  .def("GetBlocks", &BlockDiagonalOperator::GetBlocks);
93 
94  py::class_<BlockRowOperator, LinearOperator, std::shared_ptr<BlockRowOperator>> brOp(m, "BlockRowOperator");
95  brOp
96  .def(py::init<std::vector<std::shared_ptr<LinearOperator>> const&>())
97  .def("Apply", &BlockRowOperator::Apply)
98  .def("ApplyTranspose", &BlockRowOperator::ApplyTranspose)
99  .def("GetMatrix", &BlockRowOperator::GetMatrix)
100  .def("GetBlock", &BlockRowOperator::GetBlock)
101  .def("GetBlocks", &BlockRowOperator::GetBlocks);
102 
103  py::class_<CompanionMatrix, LinearOperator, std::shared_ptr<CompanionMatrix>> compMat(m, "CompanionMatrix");
104  compMat
105  .def(py::init<Eigen::VectorXd const&>())
106  .def("Apply", &CompanionMatrix::Apply)
107  .def("ApplyTranspose", &CompanionMatrix::ApplyTranspose)
108  .def("GetMatrix", &CompanionMatrix::GetMatrix);
109 
110  py::class_<ConcatenateOperator, LinearOperator, std::shared_ptr<ConcatenateOperator>> concatOp(m, "ConcatenateOperator");
111  concatOp
112  .def(py::init<std::vector<std::shared_ptr<LinearOperator>> const&, const int>())
113  .def("Apply", &ConcatenateOperator::Apply)
114  .def("ApplyTranspose", &ConcatenateOperator::ApplyTranspose)
115  .def("GetMatrix", &ConcatenateOperator::GetMatrix)
116  .def("VStack", &ConcatenateOperator::VStack)
117  .def("HStack", &ConcatenateOperator::HStack);
118 
119  py::class_<DiagonalOperator, LinearOperator, std::shared_ptr<DiagonalOperator>> diagOp(m, "DiagonalOperator");
120  diagOp
121  .def(py::init<Eigen::VectorXd const&>())
122  .def("Apply", &DiagonalOperator::Apply)
123  .def("ApplyTranspose", &DiagonalOperator::ApplyTranspose)
124  .def("GetMatrix", &DiagonalOperator::GetMatrix);
125 
126  py::class_<KroneckerProductOperator, LinearOperator, std::shared_ptr<KroneckerProductOperator>> kpOp(m, "KroneckerProductOperator");
127  kpOp
128  .def(py::init<std::shared_ptr<LinearOperator>, std::shared_ptr<LinearOperator>>())
129  .def("Apply", &KroneckerProductOperator::Apply)
130  .def("ApplyTranspose", &KroneckerProductOperator::ApplyTranspose);
131 
132  py::class_<ProductOperator, LinearOperator, std::shared_ptr<ProductOperator>> prOp(m, "ProductOperator");
133  prOp
134  .def(py::init<std::shared_ptr<LinearOperator>, std::shared_ptr<LinearOperator>>())
135  .def("Apply", &ProductOperator::Apply)
136  .def("ApplyTranspose", &ProductOperator::ApplyTranspose)
137  .def("GetMatrix", &ProductOperator::GetMatrix);
138 
139  py::class_<SumOperator, LinearOperator, std::shared_ptr<SumOperator>> sumOp(m, "SumOperator");
140  sumOp
141  .def(py::init<std::shared_ptr<LinearOperator>, std::shared_ptr<LinearOperator>>())
142  .def("Apply", &SumOperator::Apply)
143  .def("ApplyTranspose", &SumOperator::ApplyTranspose)
144  .def("GetMatrix", &SumOperator::GetMatrix);
145 
146  py::class_<ZeroOperator, LinearOperator, std::shared_ptr<ZeroOperator>> zeroOp(m, "ZeroOperator");
147  zeroOp
148  .def(py::init<int, int>())
149  .def("Apply", &ZeroOperator::Apply)
150  .def("ApplyTranspose", &ZeroOperator::ApplyTranspose);
151 
152  py::class_<SliceOperator, LinearOperator, std::shared_ptr<SliceOperator>>(m, "SliceOperator")
153  .def(py::init<int, int,int,int>())
154  .def("Apply", &SliceOperator::Apply)
155  .def("ApplyTranspose", &SliceOperator::ApplyTranspose);
156 
157  py::class_<HessianOperator, LinearOperator, std::shared_ptr<HessianOperator>>(m, "HessianOperator")
158  .def(py::init<std::shared_ptr<ModPiece>const&, std::vector<Eigen::VectorXd>const&, unsigned int, unsigned int, unsigned int, Eigen::VectorXd const&, double, double>(),
159  py::arg("pieceIn"),
160  py::arg("inputsIn"),
161  py::arg("outWrtIn"),
162  py::arg("inWrt1In"),
163  py::arg("inWrt2In"),
164  py::arg("sensIn"),
165  py::arg("scaleIn")=1.0,
166  py::arg("nuggetIn")=0.0)
167  .def("Apply", &HessianOperator::Apply)
168  .def("ApplyTranspose", &HessianOperator::ApplyTranspose);
169 
170  py::class_<GaussNewtonOperator, LinearOperator, std::shared_ptr<GaussNewtonOperator>>(m, "GaussNewtonOperator")
171  .def(py::init<std::shared_ptr<ModPiece>const&, std::shared_ptr<ModPiece>const&, std::vector<Eigen::VectorXd>const&, unsigned int, double, double>(),
172  py::arg("forwardModelIn"),
173  py::arg("noiseModelIn"),
174  py::arg("inputsIn"),
175  py::arg("inWrt"),
176  py::arg("scaleIn")=1.0,
177  py::arg("nuggetIn")=0.0)
178  .def("Apply", &GaussNewtonOperator::Apply)
179  .def("ApplyTranspose", &GaussNewtonOperator::ApplyTranspose);
180 
181  py::class_<GeneralizedEigenSolver, std::shared_ptr<GeneralizedEigenSolver>>(m,"GeneralizedEigenSolver")
182  .def("eigenvalues", &GeneralizedEigenSolver::eigenvalues)
183  .def("eigenvectors", &GeneralizedEigenSolver::eigenvectors);
184 
185  py::class_<StochasticEigenSolver, GeneralizedEigenSolver, std::shared_ptr<StochasticEigenSolver>>(m,"StochasticEigenSolver")
186  .def(py::init<int,double,double,int,int,int,int>(),
187  py::arg("numEigsIn"),
188  py::arg("eigRelTolIn")=0.0,
189  py::arg("eigAbsTolIn")=0.0,
190  py::arg("expectedRankIn")=-1,
191  py::arg("samplingFactorIn")=-1,
192  py::arg("blockSize=10"),
193  py::arg("verbosityIn")=0)
194  .def(py::init( [](py::dict d) {return new StochasticEigenSolver(ConvertDictToPtree(d));}))
195  .def("compute", &StochasticEigenSolver::compute, py::arg("A"), py::arg("B")=nullptr, py::arg("Binv")=nullptr, py::call_guard<py::scoped_ostream_redirect,
196  py::scoped_estream_redirect>());
197 
198 };
std::shared_ptr< LinearOperator > Linear() const
Eigen::VectorXd const & Offset() const
std::vector< std::shared_ptr< LinearOperator > > const & GetBlocks() const
virtual Eigen::MatrixXd GetMatrix() override
std::shared_ptr< LinearOperator > GetBlock(int i) const
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
std::vector< std::shared_ptr< LinearOperator > > const & GetBlocks() const
virtual Eigen::MatrixXd GetMatrix() override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
std::shared_ptr< LinearOperator > GetBlock(int i) const
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd GetMatrix() override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
static std::shared_ptr< ConcatenateOperator > HStack(std::shared_ptr< LinearOperator > Ain, std::shared_ptr< LinearOperator > Bin)
static std::shared_ptr< ConcatenateOperator > VStack(std::shared_ptr< LinearOperator > Ain, std::shared_ptr< LinearOperator > Bin)
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd GetMatrix() override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd GetMatrix() override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Eigen::VectorXd const & eigenvalues() const
Eigen::MatrixXd const & eigenvectors() const
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x)
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x)
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Generic linear operator base class.
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x)=0
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
virtual Eigen::MatrixXd GetMatrix() override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Two-pass stochastic algorithm for computing generalized eigenvalues from matrix products.
virtual StochasticEigenSolver & compute(std::shared_ptr< LinearOperator > const &A, std::shared_ptr< LinearOperator > B=nullptr, std::shared_ptr< LinearOperator > Binv=nullptr)
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: SumOperator.cpp:21
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: SumOperator.cpp:15
virtual Eigen::MatrixXd GetMatrix() override
Definition: SumOperator.h:26
Base class for MUQ's modelling envronment.
Definition: WorkPiece.h:40
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: ZeroOperator.h:21
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Definition: ZeroOperator.h:15
void LinearOperatorWrapper(pybind11::module &m)
boost::property_tree::ptree ConvertDictToPtree(pybind11::dict dict)