Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions internal/kernels/kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -302,16 +317,17 @@ 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
block := o * axisLen * inner
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
}
}
Expand All @@ -321,16 +337,16 @@ 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
block := o * axisLen * inner
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
}
}
Expand Down
91 changes: 91 additions & 0 deletions nan_test.go
Original file line number Diff line number Diff line change
@@ -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})
}
Loading