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.
// Typed matrices are built in. Shape and coordinates contain explicit Int64s.
let matrix = NDArray<Int64>(shape: List(2, 3), fill: 0)
for row in Range(2) {
for column in Range(3) {
matrix[row, column] = row * 3 + column + 1
}
}
print(matrix.shape())
print(matrix.strides())
print(matrix.to_list())
let alternate = matrix.reshape(shape: List(3, 2))
alternate[0, 0] = 99 // Shared fixed buffer; the original sees this value.
print(matrix[0, 0])
let transposed = matrix.transpose() // Independent contiguous copy.
print(transposed.shape())
print(transposed.to_list())
let flat = matrix.flatten() // Independent fixed Array.
flat[0] = -1
print(matrix[0, 0])
let cube = NDArray<Bool>(shape: List(2, 2, 2), fill: false)
cube[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.
Use Aner 0.0.3 or newer. 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. The revised source requires Aner 0.0.4 or newer; earlier executables may reject the syntax.
Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution