diff --git a/cmplxs/cmplxs.go b/cmplxs/cmplxs.go index 0d6fd923..91a66948 100644 --- a/cmplxs/cmplxs.go +++ b/cmplxs/cmplxs.go @@ -13,11 +13,18 @@ import ( "gonum.org/v1/gonum/internal/asm/c128" ) +const ( + zeroLength = "cmplxs: zero length slice" + shortSpan = "cmplxs: slice length less than 2" + badLength = "cmplxs: slice lengths do not match" + badDstLength = "cmplxs: destination slice length does not match input" +) + // Abs calculates the absolute values of the elements of s, and stores them in dst. -// It panics if the lengths of dst and s do not match. +// It panics if the argument lengths do not match. func Abs(dst []float64, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: length of the slices do not match") + panic(badDstLength) } for i, v := range s { dst[i] = cmplx.Abs(v) @@ -25,22 +32,23 @@ func Abs(dst []float64, s []complex128) { } // Add adds, element-wise, the elements of s and dst, and stores the result in dst. -// It panics if the lengths of dst and s do not match. +// It panics if the argument lengths do not match. func Add(dst, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: length of the slices do not match") + panic(badLength) } c128.AxpyUnitaryTo(dst, 1, s, dst) } // AddTo adds, element-wise, the elements of s and t and -// stores the result in dst. It panics if the lengths of s, t and dst do not match. +// stores the result in dst. +// It panics if the argument lengths do not match. func AddTo(dst, s, t []complex128) []complex128 { if len(s) != len(t) { - panic("cmplxs: length of adders do not match") + panic(badLength) } if len(dst) != len(s) { - panic("cmplxs: length of destination does not match length of adder") + panic(badDstLength) } c128.AxpyUnitaryTo(dst, 1, s, t) return dst @@ -52,22 +60,25 @@ func AddConst(c complex128, dst []complex128) { } // AddScaled performs dst = dst + alpha * s. -// It panics if the lengths of dst and s are not equal. +// It panics if the slice argument lengths do not match. func AddScaled(dst []complex128, alpha complex128, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: length of destination and source to not match") + panic(badLength) } c128.AxpyUnitaryTo(dst, alpha, s, dst) } // AddScaledTo performs dst = y + alpha * s, where alpha is a scalar, // and dst, y and s are all slices. -// It panics if the lengths of dst, y, and s are not equal. +// It panics if the slice argument lengths do not match. // // At the return of the function, dst[i] = y[i] + alpha * s[i] func AddScaledTo(dst, y []complex128, alpha complex128, s []complex128) []complex128 { - if len(dst) != len(s) || len(dst) != len(y) { - panic("cmplxs: lengths of slices do not match") + if len(s) != len(y) { + panic(badLength) + } + if len(dst) != len(y) { + panic(badDstLength) } c128.AxpyUnitaryTo(dst, alpha, s, y) return dst @@ -87,10 +98,13 @@ func Count(f func(complex128) bool, s []complex128) int { // Complex fills each of the elements of dst with the complex number // constructed from the corresponding elements of real and imag. -// A panic will occur if the lengths of arguments do not match. +// It panics if the argument lengths do not match. func Complex(dst []complex128, real, imag []float64) []complex128 { - if len(dst) != len(real) || len(dst) != len(imag) { - panic("cmplxs: length of destination does not match length of the source") + if len(real) != len(imag) { + panic(badLength) + } + if len(dst) != len(real) { + panic(badDstLength) } if len(dst) == 0 { return dst @@ -104,10 +118,10 @@ func Complex(dst []complex128, real, imag []float64) []complex128 { // CumProd finds the cumulative product of elements of s and store it in // place into dst so that // dst[i] = s[i] * s[i-1] * s[i-2] * ... * s[0] -// A panic will occur if the lengths of arguments do not match. +// It panics if the argument lengths do not match. func CumProd(dst, s []complex128) []complex128 { if len(dst) != len(s) { - panic("cmplxs: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -118,10 +132,10 @@ func CumProd(dst, s []complex128) []complex128 { // CumSum finds the cumulative sum of elements of s and stores it in place // into dst so that // dst[i] = s[i] + s[i-1] + s[i-2] + ... + s[0] -// A panic will occur if the lengths of arguments do not match. +// It panics if the argument lengths do not match. func CumSum(dst, s []complex128) []complex128 { if len(dst) != len(s) { - panic("cmplxs: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -130,10 +144,10 @@ func CumSum(dst, s []complex128) []complex128 { } // Distance computes the L-norm of s - t. See Norm for special cases. -// A panic will occur if the lengths of s and t do not match. +// It panics if the slice argument lengths do not match. func Distance(s, t []complex128, L float64) float64 { if len(s) != len(t) { - panic("cmplxs: slice lengths do not match") + panic(badLength) } if len(s) == 0 { return 0 @@ -165,31 +179,34 @@ func Distance(s, t []complex128, L float64) float64 { } // Div performs element-wise division dst / s -// and stores the result in dst. It panics if the -// lengths of s and t are not equal. +// and stores the result in dst. +// It panics if the argument lengths do not match. func Div(dst, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: slice lengths do not match") + panic(badLength) } c128.Div(dst, s) } // DivTo performs element-wise division s / t -// and stores the result in dst. It panics if the -// lengths of s, t, and dst are not equal. +// and stores the result in dst. +// It panics if the argument lengths do not match. func DivTo(dst, s, t []complex128) []complex128 { - if len(s) != len(t) || len(dst) != len(t) { - panic("cmplxs: slice lengths do not match") + if len(s) != len(t) { + panic(badLength) + } + if len(dst) != len(s) { + panic(badDstLength) } return c128.DivTo(dst, s, t) } // Dot computes the dot product of s1 and s2, i.e. // sum_{i = 1}^N conj(s1[i])*s2[i]. -// A panic will occur if lengths of arguments do not match. +// It panics if the argument lengths do not match. func Dot(s1, s2 []complex128) complex128 { if len(s1) != len(s2) { - panic("cmplxs: lengths of the slices do not match") + panic(badLength) } return c128.DotUnitary(s1, s2) } @@ -309,10 +326,10 @@ func HasNaN(s []complex128) bool { } // Imag places the imaginary components of src into dst. -// A panic will occur if the lengths of arguments do not match. +// It panics if the argument lengths do not match. func Imag(dst []float64, src []complex128) []float64 { if len(dst) != len(src) { - panic("cmplxs: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -341,17 +358,18 @@ func LogSpan(dst []complex128, l, u complex128) []complex128 { } // MaxAbs returns the maximum absolute value in the input slice. -// If the slice is empty, MaxAbs will panic. +// It panics if s is zero length. func MaxAbs(s []complex128) complex128 { return s[MaxAbsIdx(s)] } // MaxAbsIdx returns the index of the maximum absolute value in the input slice. // If several entries have the maximum absolute value, the first such index is -// returned. If the slice is empty, MaxAbsIdx will panic. +// returned. +// It panics if s is zero length. func MaxAbsIdx(s []complex128) int { if len(s) == 0 { - panic("cmplxs: zero slice length") + panic(zeroLength) } max := math.NaN() var ind int @@ -368,17 +386,17 @@ func MaxAbsIdx(s []complex128) int { } // MinAbs returns the minimum absolute value in the input slice. -// If the slice is empty, MinAbs will panic. +// It panics if s is zero length. func MinAbs(s []complex128) complex128 { return s[MinAbsIdx(s)] } // MinAbsIdx returns the index of the minimum absolute value in the input slice. If several -// entries have the minimum absolute value, the first such index is returned. If the slice -// is empty, MinAbsIdx will panic. +// entries have the minimum absolute value, the first such index is returned. +// It panics if s is zero length. func MinAbsIdx(s []complex128) int { if len(s) == 0 { - panic("cmplxs: zero slice length") + panic(zeroLength) } min := math.NaN() var ind int @@ -395,11 +413,11 @@ func MinAbsIdx(s []complex128) int { } // Mul performs element-wise multiplication between dst -// and s and stores the result in dst. Panics if the -// lengths of s and t are not equal. +// and s and stores the result in dst. +// It panics if the argument lengths do not match. func Mul(dst, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: slice lengths do not match") + panic(badLength) } for i, val := range s { dst[i] *= val @@ -407,11 +425,14 @@ func Mul(dst, s []complex128) { } // MulTo performs element-wise multiplication between s -// and t and stores the result in dst. Panics if the -// lengths of s, t, and dst are not equal. +// and t and stores the result in dst. +// It panics if the argument lengths do not match. func MulTo(dst, s, t []complex128) []complex128 { - if len(s) != len(t) || len(dst) != len(t) { - panic("cmplxs: slice lengths do not match") + if len(s) != len(t) { + panic(badLength) + } + if len(dst) != len(s) { + panic(badDstLength) } for i, val := range t { dst[i] = val * s[i] @@ -420,12 +441,12 @@ func MulTo(dst, s, t []complex128) []complex128 { } // NearestIdx returns the index of the element in s -// whose value is nearest to v. If several such +// whose value is nearest to v. If several such // elements exist, the lowest index is returned. -// NearestIdx panics if len(s) == 0. +// It panics if s is zero length. func NearestIdx(s []complex128, v complex128) int { if len(s) == 0 { - panic("cmplxs: zero length slice") + panic(zeroLength) } switch { case cmplx.IsNaN(v): @@ -495,10 +516,10 @@ func Prod(s []complex128) complex128 { } // Real places the real components of src into dst. -// A panic will occur if the lengths of arguments do not match. +// It panics if the argument lengths do not match. func Real(dst []float64, src []complex128) []float64 { if len(dst) != len(src) { - panic("cmplxs: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -539,9 +560,10 @@ func Scale(c complex128, dst []complex128) { } // ScaleTo multiplies the elements in s by c and stores the result in dst. +// It panics if the slice argument lengths do not match. func ScaleTo(dst []complex128, c complex128, s []complex128) []complex128 { if len(dst) != len(s) { - panic("cmplxs: lengths of slices do not match") + panic(badDstLength) } if len(dst) > 0 { c128.ScalUnitaryTo(dst, c, s) @@ -552,8 +574,7 @@ func ScaleTo(dst []complex128, c complex128, s []complex128) []complex128 { // Span returns a set of N equally spaced points between l and u, where N // is equal to the length of the destination. The first element of the destination // is l, the final element of the destination is u. -// -// Panics if len(dst) < 2. +// It panics if the length of dst is less than 2. // // Span also returns the mutated slice dst, so that it can be used in range expressions, // like: @@ -562,7 +583,7 @@ func ScaleTo(dst []complex128, c complex128, s []complex128) []complex128 { func Span(dst []complex128, l, u complex128) []complex128 { n := len(dst) if n < 2 { - panic("cmplxs: destination must have length >1") + panic(shortSpan) } // Special cases for Inf and NaN. @@ -605,23 +626,24 @@ func Span(dst []complex128, l, u complex128) []complex128 { return dst } -// Sub subtracts, element-wise, the elements of s from dst. Panics if -// the lengths of dst and s do not match. +// Sub subtracts, element-wise, the elements of s from dst. +// It panics if the argument lengths do not match. func Sub(dst, s []complex128) { if len(dst) != len(s) { - panic("cmplxs: length of the slices do not match") + panic(badLength) } c128.AxpyUnitaryTo(dst, -1, s, dst) } // SubTo subtracts, element-wise, the elements of t from s and -// stores the result in dst. Panics if the lengths of s, t and dst do not match. +// stores the result in dst. +// It panics if the argument lengths do not match. func SubTo(dst, s, t []complex128) []complex128 { if len(s) != len(t) { - panic("cmplxs: length of subtractor and subtractee do not match") + panic(badLength) } if len(dst) != len(s) { - panic("cmplxs: length of destination does not match length of subtractor") + panic(badDstLength) } c128.AxpyUnitaryTo(dst, -1, t, s) return dst diff --git a/floats/floats.go b/floats/floats.go index 60d1901f..ec9220da 100644 --- a/floats/floats.go +++ b/floats/floats.go @@ -13,23 +13,31 @@ import ( "gonum.org/v1/gonum/internal/asm/f64" ) +const ( + zeroLength = "floats: zero length slice" + shortSpan = "floats: slice length less than 2" + badLength = "floats: slice lengths do not match" + badDstLength = "floats: destination slice length does not match input" +) + // Add adds, element-wise, the elements of s and dst, and stores the result in dst. -// It panics if the lengths of dst and s do not match. +// It panics if the argument lengths do not match. func Add(dst, s []float64) { if len(dst) != len(s) { - panic("floats: length of the slices do not match") + panic(badDstLength) } f64.AxpyUnitaryTo(dst, 1, s, dst) } // AddTo adds, element-wise, the elements of s and t and -// stores the result in dst. It panics if the lengths of s, t and dst do not match. +// stores the result in dst. +// It panics if the argument lengths do not match. func AddTo(dst, s, t []float64) []float64 { if len(s) != len(t) { - panic("floats: length of adders do not match") + panic(badLength) } if len(dst) != len(s) { - panic("floats: length of destination does not match length of adder") + panic(badDstLength) } f64.AxpyUnitaryTo(dst, 1, s, t) return dst @@ -41,22 +49,25 @@ func AddConst(c float64, dst []float64) { } // AddScaled performs dst = dst + alpha * s. -// It panics if the lengths of dst and s are not equal. +// It panics if the slice argument lengths do not match. func AddScaled(dst []float64, alpha float64, s []float64) { if len(dst) != len(s) { - panic("floats: length of destination and source to not match") + panic(badLength) } f64.AxpyUnitaryTo(dst, alpha, s, dst) } // AddScaledTo performs dst = y + alpha * s, where alpha is a scalar, // and dst, y and s are all slices. -// It panics if the lengths of dst, y, and s are not equal. +// It panics if the slice argument lengths do not match. // // At the return of the function, dst[i] = y[i] + alpha * s[i] func AddScaledTo(dst, y []float64, alpha float64, s []float64) []float64 { - if len(dst) != len(s) || len(dst) != len(y) { - panic("floats: lengths of slices do not match") + if len(s) != len(y) { + panic(badLength) + } + if len(dst) != len(y) { + panic(badDstLength) } f64.AxpyUnitaryTo(dst, alpha, s, y) return dst @@ -86,10 +97,10 @@ func (a argsort) Swap(i, j int) { // At the conclusion of Argsort, dst will contain the original elements of dst // but sorted in increasing order, and inds will contain the original position // of the elements in the slice such that dst[i] = origDst[inds[i]]. -// It panics if the lengths of dst and inds do not match. +// It panics if the argument lengths do not match. func Argsort(dst []float64, inds []int) { if len(dst) != len(inds) { - panic("floats: length of inds does not match length of slice") + panic(badDstLength) } for i := range dst { inds[i] = i @@ -113,13 +124,13 @@ func Count(f func(float64) bool, s []float64) int { // CumProd finds the cumulative product of the first i elements in // s and puts them in place into the ith element of the -// destination dst. A panic will occur if the lengths of arguments -// do not match. +// destination dst. +// It panics if the argument lengths do not match. // // At the return of the function, dst[i] = s[i] * s[i-1] * s[i-2] * ... func CumProd(dst, s []float64) []float64 { if len(dst) != len(s) { - panic("floats: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -129,13 +140,13 @@ func CumProd(dst, s []float64) []float64 { // CumSum finds the cumulative sum of the first i elements in // s and puts them in place into the ith element of the -// destination dst. A panic will occur if the lengths of arguments -// do not match. +// destination dst. +// It panics if the argument lengths do not match. // // At the return of the function, dst[i] = s[i] + s[i-1] + s[i-2] + ... func CumSum(dst, s []float64) []float64 { if len(dst) != len(s) { - panic("floats: length of destination does not match length of the source") + panic(badDstLength) } if len(dst) == 0 { return dst @@ -144,10 +155,10 @@ func CumSum(dst, s []float64) []float64 { } // Distance computes the L-norm of s - t. See Norm for special cases. -// A panic will occur if the lengths of s and t do not match. +// It panics if the slice argument lengths do not match. func Distance(s, t []float64, L float64) float64 { if len(s) != len(t) { - panic("floats: slice lengths do not match") + panic(badLength) } if len(s) == 0 { return 0 @@ -178,31 +189,34 @@ func Distance(s, t []float64, L float64) float64 { } // Div performs element-wise division dst / s -// and stores the value in dst. It panics if the -// lengths of s and t are not equal. +// and stores the value in dst. +// It panics if the argument lengths do not match. func Div(dst, s []float64) { if len(dst) != len(s) { - panic("floats: slice lengths do not match") + panic(badLength) } f64.Div(dst, s) } // DivTo performs element-wise division s / t -// and stores the value in dst. It panics if the -// lengths of s, t, and dst are not equal. +// and stores the value in dst. +// It panics if the argument lengths do not match. func DivTo(dst, s, t []float64) []float64 { - if len(s) != len(t) || len(dst) != len(t) { - panic("floats: slice lengths do not match") + if len(s) != len(t) { + panic(badLength) + } + if len(dst) != len(s) { + panic(badDstLength) } return f64.DivTo(dst, s, t) } // Dot computes the dot product of s1 and s2, i.e. // sum_{i = 1}^N s1[i]*s2[i]. -// A panic will occur if lengths of arguments do not match. +// It panics if the argument lengths do not match. func Dot(s1, s2 []float64) float64 { if len(s1) != len(s2) { - panic("floats: lengths of the slices do not match") + panic(badLength) } return f64.DotUnitary(s1, s2) } @@ -324,7 +338,7 @@ func HasNaN(s []float64) bool { // LogSpan returns a set of n equally spaced points in log space between, // l and u where N is equal to len(dst). The first element of the // resulting dst will be l and the final element of dst will be u. -// Panics if len(dst) < 2 +// It panics if the length of dst is less than 2. // Note that this call will return NaNs if either l or u are negative, and // will return all zeros if l or u is zero. // Also returns the mutated slice dst, so that it can be used in range, like: @@ -365,11 +379,11 @@ func Max(s []float64) float64 { } // MaxIdx returns the index of the maximum value in the input slice. If several -// entries have the maximum value, the first such index is returned. If the slice -// is empty, MaxIdx will panic. +// entries have the maximum value, the first such index is returned. +// It panics if s is zero length. func MaxIdx(s []float64) int { if len(s) == 0 { - panic("floats: zero slice length") + panic(zeroLength) } max := math.NaN() var ind int @@ -385,17 +399,18 @@ func MaxIdx(s []float64) int { return ind } -// Min returns the minimum value in the input slice. If the slice is empty, Min will panic. +// Min returns the minimum value in the input slice. +// It panics if s is zero length. func Min(s []float64) float64 { return s[MinIdx(s)] } // MinIdx returns the index of the minimum value in the input slice. If several -// entries have the minimum value, the first such index is returned. If the slice -// is empty, MinIdx will panic. +// entries have the minimum value, the first such index is returned. +// It panics if s is zero length. func MinIdx(s []float64) int { if len(s) == 0 { - panic("floats: zero slice length") + panic(zeroLength) } min := math.NaN() var ind int @@ -412,11 +427,11 @@ func MinIdx(s []float64) int { } // Mul performs element-wise multiplication between dst -// and s and stores the value in dst. Panics if the -// lengths of s and t are not equal. +// and s and stores the value in dst. +// It panics if the argument lengths do not match. func Mul(dst, s []float64) { if len(dst) != len(s) { - panic("floats: slice lengths do not match") + panic(badLength) } for i, val := range s { dst[i] *= val @@ -424,11 +439,14 @@ func Mul(dst, s []float64) { } // MulTo performs element-wise multiplication between s -// and t and stores the value in dst. Panics if the -// lengths of s, t, and dst are not equal. +// and t and stores the value in dst. +// It panics if the argument lengths do not match. func MulTo(dst, s, t []float64) []float64 { - if len(s) != len(t) || len(dst) != len(t) { - panic("floats: slice lengths do not match") + if len(s) != len(t) { + panic(badLength) + } + if len(dst) != len(s) { + panic(badDstLength) } for i, val := range t { dst[i] = val * s[i] @@ -437,12 +455,12 @@ func MulTo(dst, s, t []float64) []float64 { } // NearestIdx returns the index of the element in s -// whose value is nearest to v. If several such +// whose value is nearest to v. If several such // elements exist, the lowest index is returned. -// NearestIdx panics if len(s) == 0. +// It panics if s is zero length. func NearestIdx(s []float64, v float64) int { if len(s) == 0 { - panic("floats: zero length slice") + panic(zeroLength) } switch { case math.IsNaN(v): @@ -472,10 +490,10 @@ func NearestIdx(s []float64, v float64) int { // by Span with length n and bounds l and u whose value is closest // to v. That is, NearestIdxForSpan(n, l, u, v) is equivalent to // Nearest(Span(make([]float64, n),l,u),v) without an allocation. -// NearestIdxForSpan panics if n is less than two. +// It panics if n is less than two. func NearestIdxForSpan(n int, l, u float64, v float64) int { - if n <= 1 { - panic("floats: span must have length >1") + if n < 2 { + panic(shortSpan) } if math.IsNaN(v) { return 0 @@ -626,9 +644,10 @@ func Scale(c float64, dst []float64) { } // ScaleTo multiplies the elements in s by c and stores the result in dst. +// It panics if the slice argument lengths do not match. func ScaleTo(dst []float64, c float64, s []float64) []float64 { if len(dst) != len(s) { - panic("floats: lengths of slices do not match") + panic(badDstLength) } if len(dst) > 0 { f64.ScalUnitaryTo(dst, c, s) @@ -639,8 +658,7 @@ func ScaleTo(dst []float64, c float64, s []float64) []float64 { // Span returns a set of N equally spaced points between l and u, where N // is equal to the length of the destination. The first element of the destination // is l, the final element of the destination is u. -// -// Panics if len(dst) < 2. +// It panics if the length of dst is less than 2. // // Span also returns the mutated slice dst, so that it can be used in range expressions, // like: @@ -649,7 +667,7 @@ func ScaleTo(dst []float64, c float64, s []float64) []float64 { func Span(dst []float64, l, u float64) []float64 { n := len(dst) if n < 2 { - panic("floats: destination must have length >1") + panic(shortSpan) } // Special cases for Inf and NaN. @@ -700,23 +718,24 @@ func Span(dst []float64, l, u float64) []float64 { return dst } -// Sub subtracts, element-wise, the elements of s from dst. Panics if -// the lengths of dst and s do not match. +// Sub subtracts, element-wise, the elements of s from dst. +// It panics if the argument lengths do not match. func Sub(dst, s []float64) { if len(dst) != len(s) { - panic("floats: length of the slices do not match") + panic(badLength) } f64.AxpyUnitaryTo(dst, -1, s, dst) } // SubTo subtracts, element-wise, the elements of t from s and -// stores the result in dst. Panics if the lengths of s, t and dst do not match. +// stores the result in dst. +// It panics if the argument lengths do not match. func SubTo(dst, s, t []float64) []float64 { if len(s) != len(t) { - panic("floats: length of subtractor and subtractee do not match") + panic(badLength) } if len(dst) != len(s) { - panic("floats: length of destination does not match length of subtractor") + panic(badDstLength) } f64.AxpyUnitaryTo(dst, -1, t, s) return dst @@ -732,7 +751,7 @@ func Sum(s []float64) float64 { // - s is not sorted func Within(s []float64, v float64) int { if len(s) < 2 { - panic("floats: slice length less than 2") + panic(shortSpan) } if !sort.Float64sAreSorted(s) { panic("floats: input slice not sorted")