gonum.org/v1/gonum@v0.15.1-0.20240517103525-f853624cb1bb/lapack/testlapack/dptsv.go (about) 1 // Copyright ©2023 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package testlapack 6 7 import ( 8 "fmt" 9 "testing" 10 11 "golang.org/x/exp/rand" 12 ) 13 14 type Dptsver interface { 15 Dptsv(n, nrhs int, d, e []float64, b []float64, ldb int) (ok bool) 16 } 17 18 func DptsvTest(t *testing.T, impl Dptsver) { 19 rnd := rand.New(rand.NewSource(1)) 20 for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 20, 50, 51, 52, 53, 54, 100} { 21 for _, nrhs := range []int{0, 1, 2, 3, 4, 5, 10, 20, 50} { 22 for _, ldb := range []int{max(1, nrhs), nrhs + 3} { 23 dptsvTest(t, impl, rnd, n, nrhs, ldb) 24 } 25 } 26 } 27 } 28 29 func dptsvTest(t *testing.T, impl Dptsver, rnd *rand.Rand, n, nrhs, ldb int) { 30 const tol = 1e-15 31 32 name := fmt.Sprintf("n=%v", n) 33 34 // Generate a random diagonally dominant symmetric tridiagonal matrix A. 35 d, e := newRandomSymTridiag(n, rnd) 36 37 // Generate a random solution matrix X. 38 xWant := randomGeneral(n, nrhs, ldb, rnd) 39 40 // Compute the right-hand side. 41 b := zeros(n, nrhs, ldb) 42 dstmm(n, nrhs, d, e, xWant.Data, xWant.Stride, b.Data, b.Stride) 43 44 // Solve A*X=B. 45 ok := impl.Dptsv(n, nrhs, d, e, b.Data, b.Stride) 46 if !ok { 47 t.Errorf("%v: Dptsv failed", name) 48 return 49 } 50 51 resid := dpttrsResidual(b, xWant) 52 if resid > tol { 53 t.Errorf("%v: unexpected solution: |diff| = %v, want <= %v", name, resid, tol) 54 } 55 }