Beispiel #1
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)
}
Beispiel #2
0
func ExampleCholFact_Solve() {
	// A = V' V, with V = [1, 1; 2, 1]
	v := mat.NewRows([][]float64{
		{1, 1},
		{2, 1},
	})
	a := mat.Mul(mat.T(v), v)

	// x = [1; 2]
	// b = V' V x
	//   = V' [1, 1; 2, 1] [1; 2]
	//   = [1, 2; 1, 1] [3; 4]
	//   = [11; 7]
	b := []float64{11, 7}

	chol, err := Chol(a)
	if err != nil {
		fmt.Println(err)
		return
	}
	x, err := chol.Solve(b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g", x)
	// Output:
	// [1 2]
}
Beispiel #3
0
func ExampleInvertPosDef() {
	// A = V' V, with V = [1, 1; 2, 1]
	v := mat.NewRows([][]float64{
		{1, 1},
		{2, 1},
	})
	a := mat.Mul(mat.T(v), v)

	b, err := InvertPosDef(a)
	if err != nil {
		fmt.Println(err)
		return
	}
	for j := 0; j < 2; j++ {
		for i := 0; i < 2; i++ {
			if i > 0 {
				fmt.Printf(" ")
			}
			fmt.Printf("%.3g", b.At(i, j))
		}
		fmt.Println()
	}
	// Output:
	// 2 -3
	// -3 5
}
Beispiel #4
0
func TestSVD_vsEig(t *testing.T) {
	m, n := 150, 100
	a := randMat(m, n)
	g := mat.Mul(mat.T(a), a)

	// Take eigen decomposition of Gram matrix.
	_, eigs, err := EigSymm(g)
	if err != nil {
		t.Fatal(err)
	}
	// Sort in descending order.
	sort.Sort(sort.Reverse(sort.Float64Slice(eigs)))
	// Take square root of eigenvalues.
	for i := range eigs {
		// Clip small negative values to zero.
		eigs[i] = math.Sqrt(math.Max(0, eigs[i]))
	}

	// Take singular value decomposition.
	_, svals, _, err := SVD(a)
	if err != nil {
		t.Fatal(err)
	}

	testSliceEq(t, eigs, svals)
}
Beispiel #5
0
func overDetProb(m, n int) (a *mat.Mat, b, x []float64, err error) {
	if m < n {
		panic("expect m >= n")
	}

	a = randMat(m, n)
	b = randVec(m)

	// Compute pseudo-inverse explicitly.
	// y <- (A' A) \ b
	x, err = SolveSymm(mat.Mul(mat.T(a), a), mat.MulVec(mat.T(a), b))
	if err != nil {
		return nil, nil, nil, err
	}
	return
}
Beispiel #6
0
func TestLUFact_Solve_t(t *testing.T) {
	n := 100
	// Random square matrix.
	a := randMat(n, n)
	// Random vector.
	want := randVec(n)

	// Factorize.
	lu, err := LU(a)
	if err != nil {
		t.Fatal(err)
	}

	// Solve un-transposed system.
	b := mat.MulVec(a, want)
	got, err := lu.Solve(false, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)

	// Then solve transposed system.
	b = mat.MulVec(mat.T(a), want)
	got, err = lu.Solve(true, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Beispiel #7
0
func underDetProb(m, n int) (a *mat.Mat, b, x []float64, err error) {
	if m > n {
		panic("expect m <= n")
	}

	a = randMat(m, n)
	b = randVec(m)

	// Compute pseudo-inverse explicitly.
	// y <- (A A') \ b
	y, err := SolveSymm(mat.Mul(a, mat.T(a)), b)
	if err != nil {
		return nil, nil, nil, err
	}
	// x <- A' y
	x = mat.MulVec(mat.T(a), y)
	return
}
Beispiel #8
0
func TestSolvePosDef(t *testing.T) {
	n := 100
	// Random symmetric positive definite matrix.
	a := randMat(2*n, n)
	a = mat.Mul(mat.T(a), a)
	// Random vector.
	want := randVec(n)
	b := mat.MulVec(a, want)

	got, err := SolvePosDef(a, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Beispiel #9
0
// Minimum-norm solution to under-constrained system by QR decomposition.
func TestQRFact_Solve_underdetermined(t *testing.T) {
	m, n := 100, 150
	a, b, want, err := underDetProb(m, n)
	if err != nil {
		t.Fatal(err)
	}

	// Take QR factorization of transpose.
	qr, err := QR(mat.T(a))
	if err != nil {
		t.Fatal(err)
	}
	got, err := qr.Solve(true, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Beispiel #10
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)
}
Beispiel #11
0
func TestCholFact_Solve(t *testing.T) {
	n := 100
	// Random symmetric positive definite matrix.
	a := randMat(2*n, n)
	a = mat.Mul(mat.T(a), a)
	// Random vector.
	want := randVec(n)
	b := mat.MulVec(a, want)

	// Factorize matrix.
	chol, err := Chol(a)
	if err != nil {
		t.Fatal(err)
	}

	got, err := chol.Solve(b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Beispiel #12
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)
	}
}