mdarray cheatsheet
Using mdarray (Rust) when coming from NumPy (Python)

Table of Contents

1. Introduction

This document is a practical guide for NumPy users who want to use the Rust mdarray library.

2. NumPy / mdarray Comparisons

2.1. Creating Arrays

NumPy (Python) mdarray (Rust)
a = np.array([1, 2, 3]) let mut a = tensor![[1, 2, 3]];
b = np.array([[1, 2], [3, 4]]) let mut b = tensor![[1, 2],[3,4]];
np.fromfunction(lambda i, j: i + j, (2, 2)) DTensor::<usize, 2>::from_fn([2, 2], | i | i[0] + i[1])
np.arange(10) DTensor::<usize, 1>::from_fn([10], | i | i[0])
np.eye(3) DTensor::<usize, 2>::from_fn([3,3], | i | if i[0] == i[1] {1} else {0})
np.ones((2, 3)) DTensor::from_elem([2, 3], 1.0)
np.zeros((2, 3)) DTensor::from_elem([2, 3], 0.0)
np.array([]) DTensor::<f64, 2>::new()

2.2. Indexing, Slicing, Reshaping, Filtering

NumPy (Python) mdarray (Rust)
a[0,1] a[[0,1]]
b[0,:] b.view(0, ..)
a[1:3, : 2] a.view(1..3,..2)
b.reshape((1, 4)) b.reshape([1, 4])
np.diag(b) b.diag(0)
b.flatten() b.flatten()
iter(b.flatten()) b.into_iter()
np.any(a == x) a.contains(&x)
a<1 mdarray::expr::map(a, | x | x < 1).eval() 1

2.3. Sizes and Dimensions

NumPy (Python) mdarray (Rust)
a.size() a.len()
a.shape a.shape()
a.dtype ??

2.4. Mathematical Operations

NumPy (Python) mdarray (Rust)
a.T a.transpose()
a.sum() a.into_iter().sum::<i32>()
np.sqrt(a) a.map( | x | x.sqrt())
2*a fill(2) * b.clone() 2
a0+a1 a0.clone() + a1.clone()

3. Detailed Examples

Footnotes:

1

Requires the import use mdarray::expr::Expression;

2

Requires the import use mdarray::expr::fill;

Created: 2025-07-28 Mon 15:05

Validate