aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
linear-function-composition.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace aligator {
7
8namespace detail {
9
10template <typename _FunType> struct linear_func_composition_impl : _FunType {
11 using FunType = _FunType;
12 using Scalar = typename FunType::Scalar;
14 using BaseData = StageFunctionDataTpl<Scalar>;
15
16 shared_ptr<FunType> func;
17 MatrixXs A;
18 VectorXs b;
19
20 struct Data : BaseData {
21 shared_ptr<BaseData> sub_data;
23 : BaseData(ptr.ndx1, ptr.nu, ptr.ndx2, ptr.nr),
24 sub_data(ptr.func->createData()) {}
25 };
26
27 linear_func_composition_impl(shared_ptr<FunType> func, const ConstMatrixRef A,
28 const ConstVectorRef b)
29 : FunType(func->ndx1, func->nu, func->ndx2, (int)A.rows()), func(func),
30 A(A), b(b) {
31 if (func == 0) {
32 ALIGATOR_RUNTIME_ERROR("Underlying function cannot be nullptr.");
33 }
34 if (A.rows() != b.rows()) {
35 ALIGATOR_RUNTIME_ERROR("Incompatible dimensions: A.rows() != b.rows()");
36 }
37 if (A.cols() != func->nr) {
38 ALIGATOR_RUNTIME_ERROR("Incompatible dimensions: A.cols() != func.nr");
39 }
40 }
41
42 linear_func_composition_impl(shared_ptr<FunType> func, const ConstMatrixRef A)
43 : linear_func_composition_impl(func, A, VectorXs::Zero(A.rows())) {}
44
45 shared_ptr<BaseData> createData() const {
46 return std::make_shared<Data>(*this);
47 }
48};
49} // namespace detail
50
51template <typename _Scalar>
53 : detail::linear_func_composition_impl<StageFunctionTpl<_Scalar>> {
54 using Scalar = _Scalar;
57 using Base = typename Impl::FunType;
58 using Data = typename Impl::Data;
59 using BaseData = StageFunctionDataTpl<Scalar>;
60
61 using Impl::A;
62 using Impl::b;
63 using Impl::createData;
64 using Impl::func;
65 using Impl::Impl;
66
67 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
68 const ConstVectorRef &y, BaseData &data) const;
69
70 void computeJacobians(const ConstVectorRef &x, const ConstVectorRef &u,
71 const ConstVectorRef &y, BaseData &data) const;
72};
73
74template <typename _Scalar>
76 : detail::linear_func_composition_impl<UnaryFunctionTpl<_Scalar>> {
77 using Scalar = _Scalar;
81 using Data = typename Impl::Data;
82 using BaseData = StageFunctionDataTpl<Scalar>;
83
84 using Impl::A;
85 using Impl::b;
86 using Impl::createData;
87 using Impl::func;
88 using Impl::Impl;
89
90 void evaluate(const ConstVectorRef &x, BaseData &data) const {
91 Data &d = static_cast<Data &>(data);
92
93 func->evaluate(x, *d.sub_data);
94 data.value_ = b;
95 data.value_.noalias() += A * d.sub_data->value_;
96 }
97
98 void computeJacobians(const ConstVectorRef &x, BaseData &data) const {
99 Data &d = static_cast<Data &>(data);
100
101 func->computeJacobians(x, *d.sub_data);
102 data.jac_buffer_.noalias() = A * d.sub_data->jac_buffer_;
103 }
104};
105
107template <typename Scalar>
108auto linear_compose(shared_ptr<StageFunctionTpl<Scalar>> func,
109 const typename math_types<Scalar>::ConstMatrixRef A,
110 const typename math_types<Scalar>::ConstVectorRef b) {
111 return std::make_shared<LinearFunctionCompositionTpl<Scalar>>(func, A, b);
112}
113
115template <typename Scalar>
116auto linear_compose(shared_ptr<UnaryFunctionTpl<Scalar>> func,
117 const typename math_types<Scalar>::ConstMatrixRef A,
118 const typename math_types<Scalar>::ConstVectorRef b) {
119 return std::make_shared<LinearUnaryFunctionCompositionTpl<Scalar>>(func, A,
120 b);
121}
122
123} // namespace aligator
124
125#include "aligator/modelling/linear-function-composition.hxx"
#define ALIGATOR_RUNTIME_ERROR(msg)
Definition exceptions.hpp:6
Base definitions for ternary functions.
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.
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &y, BaseData &data) const
Evaluate the function.
void computeJacobians(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &y, BaseData &data) const
Compute Jacobians of this function.
void computeJacobians(const ConstVectorRef &x, BaseData &data) const
void evaluate(const ConstVectorRef &x, BaseData &data) const
virtual void evaluate(const ConstVectorRef &x, Data &data) const =0
virtual void computeJacobians(const ConstVectorRef &x, Data &data) const =0
linear_func_composition_impl(shared_ptr< FunType > func, const ConstMatrixRef A, const ConstVectorRef b)
linear_func_composition_impl(shared_ptr< FunType > func, const ConstMatrixRef A)