File: dptcon.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (81 lines) | stat: -rw-r--r-- 2,076 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright ©2023 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package testlapack

import (
	"fmt"
	"math"
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/floats"
	"gonum.org/v1/gonum/lapack"
)

type Dptconer interface {
	Dptcon(n int, d, e []float64, anorm float64, work []float64) (rcond float64)

	Dpttrf(n int, d, e []float64) (ok bool)
	Dpttrs(n, nrhs int, d, e []float64, b []float64, ldb int)
}

func DptconTest(t *testing.T, impl Dptconer) {
	rnd := rand.New(rand.NewSource(1))
	for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 20, 50, 51, 52, 53, 54, 100} {
		dptconTest(t, impl, rnd, n)
	}
}

func dptconTest(t *testing.T, impl Dptconer, rnd *rand.Rand, n int) {
	const tol = 1e-15

	name := fmt.Sprintf("n=%v", n)

	// Generate a random diagonally dominant symmetric tridiagonal matrix A.
	d, e := newRandomSymTridiag(n, rnd)
	aNorm := dlanst(lapack.MaxColumnSum, n, d, e)

	// Compute the Cholesky factorization of A.
	ok := impl.Dpttrf(n, d, e)
	if !ok {
		t.Errorf("%v: bad test matrix, Dpttrf failed", name)
		return
	}

	// Compute the reciprocal of the condition number of A.
	dCopy := make([]float64, len(d))
	copy(dCopy, d)
	eCopy := make([]float64, len(e))
	copy(eCopy, e)
	work := make([]float64, 3*n)
	rcondGot := impl.Dptcon(n, d, e, aNorm, work)

	// Check that Dptcon didn't modify d and e.
	if !floats.Equal(d, dCopy) {
		t.Errorf("%v: unexpected modification of d", name)
	}
	if !floats.Equal(e, eCopy) {
		t.Errorf("%v: unexpected modification of e", name)
	}

	// Compute the norm of A⁻¹.
	aInv, lda := make([]float64, n*n), max(1, n)
	for i := 0; i < n; i++ {
		aInv[i*lda+i] = 1
	}
	impl.Dpttrs(n, n, d, e, aInv, lda)
	aInvNorm := dlange(lapack.MaxColumnSum, n, n, aInv, lda)

	rcondWant := 1.0
	if aNorm > 0 && aInvNorm > 0 {
		rcondWant = 1 / aNorm / aInvNorm
	}

	diff := math.Abs(rcondGot - rcondWant)
	if diff > tol {
		t.Errorf("%v: unexpected value of rcond. got=%v, want=%v (diff=%v)", name, rcondGot, rcondWant, diff)
	}
}