File: dlantr.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (146 lines) | stat: -rw-r--r-- 3,690 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright ©2015 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/blas"
	"gonum.org/v1/gonum/floats"
	"gonum.org/v1/gonum/lapack"
)

type Dlantrer interface {
	Dlanger
	Dlantr(norm lapack.MatrixNorm, uplo blas.Uplo, diag blas.Diag, m, n int, a []float64, lda int, work []float64) float64
}

func DlantrTest(t *testing.T, impl Dlantrer) {
	rnd := rand.New(rand.NewSource(1))
	for _, m := range []int{0, 1, 2, 3, 4, 5, 10} {
		for _, n := range []int{0, 1, 2, 3, 4, 5, 10} {
			for _, uplo := range []blas.Uplo{blas.Lower, blas.Upper} {
				if uplo == blas.Upper && m > n {
					continue
				}
				if uplo == blas.Lower && n > m {
					continue
				}
				for _, diag := range []blas.Diag{blas.NonUnit, blas.Unit} {
					for _, lda := range []int{max(1, n), n + 3} {
						dlantrTest(t, impl, rnd, uplo, diag, m, n, lda)
					}
				}
			}
		}
	}
}

func dlantrTest(t *testing.T, impl Dlantrer, rnd *rand.Rand, uplo blas.Uplo, diag blas.Diag, m, n, lda int) {
	const tol = 1e-14

	// Generate a random triangular matrix. If the matrix has unit diagonal,
	// don't set the diagonal elements to 1.
	a := make([]float64, max(0, (m-1)*lda+n))
	for i := range a {
		a[i] = rnd.NormFloat64()
	}
	rowsum := make([]float64, m)
	colsum := make([]float64, n)
	var frobWant, maxabsWant float64
	if diag == blas.Unit {
		// Account for the unit diagonal.
		for i := 0; i < min(m, n); i++ {
			rowsum[i] = 1
			colsum[i] = 1
		}
		frobWant = float64(min(m, n))
		if min(m, n) > 0 {
			maxabsWant = 1
		}
	}
	if uplo == blas.Upper {
		for i := 0; i < min(m, n); i++ {
			start := i
			if diag == blas.Unit {
				start = i + 1
			}
			for j := start; j < n; j++ {
				aij := 2*rnd.Float64() - 1
				a[i*lda+j] = aij
				rowsum[i] += math.Abs(aij)
				colsum[j] += math.Abs(aij)
				maxabsWant = math.Max(maxabsWant, math.Abs(aij))
				frobWant += aij * aij
			}
		}
	} else {
		for i := 0; i < m; i++ {
			end := i
			if diag == blas.Unit {
				end = i - 1
			}
			for j := 0; j <= min(end, n-1); j++ {
				aij := 2*rnd.Float64() - 1
				a[i*lda+j] = aij
				rowsum[i] += math.Abs(aij)
				colsum[j] += math.Abs(aij)
				maxabsWant = math.Max(maxabsWant, math.Abs(aij))
				frobWant += aij * aij
			}
		}
	}
	frobWant = math.Sqrt(frobWant)
	var maxcolsumWant, maxrowsumWant float64
	if n > 0 {
		maxcolsumWant = floats.Max(colsum)
	}
	if m > 0 {
		maxrowsumWant = floats.Max(rowsum)
	}

	aCopy := make([]float64, len(a))
	copy(aCopy, a)

	for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxColumnSum, lapack.MaxRowSum, lapack.Frobenius} {
		name := fmt.Sprintf("norm=%v,uplo=%v,diag=%v,m=%v,n=%v,lda=%v", string(norm), string(uplo), string(diag), m, n, lda)

		var work []float64
		if norm == lapack.MaxColumnSum {
			work = make([]float64, n)
		}
		normGot := impl.Dlantr(norm, uplo, diag, m, n, a, lda, work)

		if !floats.Equal(a, aCopy) {
			t.Fatalf("%v: unexpected modification of a", name)
		}

		if norm == lapack.MaxAbs {
			// MaxAbs norm involves no floating-point computation,
			// so we expect exact equality here.
			if normGot != maxabsWant {
				t.Errorf("%v: unexpected result; got %v, want %v", name, normGot, maxabsWant)
			}
			continue
		}

		var normWant float64
		switch norm {
		case lapack.MaxColumnSum:
			normWant = maxcolsumWant
		case lapack.MaxRowSum:
			normWant = maxrowsumWant
		case lapack.Frobenius:
			normWant = frobWant
		}
		if math.Abs(normGot-normWant) >= tol {
			t.Errorf("%v: unexpected result; got %v, want %v", name, normGot, normWant)
		}
	}
}