| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent acfe4b7 commit 9de8583
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,19 @@ | |||
| 7 | 7 | API reference | |
| 8 | 8 | ============= | |
| 9 | 9 | ||
| 10 | + Containers | ||
| 11 | + ---------- | ||
| 12 | + | ||
| 10 | 13 | .. toctree:: | |
| 11 | 14 | :maxdepth: 2 | |
| 12 | 15 | ||
| 13 | 16 | pyarray | |
| 14 | 17 | pytensor | |
| 18 | + | ||
| 19 | + Numpy universal functions | ||
| 20 | + ------------------------- | ||
| 21 | + | ||
| 22 | + .. toctree:: | ||
| 23 | + :maxdepth: 2 | ||
| 24 | + | ||
| 25 | + pyvectorize | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,7 +64,6 @@ namespace xt | |||
| 64 | 64 | template <class A> | |
| 65 | 65 | class pyarray_backstrides | |
| 66 | 66 | { | |
| 67 | - | ||
| 68 | 67 | public: | |
| 69 | 68 | ||
| 70 | 69 | using array_type = A; | |
@@ -104,7 +103,13 @@ namespace xt | |||
| 104 | 103 | ||
| 105 | 104 | /** | |
| 106 | 105 | * @class pyarray | |
| 107 | - * @brief Wrapper on the Python buffer protocol. | ||
| 106 | + * @brief Multidimensional container providing the xtensor container semantics to a numpy array. | ||
| 107 | + * | ||
| 108 | + * pyarray is similar to the xarray container in that it has a dynamic dimensionality. Reshapes of | ||
| 109 | + * a pyarray container are reflected in the underlying numpy array. | ||
| 110 | + * | ||
| 111 | + * @tparam T The type of the element stored in the pyarray. | ||
| 112 | + * @sa pytensor | ||
| 108 | 113 | */ | |
| 109 | 114 | template <class T> | |
| 110 | 115 | class pyarray : public pycontainer<pyarray<T>>, | |
@@ -210,6 +215,10 @@ namespace xt | |||
| 210 | 215 | * pyarray implementation * | |
| 211 | 216 | **************************/ | |
| 212 | 217 | ||
| 218 | + /** | ||
| 219 | + * @name Constructors | ||
| 220 | + */ | ||
| 221 | + //@{ | ||
| 213 | 222 | template <class T> | |
| 214 | 223 | inline pyarray<T>::pyarray() | |
| 215 | 224 | { | |
@@ -220,6 +229,9 @@ namespace xt | |||
| 220 | 229 | m_data[0] = T(); | |
| 221 | 230 | } | |
| 222 | 231 | ||
| 232 | + /** | ||
| 233 | + * Allocates a pyarray with nested initializer lists. | ||
| 234 | + */ | ||
| 223 | 235 | template <class T> | |
| 224 | 236 | inline pyarray<T>::pyarray(const value_type& t) | |
| 225 | 237 | { | |
@@ -283,6 +295,12 @@ namespace xt | |||
| 283 | 295 | init_from_python(); | |
| 284 | 296 | } | |
| 285 | 297 | ||
| 298 | + /** | ||
| 299 | + * Allocates an uninitialized pyarray with the specified shape and | ||
| 300 | + * layout. | ||
| 301 | + * @param shape the shape of the pyarray | ||
| 302 | + * @param l the layout of the pyarray | ||
| 303 | + */ | ||
| 286 | 304 | template <class T> | |
| 287 | 305 | inline pyarray<T>::pyarray(const shape_type& shape, layout l) | |
| 288 | 306 | { | |
@@ -291,6 +309,13 @@ namespace xt | |||
| 291 | 309 | init_array(shape, strides); | |
| 292 | 310 | } | |
| 293 | 311 | ||
| 312 | + /** | ||
| 313 | + * Allocates a pyarray with the specified shape and layout. Elements | ||
| 314 | + * are initialized to the specified value. | ||
| 315 | + * @param shape the shape of the pyarray | ||
| 316 | + * @param value the value of the elements | ||
| 317 | + * @param l the layout of the pyarray | ||
| 318 | + */ | ||
| 294 | 319 | template <class T> | |
| 295 | 320 | inline pyarray<T>::pyarray(const shape_type& shape, const_reference value, layout l) | |
| 296 | 321 | { | |
@@ -300,32 +325,56 @@ namespace xt | |||
| 300 | 325 | std::fill(m_data.begin(), m_data.end(), value); | |
| 301 | 326 | } | |
| 302 | 327 | ||
| 328 | + /** | ||
| 329 | + * Allocates an uninitialized pyarray with the specified shape and strides. | ||
| 330 | + * Elements are initialized to the specified value. | ||
| 331 | + * @param shape the shape of the pyarray | ||
| 332 | + * @param strides the strides of the pyarray | ||
| 333 | + * @param value the value of the elements | ||
| 334 | + */ | ||
| 303 | 335 | template <class T> | |
| 304 | 336 | inline pyarray<T>::pyarray(const shape_type& shape, const strides_type& strides, const_reference value) | |
| 305 | 337 | { | |
| 306 | 338 | init_array(shape, strides); | |
| 307 | 339 | std::fill(m_data.begin(), m_data.end(), value); | |
| 308 | 340 | } | |
| 309 | 341 | ||
| 342 | + /** | ||
| 343 | + * Allocates an uninitialized pyarray with the specified shape and strides. | ||
| 344 | + * @param shape the shape of the pyarray | ||
| 345 | + * @param strides the strides of the pyarray | ||
| 346 | + */ | ||
| 310 | 347 | template <class T> | |
| 311 | 348 | inline pyarray<T>::pyarray(const shape_type& shape, const strides_type& strides) | |
| 312 | 349 | { | |
| 313 | 350 | init_array(shape, strides); | |
| 314 | 351 | } | |
| 352 | + //@} | ||
| 315 | 353 | ||
| 354 | + /** | ||
| 355 | + * @name Extended copy semantic | ||
| 356 | + */ | ||
| 357 | + //@{ | ||
| 358 | + /** | ||
| 359 | + * The extended copy constructor. | ||
| 360 | + */ | ||
| 316 | 361 | template <class T> | |
| 317 | 362 | template <class E> | |
| 318 | 363 | inline pyarray<T>::pyarray(const xexpression<E>& e) | |
| 319 | 364 | { | |
| 320 | 365 | semantic_base::assign(e); | |
| 321 | 366 | } | |
| 322 | 367 | ||
| 368 | + /** | ||
| 369 | + * The extended assignment operator. | ||
| 370 | + */ | ||
| 323 | 371 | template <class T> | |
| 324 | 372 | template <class E> | |
| 325 | 373 | inline auto pyarray<T>::operator=(const xexpression<E>& e) -> self_type& | |
| 326 | 374 | { | |
| 327 | 375 | return semantic_base::operator=(e); | |
| 328 | 376 | } | |
| 377 | + //@} | ||
| 329 | 378 | ||
| 330 | 379 | template <class T> | |
| 331 | 380 | inline auto pyarray<T>::ensure(pybind11::handle h) -> self_type | |
@@ -397,8 +446,9 @@ namespace xt | |||
| 397 | 446 | template <class T> | |
| 398 | 447 | inline auto pyarray<T>::backstrides_impl() const noexcept -> const inner_backstrides_type& | |
| 399 | 448 | { | |
| 400 | - // The pyarray object may be copied, invalidating m_backstrides. Building | ||
| 401 | - // it each time it is needed avoids tricky bugs. | ||
| 449 | + // m_backstrides wraps the numpy array backstrides, which is a raw pointer. | ||
| 450 | + // The address of the raw pointer stored in the wrapper would be invalidated when the pyarray is copied. | ||
| 451 | + // Hence, we build a new backstrides object (cheap wrapper around the underlying pointer) upon access. | ||
| 402 | 452 | m_backstrides = backstrides_type(*this); | |
| 403 | 453 | return m_backstrides; | |
| 404 | 454 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,16 @@ | |||
| 31 | 31 | namespace xt | |
| 32 | 32 | { | |
| 33 | 33 | ||
| 34 | + /** | ||
| 35 | + * @class pycontainer | ||
| 36 | + * @brief Base class for xtensor containers wrapping numpy arryays. | ||
| 37 | + * | ||
| 38 | + * The pycontainer class should not be instantiated directly. Instead, used should | ||
| 39 | + * use pytensor and pyarray instancs. | ||
| 40 | + * | ||
| 41 | + * @tparam D The derived type, i.e. the inheriting class for which pycontainer | ||
| 42 | + * provides the interface. | ||
| 43 | + */ | ||
| 34 | 44 | template <class D> | |
| 35 | 45 | class pycontainer : public pybind11::object, | |
| 36 | 46 | public xcontainer<D> | |
@@ -191,6 +201,10 @@ namespace xt | |||
| 191 | 201 | return reinterpret_cast<PyArrayObject*>(this->m_ptr); | |
| 192 | 202 | } | |
| 193 | 203 | ||
| 204 | + /** | ||
| 205 | + * Reshapes the container. | ||
| 206 | + * @param shape the new shape | ||
| 207 | + */ | ||
| 194 | 208 | template <class D> | |
| 195 | 209 | inline void pycontainer<D>::reshape(const shape_type& shape) | |
| 196 | 210 | { | |
@@ -200,6 +214,11 @@ namespace xt | |||
| 200 | 214 | } | |
| 201 | 215 | } | |
| 202 | 216 | ||
| 217 | + /** | ||
| 218 | + * Reshapes the container. | ||
| 219 | + * @param shape the new shape | ||
| 220 | + * @param l the new layout | ||
| 221 | + */ | ||
| 203 | 222 | template <class D> | |
| 204 | 223 | inline void pycontainer<D>::reshape(const shape_type& shape, layout l) | |
| 205 | 224 | { | |
@@ -208,6 +227,11 @@ namespace xt | |||
| 208 | 227 | reshape(shape, strides); | |
| 209 | 228 | } | |
| 210 | 229 | ||
| 230 | + /** | ||
| 231 | + * Reshapes the container. | ||
| 232 | + * @param shape the new shape | ||
| 233 | + * @param strides the new strides | ||
| 234 | + */ | ||
| 211 | 235 | template <class D> | |
| 212 | 236 | inline void pycontainer<D>::reshape(const shape_type& shape, const strides_type& strides) | |
| 213 | 237 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,6 +81,20 @@ namespace xt | |||
| 81 | 81 | using temporary_type = pytensor<T, N>; | |
| 82 | 82 | }; | |
| 83 | 83 | ||
| 84 | + /** | ||
| 85 | + * @class pytensor | ||
| 86 | + * @brief Multidimensional container providing the xtensor container semantics wrapping a numpy array. | ||
| 87 | + * | ||
| 88 | + * pytensor is similar to the xtensor container in that it has a static dimensionality. | ||
| 89 | + * | ||
| 90 | + * Unlike with the pyarray container, pytensor cannot be reshaped with a different number of dimensions | ||
| 91 | + * and reshapes are not reflected on the Python side. However, pytensor has benefits compared to pyarray | ||
| 92 | + * in terms of performances. pytensor shapes are stack-allocated which makes iteration upon pytensor | ||
| 93 | + * faster than with pyarray. | ||
| 94 | + * | ||
| 95 | + * @tparam T The type of the element stored in the pyarray. | ||
| 96 | + * @sa pyarray | ||
| 97 | + */ | ||
| 84 | 98 | template <class T, std::size_t N> | |
| 85 | 99 | class pytensor : public pycontainer<pytensor<T, N>>, | |
| 86 | 100 | public xcontainer_semantic<pytensor<T, N>> | |
@@ -158,6 +172,13 @@ namespace xt | |||
| 158 | 172 | * pytensor implementation * | |
| 159 | 173 | ***************************/ | |
| 160 | 174 | ||
| 175 | + /** | ||
| 176 | + * @name Constructors | ||
| 177 | + */ | ||
| 178 | + //@{ | ||
| 179 | + /** | ||
| 180 | + * Allocates an uninitialized pytensor that holds 1 element. | ||
| 181 | + */ | ||
| 161 | 182 | template <class T, std::size_t N> | |
| 162 | 183 | inline pytensor<T, N>::pytensor() | |
| 163 | 184 | { | |
@@ -167,6 +188,9 @@ namespace xt | |||
| 167 | 188 | m_data[0] = T(); | |
| 168 | 189 | } | |
| 169 | 190 | ||
| 191 | + /** | ||
| 192 | + * Allocates a pytensor with a nested initializer list. | ||
| 193 | + */ | ||
| 170 | 194 | template <class T, std::size_t N> | |
| 171 | 195 | inline pytensor<T, N>::pytensor(nested_initializer_list_t<T, N> t) | |
| 172 | 196 | { | |
@@ -195,13 +219,26 @@ namespace xt | |||
| 195 | 219 | init_from_python(); | |
| 196 | 220 | } | |
| 197 | 221 | ||
| 222 | + /** | ||
| 223 | + * Allocates an uninitialized pytensor with the specified shape and | ||
| 224 | + * layout. | ||
| 225 | + * @param shape the shape of the pytensor | ||
| 226 | + * @param l the layout of the pytensor | ||
| 227 | + */ | ||
| 198 | 228 | template <class T, std::size_t N> | |
| 199 | 229 | inline pytensor<T, N>::pytensor(const shape_type& shape, layout l) | |
| 200 | 230 | { | |
| 201 | 231 | compute_strides(shape, l, m_strides); | |
| 202 | 232 | init_tensor(shape, m_strides); | |
| 203 | 233 | } | |
| 204 | 234 | ||
| 235 | + /** | ||
| 236 | + * Allocates a pytensor with the specified shape and layout. Elements | ||
| 237 | + * are initialized to the specified value. | ||
| 238 | + * @param shape the shape of the pytensor | ||
| 239 | + * @param value the value of the elements | ||
| 240 | + * @param l the layout of the pytensor | ||
| 241 | + */ | ||
| 205 | 242 | template <class T, std::size_t N> | |
| 206 | 243 | inline pytensor<T, N>::pytensor(const shape_type& shape, | |
| 207 | 244 | const_reference value, | |
@@ -212,6 +249,13 @@ namespace xt | |||
| 212 | 249 | std::fill(m_data.begin(), m_data.end(), value); | |
| 213 | 250 | } | |
| 214 | 251 | ||
| 252 | + /** | ||
| 253 | + * Allocates an uninitialized pytensor with the specified shape and strides. | ||
| 254 | + * Elements are initialized to the specified value. | ||
| 255 | + * @param shape the shape of the pytensor | ||
| 256 | + * @param strides the strides of the pytensor | ||
| 257 | + * @param value the value of the elements | ||
| 258 | + */ | ||
| 215 | 259 | template <class T, std::size_t N> | |
| 216 | 260 | inline pytensor<T, N>::pytensor(const shape_type& shape, | |
| 217 | 261 | const strides_type& strides, | |
@@ -221,26 +265,43 @@ namespace xt | |||
| 221 | 265 | std::fill(m_data.begin(), m_data.end(), value); | |
| 222 | 266 | } | |
| 223 | 267 | ||
| 268 | + /** | ||
| 269 | + * Allocates an uninitialized pytensor with the specified shape and strides. | ||
| 270 | + * @param shape the shape of the pytensor | ||
| 271 | + * @param strides the strides of the pytensor | ||
| 272 | + */ | ||
| 224 | 273 | template <class T, std::size_t N> | |
| 225 | 274 | inline pytensor<T, N>::pytensor(const shape_type& shape, | |
| 226 | 275 | const strides_type& strides) | |
| 227 | 276 | { | |
| 228 | 277 | init_tensor(shape, strides); | |
| 229 | 278 | } | |
| 230 | - | ||
| 279 | + //@} | ||
| 280 | + | ||
| 281 | + /** | ||
| 282 | + * @name Extended copy semantic | ||
| 283 | + */ | ||
| 284 | + //@{ | ||
| 285 | + /** | ||
| 286 | + * The extended copy constructor. | ||
| 287 | + */ | ||
| 231 | 288 | template <class T, std::size_t N> | |
| 232 | 289 | template <class E> | |
| 233 | 290 | inline pytensor<T, N>::pytensor(const xexpression<E>& e) | |
| 234 | 291 | { | |
| 235 | 292 | semantic_base::assign(e); | |
| 236 | 293 | } | |
| 237 | 294 | ||
| 295 | + /** | ||
| 296 | + * The extended assignment operator. | ||
| 297 | + */ | ||
| 238 | 298 | template <class T, std::size_t N> | |
| 239 | 299 | template <class E> | |
| 240 | 300 | inline auto pytensor<T, N>::operator=(const xexpression<E>& e) -> self_type& | |
| 241 | 301 | { | |
| 242 | 302 | return semantic_base::operator=(e); | |
| 243 | 303 | } | |
| 304 | + //@} | ||
| 244 | 305 | ||
| 245 | 306 | template <class T, std::size_t N> | |
| 246 | 307 | inline auto pytensor<T, N>::ensure(pybind11::handle h) -> self_type | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,12 +33,16 @@ namespace xt | |||
| 33 | 33 | } | |
| 34 | 34 | }; | |
| 35 | 35 | ||
| 36 | + /** | ||
| 37 | + * @brief Create numpy universal function from scalar function. | ||
| 38 | + */ | ||
| 36 | 39 | template <class R, class... Args> | |
| 37 | 40 | inline pyvectorizer<R(*)(Args...), R, Args...> pyvectorize(R(*f) (Args...)) | |
| 38 | 41 | { | |
| 39 | 42 | return pyvectorizer<R(*) (Args...), R, Args...>(f); | |
| 40 | 43 | } | |
| 41 | 44 | ||
| 45 | + /// @cond DOXYGEN_INCLUDE_OVERLOADS | ||
| 42 | 46 | template <class F, class R, class... Args> | |
| 43 | 47 | inline pyvectorizer<F, R, Args...> pyvectorize(F&& f, R(*) (Args...)) | |
| 44 | 48 | { | |
@@ -50,6 +54,7 @@ namespace xt | |||
| 50 | 54 | { | |
| 51 | 55 | return pyvectorize(std::forward<F>(f), (detail::get_function_type<F>*)nullptr); | |
| 52 | 56 | } | |
| 57 | + /// @endcond | ||
| 53 | 58 | } | |
| 54 | 59 | ||
| 55 | 60 | #endif | |
| Back | FazBrowse Home | New Git URL |
0 commit comments