DUNE-FUNCTIONS (unstable)

differentiablefunction_imp.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_DIFFERENTIABLE_FUNCTION_IMP_HH
8#define DUNE_FUNCTIONS_COMMON_DIFFERENTIABLE_FUNCTION_IMP_HH
9
10#include <dune/common/exceptions.hh>
11#include <dune/common/concept.hh>
12
13#include <dune/functions/common/type_traits.hh>
14
15
16namespace Dune {
17namespace Functions {
18namespace Imp {
19
23struct HasFreeDerivative
24{
25 template<class F>
26 auto require(F&& f) -> decltype(
27 derivative(f)
28 );
29};
30
31
32
33template<class Dummy, class F,
34 std::enable_if_t<
35 models< HasFreeDerivative, F>() , int> = 0>
36auto derivativeIfImplemented(const F& f) -> decltype(derivative(f))
37{
38 return derivative(f);
39}
40
41
42
43template<class Dummy, class F,
44 std::enable_if_t<
45 not(models< HasFreeDerivative, F>()) , int> = 0>
46Dummy derivativeIfImplemented(const F& f)
47{
48 DUNE_THROW(Dune::NotImplemented, "Derivative not implemented");
49}
50
51
52
53template<class Signature, class DerivativeInterface>
54class DifferentiableFunctionWrapperInterface
55{};
56
57// Interface of type erasure wrapper
58//
59// Notice that the basic interface of polymorphic classes (destructor, clone, ...)
60// will be added by the type erasure foundation classes.
61template<class Range, class Domain, class DerivativeInterface>
62class DifferentiableFunctionWrapperInterface<Range(Domain), DerivativeInterface>
63{
64public:
65 virtual Range operator() (const Domain& x) const = 0;
66
67 virtual DerivativeInterface derivative() const = 0;
68};
69
70
71
72template<class Signature, class DerivativeInterface, class B>
73class DifferentiableFunctionWrapperImplementation
74{};
75
76// Implementation of type erasure wrapper
77template<class Range, class Domain, class DerivativeInterface, class B>
78class DifferentiableFunctionWrapperImplementation< Range(Domain), DerivativeInterface, B> :
79 public B
80{
81public:
82
83 using B::B;
84 using Wrapped = typename B::Wrapped;
85
86 virtual Range operator() (const Domain& x) const
87 {
88 return this->get()(x);
89 }
90
91 virtual DerivativeInterface derivative() const
92 {
93 return derivativeIfImplemented<DerivativeInterface, Wrapped>(this->get());
94 }
95};
96
97
98
99}}} // namespace Dune::Functions::Imp
100
101
102
103#endif // DUNE_FUNCTIONS_COMMON_DIFFERENTIABLE_FUNCTION_IMP_HH
TrigonometricFunction< K, -cosFactor, sinFactor > derivative(const TrigonometricFunction< K, sinFactor, cosFactor > &f)
Obtain derivative of TrigonometricFunction function.
Definition: trigonometricfunction.hh:43
Definition: polynomial.hh:17
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Aug 14, 22:29, 2024)