aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
expose-function-ops.cpp
Go to the documentation of this file.
1
3
8
9namespace aligator {
10namespace python {
11
12using context::MatrixXs;
13using context::Scalar;
17using context::VectorXs;
18
19template <typename Base> void exposeSliceExpression(const char *name) {
20 // FUNCTION SLICE
21
22 using FunctionSliceXpr = FunctionSliceXprTpl<Scalar, Base>;
23
24 bp::register_ptr_to_python<shared_ptr<FunctionSliceXpr>>();
25 bp::class_<FunctionSliceXpr, bp::bases<Base>>(
26 name,
27 "Represents a slice of an expression according to either a single index "
28 "or an array of indices.",
29 bp::init<shared_ptr<Base>, std::vector<int> const &>(
30 bp::args("self", "func", "indices")))
31 .def(bp::init<shared_ptr<Base>, const int>(
32 "Constructor from a single index.", bp::args("self", "func", "idx")))
33 .def_readonly("func", &FunctionSliceXpr::func, "Underlying function.")
34 .def_readonly("indices", &FunctionSliceXpr::indices,
35 "Indices of the slice.");
36}
37
38template <typename LFC>
40 : bp::def_visitor<LinFunctionCompositionVisitor<LFC>> {
41 using FunType = typename LFC::Base;
42
43 template <class PyClass> void visit(PyClass &cl) const {
44 cl.def(bp::init<shared_ptr<FunType>, const MatrixXs, const VectorXs>(
45 "Construct a composition from the underlying function, weight "
46 "matrix "
47 ":math:`A` and bias :math:`b`.",
48 bp::args("self", "func", "A", "b")))
49 .def(bp::init<shared_ptr<FunType>, const context::MatrixXs>(
50 "Constructor where the bias :math:`b` is assumed to be zero.",
51 bp::args("self", "func", "A")))
52 .def_readonly("func", &LFC::func, "The underlying function.")
53 .def_readonly("A", &LFC::A, "Weight matrix.")
54 .def_readonly("b", &LFC::b, "Bias vector.");
55 bp::class_<typename LFC::Data, bp::bases<StageFunctionData>,
56 boost::noncopyable>("LFC", bp::no_init)
57 .def_readonly("sub_data", &LFC::Data::sub_data);
58
59 bp::def(
60 "linear_compose",
61 +[](shared_ptr<FunType> func, const MatrixXs &A, const VectorXs &b) {
62 return linear_compose(func, A, b);
63 },
64 bp::args("func", "A", "b"));
65 }
66};
67
69
70 exposeSliceExpression<StageFunction>("StageFunctionSliceXpr");
71 exposeSliceExpression<UnaryFunction>("UnaryFunctionSliceXpr");
72
73 using FunctionSliceData = FunctionSliceDataTpl<Scalar>;
74 bp::class_<FunctionSliceData, bp::bases<StageFunctionData>,
75 boost::noncopyable>("FunctionSliceData", bp::no_init)
76 .def_readonly("sub_data", &FunctionSliceData::sub_data,
77 "Underlying function's data.");
78
80
81 using LinearFunctionComposition = LinearFunctionCompositionTpl<Scalar>;
82 using LinearUnaryFunctionComposition =
83 LinearUnaryFunctionCompositionTpl<Scalar>;
84
85 bp::register_ptr_to_python<shared_ptr<LinearFunctionComposition>>();
86 bp::class_<LinearFunctionComposition, bp::bases<StageFunction>>(
87 "LinearFunctionComposition",
88 "Function composition :math:`r(x) = Af(x, u, y) + b`.", bp::no_init)
89 .def(LinFunctionCompositionVisitor<LinearFunctionComposition>());
90
91 bp::register_ptr_to_python<shared_ptr<LinearUnaryFunctionComposition>>();
92 bp::class_<LinearUnaryFunctionComposition, bp::bases<UnaryFunction>>(
93 "LinearUnaryFunctionComposition",
94 "Function composition for unary functions: :math:`r(x) = Af(x) + b`.",
95 bp::no_init)
96 .def(LinFunctionCompositionVisitor<LinearUnaryFunctionComposition>());
97}
98
99} // namespace python
100} // namespace aligator
StageFunctionTpl< Scalar > StageFunction
Definition context.hpp:17
UnaryFunctionTpl< Scalar > UnaryFunction
Definition context.hpp:18
StageFunctionDataTpl< Scalar > StageFunctionData
Definition context.hpp:19
void exposeSliceExpression(const char *name)
Main package namespace.
auto linear_compose(shared_ptr< StageFunctionTpl< Scalar > > func, const typename math_types< Scalar >::ConstMatrixRef A, const typename math_types< Scalar >::ConstVectorRef b)
Create a linear composition of the input function func.