DUNE PDELab (git)

differentiablefunctionfromcallables.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_COMMON_DIFFEREENTIONABEFUNCTIONFROMCALLABLES_HH
8#define DUNE_FUNCTIONS_COMMON_DIFFEREENTIONABEFUNCTIONFROMCALLABLES_HH
9
10
12#include <dune/common/hybridutilities.hh>
13
14#include <dune/functions/common/signature.hh>
15
16#include <dune/functions/common/differentiablefunction.hh>
17#include <dune/functions/common/functionconcepts.hh>
18
19
20
21namespace Dune {
22namespace Functions {
23
24
25
26template<class Signature, template<class> class DerivativeTraits, class... Callables>
27class DifferentiableFunctionFromCallables;
28
29
30
47template<class Range, class Domain, template<class> class DerivativeTraits, class F>
48class DifferentiableFunctionFromCallables<Range(Domain), DerivativeTraits, F>
49{
50public:
51
53 using Signature = Range(Domain);
54
55 using RawSignature = typename SignatureTraits<Signature>::RawSignature;
56
58 using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(Domain);
59
61 using Derivative = DifferentiableFunction<DerivativeSignature, DerivativeTraits>;
62
64 template<class FF, disableCopyMove<DifferentiableFunctionFromCallables, FF> = 0>
66 f_(std::forward<FF>(f))
67 {}
68
70 Range operator() (const Domain& x) const
71 {
72 return f_(x);
73 }
74
80 friend Derivative derivative(const DifferentiableFunctionFromCallables& t)
81 {
82 DUNE_THROW(Dune::NotImplemented, "Derivative not implemented");
83 }
84
85private:
86 F f_;
87};
88
89
90
107template<class Range, class Domain, template<class> class DerivativeTraits, class F, class DF, class... Derivatives>
108class DifferentiableFunctionFromCallables<Range(Domain), DerivativeTraits, F, DF, Derivatives...>
109{
110public:
111
112 using Signature = Range(Domain);
113 using RawSignature = typename SignatureTraits<Signature>::RawSignature;
114 using DerivativeSignature = typename DerivativeTraits<RawSignature>::Range(Domain);
115
116 using Derivative = DifferentiableFunctionFromCallables<DerivativeSignature, DerivativeTraits, DF, Derivatives...>;
117
124 template<class FF, class DFF, class... DDFF>
125 DifferentiableFunctionFromCallables(FF&& f, DFF&& df, DDFF&&... ddf) :
126 f_(std::forward<FF>(f)),
127 df_(std::forward<DFF>(df), std::forward<DDFF>(ddf)...)
128 {}
129
131 Range operator() (const Domain& x) const
132 {
133 return f_(x);
134 }
135
141 friend Derivative derivative(const DifferentiableFunctionFromCallables& t)
142 {
143 return t.df_;
144 }
145
146private:
147 F f_;
148 Derivative df_;
149};
150
151
166template<class Signature, template<class> class DerivativeTraits, class... F>
167DifferentiableFunctionFromCallables<Signature, DerivativeTraits, F...>
168 makeDifferentiableFunctionFromCallables(const SignatureTag<Signature, DerivativeTraits>& signatureTag, F&&... f)
169{
170 return DifferentiableFunctionFromCallables<Signature, DerivativeTraits, F...>(f...);
171}
172
173
174
175} // namespace Functions
176} // namespace Dune
177
178#endif //DUNE_FUNCTIONS_COMMON_DIFFEREENTIONABEFUNCTIONFROMCALLABLES_HH
DifferentiableFunctionFromCallables(FF &&f, DFF &&df, DDFF &&... ddf)
Constructor copying the given functions.
Definition: differentiablefunctionfromcallables.hh:125
DifferentiableFunction< DerivativeSignature, DerivativeTraits > Derivative
Type of derivative.
Definition: differentiablefunctionfromcallables.hh:61
DifferentiableFunctionFromCallables(FF &&f)
Constructor copying the given function.
Definition: differentiablefunctionfromcallables.hh:65
Range(Domain) Signature
Signature of function.
Definition: differentiablefunctionfromcallables.hh:53
typename DerivativeTraits< RawSignature >::Range(Domain) DerivativeSignature
Signature of derivative.
Definition: differentiablefunctionfromcallables.hh:58
Default exception for dummy implementations.
Definition: exceptions.hh:263
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
friend Derivative derivative(const DifferentiableFunctionFromCallables &t)
Get derivative of DifferentiableFunctionFromCallables.
Definition: differentiablefunctionfromcallables.hh:80
DifferentiableFunctionFromCallables< Signature, DerivativeTraits, F... > makeDifferentiableFunctionFromCallables(const SignatureTag< Signature, DerivativeTraits > &signatureTag, F &&... f)
Create a DifferentiableFunction from callables.
Definition: differentiablefunctionfromcallables.hh:168
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Helper class to deduce the signature of a callable.
Definition: signature.hh:60
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)