From fe8d63769657a9bef5073c524172c37000fe2891 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 4 Nov 2026 12:51:19 +0100 Subject: [PATCH] NDArray: document the axis, which reductions have accepted by keyword since ruby#883 The page listed the reductions and showed `a.sum`, so a reader had no way to learn that `sum`/`prod`/`mean`/`max`/`min`/`argmax`/`argmin` reduce along an axis at all, let alone that go-embedded-ruby/ruby#883 taught them NumPy's `axis:` spelling. `cumsum` was missing from the list entirely. Every figure in the new example was taken from a real run of rbgo built at go-embedded-ruby/ruby@6eb7e4ad, not from reading the binding: p a.sum # => 15.0 p a.sum(axis: 0).to_a # => [3.0, 5.0, 7.0] p a.argmax(axis: 1).to_a # => [2.0, 2.0] The same run confirms the two statements the prose makes: `a.sum(0)` and `a.sum(axis: 0)` return the same array, and `a.sum(0, axis: 0)` raises ArgumentError("axis given both positionally and as a keyword"). numericsurface against that binary: 258 names over 11 module sections, 0 disagree. Co-Authored-By: Claude Opus 5 --- content/en/scientific-stack.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/content/en/scientific-stack.md b/content/en/scientific-stack.md index 558b96c..7f3d422 100644 --- a/content/en/scientific-stack.md +++ b/content/en/scientific-stack.md @@ -33,13 +33,21 @@ p Math.hypot(3, 4) # => 5.0 Binds [go-ndarray](https://github.com/go-ndarray/ndarray): constructors `zeros`/`ones`/`full`/`arange`/`from`, element-wise `+ - * /` with scalar broadcasting, ufuncs (`sqrt`/`exp`/`log`/`sin`/`cos`/`abs`), reductions -(`sum`/`mean`/`max`/`min`/`prod`/`argmax`/`argmin`), `matmul`/`dot`, -`transpose`/`reshape`/`flatten`, and `shape`/`to_a`/`[]`. +(`sum`/`mean`/`max`/`min`/`prod`/`argmax`/`argmin`) and `cumsum`, +`matmul`/`dot`, `transpose`/`reshape`/`flatten`, and `shape`/`to_a`/`[]`. + +A reduction with no axis collapses the whole array; with an axis it reduces +along it. The axis may be given positionally or by keyword, the way NumPy +spells it — `a.sum(0)` and `a.sum(axis: 0)` are the same call, and giving both +at once raises `ArgumentError`. `keepdims: true` keeps the reduced axes at +length 1. ```ruby a = NDArray.arange(0, 6).reshape(2, 3) -p a.sum # => 15.0 -p a.transpose.shape # => [3, 2] +p a.sum # => 15.0 +p a.sum(axis: 0).to_a # => [3.0, 5.0, 7.0] +p a.argmax(axis: 1).to_a # => [2.0, 2.0] +p a.transpose.shape # => [3, 2] ``` ## FFT — numpy.fft-style transforms {#fft-numpyfft-style-transforms}