version 0.4.1
differentiablefunction.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
10#pragma once
11#include <tuple>
12#include <type_traits>
13
14#include <dune/common/hybridutilities.hh>
15#include <dune/functions/common/differentiablefunctionfromcallables.hh>
16
19
20namespace Ikarus {
21
22namespace Impl {
23
29 template <typename... Args>
30 struct Functions
31 {
32 std::tuple<Args...> args;
33 };
34} // namespace Impl
35
43template <typename... Args>
44auto functions(Args&&... args) {
45 return Impl::Functions<std::remove_cvref_t<Args>...>{std::forward_as_tuple(std::forward<Args>(args)...)};
46}
47
48#ifndef DOXYGEN
49template <class Signature, template <class> class DerivativeTraits, class... F>
50class DifferentiableFunction;
51#endif
52
60template <class Range, class D, template <class> class DerivativeTraits, class F, class... FF>
61class DifferentiableFunction<Range(D), DerivativeTraits, F, FF...>
62 : private Dune::Functions::DifferentiableFunctionFromCallables<Range(D), DerivativeTraits, F, FF...>
63{
64 using Base = Dune::Functions::DifferentiableFunctionFromCallables<Range(D), DerivativeTraits, F, FF...>;
65
66public:
67 using Domain = std::remove_cvref_t<D>;
68 static constexpr auto nDerivatives = sizeof...(FF);
69
70 template <class... FFF>
72 : Base(std::forward<FFF>(f)...) {}
73 using Traits = DerivativeTraitsFromCallables<Impl::Functions<F, FF...>, D>;
74
75 using Derivative = DifferentiableFunction<typename Traits::template Range<1>(D), DerivativeTraits, FF...>;
76
77 Range operator()(const D& x) const { return Base::operator()(x); }
78
83 friend Derivative derivative(const DifferentiableFunction& t) {
84 auto df = derivative(static_cast<const Base&>(t));
85 return Derivative(df);
86 }
87};
88
98template <typename... F, typename Arg>
99auto makeDifferentiableFunction(const Impl::Functions<F...>& derivativesFunctions, const Arg& parameter) {
100 DerivativeTraitsFromCallables t(derivativesFunctions, parameter);
101 using DerivTraits = decltype(t);
102 auto la = []<typename... FF>(FF&&... f) {
103 return Ikarus::DifferentiableFunction<typename DerivTraits::template Range<0>(typename DerivTraits::Domain),
104 DerivTraits::template DerivativeTraits, std::remove_cvref_t<FF>...>(
105 std::forward<FF>(f)...);
106 };
107 return std::apply(la, derivativesFunctions.args);
108}
109
110} // namespace Ikarus
Contains stl-like type traits.
Contains derivative traits for common vbalue and derivative relations used for makeDifferentiableFunc...
Definition: assemblermanipulatorbuildingblocks.hh:22
auto makeDifferentiableFunction(const Impl::Functions< F... > &derivativesFunctions, const Arg &parameter)
Factory method for DifferentiableFunction It is a function taking several callables and the argument ...
Definition: differentiablefunction.hh:99
auto functions(Args &&... args)
Creates a Functions object.
Definition: differentiablefunction.hh:44
DerivativeTraitsFromCallables(const Impl::Functions< DerivativeArgs... > &derivativesFunctions, Arg &&parameterI) -> DerivativeTraitsFromCallables< Impl::Functions< DerivativeArgs... >, Arg >
std::remove_cvref_t< D > Domain
Definition: differentiablefunction.hh:67
DifferentiableFunction(FFF &&... f)
Definition: differentiablefunction.hh:71
DifferentiableFunction< typename Traits::template Range< 1 >(D), DerivativeTraits, FF... > Derivative
Definition: differentiablefunction.hh:75
DerivativeTraitsFromCallables< Impl::Functions< F, FF... >, D > Traits
Definition: differentiablefunction.hh:73
Range operator()(const D &x) const
Definition: differentiablefunction.hh:77
friend Derivative derivative(const DifferentiableFunction &t)
Get derivative of DifferentiableFunction.
Definition: differentiablefunction.hh:83