|
| 1 | +package ndarray |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// Sums along a contiguous axis of float32 and complex arrays are pairwise, as |
| 10 | +// NumPy's are (FLOAT/CFLOAT_pairwise_sum, numpy/_core/src/umath/ |
| 11 | +// loops_utils.h.src). The expectations below come from exact arithmetic, not |
| 12 | +// from running the code. |
| 13 | + |
| 14 | +// TestFloat32SumDoesNotSaturate: 2^24 followed by 1000 ones. Added in order |
| 15 | +// in float32, every 2^24 + 1 rounds back to 2^24, so the sum stays 2^24. |
| 16 | +// Pairwise, the ones are summed among themselves first (1000, exact), and |
| 17 | +// 2^24 + 1000 is representable (float32's spacing at 2^24 is 2). |
| 18 | +func TestFloat32SumDoesNotSaturate(t *testing.T) { |
| 19 | + const n = 1000 |
| 20 | + v := make([]float32, n+1) |
| 21 | + v[0] = 1 << 24 |
| 22 | + for i := 1; i <= n; i++ { |
| 23 | + v[i] = 1 |
| 24 | + } |
| 25 | + a := must(FromSlice(v, len(v))) |
| 26 | + if got, want := a.Sum(), float64(1<<24+n); got != want { |
| 27 | + t.Errorf("Sum = %v, want %v (in order it would be %v)", got, want, float64(1<<24)) |
| 28 | + } |
| 29 | + // The same along the last axis of a 2-D array, row by row. |
| 30 | + two := must(FromSlice(append(append([]float32{}, v...), v...), 2, len(v))) |
| 31 | + rows, ok := Data[float32](must(two.SumAxis(1, false))) |
| 32 | + if !ok || len(rows) != 2 { |
| 33 | + t.Fatalf("SumAxis(1) of a 2-row float32 array: %v, %v", rows, ok) |
| 34 | + } |
| 35 | + for i, got := range rows { |
| 36 | + if got != 1<<24+n { |
| 37 | + t.Errorf("row %d: %v, want %v", i, got, 1<<24+n) |
| 38 | + } |
| 39 | + } |
| 40 | + c := make([]complex64, n+1) |
| 41 | + c[0] = complex(1<<24, -(1 << 24)) |
| 42 | + for i := 1; i <= n; i++ { |
| 43 | + c[i] = complex(1, -1) |
| 44 | + } |
| 45 | + got := Item[complex64](must(FromSlice(c, len(c))).SumAll()) |
| 46 | + if want := complex64(complex(1<<24+n, -(1<<24 + n))); got != want { |
| 47 | + t.Errorf("complex64 sum = %v, want %v", got, want) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestFloat32SumError compares the sum of 2^16 float32 values of mixed sign |
| 52 | +// and magnitude with the exact sum (math/big): pairwise must be far closer |
| 53 | +// than a sum in order. |
| 54 | +func TestFloat32SumError(t *testing.T) { |
| 55 | + const n = 1 << 16 |
| 56 | + v := make([]float32, n) |
| 57 | + x := uint32(12345) |
| 58 | + for i := range v { |
| 59 | + x = x*1664525 + 1013904223 // a fixed LCG: reproducible, no seed to drift |
| 60 | + v[i] = float32(int32(x)>>8) / float32(1<<8) * float32(1+int(x%7)) |
| 61 | + } |
| 62 | + exact := new(big.Float).SetPrec(256) |
| 63 | + var inOrder float32 |
| 64 | + for _, f := range v { |
| 65 | + exact.Add(exact, new(big.Float).SetFloat64(float64(f))) |
| 66 | + inOrder += f |
| 67 | + } |
| 68 | + e, _ := exact.Float64() |
| 69 | + got := must(FromSlice(v, n)).Sum() |
| 70 | + errPairwise, errInOrder := math.Abs(got-e), math.Abs(float64(inOrder)-e) |
| 71 | + t.Logf("exact %v: pairwise %v (error %g), in order %v (error %g)", e, got, errPairwise, inOrder, errInOrder) |
| 72 | + if errPairwise*10 > errInOrder { |
| 73 | + t.Errorf("pairwise error %g is not ten times below the in-order error %g", errPairwise, errInOrder) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestProdAlongANonContiguousAxis: a product along axis 0 of a 2-D integer |
| 78 | +// array keeps the row-by-row loop (the reduced axis is not contiguous), and |
| 79 | +// accumulates in int64 as NumPy does for int32. |
| 80 | +func TestProdAlongANonContiguousAxis(t *testing.T) { |
| 81 | + a := must(FromSlice([]int32{1, 2, 3, 4, 5, -6}, 2, 3)) |
| 82 | + p := must(a.ProdAxis(0, false)) |
| 83 | + got, ok := Data[int64](p) |
| 84 | + if !ok || len(got) != 3 || got[0] != 4 || got[1] != 10 || got[2] != -18 { |
| 85 | + t.Errorf("ProdAxis(0) = %v %v, want int64 [4 10 -18]", p.DType(), got) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestPairwiseSumBranches covers each length class of NumPy's scheme and |
| 90 | +// checks it against the exact sum of small integers (exact in float32), and |
| 91 | +// that a sum of only -0 stays -0. |
| 92 | +func TestPairwiseSumBranches(t *testing.T) { |
| 93 | + for _, n := range []int{1, 2, 7, 8, 9, 15, 16, 127, 128, 129, 130, 1000, 4099} { |
| 94 | + v := make([]float32, n) |
| 95 | + c := make([]complex64, n) |
| 96 | + want := 0 |
| 97 | + for i := range v { |
| 98 | + v[i] = float32(i%13 - 6) |
| 99 | + c[i] = complex(float32(i%5), float32(-(i % 3))) |
| 100 | + want += i%13 - 6 |
| 101 | + } |
| 102 | + if got := must(FromSlice(v, n)).Sum(); got != float64(want) { |
| 103 | + t.Errorf("n=%d: float32 sum %v, want %d", n, got, want) |
| 104 | + } |
| 105 | + var wre, wim int |
| 106 | + for i := range c { |
| 107 | + wre += i % 5 |
| 108 | + wim -= i % 3 |
| 109 | + } |
| 110 | + if got := Item[complex64](must(FromSlice(c, n)).SumAll()); got != complex(float32(wre), float32(wim)) { |
| 111 | + t.Errorf("n=%d: complex64 sum %v, want (%d%+di)", n, got, wre, wim) |
| 112 | + } |
| 113 | + } |
| 114 | + negZero := float32(math.Copysign(0, -1)) |
| 115 | + if got := must(FromSlice([]float32{negZero, negZero, negZero}, 3)).Sum(); !math.Signbit(got) { |
| 116 | + t.Errorf("a sum of -0 is %v, want -0", got) |
| 117 | + } |
| 118 | + nz := complex(negZero, negZero) |
| 119 | + if got := Item[complex64](must(FromSlice([]complex64{nz, nz}, 2)).SumAll()); !math.Signbit(float64(real(got))) || !math.Signbit(float64(imag(got))) { |
| 120 | + t.Errorf("a complex sum of -0 is %v, want -0-0i", got) |
| 121 | + } |
| 122 | +} |
0 commit comments