Matrices, reshape, and shared storage
Create explicit shapes and element coordinates, share fixed reshape storage, copy dynamic data safely, and inspect detached transpose and axis permutations.
Download .aner source// Typed matrices are built in. Shape and coordinates contain explicit Int64s.
let matrix = NDArray<Int64>(shape: List(2, 3), fill: 0)
matrix.set(List(0, 0), 1)
matrix.set(List(0, 1), 2)
matrix.set(List(0, 2), 3)
matrix.set(List(1, 0), 4)
matrix.set(List(1, 1), 5)
matrix.set(List(1, 2), 6)
print(matrix.shape())
print(matrix.strides())
print(matrix.to_list())
let alternate = matrix.reshape(shape: List(3, 2))
alternate.set(List(0, 0), 99) // Shared fixed buffer; the original sees this value.
print(matrix.get(List(0, 0)))
let transposed = matrix.transpose() // Independent contiguous copy.
print(transposed.shape())
print(transposed.to_list())
let flat = matrix.flatten() // Independent fixed Array.
flat.set(0, -1)
print(matrix.get(List(0, 0)))
let cube = NDArray<Bool>(shape: List(2, 2, 2), fill: false)
cube.set(List(1, 0, 1), true)
print(cube.permute(axes: List(2, 0, 1)).shape())
let empty = NDArray<String>(shape: List(2, 0, 3), fill: "")
print(empty.len())
print(empty.strides())
Make it your experiment.
With Aner installed, save this program as examples/ndarray_basic.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/ndarray_basic.aner
aner run examples/ndarray_basic.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Use the compatible native Aner interpreter and VS Code extension 0.1.27 or newer. NDArray is built in and requires no import. Declare an explicit dtype and shape; fixed reshapes share storage, dynamic reshapes copy, and numerical broadcasting requires matching dtypes and compatible trailing axes. See the multidimensional array guide for method contracts and Big O complexity.
Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution