Proudly made in Namek by serge-sans-paille
/me
$ whoami
sguelton
<3
C++
def incr(x):
return x + 1
template<class X>
auto incr(X&& x) -> decltype(x + 1)
{
return x + 1;
}
import numpy as np
def l2norm(x):
return np.sqrt(np.sum(np.abs(x)**2, axis=1))
#include <numpy/abs.hpp>
#include <numpy/sqr.hpp>
#include <numpy/sqrt.hpp>
#include <numpy/sum.hpp>
namespace np = numpy;
template<class X>
auto l2norm(X&& x)
-> decltype(np::sqrt(np::sum(np::sqr(np::abs(std::forward<X>(x))), 1)))
{
return np::sqrt(np::sum(np::sqr(np::abs(std::forward<X>(x))), 1));
}
import numpy as np
def create_grid(x):
N = x.shape[0]
z = np.zeros((N, N, 3))
z[:,:,0], z[:,:,1] = x.reshape(-1,1), x
return z.reshape(N*N, 3)
#include <numpy/zeros.hpp>
namespace np = numpy;
template<class X>
auto create_grid(X&& x)
-> decltype(np::zeros(std::forward<X>(x).shape[0], std::forward<X>(x).shape[0], 3L))
{
auto const N = std::forward<X>(x).shape[0];
auto z = np::zeros(std::array<long, 3>{{N, N, 3}});
std::tie(z(np::slice(), np::slice(), 0), z(np::slice(), np::slice(), 1)) =
std::make_tuple(std::forward<X>(x).reshape(-1, 1), std::forward<X>(x));
return z.reshape(N*N, 3);
}
auto
and decltype
)A numeric Python to C++ translator
2 core developers, ~50 download per day on PyPI, scarce but valuable user reports!
.py
into an AST (import ast
...)nt2
+ pythonic
headers.so
)Provides a framework for easy encapsulation of C(++) functions and classes, and conversion between Python and C++ objects
/!\
Conversions are based on a linear search for a good candidate
BOOST_PYTHON_MODULE(foo)
{
import_array()
boost::python::register_exception_translator<pythonic::types::TypeError>(
&pythonic::translate_TypeError);
pythonic::python_to_pythran<pythonic::types::ndarray<double,1>>();
pythonic::python_to_pythran<pythonic::types::ndarray<double,2>>();
pythonic::pythran_to_python<typename foo::foo:type<pythonic::types::ndarray<double,2>>::result_type>();
boost::python::def("foo", &foo::foo);
foo::__init__()();
}
We want to be able to represent
a = map; a(foo, [1]); a(bar,[1], [2])
struct map {
template<class F, class... Args>
auto operator()(F&& f, Args&&... args) const
-> python::list<decltype(std::forward<F>(f)(std::forward<Args>(args)...))>;
};
Needs a specialization for F = None
...
Provides transparent manipulation of abstract vector registers, for all mathematical functions
ndarray
classCombines expression templates with Boost.SIMD for aggregated vector operations
Pythran compiler turns
a = b + c; d = a * 3
into d = (b + c) * 3
but does not forward-substitute
a = b + c; c[0] = 1; d = a * 3
nor a = b + c; d = a * 3; e = a ** 2
template<class Arg, class... S>
struct numpy_gexpr;
S
can be a scalar, a contiguous_slice
or a slice
numpy_gexpr
?static constexpr size_t value = std::remove_reference<Arg>::type::value
- count_long<S...>::value;
where
template<class T, class... Types>
struct count_long<T, Types...> {
static constexpr size_t value = count_long<T>::value + count_long<Types...>::value;
};
template<>
struct count_long<> {
static constexpr size_t value = 0;
};
std::copy
is specialized to use ::memove
when working on flat pointers
Use a type trait to encode the stride information, based on the slice
type
static const bool is_strided =
std::remove_reference<Arg>::type::is_strided or
(((sizeof...(S) - count_long<S...>::value) == value)
and not std::is_same<contiguous_slice,
typename std::tuple_element<sizeof...(S) - 1, std::tuple<S...>>::type>::value);
shared_ptr
shared_ptr
shared_ptr
and make_shared
Optionaluse of an
atomic
counter// in ndarray class
auto fast(long i) const & // no copy there
-> decltype(type_helper<ndarray const &>::get(*this, i))
{
return type_helper<ndarray const &>::get(*this, i);
}
auto fast(long i) && // implies a copy
-> decltype(type_helper<ndarray>::get(std::move(*this), i))
{
return type_helper<ndarray>::get(std::move(*this), i);
}
Learning Python makes you a better C++ dev! It favors:
doctest
ing, unittest
ing...and learning C++ makes you a better Python dev! It favors: