ndarray-c-nim Documentation

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

Overview

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.

Features

Multi-dimensional arrays (ndim ≥ 2)
OpenMP parallelization for performance
BLAS-optimized operations for linear algebra
Automatic memory management - no cleanup needed!
Simple API - use @[2, 3] instead of @[2.csize_t, 3]
Type-safe Nim bindings

Quick Start

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!

Documentation

Installation

Prerequisites

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

Install Nim bindings

nimble install ndarray_c_nim

Key API Categories

Array Creation

Element Access

Arithmetic Operations (In-place)

Comparison and Logical (Returns new array)

Linear Algebra (Returns new array)

Array Manipulation

Aggregation

I/O Operations

Examples

Matrix Multiplication

import ndarray

let a = newOnes(@[2, 3])
let b = newOnes(@[3, 4])
let c = a.newMatmul(b)

c.print("Result", 2)

Aggregation

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

Conditional Operations

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)

Important Notes

Links


Generated with Nim's documentation generator