all: remove redundant bad prefix and make panic strings const

This commit is contained in:
Dan Kortschak
2019-12-02 14:21:23 +10:30
parent 54be3505db
commit 889a9573ff
19 changed files with 42 additions and 54 deletions

View File

@@ -11,7 +11,7 @@ import (
const ( const (
badFact = "mat: use without successful factorization" badFact = "mat: use without successful factorization"
badNoVect = "mat: eigenvectors not computed" noVectors = "mat: eigenvectors not computed"
) )
// EigenSym is a type for creating and manipulating the Eigen decomposition of // EigenSym is a type for creating and manipulating the Eigen decomposition of
@@ -103,7 +103,7 @@ func (e *EigenSym) VectorsTo(dst *Dense) {
panic(badFact) panic(badFact)
} }
if !e.vectorsComputed { if !e.vectorsComputed {
panic(badNoVect) panic(noVectors)
} }
r, c := e.vectors.Dims() r, c := e.vectors.Dims()
if dst.IsEmpty() { if dst.IsEmpty() {
@@ -320,7 +320,7 @@ func (e *Eigen) VectorsTo(dst *CDense) {
panic(badFact) panic(badFact)
} }
if e.kind&EigenRight == 0 { if e.kind&EigenRight == 0 {
panic(badNoVect) panic(noVectors)
} }
if dst.IsEmpty() { if dst.IsEmpty() {
dst.ReuseAs(e.n, e.n) dst.ReuseAs(e.n, e.n)
@@ -346,7 +346,7 @@ func (e *Eigen) LeftVectorsTo(dst *CDense) {
panic(badFact) panic(badFact)
} }
if e.kind&EigenLeft == 0 { if e.kind&EigenLeft == 0 {
panic(badNoVect) panic(noVectors)
} }
if dst.IsEmpty() { if dst.IsEmpty() {
dst.ReuseAs(e.n, e.n) dst.ReuseAs(e.n, e.n)

View File

@@ -15,9 +15,9 @@ See https://github.com/deepmind/torch-cephes/blob/master/LICENSE.txt and
https://lists.debian.org/debian-legal/2004/12/msg00295.html https://lists.debian.org/debian-legal/2004/12/msg00295.html
*/ */
var ( const (
badParamOutOfBounds = "cephes: parameter out of bounds" paramOutOfBounds = "cephes: parameter out of bounds"
badParamFunctionSingularity = "cephes: function singularity" errParamFunctionSingularity = "cephes: function singularity"
) )
const ( const (

View File

@@ -69,7 +69,7 @@ func Igam(a, x float64) float64 {
} }
if x < 0 || a <= 0 { if x < 0 || a <= 0 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
// Asymptotic regime where a ~ x; see [2]. // Asymptotic regime where a ~ x; see [2].
@@ -101,7 +101,7 @@ func IgamC(a, x float64) float64 {
switch { switch {
case x < 0, a <= 0: case x < 0, a <= 0:
panic(badParamOutOfBounds) panic(paramOutOfBounds)
case x == 0: case x == 0:
return 1 return 1
case math.IsInf(x, 0): case math.IsInf(x, 0):

View File

@@ -26,7 +26,7 @@ func IgamI(a, p float64) float64 {
dithresh := 5.0 * machEp dithresh := 5.0 * machEp
if p < 0 || p > 1 || a <= 0 { if p < 0 || p > 1 || a <= 0 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
if p == 0 { if p == 0 {

View File

@@ -24,7 +24,7 @@ const (
// Incbet computes the regularized incomplete beta function. // Incbet computes the regularized incomplete beta function.
func Incbet(aa, bb, xx float64) float64 { func Incbet(aa, bb, xx float64) float64 {
if aa <= 0 || bb <= 0 { if aa <= 0 || bb <= 0 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
if xx <= 0 || xx >= 1 { if xx <= 0 || xx >= 1 {
if xx == 0 { if xx == 0 {
@@ -33,7 +33,7 @@ func Incbet(aa, bb, xx float64) float64 {
if xx == 1 { if xx == 1 {
return 1 return 1
} }
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
var flag int var flag int

View File

@@ -108,13 +108,13 @@ func Ndtri(y0 float64) float64 {
if y0 <= 0.0 { if y0 <= 0.0 {
if y0 < 0 { if y0 < 0 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
return math.Inf(-1) return math.Inf(-1)
} }
if y0 >= 1.0 { if y0 >= 1.0 {
if y0 > 1 { if y0 > 1 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
return math.Inf(1) return math.Inf(1)
} }

View File

@@ -48,15 +48,15 @@ func Zeta(x, q float64) float64 {
} }
if x < 1 { if x < 1 {
panic(badParamOutOfBounds) panic(paramOutOfBounds)
} }
if q <= 0 { if q <= 0 {
if q == math.Floor(q) { if q == math.Floor(q) {
panic(badParamFunctionSingularity) panic(errParamFunctionSingularity)
} }
if x != math.Floor(x) { if x != math.Floor(x) {
panic(badParamOutOfBounds) // Because q^-x not defined panic(paramOutOfBounds) // Because q^-x not defined
} }
} }

View File

@@ -33,9 +33,7 @@ var (
ErrZeroRow = errors.New("lp: A has a row of all zeros") ErrZeroRow = errors.New("lp: A has a row of all zeros")
) )
var ( const badShape = "lp: size mismatch"
badShape = "lp: size mismatch"
)
// TODO(btracey): Should these tolerances be part of a settings struct? // TODO(btracey): Should these tolerances be part of a settings struct?

View File

@@ -75,6 +75,4 @@ func (err ErrGrad) Error() string {
} }
// List of shared panic strings // List of shared panic strings
var ( const badProblem = "optimize: objective function is undefined"
badProblem = "optimize: objective function is undefined"
)

View File

@@ -10,6 +10,4 @@
// significance due to prior use as benchmark cases. // significance due to prior use as benchmark cases.
package functions // import "gonum.org/v1/gonum/optimize/functions" package functions // import "gonum.org/v1/gonum/optimize/functions"
const ( const badInputDim = "functions: wrong input dimension"
badInputDim = "functions: wrong input dimension"
)

View File

@@ -10,10 +10,10 @@ import (
) )
const ( const (
badNegInput = "combin: negative input" errNegInput = "combin: negative input"
badSetSize = "combin: n < k" badSetSize = "combin: n < k"
badInput = "combin: wrong input slice length" badInput = "combin: wrong input slice length"
nonpositiveDimension = "combin: non-positive dimension" errNonpositiveDimension = "combin: non-positive dimension"
) )
// Binomial returns the binomial coefficient of (n,k), also commonly referred to // Binomial returns the binomial coefficient of (n,k), also commonly referred to
@@ -28,7 +28,7 @@ const (
// No check is made for overflow. // No check is made for overflow.
func Binomial(n, k int) int { func Binomial(n, k int) int {
if n < 0 || k < 0 { if n < 0 || k < 0 {
panic(badNegInput) panic(errNegInput)
} }
if n < k { if n < k {
panic(badSetSize) panic(badSetSize)
@@ -61,7 +61,7 @@ func GeneralizedBinomial(n, k float64) float64 {
// See GeneralizedBinomial for more information. // See GeneralizedBinomial for more information.
func LogGeneralizedBinomial(n, k float64) float64 { func LogGeneralizedBinomial(n, k float64) float64 {
if n < 0 || k < 0 { if n < 0 || k < 0 {
panic(badNegInput) panic(errNegInput)
} }
if n < k { if n < k {
panic(badSetSize) panic(badSetSize)
@@ -193,7 +193,7 @@ func nextCombination(s []int, n, k int) {
// [0,n) integers, if n or k are non-negative, or if k is greater than n. // [0,n) integers, if n or k are non-negative, or if k is greater than n.
func CombinationIndex(comb []int, n, k int) int { func CombinationIndex(comb []int, n, k int) int {
if n < 0 || k < 0 { if n < 0 || k < 0 {
panic(badNegInput) panic(errNegInput)
} }
if n < k { if n < k {
panic(badSetSize) panic(badSetSize)
@@ -359,7 +359,7 @@ func IdxFor(sub, dims []int) int {
v := sub[i] v := sub[i]
d := dims[i] d := dims[i]
if d <= 0 { if d <= 0 {
panic(nonpositiveDimension) panic(errNonpositiveDimension)
} }
if v < 0 || v >= d { if v < 0 || v >= d {
panic("combin: invalid subscript") panic("combin: invalid subscript")
@@ -386,7 +386,7 @@ func SubFor(sub []int, idx int, dims []int) []int {
panic(badInput) panic(badInput)
} }
if idx < 0 { if idx < 0 {
panic(badNegInput) panic(errNegInput)
} }
stride := 1 stride := 1
for i := len(dims) - 1; i >= 1; i-- { for i := len(dims) - 1; i >= 1; i-- {
@@ -396,7 +396,7 @@ func SubFor(sub []int, idx int, dims []int) []int {
v := idx / stride v := idx / stride
d := dims[i] d := dims[i]
if d < 0 { if d < 0 {
panic(nonpositiveDimension) panic(errNonpositiveDimension)
} }
if v >= dims[i] { if v >= dims[i] {
panic("combin: index too large") panic("combin: index too large")
@@ -528,7 +528,7 @@ func (p *PermutationGenerator) Permutation(dst []int) []int {
// [0,n) integers, if n or k are non-negative, or if k is greater than n. // [0,n) integers, if n or k are non-negative, or if k is greater than n.
func PermutationIndex(perm []int, n, k int) int { func PermutationIndex(perm []int, n, k int) int {
if n < 0 || k < 0 { if n < 0 || k < 0 {
panic(badNegInput) panic(errNegInput)
} }
if n < k { if n < k {
panic(badSetSize) panic(badSetSize)

View File

@@ -4,4 +4,4 @@
package distmat package distmat
var badDim = "distmat: dimension mismatch" const badDim = "distmat: dimension mismatch"

View File

@@ -4,7 +4,7 @@
package distmv package distmv
var ( const (
badQuantile = "distmv: quantile not between 0 and 1" badQuantile = "distmv: quantile not between 0 and 1"
badReceiver = "distmv: input slice is not nil or the correct length" badReceiver = "distmv: input slice is not nil or the correct length"
badSizeMismatch = "distmv: size mismatch" badSizeMismatch = "distmv: size mismatch"

View File

@@ -15,9 +15,7 @@ import (
"gonum.org/v1/gonum/stat/distuv" "gonum.org/v1/gonum/stat/distuv"
) )
var ( const badInputLength = "distmv: input slice length mismatch"
badInputLength = "distmv: input slice length mismatch"
)
// Normal is a multivariate normal distribution (also known as the multivariate // Normal is a multivariate normal distribution (also known as the multivariate
// Gaussian distribution). Its pdf in k dimensions is given by // Gaussian distribution). Its pdf in k dimensions is given by

View File

@@ -10,11 +10,11 @@ type Parameter struct {
Value float64 Value float64
} }
var ( const (
badPercentile = "distuv: percentile out of bounds" badPercentile = "distuv: percentile out of bounds"
badLength = "distuv: slice length mismatch" badLength = "distuv: slice length mismatch"
badSuffStat = "distuv: wrong suffStat length" badSuffStat = "distuv: wrong suffStat length"
badNoSamples = "distuv: must have at least one sample" errNoSamples = "distuv: must have at least one sample"
) )
const ( const (

View File

@@ -50,7 +50,7 @@ func (l *Laplace) Fit(samples, weights []float64) {
} }
if len(samples) == 0 { if len(samples) == 0 {
panic(badNoSamples) panic(errNoSamples)
} }
if len(samples) == 1 { if len(samples) == 1 {
l.Mu = samples[0] l.Mu = samples[0]

View File

@@ -205,7 +205,7 @@ func (p *ProposalNormal) ConditionalRand(x, y []float64) []float64 {
x = make([]float64, p.normal.Dim()) x = make([]float64, p.normal.Dim())
} }
if len(x) != len(y) { if len(x) != len(y) {
panic(badLengthMismatch) panic(errLengthMismatch)
} }
p.normal.SetMean(y) p.normal.SetMean(y)
p.normal.Rand(x) p.normal.Rand(x)

View File

@@ -14,9 +14,7 @@ import (
"gonum.org/v1/gonum/stat/distmv" "gonum.org/v1/gonum/stat/distmv"
) )
var ( const errLengthMismatch = "samplemv: slice length mismatch"
badLengthMismatch = "samplemv: slice length mismatch"
)
var ( var (
_ Sampler = LatinHypercube{} _ Sampler = LatinHypercube{}
@@ -62,7 +60,7 @@ type SampleUniformWeighted struct {
func (w SampleUniformWeighted) SampleWeighted(batch *mat.Dense, weights []float64) { func (w SampleUniformWeighted) SampleWeighted(batch *mat.Dense, weights []float64) {
r, _ := batch.Dims() r, _ := batch.Dims()
if r != len(weights) { if r != len(weights) {
panic(badLengthMismatch) panic(errLengthMismatch)
} }
w.Sample(batch) w.Sample(batch)
for i := range weights { for i := range weights {
@@ -143,7 +141,7 @@ func (l Importance) SampleWeighted(batch *mat.Dense, weights []float64) {
func importance(batch *mat.Dense, weights []float64, target distmv.LogProber, proposal distmv.RandLogProber) { func importance(batch *mat.Dense, weights []float64, target distmv.LogProber, proposal distmv.RandLogProber) {
r, _ := batch.Dims() r, _ := batch.Dims()
if r != len(weights) { if r != len(weights) {
panic(badLengthMismatch) panic(errLengthMismatch)
} }
for i := 0; i < r; i++ { for i := 0; i < r; i++ {
v := batch.RawRowView(i) v := batch.RawRowView(i)

View File

@@ -13,9 +13,7 @@ import (
"gonum.org/v1/gonum/stat/distuv" "gonum.org/v1/gonum/stat/distuv"
) )
var ( const badLengthMismatch = "sample: slice length mismatch"
badLengthMismatch = "sample: slice length mismatch"
)
var ( var (
_ Sampler = LatinHypercube{} _ Sampler = LatinHypercube{}