File: eigen_example_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 (65 lines) | stat: -rw-r--r-- 1,281 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
// Copyright ©2018 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 mat_test

import (
	"fmt"
	"log"

	"gonum.org/v1/gonum/mat"
)

func ExampleEigenSym() {
	a := mat.NewSymDense(2, []float64{
		7, 0.5,
		0.5, 1,
	})
	fmt.Printf("A = %v\n\n", mat.Formatted(a, mat.Prefix("    ")))

	var eigsym mat.EigenSym
	ok := eigsym.Factorize(a, true)
	if !ok {
		log.Fatal("Symmetric eigendecomposition failed")
	}
	fmt.Printf("Eigenvalues of A:\n%1.3f\n\n", eigsym.Values(nil))

	var ev mat.Dense
	eigsym.VectorsTo(&ev)
	fmt.Printf("Eigenvectors of A:\n%1.3f\n\n", mat.Formatted(&ev))

	// Output:
	// A = ⎡  7  0.5⎤
	//     ⎣0.5    1⎦
	//
	// Eigenvalues of A:
	// [0.959 7.041]
	//
	// Eigenvectors of A:
	// ⎡ 0.082  -0.997⎤
	// ⎣-0.997  -0.082⎦
	//
}

func ExampleEigen() {
	a := mat.NewDense(2, 2, []float64{
		1, -1,
		1, 1,
	})
	fmt.Printf("A = %v\n\n", mat.Formatted(a, mat.Prefix("    ")))

	var eig mat.Eigen
	ok := eig.Factorize(a, mat.EigenLeft)
	if !ok {
		log.Fatal("Eigendecomposition failed")
	}
	fmt.Printf("Eigenvalues of A:\n%v\n", eig.Values(nil))

	// Output:
	// A = ⎡ 1  -1⎤
	//     ⎣ 1   1⎦
	//
	// Eigenvalues of A:
	// [(1+1i) (1-1i)]
}