Nim bindings for the ndarray-c library - a numpy-like ndarray library for C with multi-dimensional arrays, OpenMP parallelization, and BLAS-optimized operations.
Repository: https://github.com/jailop/ndarray-nim
ndarray-c-nim provides a clean, idiomatic Nim interface to the ndarray-c library with automatic memory management through Nim's destructors and move semantics.
@[2, 3] instead of @[2.csize_t, 3]import ndarray
# Create a 2x3 array of ones
let arr = newOnes(@[2, 3])
# Set value at position (1, 2)
arr.set(@[1, 2], 42.0)
# Print the array
arr.print("My Array", 2)
# No cleanup needed - automatic memory management!
First, install the ndarray-c library:
git clone https://github.com/jailop/ndarray-c.git
cd ndarray-c
mkdir build && cd build
cmake ..
make
sudo make install
nimble install ndarray_c_nim
newZeros, newOnes, newFullnewFromDatanewRandomUniform, newRandomNormalnewArange, newLinspaceget(pos), set(pos, value)setSlice, fillSlice, getSlicePtr, copySliceadd, mul - element-wise operationsaddScalar, mulScalar - scalar operationsaxpby - linear combinationclipMin, clipMax, clip - clippingabs, sign - element-wise functionsnewEqual, newLess, newGreaternewEqualScalar, newLessScalar, newGreaterScalarnewLogicalAnd, newLogicalOr, newLogicalNotnewWhere - conditional selectionnewMatmul - matrix multiplicationnewTensordot - tensor contractionnewTranspose - transposereshape - reshape in-placenewTake - extract slicenewStack - stack arrays along new axisnewConcat - concatenate arraysnewAggregate(axis, type) - aggregate along axisscalarAggregate(type) - aggregate all elementsaggrSum, aggrMean, aggrStd, aggrMax, aggrMinsave(filename) - save to binary filenewLoad(filename) - load from binary fileimport ndarray
let a = newOnes(@[2, 3])
let b = newOnes(@[3, 4])
let c = a.newMatmul(b)
c.print("Result", 2)
import ndarray
let arr = newArange(@[3, 4], 0.0, 12.0, 1.0)
# Sum along axis 0
let sumAxis0 = arr.newAggregate(0, aggrSum)
# Mean of all elements
let meanAll = arr.scalarAggregate(aggrMean)
echo "Mean: ", meanAll
import ndarray
let data = newArange(@[2, 3], 0.0, 6.0, 1.0)
let mask = data.newGreaterScalar(2.5)
let zeros = newZeros(@[2, 3])
let filtered = newWhere(mask, data, zeros)
filtered.print("Filtered", 2)
ndim ≥ 2cdouble (C double precision floats)@[2, 3] instead of @[2.csize_t, 3] - both work!Generated with Nim's documentation generator