diff --git a/docs/index.rst b/docs/index.rst index 3a145d0..9b2acf2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -45,7 +45,7 @@ cppmat .. tip:: - This document should be considered as a quick-start guide. A lot effort has been spent on the readability of the code itself. One is highly encouraged to answer more advanced questions that arise from this guide directly using the code. + This document should be considered as a quick-start guide. A lot effort has been spent on the readability of the code itself (in particular the ``*.h`` files should be instructive). One is highly encouraged to answer more advanced questions that arise from this guide directly using the code. Download buttons to the relevant files are included throughout this reader. This header-only module provides C++ classes and several accompanying methods to work with n-d arrays and/or tensors. It's usage, programmatically and from a compilation perspective, is really simple. One just has to ``#include `` and tell your compiler where cppmat is located (and to use the C++14 or younger standard). Really, that's it! diff --git a/src/cppmat/fix_regular_vector.h b/src/cppmat/fix_regular_vector.h index cea7801..231c776 100644 --- a/src/cppmat/fix_regular_vector.h +++ b/src/cppmat/fix_regular_vector.h @@ -23,6 +23,9 @@ namespace tiny { template class vector : public cppmat::tiny::array { +private: + size_t mIstore=0; + public: // constructor: allocate, don't initialize @@ -48,6 +51,11 @@ class vector : public cppmat::tiny::array // forward difference (x0, x1-x0, x2-x1, ...) vector diff() const; + // STL-like access + void push_back(const X &value); + void clear(); + + }; // ================================================================================================= diff --git a/src/cppmat/fix_regular_vector.hpp b/src/cppmat/fix_regular_vector.hpp index e44ca6b..40edfa0 100644 --- a/src/cppmat/fix_regular_vector.hpp +++ b/src/cppmat/fix_regular_vector.hpp @@ -93,6 +93,32 @@ vector vector::diff() const return out; } +// ================================================================================================= +// STL-like behaviour +// ================================================================================================= + +template +inline +void vector::push_back(const X &value) +{ + assert( mIstore < N ); + + this->mData[mIstore] = value; + + mIstore++; +} + +// ------------------------------------------------------------------------------------------------- + +template +inline +void vector::clear() +{ + mIstore = 0; + + for ( size_t i = 0 ; i < N ; ++i ) this->mData[i] = static_cast(0); +} + // ================================================================================================= }} // namespace ... diff --git a/src/cppmat/pybind11_fix_cartesian_vector.hpp b/src/cppmat/pybind11_fix_cartesian_vector.hpp index 04f7a17..dbf959d 100644 --- a/src/cppmat/pybind11_fix_cartesian_vector.hpp +++ b/src/cppmat/pybind11_fix_cartesian_vector.hpp @@ -4,8 +4,8 @@ ================================================================================================= */ -#ifndef CPPMAT_TENSOR2_PYBIND11_HPP -#define CPPMAT_TENSOR2_PYBIND11_HPP +#ifndef CPPMAT_FIX_CARTESIAN_VECTOR_PYBIND11_HPP +#define CPPMAT_FIX_CARTESIAN_VECTOR_PYBIND11_HPP #include "pybind11.h" @@ -18,58 +18,8 @@ namespace detail { // type caster: cppmat::tiny::cartesian::vector <-> NumPy-array // ================================================================================================= -template struct type_caster> -{ -public: - - using Arr = cppmat::tiny::cartesian::vector; - - PYBIND11_TYPE_CASTER(Arr, _("cppmat::tiny::cartesian::vector")); - - // Python -> C++ - // ------------- - - bool load(py::handle src, bool convert) - { - // - basic pybind11 check - if ( !convert && !py::array_t::check_(src) ) return false; - - // - storage requirements : contiguous and row-major storage from NumPy - auto buf = py::array_t::ensure(src); - // - check - if ( !buf ) return false; - - // - rank of the input array (number of indices) - auto rank = buf.ndim(); - // - check - if ( rank != 1 ) return false; - - // - check shape in each direction - for ( ssize_t i = 0 ; i < rank ; ++i ) - if ( static_cast(buf.shape()[i]) != ND ) - return false; - - // - all checks passed : create the proper C++ variable - value = cppmat::tiny::cartesian::vector::Copy(buf.data(), buf.data()+buf.size()); - - // - signal successful variable creation - return true; - } - - // C++ -> Python - // ------------- - - static py::handle cast( - const cppmat::tiny::cartesian::vector& src, py::return_value_policy policy, py::handle parent - ) - { - // - create Python variable (all variables are copied) - py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data()); - - // - release variable to Python - return a.release(); - } -}; +template struct type_caster> : + list_caster, X> { }; // ================================================================================================= diff --git a/src/cppmat/pybind11_fix_regular_vector.hpp b/src/cppmat/pybind11_fix_regular_vector.hpp index e2a5f43..40c51b8 100644 --- a/src/cppmat/pybind11_fix_regular_vector.hpp +++ b/src/cppmat/pybind11_fix_regular_vector.hpp @@ -18,56 +18,8 @@ namespace detail { // type caster: cppmat::tiny::vector <-> NumPy-array // ================================================================================================= -template struct type_caster> -{ -public: - - using Arr = cppmat::tiny::vector; - - PYBIND11_TYPE_CASTER(Arr, _("cppmat::tiny::vector")); - - // Python -> C++ - // ------------- - - bool load(py::handle src, bool convert) - { - // - basic pybind11 check - if ( !convert && !py::array_t::check_(src) ) return false; - - // - storage requirements : contiguous and row-major storage from NumPy - auto buf = py::array_t::ensure(src); - // - check - if ( !buf ) return false; - - // - rank of the input array (number of indices) - auto rank = buf.ndim(); - // - check - if ( rank != 1 ) return false; - - // - check shape in each direction - if ( static_cast(buf.shape()[0]) != N ) return false; - - // - all checks passed : create the proper C++ variable - value = cppmat::tiny::vector::Copy(buf.data(), buf.data()+buf.size()); - - // - signal successful variable creation - return true; - } - - // C++ -> Python - // ------------- - - static py::handle cast( - const cppmat::tiny::vector& src, py::return_value_policy policy, py::handle parent - ) - { - // - create Python variable (all variables are copied) - py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data()); - - // - release variable to Python - return a.release(); - } -}; +template struct type_caster> : + list_caster, X> { }; // ================================================================================================= diff --git a/src/cppmat/pybind11_var_cartesian_vector.hpp b/src/cppmat/pybind11_var_cartesian_vector.hpp index 756977c..57a5e86 100644 --- a/src/cppmat/pybind11_var_cartesian_vector.hpp +++ b/src/cppmat/pybind11_var_cartesian_vector.hpp @@ -18,58 +18,8 @@ namespace detail { // type caster: cppmat::cartesian::vector <-> NumPy-array // ================================================================================================= -template struct type_caster> -{ -public: - - PYBIND11_TYPE_CASTER(cppmat::cartesian::vector, _("cppmat::cartesian::vector")); - - // Python -> C++ - // ------------- - - bool load(py::handle src, bool convert) - { - // - basic pybind11 check - if ( !convert && !py::array_t::check_(src) ) return false; - - // - storage requirements : contiguous and row-major storage from NumPy - auto buf = py::array_t::ensure(src); - // - check - if ( !buf ) return false; - - // - rank of the input array (number of indices) - auto rank = buf.ndim(); - // - check - if ( rank != 1 ) return false; - - // - read number of dimensions (shape in each direction) - size_t nd = static_cast(buf.shape()[0]); - // - check - for ( ssize_t i = 0 ; i < rank ; ++i ) - if ( static_cast(buf.shape()[i]) != nd ) - return false; - - // - all checks passed : create the proper C++ variable - value = cppmat::cartesian::vector::Copy(nd, buf.data(), buf.data()+buf.size()); - - // - signal successful variable creation - return true; - } - - // C++ -> Python - // ------------- - - static py::handle cast( - const cppmat::cartesian::vector& src, py::return_value_policy policy, py::handle parent - ) - { - // - create Python variable (all variables are copied) - py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data()); - - // - release variable to Python - return a.release(); - } -}; +template struct type_caster> : + list_caster, X> { }; // ================================================================================================= diff --git a/src/cppmat/pybind11_var_regular_array.hpp b/src/cppmat/pybind11_var_regular_array.hpp index 09db991..022191e 100644 --- a/src/cppmat/pybind11_var_regular_array.hpp +++ b/src/cppmat/pybind11_var_regular_array.hpp @@ -4,8 +4,8 @@ ================================================================================================= */ -#ifndef CPPMAT_VAR_ARRAY_PYBIND11_HPP -#define CPPMAT_VAR_ARRAY_PYBIND11_HPP +#ifndef CPPMAT_VAR_REGULAR_ARRAY_PYBIND11_HPP +#define CPPMAT_VAR_REGULAR_ARRAY_PYBIND11_HPP #include "pybind11.h" diff --git a/src/cppmat/pybind11_var_regular_vector.hpp b/src/cppmat/pybind11_var_regular_vector.hpp index acf35e7..684d2d1 100644 --- a/src/cppmat/pybind11_var_regular_vector.hpp +++ b/src/cppmat/pybind11_var_regular_vector.hpp @@ -18,51 +18,8 @@ namespace detail { // type caster: cppmat::vector <-> NumPy-array // ================================================================================================= -template struct type_caster> -{ -public: - - PYBIND11_TYPE_CASTER(cppmat::vector, _("cppmat::vector")); - - // Python -> C++ - // ------------- - - bool load(py::handle src, bool convert) - { - // - basic pybind11 check - if ( !convert && !py::array_t::check_(src) ) return false; - - // - storage requirements : contiguous and row-major storage from NumPy - auto buf = py::array_t::ensure(src); - // - check - if ( !buf ) return false; - - // - rank of the input array (number of indices) - auto rank = buf.ndim(); - // - check - if ( rank != 1 ) return false; - - // - all checks passed : create the proper C++ variable - value = cppmat::vector::Copy(buf.data(), buf.data()+buf.size()); - - // - signal successful variable creation - return true; - } - - // C++ -> Python - // ------------- - - static py::handle cast( - const cppmat::vector& src, py::return_value_policy policy, py::handle parent - ) - { - // - create Python variable (all variables are copied) - py::array a(std::move(src.shape()), std::move(src.strides(true)), src.data()); - - // - release variable to Python - return a.release(); - } -}; +template struct type_caster> : + list_caster, X> { }; // ================================================================================================= diff --git a/src/cppmat/var_regular_vector.h b/src/cppmat/var_regular_vector.h index 67c4d94..ecb5163 100644 --- a/src/cppmat/var_regular_vector.h +++ b/src/cppmat/var_regular_vector.h @@ -71,9 +71,10 @@ class vector : public cppmat::array void resize(size_t n); void resize(size_t n, const X &D); - // inset item - void push_back(const X &D); + // STL-like access + void push_back(const X &value); void append(const cppmat::array &A); + void clear(); // discrete difference (x1-x0, x2-x1, ...) vector diff() const; diff --git a/src/cppmat/var_regular_vector.hpp b/src/cppmat/var_regular_vector.hpp index 2a129b6..ef99370 100644 --- a/src/cppmat/var_regular_vector.hpp +++ b/src/cppmat/var_regular_vector.hpp @@ -244,9 +244,9 @@ void vector::resize(size_t n, const X &D) template inline -void vector::push_back(const X &D) +void vector::push_back(const X &value) { - this->mData.push_back(D); + this->mData.push_back(value); this->mShape[0]++; this->mSize++; } @@ -265,6 +265,15 @@ void vector::append(const cppmat::array &A) this->mSize += A.size(); } +// ------------------------------------------------------------------------------------------------- + +template +inline +void vector::clear() +{ + this->mData.clear(); +} + // ================================================================================================= // discrete difference (x1-x0, x2-x1, ...) // =================================================================================================