Skip to content

Class star::NDArray

template <typename T>

ClassList > star > NDArray

Modern n-dimensional array class with xtensor-style API. More...

  • #include <stards.h>

Public Types

Type Name
typedef typename std::vector< T >::const_iterator const_iterator
typedef typename std::vector< T >::const_reverse_iterator const_reverse_iterator
typedef typename std::vector< T >::iterator iterator
typedef typename std::vector< T >::reverse_iterator reverse_iterator
typedef T value_type

Public Functions

Type Name
NDArray () = default
Default constructor.
NDArray (const std::vector< size_t > & shape_in)
Constructor with shape.
NDArray (const std::vector< size_t > & shape_in, const T & initial_value)
Constructor with shape and initial value.
NDArray (const std::vector< T > & data_in, const std::vector< size_t > & shape_in)
Constructor with data and shape.
NDArray (std::vector< T > && data_in, const std::vector< size_t > & shape_in)
Move constructor with data and shape (zero-copy for Python bindings).
NDArray (const NDArray & other)
Copy constructor.
T & at (const std::vector< size_t > & indices)
Get Array element.
const T & at (const std::vector< size_t > & indices) const
iterator begin ()
const_iterator begin () const
const_iterator cbegin () const
const_iterator cend () const
std::vector< T > & data ()
Get reference to internal data vector.
const std::vector< T > & data () const
T * data_ptr ()
Get raw pointer to data.
const T * data_ptr () const
size_t dimension () const
Get number of dimensions.
iterator end ()
const_iterator end () const
T & flat (size_t index)
Flat indexing for 1D access.
const T & flat (size_t index) const
T & operator() (Indices... indices)
Variadic operator() for multi-dimensional indexing.
const T & operator() (Indices... indices) const
reverse_iterator rbegin ()
const_reverse_iterator rbegin () const
reverse_iterator rend ()
const_reverse_iterator rend () const
void reshape (const std::vector< size_t > & new_shape)
Reshape array (no reallocation).
void resize (const std::vector< size_t > & new_shape, const T & fill_value=T{})
Resize array (with reallocation).
const std::vector< size_t > & shape () const
Get shape vector.
size_t shape (size_t dim) const
Get specific dimension size.
size_t size () const
Get total number of elements.
const std::vector< size_t > & strides () const
Get strides vector.

Public Static Functions

Type Name
std::enable_if< std::is_arithmetic< U >::value, NDArray< T > >::type arange (T start, T stop, T step=T{1})
Create range array (numeric types only).
NDArray< T > empty (const std::vector< size_t > & shape)
Create uninitialized array.
NDArray from_vector_move (std::vector< T > & data_in, const std::vector< size_t > & shape_in)
Factory method to create NDArray by moving vector (for Python bindings) SWIG doesn't handle rvalue references well, so we use this factory method.
NDArray< T > full (const std::vector< size_t > & shape, const T & value)
Create array filled with specific value.
NDArray< T > ones (const std::vector< size_t > & shape)
Create one-initialized array.
NDArray< T > zeros (const std::vector< size_t > & shape)
Create zero-initialized array.

Detailed Description

This class implements an n-dimensional array stored as a flat 1D vector with operator() indexing, iterators, and static factory methods.

Public Types Documentation

typedef const_iterator

using star::NDArray< T >::const_iterator = typename std::vector<T>::const_iterator;

typedef const_reverse_iterator

using star::NDArray< T >::const_reverse_iterator = typename std::vector<T>::const_reverse_iterator;

typedef iterator

using star::NDArray< T >::iterator = typename std::vector<T>::iterator;

typedef reverse_iterator

using star::NDArray< T >::reverse_iterator = typename std::vector<T>::reverse_iterator;

typedef value_type

using star::NDArray< T >::value_type = T;

Public Functions Documentation

function NDArray [1/6]

Default constructor.

star::NDArray::NDArray () = default


function NDArray [2/6]

Constructor with shape.

inline explicit star::NDArray::NDArray (
    const std::vector< size_t > & shape_in
) 

Parameters:

  • shape_in Dimensions of the array

function NDArray [3/6]

Constructor with shape and initial value.

inline star::NDArray::NDArray (
    const std::vector< size_t > & shape_in,
    const T & initial_value
) 

Parameters:

  • shape_in Dimensions of the array
  • initial_value Value to initialize all elements with

function NDArray [4/6]

Constructor with data and shape.

inline star::NDArray::NDArray (
    const std::vector< T > & data_in,
    const std::vector< size_t > & shape_in
) 

Parameters:

  • data_in Flat data to use
  • shape_in Dimensions of the array

function NDArray [5/6]

Move constructor with data and shape (zero-copy for Python bindings).

inline star::NDArray::NDArray (
    std::vector< T > && data_in,
    const std::vector< size_t > & shape_in
) 

Parameters:

  • data_in Flat data to move from
  • shape_in Dimensions of the array

function NDArray [6/6]

Copy constructor.

inline star::NDArray::NDArray (
    const NDArray & other
) 

Parameters:


function at [1/2]

Get Array element.

inline T & star::NDArray::at (
    const std::vector< size_t > & indices
) 

Deprecated

Use operator() instead


function at [2/2]

inline const T & star::NDArray::at (
    const std::vector< size_t > & indices
) const

function begin [1/2]

inline iterator star::NDArray::begin () 

function begin [2/2]

inline const_iterator star::NDArray::begin () const

function cbegin

inline const_iterator star::NDArray::cbegin () const

function cend

inline const_iterator star::NDArray::cend () const

function data [1/2]

Get reference to internal data vector.

inline std::vector< T > & star::NDArray::data () 


function data [2/2]

inline const std::vector< T > & star::NDArray::data () const

function data_ptr [1/2]

Get raw pointer to data.

inline T * star::NDArray::data_ptr () 


function data_ptr [2/2]

inline const T * star::NDArray::data_ptr () const

function dimension

Get number of dimensions.

inline size_t star::NDArray::dimension () const


function end [1/2]

inline iterator star::NDArray::end () 

function end [2/2]

inline const_iterator star::NDArray::end () const

function flat [1/2]

Flat indexing for 1D access.

inline T & star::NDArray::flat (
    size_t index
) 


function flat [2/2]

inline const T & star::NDArray::flat (
    size_t index
) const

function operator()

Variadic operator() for multi-dimensional indexing.

template<typename... Indices>
inline T & star::NDArray::operator() (
    Indices... indices
) 


function operator()

template<typename... Indices>
inline const T & star::NDArray::operator() (
    Indices... indices
) const

function rbegin [1/2]

inline reverse_iterator star::NDArray::rbegin () 

function rbegin [2/2]

inline const_reverse_iterator star::NDArray::rbegin () const

function rend [1/2]

inline reverse_iterator star::NDArray::rend () 

function rend [2/2]

inline const_reverse_iterator star::NDArray::rend () const

function reshape

Reshape array (no reallocation).

inline void star::NDArray::reshape (
    const std::vector< size_t > & new_shape
) 


function resize

Resize array (with reallocation).

inline void star::NDArray::resize (
    const std::vector< size_t > & new_shape,
    const T & fill_value=T{}
) 


function shape [1/2]

Get shape vector.

inline const std::vector< size_t > & star::NDArray::shape () const


function shape [2/2]

Get specific dimension size.

inline size_t star::NDArray::shape (
    size_t dim
) const


function size

Get total number of elements.

inline size_t star::NDArray::size () const


function strides

Get strides vector.

inline const std::vector< size_t > & star::NDArray::strides () const


Public Static Functions Documentation

function arange

Create range array (numeric types only).

template<typename U>
static inline std::enable_if< std::is_arithmetic< U >::value, NDArray < T > >::type star::NDArray::arange (
    T start,
    T stop,
    T step=T{1}
) 


function empty

Create uninitialized array.

static inline NDArray < T > star::NDArray::empty (
    const std::vector< size_t > & shape
) 


function from_vector_move

Factory method to create NDArray by moving vector (for Python bindings) SWIG doesn't handle rvalue references well, so we use this factory method.

static inline NDArray star::NDArray::from_vector_move (
    std::vector< T > & data_in,
    const std::vector< size_t > & shape_in
) 

Parameters:

  • data_in Vector to move from
  • shape_in Dimensions of the array

function full

Create array filled with specific value.

static inline NDArray < T > star::NDArray::full (
    const std::vector< size_t > & shape,
    const T & value
) 


function ones

Create one-initialized array.

static inline NDArray < T > star::NDArray::ones (
    const std::vector< size_t > & shape
) 


function zeros

Create zero-initialized array.

static inline NDArray < T > star::NDArray::zeros (
    const std::vector< size_t > & shape
) 


Friends Documentation

friend operator<<

Write array to output stream.

inline std::ostream & star::NDArray::operator<< (
    std::ostream & os,
    const NDArray < T > & arr
) 

Parameters:

  • os Output stream

friend operator>>

Read array from input stream.

inline std::istream & star::NDArray::operator>> (
    std::istream & is,
    NDArray < T > & arr
) 

Parameters:

  • is Input stream


The documentation for this class was generated from the following file StarDS/include/stards.h