Broadcasting and matrix products
Combine compatible trailing axes without expanded inputs, distinguish elementwise multiplication from matrix products, and copy checked Float64 results into a Tensor.
Download .aner source// Native broadcasting follows trailing dimensions: equal extents or one == 1.
let matrix = NDArray<Float64>(shape: List(2, 3), fill: 2.0)
let bias = NDArray<Float64>(shape: List(3), fill: 0.0)
bias.set(List(0), 10.0)
bias.set(List(1), 20.0)
bias.set(List(2), 30.0)
let shifted = matrix.add(bias)
print(shifted.shape())
print(shifted.to_list())
let columns = NDArray<Float64>(shape: List(2, 1), fill: 5.0)
print(columns.mul(bias).to_list())
let right = NDArray<Float64>(shape: List(3, 2), fill: 1.0)
let product = matrix.matmul(right)
print(product.shape())
print(product.to_list())
print(product.mean())
let detached_tensor = product.to_tensor()
print(type_of(detached_tensor))
let integers = Array<Int64>(size: 6, fill: 1)
print(integers.reshape(List(2, 3)).to_float().add(bias).to_list())
Make it your experiment.
With Aner installed, save this program as examples/ndarray_numeric.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/ndarray_numeric.aner
aner run examples/ndarray_numeric.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