Skip to content
Snippets Groups Projects
Commit 45e90f13 authored by Carsten Gräser's avatar Carsten Gräser
Browse files

[python] Add functions for construction of callable functions

parent 61295d8f
No related branches found
No related tags found
No related merge requests found
......@@ -14,9 +14,14 @@
#include <type_traits>
#include <dune/common/exceptions.hh>
#include <dune/common/std/apply.hh>
#include <dune/functions/common/signature.hh>
#include <dune/functions/common/differentiablefunctionfromcallables.hh>
#include <dune/fufem/python/reference.hh>
#include <dune/fufem/python/module.hh>
#include <dune/fufem/python/callable.hh>
namespace Python
......@@ -522,6 +527,41 @@ Reference keys(const Reference& dict)
return PyDict_Keys(dict);
}
/**
* \brief Create a callable function for given signature
*
* \tparam Signature Signature of function to create
*/
template<class Signature, template<class> class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits>
auto makeFunction(const Reference& f)
{
using Range = typename Dune::Functions::SignatureTraits<Signature>::Range;
using Domain = typename Dune::Functions::SignatureTraits<Signature>::Domain;
return [callable = Python::Callable(f)](const Domain& x) {
return callable(x).template toC<Range>();
};
}
/**
* \brief Create a callable differentiable function for given signature
*
* \tparam Signature Signature of function to create
* \tparam DerivativeTraits Traits template to use for construction of derivative signatures
* \tparam R Must derive from Python::Reference
* \param f Callable python objects for the function and its derivatives
*/
template<class Signature, template<class> class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits, class... R>
auto makeDifferentiableFunction(const R&... f)
{
auto signatureTag = Dune::Functions::SignatureTag<Signature, DerivativeTraits>();
auto derivativeSignatureTags = Dune::Functions::derivativeSignatureTags<sizeof...(f)-1>(signatureTag);
return Dune::Std::apply([&](auto... tag) {
return Dune::Functions::makeDifferentiableFunctionFromCallables(signatureTag,
makeFunction<typename decltype(tag)::Signature>(f)...);
}, derivativeSignatureTags);
}
/**
* \brief If a python error occured throw an exception and clear python error indicator
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment