From ad09483cb66fe51b48cfd35b846b96e529cb0ce0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 4 Oct 2026 11:04:00 +0200 Subject: [PATCH] fix: propagate NaN in axis/arg reductions and Maximum/Minimum like numpy Max/Min were already NaN-propagating, but the rest of the family was not: - MaxAxis/MinAxis used `v > dst`, so a NaN after the first element was skipped and a leading NaN stuck: max over [9, NaN, 3] gave 9 (numpy: NaN). - ArgMax/ArgMin and their axis forms ignored NaN the same way: argmax of [inf, NaN] gave 0 (numpy: 1, the index of the first NaN). - Maximum/Minimum used math.Max/math.Min, which special-case +-Inf ahead of NaN: maximum(NaN, +Inf) gave +Inf (numpy: NaN). Found by a differential sweep against numpy 2.5.3 (2640 random cases with NaN/Inf/signed zeros, strided and transposed views). The expectations in nan_test.go are numpy values. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/kernels/kernels.go | 56 +++++++++++++++-------- nan_test.go | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 nan_test.go diff --git a/internal/kernels/kernels.go b/internal/kernels/kernels.go index 1770da4..6c84060 100644 --- a/internal/kernels/kernels.go +++ b/internal/kernels/kernels.go @@ -113,17 +113,21 @@ func LessEqual(dst, a, b []float64) { } } -// Maximum writes the pairwise maximum of a[i] and b[i] into dst[i]. +// Maximum writes the pairwise maximum of a[i] and b[i] into dst[i]. It is +// NaN-propagating like numpy.maximum: it uses the builtin max, not math.Max, +// because math.Max returns +Inf for max(NaN, +Inf) where numpy returns NaN. func Maximum(dst, a, b []float64) { for i := range dst { - dst[i] = math.Max(a[i], b[i]) + dst[i] = max(a[i], b[i]) } } -// Minimum writes the pairwise minimum of a[i] and b[i] into dst[i]. +// Minimum writes the pairwise minimum of a[i] and b[i] into dst[i], +// NaN-propagating like numpy.minimum (builtin min; math.Min returns -Inf for +// min(NaN, -Inf)). func Minimum(dst, a, b []float64) { for i := range dst { - dst[i] = math.Min(a[i], b[i]) + dst[i] = min(a[i], b[i]) } } @@ -239,7 +243,8 @@ func ProdAxis(dst, src []float64, outer, axisLen, inner int) { } } -// MaxAxis reduces the middle axis by taking the maximum. +// MaxAxis reduces the middle axis by taking the maximum. Like Max it is +// NaN-propagating (numpy.max(axis=...)). func MaxAxis(dst, src []float64, outer, axisLen, inner int) { for o := 0; o < outer; o++ { base := o * inner @@ -250,15 +255,14 @@ func MaxAxis(dst, src []float64, outer, axisLen, inner int) { for k := 1; k < axisLen; k++ { off := block + k*inner for i := 0; i < inner; i++ { - if v := src[off+i]; v > dst[base+i] { - dst[base+i] = v - } + dst[base+i] = max(dst[base+i], src[off+i]) } } } } -// MinAxis reduces the middle axis by taking the minimum. +// MinAxis reduces the middle axis by taking the minimum, NaN-propagating +// (numpy.min(axis=...)). func MinAxis(dst, src []float64, outer, axisLen, inner int) { for o := 0; o < outer; o++ { base := o * inner @@ -269,19 +273,24 @@ func MinAxis(dst, src []float64, outer, axisLen, inner int) { for k := 1; k < axisLen; k++ { off := block + k*inner for i := 0; i < inner; i++ { - if v := src[off+i]; v < dst[base+i] { - dst[base+i] = v - } + dst[base+i] = min(dst[base+i], src[off+i]) } } } } // ArgMax returns the index of the first maximum element of a (non-empty), -// matching numpy.argmax: ties go to the lowest index. +// matching numpy.argmax: ties go to the lowest index, and a NaN counts as the +// maximum, so the index of the first NaN wins (as Max returns NaN). func ArgMax(a []float64) int { best, bi := a[0], 0 + if math.IsNaN(best) { + return 0 + } for i, v := range a[1:] { + if math.IsNaN(v) { + return i + 1 + } if v > best { best, bi = v, i+1 } @@ -290,10 +299,16 @@ func ArgMax(a []float64) int { } // ArgMin returns the index of the first minimum element of a (non-empty), -// matching numpy.argmin: ties go to the lowest index. +// matching numpy.argmin: ties go to the lowest index, and the first NaN wins. func ArgMin(a []float64) int { best, bi := a[0], 0 + if math.IsNaN(best) { + return 0 + } for i, v := range a[1:] { + if math.IsNaN(v) { + return i + 1 + } if v < best { best, bi = v, i+1 } @@ -302,7 +317,8 @@ func ArgMin(a []float64) int { } // ArgMaxAxis writes into dst the index (along the middle axis) of the first -// maximum for each [outer][inner] position. Layout matches the *Axis kernels. +// maximum for each [outer][inner] position, or of the first NaN (see ArgMax). +// Layout matches the *Axis kernels. func ArgMaxAxis(dst []float64, src []float64, outer, axisLen, inner int) { for o := 0; o < outer; o++ { base := o * inner @@ -310,8 +326,8 @@ func ArgMaxAxis(dst []float64, src []float64, outer, axisLen, inner int) { for i := 0; i < inner; i++ { best := src[block+i] bi := 0 - for k := 1; k < axisLen; k++ { - if v := src[block+k*inner+i]; v > best { + for k := 1; k < axisLen && !math.IsNaN(best); k++ { + if v := src[block+k*inner+i]; v > best || math.IsNaN(v) { best, bi = v, k } } @@ -321,7 +337,7 @@ func ArgMaxAxis(dst []float64, src []float64, outer, axisLen, inner int) { } // ArgMinAxis writes into dst the index (along the middle axis) of the first -// minimum for each [outer][inner] position. +// minimum for each [outer][inner] position, or of the first NaN. func ArgMinAxis(dst []float64, src []float64, outer, axisLen, inner int) { for o := 0; o < outer; o++ { base := o * inner @@ -329,8 +345,8 @@ func ArgMinAxis(dst []float64, src []float64, outer, axisLen, inner int) { for i := 0; i < inner; i++ { best := src[block+i] bi := 0 - for k := 1; k < axisLen; k++ { - if v := src[block+k*inner+i]; v < best { + for k := 1; k < axisLen && !math.IsNaN(best); k++ { + if v := src[block+k*inner+i]; v < best || math.IsNaN(v) { best, bi = v, k } } diff --git a/nan_test.go b/nan_test.go new file mode 100644 index 0000000..86ec451 --- /dev/null +++ b/nan_test.go @@ -0,0 +1,91 @@ +package ndarray + +import ( + "math" + "reflect" + "testing" +) + +// wantNaNData is wantData with NaN == NaN, for the NaN-propagation tables. +func wantNaNData(t *testing.T, name string, a *Array, shape []int, data []float64) { + t.Helper() + if !reflect.DeepEqual(a.Shape(), shape) { + t.Fatalf("%s: shape = %v, want %v", name, a.Shape(), shape) + } + got := a.materialize() + for i := range data { + if got[i] != data[i] && !(math.IsNaN(got[i]) && math.IsNaN(data[i])) { + t.Fatalf("%s: data = %v, want %v", name, got, data) + } + } +} + +// TestNaNPropagation locks the NaN convention of the per-axis and index +// reductions and of the pairwise Maximum/Minimum to numpy's: a NaN wins. The +// expectations are numpy 2.5.3 values for the same fixture. Before the fix the +// per-axis Max/Min and every Arg* reduction skipped a NaN that was not the first +// element (and stuck to one that was), and Maximum/Minimum returned the +// infinity when the other operand was NaN (math.Max/math.Min special-case ±Inf +// ahead of NaN). +func TestNaNPropagation(t *testing.T) { + nan := math.NaN() + a := mustArr(t, ok(FromData([]float64{ + 1, nan, 3, 5, + 9, 4, nan, 7, + nan, 8, 2, 6, + }, 3, 4))) + + if got, _ := a.ArgMax(); got != 1 { + t.Errorf("ArgMax = %d, want 1 (first NaN)", got) + } + if got, _ := a.ArgMin(); got != 1 { + t.Errorf("ArgMin = %d, want 1 (first NaN)", got) + } + inf := math.Inf(1) + for _, c := range []struct { + data []float64 + argmax, am int + }{ + {[]float64{inf, nan}, 1, 1}, + {[]float64{-inf, nan}, 1, 1}, + {[]float64{nan, nan, 5}, 0, 0}, + } { + v := mustArr(t, ok(FromData(c.data, len(c.data)))) + if got, _ := v.ArgMax(); got != c.argmax { + t.Errorf("ArgMax(%v) = %d, want %d", c.data, got, c.argmax) + } + if got, _ := v.ArgMin(); got != c.am { + t.Errorf("ArgMin(%v) = %d, want %d", c.data, got, c.am) + } + } + + for _, c := range []struct { + name string + fn func(int, bool) (*Array, error) + axis int + shape []int + data []float64 + }{ + {"max0", a.MaxAxis, 0, []int{4}, []float64{nan, nan, nan, 7}}, + {"min0", a.MinAxis, 0, []int{4}, []float64{nan, nan, nan, 5}}, + {"max1", a.MaxAxis, 1, []int{3}, []float64{nan, nan, nan}}, + {"min1", a.MinAxis, 1, []int{3}, []float64{nan, nan, nan}}, + {"argmax0", a.ArgMaxAxis, 0, []int{4}, []float64{2, 0, 1, 1}}, + {"argmin0", a.ArgMinAxis, 0, []int{4}, []float64{2, 0, 1, 0}}, + {"argmax1", a.ArgMaxAxis, 1, []int{3}, []float64{1, 2, 0}}, + {"argmin1", a.ArgMinAxis, 1, []int{3}, []float64{1, 2, 0}}, + } { + got, err := c.fn(c.axis, false) + if err != nil { + t.Fatalf("%s: %v", c.name, err) + } + wantNaNData(t, c.name, got, c.shape, c.data) + } + + x := mustArr(t, ok(FromData([]float64{nan, nan, 1, -inf, nan, 2}, 6))) + y := mustArr(t, ok(FromData([]float64{inf, -inf, nan, nan, nan, 1}, 6))) + mx := mustArr(t, ok(x.Maximum(y))) + wantNaNData(t, "maximum", mx, []int{6}, []float64{nan, nan, nan, nan, nan, 2}) + mn := mustArr(t, ok(x.Minimum(y))) + wantNaNData(t, "minimum", mn, []int{6}, []float64{nan, nan, nan, nan, nan, 1}) +}