File: matgen_test.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 (81 lines) | stat: -rw-r--r-- 2,011 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 ©2017 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 (
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/blas/blas64"
)

func TestDlagsy(t *testing.T) {
	const tol = 1e-14
	rnd := rand.New(rand.NewSource(1))
	for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 50} {
		for _, lda := range []int{0, 2*n + 1} {
			if lda == 0 {
				lda = max(1, n)
			}
			// D is the identity matrix I.
			d := make([]float64, n)
			for i := range d {
				d[i] = 1
			}
			// Allocate an n×n symmetric matrix A and fill it with NaNs.
			a := nanSlice(n * lda)
			work := make([]float64, 2*n)
			// Compute A = U * D * Uᵀ where U is a random orthogonal matrix.
			Dlagsy(n, 0, d, a, lda, rnd, work)
			// A should be the identity matrix because
			//  A = U * D * Uᵀ = U * I * Uᵀ = U * Uᵀ = I.
			dist := distFromIdentity(n, a, lda)
			if dist > tol {
				t.Errorf("Case n=%v,lda=%v: |A-I|=%v is too large", n, lda, dist)
			}
		}
	}
}

func TestDlagge(t *testing.T) {
	const tol = 1e-14
	rnd := rand.New(rand.NewSource(1))
	for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 50} {
		for _, lda := range []int{0, 2*n + 1} {
			if lda == 0 {
				lda = max(1, n)
			}
			d := make([]float64, n)
			for i := range d {
				d[i] = 1
			}
			a := blas64.General{
				Rows:   n,
				Cols:   n,
				Stride: lda,
				Data:   nanSlice(n * lda),
			}
			work := make([]float64, a.Rows+a.Cols)

			Dlagge(a.Rows, a.Cols, 0, 0, d, a.Data, a.Stride, rnd, work)

			if resid := residualOrthogonal(a, false); resid > tol {
				t.Errorf("Case n=%v,lda=%v: unexpected result", n, lda)
			}
		}
	}
}

func TestRandomOrthogonal(t *testing.T) {
	const tol = 1e-14
	rnd := rand.New(rand.NewSource(1))
	for n := 1; n <= 20; n++ {
		q := randomOrthogonal(n, rnd)
		if resid := residualOrthogonal(q, false); resid > tol {
			t.Errorf("Case n=%v: Q not orthogonal; resid=%v, want<=%v", n, resid, tol)
		}
	}
}