Exemple #1
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)
}
Exemple #2
0
func TestSolveSquare(t *testing.T) {
	n := 100
	a := randMat(n, n)
	want := randVec(n)
	b := mat.MulVec(a, want)

	got, err := SolveSquare(a, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Exemple #3
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)
}
Exemple #4
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
}
Exemple #5
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
}
Exemple #6
0
func TestLUFact_Solve(t *testing.T) {
	n := 100
	// Random square matrix.
	a := randMat(n, n)
	// Random vector.
	want := randVec(n)
	b := mat.MulVec(a, want)

	lu, err := LU(a)
	if err != nil {
		t.Fatal(err)
	}
	got, err := lu.Solve(false, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Exemple #7
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)
}
Exemple #8
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)
}