NumSharp
Write NumPy in C#.
NumSharp brings NumPy's API to .NET: the same functions, dtypes, broadcasting and views, tested against NumPy's own output. Its arrays cross into Python, PyTorch, ONNX Runtime and TorchSharp, most of them without a copy.
Same names, same slicing, same output.
import numpy as npusing NumSharp;a = np.arange(12).reshape(3, 4)var a = np.arange(12).reshape(3, 4);v = a[1:, ::2] # a view, no copyvar v = a["1:, ::2"]; // a view, no copyv[0, 0] = 100 # writes through to av[0, 0] = 100; // writes through to aprint(a.sum(axis=0))Console.WriteLine(a.sum(axis: 0));[108 15 18 21]
[108 15 18 21]
import numpy as npusing NumSharp;row = np.arange(1, 10)var row = np.arange(1, 10);table = row[:, np.newaxis] * rowvar table = row[":, np.newaxis"] * row;print(table[6:, 6:])Console.WriteLine(table["6:, 6:"]);[[49 56 63] [56 64 72] [63 72 81]]
import numpy as npusing NumSharp;names = np.array(["Ada", "Bo", "Cy", "Di", "Ed"])var names = np.array(["Ada", "Bo", "Cy", "Di", "Ed"]);scores = np.array([88, 92, 79, 95, 60])var scores = np.array([88, 92, 79, 95, 60]);print(names[np.argsort(-scores)][:3])Console.WriteLine(names[np.argsort(-scores)][":3"]);['Di' 'Bo' 'Ada']
import numpy as npusing NumSharp;n = np.arange(1, 16)var n = np.arange(1, 16);fb = n.astype(str)var fb = n.astype(np.str_);fb = np.where(n % 3 == 0, "Fizz", fb)fb = np.where(n % 3 == 0, "Fizz", fb);fb = np.where(n % 5 == 0, "Buzz", fb)fb = np.where(n % 5 == 0, "Buzz", fb);fb = np.where(n % 15 == 0, "FizzBuzz", fb)fb = np.where(n % 15 == 0, "FizzBuzz", fb);print(fb)Console.WriteLine(fb);['1' '2' 'Fizz' '4' 'Buzz' 'Fizz' '7' '8' 'Fizz' 'Buzz' '11' 'Fizz' '13' '14' 'FizzBuzz']
import numpy as npusing NumSharp;pts = np.array([[0, 0], [3, 4], [6, 8]])var pts = np.array(new[,] { { 0, 0 }, { 3, 4 }, { 6, 8 } });d = pts[:, np.newaxis] - ptsvar d = pts[":, np.newaxis"] - pts;dist = np.sqrt(np.einsum("ijk,ijk->ij", d, d))var dist = np.sqrt(np.einsum("ijk,ijk->ij", d, d));print(dist)Console.WriteLine(dist);[[ 0. 5. 10.] [ 5. 0. 5.] [10. 5. 0.]]
import numpy as npusing NumSharp;np.random.seed(42)np.random.seed(42);hands = np.array(["rock", "paper", "scissors"])var hands = np.array(["rock", "paper", "scissors"]);print(np.random.randint(0, 100, size=8))Console.WriteLine(np.random.randint(0, 100, size: 8));print(np.random.choice(hands, 4))Console.WriteLine(np.random.choice(hands, 4));[51 92 14 71 60 20 82 86] ['scissors' 'scissors' 'rock' 'scissors']
import numpy as npusing NumSharp;np.random.seed(7)np.random.seed(7);xy = np.random.rand(2, 1_000_000)var xy = np.random.rand(2, 1_000_000);inside = np.square(xy).sum(axis=0) <= 1var inside = np.square(xy).sum(axis: 0) <= 1;print(4 * inside.mean())Console.WriteLine(4 * inside.mean());3.139796
import numpy as npusing NumSharp;# today sunny or rainy -> odds for tomorrow// today sunny or rainy -> odds for tomorrowP = np.array([[0.9, 0.1],var P = np.array(new[,] { { 0.9, 0.1 }, [0.5, 0.5]]) { 0.5, 0.5 } });print(np.linalg.matrix_power(P, 64)[0])Console.WriteLine(np.linalg.matrix_power(P, 64)[0]);[0.83333333 0.16666667]
import numpy as npusing NumSharp;t = np.arange(512) / 512var t = np.arange(512) / 512;wave = (np.sin(2 * np.pi * 50 * t)var wave = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)) + 0.5 * np.sin(2 * np.pi * 120 * t);spectrum = np.abs(np.fft.rfft(wave))var spectrum = np.abs(np.fft.rfft(wave));top2 = np.argsort(spectrum)[-2:]var top2 = np.argsort(spectrum)["-2:"];print(np.sort(top2))Console.WriteLine(np.sort(top2));[ 50 120]
import numpy as npusing NumSharp;b = np.zeros((6, 6), dtype=int)var b = np.zeros((6, 6), np.int64);np.put(b, [1, 8, 12, 13, 14], 1)np.put(b, new[] { 1, 8, 12, 13, 14 }, 1);for step in range(4):for (int step = 0; step < 4; step++) { n = -b var n = -b; for s in np.ndindex(3, 3): foreach (var s in np.ndindex(3, 3)) n += np.roll(b, np.array(s) - 1, (0, 1)) n += np.roll(b, np.array(s) - 1, new[] { 0, 1 }); b = np.where((n == 3) | (b & (n == 2)), 1, 0) b = np.where((n == 3) | (b & (n == 2)), 1, 0);}print(b)Console.WriteLine(b);[[0 0 0 0 0 0] [0 0 1 0 0 0] [0 0 0 1 0 0] [0 1 1 1 0 0] [0 0 0 0 0 0] [0 0 0 0 0 0]]
import numpy as npusing NumSharp; using System.Numerics;x = np.linspace(-2.0, 0.5, 26)var x = np.linspace(-2.0, 0.5, 26);y = np.linspace(-1.0, 1.0, 9)[:, np.newaxis]var y = np.linspace(-1.0, 1.0, 9)[":, np.newaxis"];c = x + 1j * yvar c = x + Complex.ImaginaryOne * y;z, n = 0 * c, np.zeros(c.shape, dtype=int)var (z, n) = (0 * c, np.zeros(c.shape, np.int64));for _ in range(9):for (int i = 0; i < 9; i++) { z = np.where(np.abs(z) <= 2, z * z + c, z) z = np.where(np.abs(z) <= 2, z * z + c, z); n += np.abs(z) <= 2 n += np.abs(z) <= 2;}print(n)Console.WriteLine(n);[[0 0 0 1 1 1 2 2 2 2 2 2 2 2 3 3 3 4 6 8 9 3 3 2 2 1] [0 0 1 2 2 2 2 2 2 2 2 3 3 3 4 5 6 8 9 9 9 6 5 4 3 2] [0 1 2 2 2 2 2 3 4 4 4 4 5 7 9 9 9 9 9 9 9 9 9 9 6 4] [0 2 3 3 3 4 4 6 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 4] [9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 6 4] [0 2 3 3 3 4 4 6 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 4] [0 1 2 2 2 2 2 3 4 4 4 4 5 7 9 9 9 9 9 9 9 9 9 9 6 4] [0 0 1 2 2 2 2 2 2 2 2 3 3 3 4 5 6 8 9 9 9 6 5 4 3 2] [0 0 0 1 1 1 2 2 2 2 2 2 2 2 3 3 3 4 6 8 9 3 3 2 2 1]]
Where an NDArray can go
Each box names a library and how an NDArray crosses into it; its color names the NumSharp package that brings it. Hover a box to open the library's own site or its page in these docs. Read the interoperability guide
Documentation
Learn NumSharp
- The absolute basics
Arrays, shapes, indexing and math, starting from zero.
- Quickstart
The main features on one page, for readers who know NumPy.
- NDArray
Creation, shape, indexing, views and copies, operators and storage.
- Data types
The 15 numeric dtypes and the text dtypes, with NumPy's names, promotion and casting.
- Strings and bytes
Fixed-width bytes and Unicode, variable-width StringDType, numpy.char.
- API reference
Every public type and member, under NumPy's names.
How it works
- Broadcasting
How operands of different shapes combine without copying.
- Memory and buffers
Unmanaged storage, ownership, lifetimes and zero-copy buffers.
- NDIter
The iterator that runs strided, buffered and fused loops.
- IL generation
SIMD kernels compiled at runtime for each dtype and layout.
- Interoperability
One buffer shared with Python, PyTorch, ONNX Runtime, ML.NET and more.
Measured against NumPy
- NumPy API coverage
Every NumPy function: present, partial or missing, and how closely it matches.
- Unit tests and oracle
The test inventory and the corpus of NumPy results NumSharp replays.
- Benchmarks
Timings per operation, dtype and size, next to NumPy's.
- NumPy compliance
What matches NumPy, and the documented differences.