cheatah
Class

ndarray::basic_ndarray

An N-dimensional array of T (a Field element type — real or complex): a view ({shape, strides, offset}) over a shared element buffer.

Copies are cheap and share the buffer; reshape/broadcast produce new views without copying elements. Index math goes through at, which bounds-checks. The element type is deduced from the data (e.g. array([1,2,3]) is integer, array([1.0,…]) is double); NDArray is the default basic_ndarray<double>. Complex element types (std::complex<double>) make complex matrices/vectors — and the complex eigenvalues a real matrix can have — first-class.

Functions

fn basic_ndarray · 3 overloads
basic_ndarray()source#
basic_ndarray(std::vector< std::size_t > shape, T fill=T{})source#
basic_ndarray(std::shared_ptr< buffer_t< T > > data, std::vector< std::size_t > shape, std::vector< std::ptrdiff_t > strides, std::size_t offset)source#

Construct an empty 0-d array with a fresh empty buffer.

Leaves shape and strides empty and the buffer holding no elements; note this is distinct from a 0-d scalar (see scalar), whose buffer holds one element.

Complexity

O(1).

Allocation

allocates the empty shared buffer.

fn const std::vector< std::size_t > & shape() const source#

The shape (dimensions).

Returns

reference to the shape vector.

Complexity

O(1).

Allocation

none.

fn const std::vector< std::ptrdiff_t > & strides() const source#

The element strides.

Returns

reference to the strides vector.

Complexity

O(1).

Allocation

none.

fn std::size_t ndim() const source#

The number of dimensions (rank).

Returns

shape().size().

Complexity

O(1).

Allocation

none.

fn std::size_t size() const source#

The element count (product of dims; 1 for a 0-d scalar).

Recomputes the overflow-checked product of the shape on each call rather than caching it; an empty (no-dimension) shape yields 1.

Returns

the number of elements.

Complexity

O(ndim).

Allocation

none; throws on size overflow.

fn item_ref · 2 overloads
T & item_ref(Ix... ixs)source#
const T & item_ref(Ix... ixs) constsource#

MUTABLE element reference by multi-index — the write path behind cheatah subscript assignment x[i] = v / x[i, j] = v.

Negative indices count from the end of their dimension; rank and bounds are checked.

Parameters
ixs

one (possibly negative) coordinate per dimension.

Returns

a writable reference into the backing buffer.

Complexity

O(ndim).

Allocation

none.

fn T & operator[](Ix i) source#

1-D subscript: mask[i] = v assignment and raw element writes.

Accepts an integer or a scoped enum column label (see Subscript), so q[QuatCol::W] = v writes column W of a 1-D row.

Parameters
i

the element index (negative counts from the end, Python-style).

Returns

a mutable reference to element i.

Complexity

O(1).

Allocation

none.

fn std::size_t item_pos(Ix... ixs) const source#

The buffer position of a (possibly negative) multi-index — rank and bounds are checked here, once, for both item_ref overloads.

Parameters
ixs

one index per dimension (negative counts from the end, Python-style).

Returns

the flat position in the buffer of that element.

Complexity

O(ndim).

Allocation

none.

fn T & at_ref(const std::vector< std::size_t > &index) source#

Writable element at index — the mutable companion to at, used to write through a view (a slice assignment addresses elements it does not own outright).

Parameters
index

one coordinate per dimension.

Returns

a reference to the element.

Complexity

O(ndim).

Allocation

none.

fn T at(const std::vector< std::size_t > &index) const source#

Read one element by a full multidimensional index (one component per axis), resolved via the array's strides.

Computes the flat buffer position as offset + sum(index[i] * strides[i]), so it correctly resolves views (including broadcast dims with stride 0). Bounds- and rank-checked.

Parameters
index

the per-axis indices; its size must equal the array's rank.

Returns

a copy of the addressed element.

Parameters
std::runtime_error

if index has the wrong rank or any component is out of range.

Complexity

O(rank).

Allocation

none.

fn const std::shared_ptr< buffer_t< T > > & buffer() const source#

The shared backing buffer.

Returns

reference to the element buffer shared_ptr.

Complexity

O(1).

Allocation

none.

fn std::size_t offset() const source#

The flat offset into the buffer where this view starts.

Returns

the offset.

Complexity

O(1).

Allocation

none.

fn std::string str() const source#

Python-style text rendering, e.g.

"[[1, 2], [3, 4]]" — the str() member, the same full form to_string and operator<< produce (io.print reaches an array through cheatah_pretty_print and operator<<, not this hook). Defers to the free to_string; defined out-of-line below, where to_string is declared.

Returns

the array formatted as nested brackets.

Complexity

O(n) in the element count.

Allocation

allocates the result string.

fn void cheatah_pretty_print(std::ostream &os, long long indent) const source#

Pretty-print hook used by io.print: renders the array in nested-bracket form but ABBREVIATES a large array with ... (numpy-style edge items), so printing a big array stays readable.

io.rprint (and str()/operator<<) keep the FULL untruncated form — slice the array and rprint the subset to see everything.

Parameters
os

destination stream.

indent

unused (arrays are self-delimiting with brackets); present so io.print detects this hook uniformly with struct pretty-printers.

Complexity

O(size) — an abbreviated axis is still stepped over; only its edge items are formatted.

Allocation

allocates intermediate strings.

fn static basic_ndarray uninitialized(std::vector< std::size_t > shape) source#

Build a contiguous array of shape whose buffer is sized but left UNINITIALIZED — for internal ops (binary ops, ufuncs, reshape, array) that immediately write every element, so paying for a zero-fill first is pure waste.

Parameters
shape

the dimensions.

Returns

an array of shape with an uninitialized buffer.

Complexity

O(ndim) beyond the allocation (no element initialization pass for a trivially-constructible T).

Allocation

allocates an uninitialized product(shape)-element buffer; overflow-checked.

Constants & variables

var std::shared_ptr< buffer_t< T > > data_ source#
var std::vector< std::size_t > shape_ source#
var std::vector< std::ptrdiff_t > strides_ source#
var std::size_t offset_ source#

Types

type T value_type source#

The stored element type (an Element T).