NumXX C++ Numerical Computing Library

Data Structures and Containers

In this section, an overview of the three fundamental NumXX types will be given. These are the building blocks used to create NumXX. The types in question are Shape, NArray and Matrix.

After reading this section, a more detailed documentation of each type can be explored via the links at the bottom of the page.

Shape

struct Shape {
    std::vector<size_t> dimensions;
}

The Shape struct is a lightweight wrapper around std::vector<size_t> that stores the dimensional structure of numerical arrays. It determines how a contiguous memory block is interpreted and accessed as a multi-dimensional array.

Member Variables:
dimensions : std::vector<size_t>
A vector that holds the size of each dimension of the array. For example, a shape of (3,4) represents a 3x4 matrix.
Purpose:

Shape provides the metadata needed to interpret flat memory as multi-dimensional data. It handles dimension queries, stride calculations, and shape transformations (transpose, flatten, reshape) that are essential for array operations.

NArray

template <typename dtype = double>
class NArray {
    std::shared_ptr<dtype> _data_ptr;
    Shape _shape;
}

The NArray class is the primary container for N-dimensional arrays in NumXX. It combines shape information with actual data storage to provide a flexible, efficient array type similar to NumPy's ndarray.

Member Variables:
dtype : (defaults to double)
Is the data type to be stored.
_data_ptr : std::shared_ptr<dtype>
A shared pointer to the contiguous memory block holding the array data. The use of shared pointers enables memory sharing between multiple NArray instances.
_shape : Shape
A Shape object that defines the dimensional structure of the array.
Purpose:

NArray serves as the workhorse container for numerical computations. Its shared pointer design allows multiple NArrays to reference the same underlying data, enabling efficient slicing, views, and memory-safe operations without unnecessary copying. This makes operations like array slicing and reshaping both fast and memory-efficient.

Matrix

template <typename dtype = double>
class Matrix final : public NArray<dtype> {
    using NArray<dtype>::_data_ptr;
    using NArray<dtype>::_shape;
}

The Matrix class is a specialized form of NArray that restricts dimensionality to a maximum of two dimensions. It inherits from NArray and adds matrix-specific functionality.

Member Variables:

Matrix inherits the same member variables from NArray:

dtype : (defaults to double)
Is the data type to be stored.
_data_ptr : std::shared_ptr<dtype>
Shared pointer to the contiguous memory block.
_shape : Shape
Shape object, constrained to have at most 2 dimensions. 1D shapes are automatically converted to row vectors with shape (1,n).
Purpose:

Matrix provides a type-safe interface for linear algebra operations. The key distinction from NArray is its multiplication behavior: the operator* performs matrix multiplication (matmul) rather than element-wise multiplication. This makes matrix algebra more intuitive and aligns with mathematical conventions. Despite being a specialized type, Matrix maintains full compatibility with NArray operations through inheritance.

More

For a more comprehensive API documentation, please navigate using the buttons below.