1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_PYTHON_COMMON_TYPE_TRAITS_HH
#define DUNE_PYTHON_COMMON_TYPE_TRAITS_HH
#include <type_traits>
#include <utility>
#include <dune/common/ftraits.hh>
#include <dune/common/rangeutilities.hh>
#include <dune/common/typetraits.hh>
#include <dune/common/std/type_traits.hh>
#include <dune/python/pybind11/numpy.h>
#include <dune/python/pybind11/pybind11.h>
namespace Dune
{
// External Forward Declarations
// -----------------------------
template< class V >
class DenseVector;
template< class K, int n >
class FieldVector;
template< class K, int m, int n >
class FieldMatrix;
namespace Imp
{
template< class B, class ST >
class block_vector_unmanaged;
template< class B, class A >
class compressed_block_vector_unmanaged;
} // namespace Imp
namespace Python
{
// IsDenseVector
// -------------
template< class T, class = void >
struct IsDenseVector
: public std::false_type
{};
template< class T >
struct IsDenseVector< T, std::enable_if_t< std::is_base_of< Dune::DenseVector< T >, T >::value > >
: public std::true_type
{};
// IsBlockVector
// -------------
template< class T, class = void >
struct IsBlockVector
: public std::false_type
{};
template< class T >
struct IsBlockVector< T, std::enable_if_t< std::is_base_of< Imp::block_vector_unmanaged< typename T::block_type, typename T::size_type >, T >::value > >
: public std::true_type
{};
template< class T >
struct IsBlockVector< T, std::enable_if_t< std::is_base_of< Imp::compressed_block_vector_unmanaged< typename T::block_type, typename T::allocator_type >, T >::value > >
: public std::true_type
{};
// IsOneTensor
// -----------
template< class T, class = void >
struct IsOneTensor
: public std::false_type
{};
template< class T >
struct IsOneTensor< T, std::enable_if_t< IsDenseVector< T >::value > >
: public std::true_type
{};
template< class T >
struct IsOneTensor< T, std::enable_if_t< IsBlockVector< T >::value && IsOneTensor< typename T::block_type >::value > >
: public std::true_type
{};
namespace detail
{
// registerOneTensorInterface
// --------------------------
template< class T, class... options >
inline static auto registerOneTensorInterface ( pybind11::class_< T, options... > cls, PriorityTag< 1 > )
-> std::enable_if_t< IsOneTensor< T >::value >
{
cls.def( "__mul__", [] ( const T &self, const T &other ) { return self * other; } );
cls.def( "__rmul__", [] ( const T &self, const T &other ) { return self * other; } );
cls.def_property_readonly( "one_norm", [] ( const T &self ) { return self.one_norm(); } );
cls.def_property_readonly( "one_norm_real", [] ( const T &self ) { return self.one_norm_real(); } );
cls.def_property_readonly( "two_norm", [] ( const T &self ) { return self.two_norm(); } );
cls.def_property_readonly( "two_norm2", [] ( const T &self ) { return self.two_norm2(); } );
cls.def_property_readonly( "infinity_norm", [] ( const T &self ) { return self.infinity_norm(); } );
cls.def_property_readonly( "infinity_norm_real", [] ( const T &self ) { return self.infinity_norm_real(); } );
}
template< class T, class... options >
inline static void registerOneTensorInterface ( pybind11::class_< T, options... > cls, PriorityTag< 0 > )
{}
template< class T, class... options >
inline static void registerOneTensorInterface ( pybind11::class_< T, options... > cls )
{
registerOneTensorInterface( cls, PriorityTag< 42 >() );
}
} // namespace detail
// FixedTensorTraits
// -----------------
template< class T, class = void >
struct FixedTensorTraits;
template< class T >
struct FixedTensorTraits< T, std::enable_if_t< IsNumber< T >::value > >
{
static constexpr std::array< ssize_t, 0 > shape () noexcept { return {{}}; }
};
template< class K, int n >
struct FixedTensorTraits< FieldVector< K, n >, void >
{
static constexpr std::array< ssize_t, 1 > shape () noexcept { return {{ n }}; }
};
template< class K, int m, int n >
struct FixedTensorTraits< FieldMatrix< K, m, n >, void >
{
static constexpr std::array< ssize_t, 2 > shape () noexcept { return {{ m, n }}; }
};
// extendArray
// -----------
template< std::size_t... i, class T, class... X >
inline static constexpr auto extendArray ( std::index_sequence< i... >, const std::array< T, sizeof...( i ) > &array, X &&... x )
-> std::enable_if_t< std::conjunction< std::is_convertible< X, T >... >::value, std::array< T, sizeof...( i )+sizeof...( X ) > >
{
return {{ array[ i ]..., std::forward< X >( x )... }};
}
template< class T, std::size_t n, class... X >
inline static constexpr std::array< T, n+sizeof...( X ) > extendArray ( const std::array< T, n > &array, X &&... x )
{
return extendArray( std::make_index_sequence< n >(), array, std::forward< X >( x )... );
}
// getFixedTensor
// --------------
template< std::size_t... k, class X, class Y, std::size_t n, class... I >
inline static auto getFixedTensor ( std::index_sequence< k... >, X &x, const Y &y, std::array< ssize_t, n > j, I... i )
-> std::enable_if_t< (sizeof...( k ) == n) >
{
x = y( j[ k ]..., i... );
}
template< std::size_t... k, class X, class Y, std::size_t n, class... I >
inline static auto getFixedTensor ( std::index_sequence< k... >, X &x, const Y &y, std::array< ssize_t, n > j, I... i )
-> std::enable_if_t< (sizeof...( k ) < n) >
{
ssize_t &it = j[ sizeof...( k ) ];
ssize_t end = it;
for( it = 0; it < end; ++it )
getFixedTensor( std::index_sequence< k..., sizeof...( k ) >(), x[ it ], y, j, i... );
}
template< class X, class Y, class... I >
inline static auto getFixedTensor ( X &x, const Y &y, I... i )
{
getFixedTensor( std::index_sequence<>(), x, y, FixedTensorTraits< X >::shape(), i... );
}
// setFixedTensor
// --------------
template< std::size_t... k, class X, class Y, std::size_t n, class... I >
inline static auto setFixedTensor ( std::index_sequence< k... >, const X &x, Y &y, std::array< ssize_t, n > j, I... i )
-> std::enable_if_t< (sizeof...( k ) == n) >
{
y( j[ k ]..., i... ) = x;
}
template< std::size_t... k, class X, class Y, std::size_t n, class... I >
inline static auto setFixedTensor ( std::index_sequence< k... >, const X &x, Y &y, std::array< ssize_t, n > j, I... i )
-> std::enable_if_t< (sizeof...( k ) < n) >
{
ssize_t &it = j[ sizeof...( k ) ];
ssize_t end = it;
for( it = 0; it < end; ++it )
setFixedTensor( std::index_sequence< k..., sizeof...( k ) >(), x[ it ], y, j, i... );
}
template< class X, class Y, class... I >
inline static auto setFixedTensor ( const X &x, Y &y, I... i )
{
setFixedTensor( std::index_sequence<>(), x, y, FixedTensorTraits< X >::shape(), i... );
}
// vectorize
// ---------
template< class F, class Y, class X >
inline static pybind11::object vectorize ( F &&f, Y (*)( X ), pybind11::array_t< typename FieldTraits< std::decay_t< X > >::field_type > xArray )
{
const auto xShape = FixedTensorTraits< std::decay_t< X > >::shape();
auto x = xArray.unchecked();
if( (std::size_t)x.ndim() < xShape.size() )
throw pybind11::value_error( "Tensor has too few dimensions" );
for( auto i : range( xShape.size() ) )
{
if( x.shape( i ) != xShape[ i ] )
throw pybind11::value_error( "Tensor has wrong shape" );
}
std::decay_t< X > xi;
if( (xShape.size() > 0) && (x.ndim() == xShape.size()) )
{
getFixedTensor( xi, x );
return pybind11::cast( f( xi ) );
}
else if( x.ndim() == xShape.size() + 1 )
{
const ssize_t size = x.shape( xShape.size() );
const auto yShape = extendArray( FixedTensorTraits< std::decay_t< Y > >::shape(), size );
pybind11::array_t< typename FieldTraits< std::decay_t< Y > >::field_type > yArray( yShape );
auto y = yArray.template mutable_unchecked< yShape.size() >();
for( auto i : range( size ) )
{
getFixedTensor( xi, x, i );
setFixedTensor( f( xi ), y, i );
}
return std::move(yArray);
}
else
throw pybind11::value_error( "Tensor has too many dimensions" );
}
template< class F, class X >
inline static auto vectorize ( F &&f, pybind11::array_t< X > xArray )
-> decltype( vectorize( std::forward< F >( f ), static_cast< pybind11::detail::function_signature_t< F > * >( nullptr ), std::move( xArray ) ) )
{
return vectorize( std::forward< F >( f ), static_cast< pybind11::detail::function_signature_t< F > * >( nullptr ), std::move( xArray ) );
}
} // namespace Python
} // namespace Dune
#endif // #ifndef DUNE_PYTHON_COMMON_TYPE_TRAITS_HH
|