Flatten
ShippingflattenRavel a Matrix / Tensor / Vector into a 1-D Vector in C- or Fortran-order (numpy ravel)
Signature
Inputs
aMatrix|Tensor|Vectorrequired— The multi-dimensional grid (or already-1-D Vector) to collapse.
Outputs
resultVector— The flattened 1-D Vector, read in the chosen memory order.
Parameters
| Key | Type | Default | Notes |
|---|---|---|---|
order | enum | c | one of: c, f |
Description
Flatten is the numpy ravel equivalent: it collapses a multi-dimensional grid of floats into a single flat Vector, reading the elements in a chosen memory order.
- `order = c` (row-major, C-style — DEFAULT): the LAST axis varies fastest. For a Matrix this walks . Because Captyse stores Matrix and Tensor data row-major already, C-order is a straight $O(n)$ copy of the backing buffer — no index arithmetic.
- `order = f` (column-major, Fortran-style): the FIRST axis varies fastest. For a Matrix this walks — i.e. it transposes the read order. For an N-D Tensor it iterates the multi-index with axis 0 fastest and the last axis slowest.
A Vector input is already 1-D, so both orders yield the same data (a pass-through). ravel is a pure reindexing, so the physical unit is preserved verbatim — though Matrix and Tensor carry no unit field, so their flattened Vector is dimensionless; only a Vector input forwards a unit. None of these variants carries a per-element σ², so there is no uncertainty to propagate.
Mathematics
Examples
Row-major flatten
A 2×3 Matrix
1 2 3
4 5 6with order = c flattens to [1, 2, 3, 4, 5, 6] — a direct copy of the row-major buffer.
Column-major flatten
The same 2×3 Matrix with order = f reads column-by-column: [1, 4, 2, 5, 3, 6]. For a square matrix this is exactly the flattened transpose.
3-D tensor, Fortran order
A 2×2×2 tensor with row-major buffer [0..8) flattened with order = f gives [0, 4, 2, 6, 1, 5, 3, 7] (axis 0 fastest, last axis slowest) — matching numpy.ravel(order='F').
Applications
- Serialising a matrix or tensor into a 1-D stream for a Vector-only downstream stage or a file/sink export.
- Converting between C- and Fortran-order layouts when interfacing with external code that expects column-major data.
- Preparing a grid's contents as a flat feature vector for statistics or regression.
- The inverse companion of reshape when you only need a flat result and don't care about the reconstructed shape.
Neat
C-order flatten is a genuine zero-work operation — it aliases the existing row-major buffer with an $O(n)$ memcpy and no index math.
The order token is parsed leniently: anything starting with `c`/`r` means row-major, `f` means Fortran, so `C`, `row-major`, `fortran` all resolve, while genuinely unknown values are a hard error.
Fortran-order ravel of an N-D tensor uses an odometer that increments axis 0 fastest, mapping each emitted position back to the row-major source offset — numpy-exact for every rank.
Known issues
A Matrix or Tensor flattens to a dimensionless Vector because those variants carry no unit field; only a Vector input forwards its unit.