Skip to content

Instantly share code, notes, and snippets.

View JohanMabille's full-sized avatar

Johan Mabille JohanMabille

View GitHub Profile
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor/xarray.hpp"
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"
#include "xtensor-python/pyvectorize.hpp"
namespace py = pybind11;
using complex_t = std::complex<double>;
void dump_numpy_constant()
{
std::cout << "NPY_BOOL = " << NPY_BOOL << std::endl;
std::cout << "NPY_BYTE = " << NPY_BYTE << std::endl;
std::cout << "NPY_UBYTE = " << NPY_UBYTE << std::endl;
std::cout << "NPY_INT8 = " << NPY_INT8 << std::endl;
std::cout << "NPY_UINT8 = " << NPY_UINT8 << std::endl;
std::cout << "NPY_SHORT = " << NPY_SHORT << std::endl;
std::cout << "NPY_USHORT = " << NPY_USHORT << std::endl;
std::cout << "NPY_INT16 = " << NPY_INT16 << std::endl;
template <class T, class A = std::allocator<T>>
class xarray
{
public:
using allocator_type = A;
using value_type = typename allocator_type::value_type;
using reference = typename allocator_type::reference;
using const_reference = typename allocator_type::const_reference;
using pointer = typename allocator_type::pointer;
template <class T, class A>
inline auto xarray<T, A>::size() const -> size_type
{
return std::accumulate(m_shape.cbegin(), m_shape.cend(), size_type(1), std::multiplies<size_type>());
}
template <class T, class A>
inline auto xarray<T, A>::dimension() const -> size_type
{
return m_shape.size();
template <class T, class A = std::allocator<T>>
class xarray
{
public:
// ... as before
private:
shape_type m_shape;
template <class T, class A = std::allocator<T>>
class xarray
{
public:
// ... as before
using strides_type = shape_type;
using storage_type = std::vector<T, A>;
void resize(const shape_type& shape, const strides_type& strides);
void reshape(const shape_type& shape, const strides_type& strides);
enum class layout_type
{
dynamic,
row_major,
column_major
};
template <class T, layout_type L = XTENSOR_DEFAULT_LAYOUT,
class A = std::allocator<T>>
class xarray
{
public:
// ... as before
void resize(const shape_type& shape, const strides_type& strides);
void resize(const shape_type& shape, layout_type l = L);
void reshape(const shape_type& shape, const strides_type& strides);
template <class shape_type, class strides_type>
std::size_t compute_strides(const shape_type& shape,
const strides_type& strides,
layout_type l)
{
std::size_t data_size = 1;
if(l == layout_type::row_major)
{
for(std::size_t i = shape.size(); i != 0; --i)
{
template <class shape_type>
std::size_t compute_size(const shape_type& shape)
{
using size_type = std::decay_t<typename shape_type::value_type>;
return std::accumulate(shape.cbegin(),
shape.cend(),
size_type(1),
std::multiplies<size_type>());
}