mirror of
https://github.com/gonum/gonum.git
synced 2025-10-21 22:29:30 +08:00
testlapack: add randomSlice and equalApproxGeneral
This commit is contained in:
@@ -38,13 +38,22 @@ func nanSlice(n int) []float64 {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// randomSlice allocates a new slice of length n filled with random values.
|
||||||
|
func randomSlice(n int, rnd *rand.Rand) []float64 {
|
||||||
|
s := make([]float64, n)
|
||||||
|
for i := range s {
|
||||||
|
s[i] = rnd.NormFloat64()
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// nanGeneral allocates a new r×c general matrix filled with NaN values.
|
// nanGeneral allocates a new r×c general matrix filled with NaN values.
|
||||||
func nanGeneral(r, c, stride int) blas64.General {
|
func nanGeneral(r, c, stride int) blas64.General {
|
||||||
if r < 0 || c < 0 {
|
if r < 0 || c < 0 {
|
||||||
panic("bad matrix size")
|
panic("bad matrix size")
|
||||||
}
|
}
|
||||||
if r == 0 || c == 0 {
|
if r == 0 || c == 0 {
|
||||||
return blas64.General{}
|
return blas64.General{Stride: max(1, c)}
|
||||||
}
|
}
|
||||||
return blas64.General{
|
return blas64.General{
|
||||||
Rows: r,
|
Rows: r,
|
||||||
@@ -758,6 +767,23 @@ func equalApprox(m, n int, a []float64, lda int, b []float64, tol float64) bool
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// equalApproxGeneral returns whether the general matrices a and b are
|
||||||
|
// approximately equal within given tolerance.
|
||||||
|
func equalApproxGeneral(a, b blas64.General, tol float64) bool {
|
||||||
|
if a.Rows != b.Rows || a.Cols != b.Cols {
|
||||||
|
panic("bad input")
|
||||||
|
}
|
||||||
|
for i := 0; i < a.Rows; i++ {
|
||||||
|
for j := 0; j < a.Cols; j++ {
|
||||||
|
diff := a.Data[i*a.Stride+j] - b.Data[i*b.Stride+j]
|
||||||
|
if math.Abs(diff) > tol {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// equalApproxTriangular returns whether the triangular matrices A and B of
|
// equalApproxTriangular returns whether the triangular matrices A and B of
|
||||||
// order n are approximately equal within given tolerance.
|
// order n are approximately equal within given tolerance.
|
||||||
func equalApproxTriangular(upper bool, n int, a []float64, lda int, b []float64, tol float64) bool {
|
func equalApproxTriangular(upper bool, n int, a []float64, lda int, b []float64, tol float64) bool {
|
||||||
|
Reference in New Issue
Block a user