|
IncliArray
|
#include <NDArray.h>
Public Types | |
| enum class | PrintType { Data , Grad } |
Public Member Functions | |
| NDArray (std::vector< int > shape, std::string label="", std::string op="", std::vector< std::reference_wrapper< NDArray > > prev={}) | |
| Construct an owning, contiguous NDArray of given shape. | |
| void | metadata (bool shapeInfo=true, bool stridesInfo=false, bool ndimInfo=false, bool sizeInfo=false, bool ownsDataInfo=false) |
| Print selected metadata fields. | |
| float | get (std::vector< int > indices, PrintType type=PrintType::Data) const |
| Read an element by multi‑dimensional indices. | |
| float | get (int index, PrintType type=PrintType::Data) const |
| Read an element by flat index. | |
| void | set (std::vector< int > indices, float value) |
| Write an element by multi‑dimensional indices. | |
| void | set (int index, float value) |
| Write an element by flat index. | |
| NDArray | slice (std::vector< std::tuple< int, int > > indices) |
| Return a non‑owning view restricted by per‑axis [start, stop) slices. | |
| bool | isContiguous () const |
| Whether the logical layout matches standard row‑major contiguous strides for the current shape. | |
| void | reshape (std::vector< int > newShape) |
| Reshape this array to a new shape with the same number of elements. | |
| void | print (PrintType type=PrintType::Data) |
| Pretty‑print the data or gradients. | |
| void | fillSequential () |
| Fill with sequential values 0, 1, 2, ... (for demos/testing). | |
| void | fill (float value) |
| Fill with a constant value. | |
| void | zeros () |
| Set all elements to 0. | |
| void | ones () |
| Set all elements to 1. | |
| void | randint (int low, int high) |
| Fill with uniform integer values in [low, high). | |
| void | rand () |
| Fill with uniform real values in [0, 1). | |
| void | rand (float low, float high) |
| Fill with uniform real values in [low, high). | |
| NDArray | operator+ (NDArray &other) |
| Broadcasted element‑wise addition (this + other). | |
| NDArray | operator+ (float value) |
| Scalar addition (this + value), shape‑preserving. | |
| NDArray | operator- (NDArray &other) |
| Broadcasted element‑wise subtraction (this - other). | |
| NDArray | operator- (float value) |
| Scalar subtraction (this - value), shape‑preserving. | |
| NDArray | operator* (NDArray &other) |
| 2D matrix multiplication (no broadcasting). | |
| NDArray | operator* (float value) |
| Scalar element wise multiplication (no broadcasting). | |
| NDArray | operator/ (NDArray &other) |
| Broadcasted element‑wise division (this / other). | |
| NDArray | operator/ (float value) |
| Scalar division (this / value), shape‑preserving, warns on zero. | |
| NDArray | operator^ (float value) |
| Scalar element-wise power (this ^ value). | |
| NDArray | element_wise_multiply (NDArray &other) |
| Broadcasted element‑wise multiplication. | |
| NDArray | element_wise_multiply (float value) |
| Scalar element‑wise multiplication (this * value). | |
| NDArray | sum () |
| Reduce all elements to a scalar sum. | |
| NDArray | sum (int axis) |
| Sum along a specified axis (keep dimension as size 1). | |
| void | backward () |
| Reverse‑mode backprop: accumulate gradients into all reachable parents from this node. | |
| NDArray | clone () |
| Materialize a contiguous, owning copy. Detached from autograd. | |
Public Attributes | |
| float * | data |
| std::vector< int > | shape |
| std::vector< int > | strides |
| int | ndim |
| int | size = 0 |
| bool | ownsData |
| float * | grad |
| std::string | op |
| std::string | label |
| std::vector< std::reference_wrapper< NDArray > > | prev |
| std::function< void()> | _backward |
Private Member Functions | |
| NDArray (std::vector< int > shape, std::vector< int > strides, float *data, bool ownsData, std::string label="", std::string op="", std::vector< std::reference_wrapper< NDArray > > prev={}) | |
| Internal constructor used for creating non-owning views or wrapping existing memory with specific strides. | |
| void | build_topo (std::unordered_set< NDArray * > &visited, NDArray *arr, std::vector< std::reference_wrapper< NDArray > > &topo) |
Build a topological ordering of nodes reachable from arr. | |
|
strong |
|
private |
Internal constructor used for creating non-owning views or wrapping existing memory with specific strides.
This does not allocate memory. When ownsData == false, the lifetime of the provided data pointer must outlive the NDArray instance.
Autograd: callers decide whether to pass graph metadata. Slice uses this constructor but returns a detached view (prev empty, op empty).
| NDArray::NDArray | ( | std::vector< int > | shape, |
| std::string | label = "", |
||
| std::string | op = "", |
||
| std::vector< std::reference_wrapper< NDArray > > | prev = {} |
||
| ) |
Construct an owning, contiguous NDArray of given shape.
Allocates data and grad, computes row‑major strides, and sets ownsData = true. This constructor creates a base tensor that can participate in autograd.
| void NDArray::backward | ( | ) |
Reverse‑mode backprop: accumulate gradients into all reachable parents from this node.
Sets this->grad to ones (dOut/dOut = 1) and walks the graph in reverse topological order, invoking each node’s _backward closure.
|
private |
Build a topological ordering of nodes reachable from arr.
DFS that collects unique nodes (by pointer) into topo such that parents appear before children, enabling reverse‑mode backprop from outputs to inputs.
| NDArray NDArray::clone | ( | ) |
Materialize a contiguous, owning copy. Detached from autograd.
If the source is contiguous, performs a fast contiguous copy; otherwise uses a stride‑aware copy. The returned tensor has no graph linkage.
| NDArray NDArray::element_wise_multiply | ( | float | value | ) |
Scalar element‑wise multiplication (this * value).
Broadcasted element‑wise multiplication.
Autograd: dA += other * dOut; dB += this * dOut.
| void NDArray::fill | ( | float | value | ) |
Fill with a constant value.
| std::runtime_error | if the array does not own its memory |
| void NDArray::fillSequential | ( | ) |
Fill with sequential values 0, 1, 2, ... (for demos/testing).
| std::runtime_error | if the array does not own its memory |
| float NDArray::get | ( | int | index, |
| NDArray::PrintType | type = PrintType::Data |
||
| ) | const |
Read an element by flat index.
Valid only for contiguous, owning arrays. Treats memory as a flat buffer regardless of shape, primarily for debugging and demos.
| index | Flat position in [0, size) |
| type | Whether to read data or grad |
| std::out_of_range | if index is outside [0, size) |
| std::runtime_error | if the array is not contiguous or not owning |
| float NDArray::get | ( | std::vector< int > | indices, |
| NDArray::PrintType | type = PrintType::Data |
||
| ) | const |
Read an element by multi‑dimensional indices.
| indices | A vector of length ndim specifying the coordinate |
| type | Whether to read from data or grad buffer |
| std::invalid_argument | if indices.size() != ndim |
| bool NDArray::isContiguous | ( | ) | const |
Whether the logical layout matches standard row‑major contiguous strides for the current shape.
| void NDArray::metadata | ( | bool | shapeInfo = true, |
| bool | stridesInfo = false, |
||
| bool | ndimInfo = false, |
||
| bool | sizeInfo = false, |
||
| bool | ownsDataInfo = false |
||
| ) |
Print selected metadata fields.
| shapeInfo | Whether to print shape |
| stridesInfo | Whether to print strides (row‑major steps) |
| ndimInfo | Whether to print number of dimensions |
| sizeInfo | Whether to print total size (elements) |
| ownsDataInfo | Whether to print ownership flag |
| void NDArray::ones | ( | ) |
Set all elements to 1.
| NDArray NDArray::operator* | ( | float | value | ) |
Scalar element wise multiplication (no broadcasting).
| value | Value to multiply array with. |
2D matrix multiplication (no broadcasting).
| std::invalid_argument | if either input is not 2D or dims mismatch Autograd: implements dA = dC * B^T and dB = A^T * dC. |
| NDArray NDArray::operator+ | ( | float | value | ) |
Scalar addition (this + value), shape‑preserving.
Broadcasted element‑wise addition (this + other).
Autograd: result records graph metadata and accumulates dA += dOut and dB += dOut with broadcasting.
| NDArray NDArray::operator- | ( | float | value | ) |
Scalar subtraction (this - value), shape‑preserving.
Broadcasted element‑wise subtraction (this - other).
Autograd: dA += dOut, dB += -dOut with broadcasting.
| NDArray NDArray::operator/ | ( | float | value | ) |
Scalar division (this / value), shape‑preserving, warns on zero.
Broadcasted element‑wise division (this / other).
Warns on division by zero. Autograd: dA += dOut / other, dB += -(this / other^2) * dOut.
| NDArray NDArray::operator^ | ( | float | value | ) |
Scalar element-wise power (this ^ value).
Raises each element to the given scalar power. Autograd: dA += value * A^(value - 1) * dOut.
| void NDArray::print | ( | NDArray::PrintType | type = PrintType::Data | ) |
Pretty‑print the data or gradients.
Prints 1D and 2D arrays in nested list form; higher‑dimensional arrays are printed flattened for brevity.
| void NDArray::rand | ( | ) |
Fill with uniform real values in [0, 1).
| std::runtime_error | if the array does not own its memory |
| void NDArray::rand | ( | float | low, |
| float | high | ||
| ) |
Fill with uniform real values in [low, high).
| std::invalid_argument | if low >= high |
| std::runtime_error | if the array does not own its memory |
| void NDArray::randint | ( | int | low, |
| int | high | ||
| ) |
Fill with uniform integer values in [low, high).
| low | Inclusive lower bound |
| high | Exclusive upper bound |
| std::runtime_error | if the array does not own its memory |
| void NDArray::reshape | ( | std::vector< int > | newShape | ) |
Reshape this array to a new shape with the same number of elements.
Only allowed for owning, contiguous arrays. Updates shape and recomputes strides to standard row‑major.
| newShape | Target shape (product must equal current size) |
| std::runtime_error | if the array is not owning or not contiguous |
| std::invalid_argument | if newShape is empty or incompatible in size |
| void NDArray::set | ( | int | index, |
| float | value | ||
| ) |
Write an element by flat index.
Valid only for contiguous, owning arrays. Writes into the underlying memory ignoring shape.
| index | Flat position in [0, size) |
| value | Value to write |
| std::out_of_range | if index is outside [0, size) |
| std::runtime_error | if the array is not contiguous or not owning |
| void NDArray::set | ( | std::vector< int > | indices, |
| float | value | ||
| ) |
Write an element by multi‑dimensional indices.
| indices | Coordinate of the element (length == ndim) |
| value | Value to write into data buffer |
| std::invalid_argument | if indices.size() != ndim |
| NDArray NDArray::slice | ( | std::vector< std::tuple< int, int > > | indices | ) |
Return a non‑owning view restricted by per‑axis [start, stop) slices.
The returned NDArray shares data with the base tensor and has updated shape/strides/offset. It is DETACHED from autograd: no graph is recorded and its _backward is a no‑op.
| indices | A vector (length == ndim) of (start, stop) pairs, inclusive start and exclusive stop for each axis |
| std::invalid_argument | if the number of slices != ndim |
| NDArray NDArray::sum | ( | ) |
Reduce all elements to a scalar sum.
Returns a 1-element NDArray holding the total sum. Autograd: distributes the upstream gradient uniformly to every input element (dA += 1 * dOut).
| NDArray NDArray::sum | ( | int | axis | ) |
Sum along a specified axis (keep dimension as size 1).
The output shape matches the input except shape[axis] == 1. Negative axes are supported (Python-style indexing). Autograd: broadcasts upstream gradient across the reduced axis into the input gradient.
| axis | The axis along which to compute the sum (supports negatives) |
| std::invalid_argument | if axis is out of range after normalization |
| void NDArray::zeros | ( | ) |
Set all elements to 0.
| std::function<void()> NDArray::_backward |
Backpropagation closure; invoked during backward(). Node‑local backward function.
| float* NDArray::data |
Raw data pointer in row‑major layout. Points to size floats. Raw data pointer in row‑major layout (length = size).
| float* NDArray::grad |
Gradient buffer aligned with logical indexing (allocated when constructed). Gradient storage parallel to data.
| std::string NDArray::label |
Optional human‑readable label for this tensor. User/debug label.
| int NDArray::ndim |
Number of dimensions (shape.size()). Number of axes.
| std::string NDArray::op |
Operation tag for debug/inspection (e.g. "+", "-", "elem_mul", "*"). Debug op tag.
| bool NDArray::ownsData |
Whether this NDArray owns and manages the memory referenced by data. True if this tensor allocated and owns its memory.
| std::vector<std::reference_wrapper<NDArray> > NDArray::prev |
Parents in autograd graph. Empty for detached tensors (slice/clone). Graph parents.
| std::vector<int> NDArray::shape |
Dimensions of the array, e.g. {rows, cols} for 2D. Shape dimensions; product equals size.
| int NDArray::size = 0 |
Total number of elements (product of shape). Total element count.
| std::vector<int> NDArray::strides |
Row‑major strides in elements; stride[i] is step for axis i. Row‑major strides per axis (in elements).