1 // -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_PYTHON_COMMON_DYNVECTOR_HH
5 #define DUNE_PYTHON_COMMON_DYNVECTOR_HH
6 
7 #include <string>
8 #include <tuple>
9 #include <utility>
10 
11 #include <dune/common/dynvector.hh>
12 
13 #include <dune/python/common/typeregistry.hh>
14 #include <dune/python/common/densevector.hh>
15 #include <dune/python/pybind11/pybind11.h>
16 #include <dune/python/pybind11/operators.h>
17 
18 namespace Dune
19 {
20 
21   namespace Python
22   {
23 
24     template< class K >
registerDynamicVector(pybind11::handle scope)25     void registerDynamicVector ( pybind11::handle scope )
26     {
27       using pybind11::operator""_a;
28 
29       typedef Dune::DynamicVector< K > DV;
30 
31       auto cls = insertClass< DV >( scope, "DynamicVector",
32           GenerateTypeName("Dune::DynamicVector",MetaType<K>()),
33           IncludeFiles{"dune/common/dynvector.hh"} ).first;
34 
35       cls.def( pybind11::init( [] () { return new DV(); } ) );
36 
37       cls.def( pybind11::init( [] ( pybind11::list x ) {
38             std::size_t size = x.size();
39             DV *self = new DV( size, K( 0 ) );
40             for( std::size_t i = 0; i < size; ++i )
41               (*self)[ i ] = x[ i ].template cast< K >();
42             return self;
43           } ), "x"_a );
44 
45       cls.def("__repr__",
46           [] (const DV &v) {
47             std::string repr = "Dune::DynamicVector: (";
48 
49             for (std::size_t i = 0; i < v.size(); ++i)
50               repr += (i > 0 ? ", " : "") + std::to_string(v[i]);
51 
52             repr += ")";
53 
54             return repr;
55           });
56 
57       registerDenseVector<DV>(cls);
58     }
59 
60   } // namespace Python
61 
62 } // namespace Dune
63 
64 #endif // #ifndef DUNE_PYTHON_COMMON_DYNVECTOR_HH
65