Пример #1
0
func TestGenMatMul(t *testing.T) {
	const (
		m = 3
		k = 4
		n = 5
	)
	alpha, beta := rand.NormFloat64(), rand.NormFloat64()
	a, b, c := randMat(m, k), randMat(k, n), randMat(m, n)
	want := mat.Plus(mat.Scale(alpha, mat.Mul(a, b)), mat.Scale(beta, c))
	// Over-write c with result.
	blas.GenMatMul(alpha, a, b, beta, c)
	checkEqualMat(t, want, c, 1e-9)
}
Пример #2
0
func TestEigSymm(t *testing.T) {
	n := 100
	a := randMat(n, n)
	a = mat.Plus(a, mat.T(a))

	// Take eigen decomposition.
	v, d, err := EigSymm(a)
	if err != nil {
		t.Fatal(err)
	}

	got := mat.Mul(mat.Mul(v, mat.NewDiag(d)), mat.T(v))
	testMatEq(t, a, got)
}
Пример #3
0
func TestLDLFact_Solve(t *testing.T) {
	n := 100
	// Random symmetric matrix.
	a := randMat(n, n)
	a = mat.Plus(a, mat.T(a))
	// Random vector.
	want := randVec(n)
	b := mat.MulVec(a, want)

	ldl, err := LDL(a)
	if err != nil {
		t.Fatal(err)
	}

	got, err := ldl.Solve(b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Пример #4
0
func TestEigSymm_vsTrace(t *testing.T) {
	n := 100
	a := randMat(n, n)
	a = mat.Plus(a, mat.T(a))
	// Compute matrix trace.
	tr := mat.Tr(a)

	// Take eigen decomposition.
	_, d, err := EigSymm(a)
	if err != nil {
		t.Fatal(err)
	}
	// Sum eigenvalues.
	var sum float64
	for _, eig := range d {
		sum += eig
	}

	if !epsEq(tr, sum, eps) {
		t.Errorf("want %.4g, got %.4g", tr, sum)
	}
}