func testInvCDF(t *testing.T, dist Dist, bounded bool) { inv := InvCDF(dist) name := fmt.Sprintf("InvCDF(%+v)", dist) cdfName := fmt.Sprintf("CDF(%+v)", dist) // Test bounds. vals := map[float64]float64{-0.01: nan, 1.01: nan} if !bounded { vals[0] = -inf vals[1] = inf } testFunc(t, name, inv, vals) if bounded { lo, hi := inv(0), inv(1) vals := map[float64]float64{ lo - 0.01: 0, lo: 0, hi: 1, hi + 0.01: 1, } testFunc(t, cdfName, dist.CDF, vals) if got := dist.CDF(lo + 0.01); !(got > 0) { t.Errorf("%s(0)=%v, but %s(%v)=0", name, lo, cdfName, lo+0.01) } if got := dist.CDF(hi - 0.01); !(got < 1) { t.Errorf("%s(1)=%v, but %s(%v)=1", name, hi, cdfName, hi-0.01) } } // Test points between. vals = map[float64]float64{} for _, p := range vec.Linspace(0, 1, 11) { if p == 0 || p == 1 { continue } x := inv(p) vals[x] = x } testFunc(t, fmt.Sprintf("InvCDF(CDF(%+v))", dist), func(x float64) float64 { return inv(dist.CDF(x)) }, vals) }
func (s Linear) Ticks(n int) (major, minor []float64) { if s.Min == s.Max { return []float64{s.Min}, []float64{} } else if s.Min > s.Max { s.Min, s.Max = s.Max, s.Min } // nticksAtLevel returns the number of ticks in [s.Min, s.Max] // at the given level. nticksAtLevel := func(level int) int { firstN, lastN, _ := s.spacingAtLevel(level, false) return int(lastN - firstN + 1) } level := autoScale(n, nticksAtLevel, s.guessLevel()) ticksAtLevel := func(level int) []float64 { firstN, lastN, spacing := s.spacingAtLevel(level, false) n := int(lastN - firstN + 1) return vec.Linspace(firstN*spacing, lastN*spacing, n) } return ticksAtLevel(level), ticksAtLevel(level - 1) }
func TestLinearTicks(t *testing.T) { l := Linear{Min: 0, Max: 100} major, minor := l.Ticks(5) wmajor, wminor := vec.Linspace(0, 100, 3), vec.Linspace(0, 100, 11) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } major, minor = l.Ticks(2) wmajor, wminor = vec.Linspace(0, 100, 2), vec.Linspace(0, 100, 3) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(2) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l.Nice(2) major, minor = l.Ticks(2) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(2) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l = Linear{Min: 15.4, Max: 16.6} major, minor = l.Ticks(5) wmajor, wminor = vec.Linspace(15.5, 16.5, 3), vec.Linspace(15.4, 16.6, 13) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l.Nice(5) major, minor = l.Ticks(5) wmajor, wminor = vec.Linspace(15, 17, 5), vec.Linspace(15, 17, 21) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test negative tick levels. l = Linear{Min: 9.9989, Max: 10} major, minor = l.Ticks(2) wmajor, wminor = vec.Linspace(9.999, 10, 2), vec.Linspace(9.999, 10, 3) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(2) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l.Nice(2) major, minor = l.Ticks(2) wmajor, wminor = vec.Linspace(9.995, 10, 2), vec.Linspace(9.995, 10, 6) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(2) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test non-default bases. l = Linear{Min: 2, Max: 9, Base: 2} major, minor = l.Ticks(5) wmajor, wminor = vec.Linspace(2, 8, 4), vec.Linspace(2, 9, 8) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l.Nice(5) major, minor = l.Ticks(5) wmajor, wminor = vec.Linspace(2, 10, 5), vec.Linspace(2, 10, 9) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } }
func TestLogTicks(t *testing.T) { // Test the obvious. l, _ := NewLog(1, 10, 10) major, minor := l.Ticks(5) wmajor, wminor := vec.Logspace(0, 1, 2, 10), vec.Linspace(1, 10, 10) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test two orders of magnitude. l, _ = NewLog(1, 100, 10) major, minor = l.Ticks(5) wmajor, wminor = vec.Logspace(0, 2, 3, 10), vec.Concat(vec.Linspace(1, 9, 9), vec.Linspace(10, 100, 10)) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test many orders of magnitude (higher tick levels). l, _ = NewLog(1, 1e8, 10) major, minor = l.Ticks(5) wmajor, wminor = vec.Logspace(0, 4, 5, 100), vec.Logspace(0, 8, 9, 10) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } major, minor = l.Ticks(4) wmajor, wminor = vec.Logspace(0, 2, 3, 10000), vec.Logspace(0, 4, 5, 100) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test minor ticks outside major ticks. l, _ = NewLog(0.91, 200, 10) major, minor = l.Ticks(5) wmajor, wminor = vec.Logspace(0, 2, 3, 10), vec.Concat(vec.Linspace(1, 9, 9), vec.Linspace(10, 100, 10), []float64{200}) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test nicing. l.Nice(5) major, minor = l.Ticks(5) wmajor, wminor = vec.Logspace(-1, 3, 5, 10), vec.Concat(vec.Linspace(0.1, 0.9, 9), vec.Linspace(1, 9, 9), vec.Linspace(10, 90, 9), vec.Linspace(100, 1000, 10)) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test negative ticks. neg := vec.Vectorize(func(x float64) float64 { return -x }) l, _ = NewLog(-1, -100, 10) major, minor = l.Ticks(5) wmajor, wminor = neg(vec.Logspace(2, 0, 3, 10)), neg(vec.Concat(vec.Linspace(100, 10, 10), vec.Linspace(9, 1, 9))) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } major, minor = l.Ticks(2) wmajor, wminor = neg(vec.Logspace(1, 0, 2, 100)), neg(vec.Logspace(2, 0, 3, 10)) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } l.Nice(5) major, minor = l.Ticks(5) wmajor, wminor = neg(vec.Logspace(2, 0, 3, 10)), neg(vec.Concat(vec.Linspace(100, 10, 10), vec.Linspace(9, 1, 9))) if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } // Test Min==Max. l, _ = NewLog(5, 5, 10) major, minor = l.Ticks(5) wmajor, wminor = []float64{5}, []float64{} if !ticksEq(major, wmajor, minor, wminor) { t.Errorf("%v.Ticks(5) = %v, %v; want %v, %v", l, major, minor, wmajor, wminor) } }