Example #1
0
func overDetProb(m, n int) (a *cmat.Mat, b, x []complex128, 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 = SolveHerm(cmat.Mul(cmat.H(a), a), cmat.MulVec(cmat.H(a), b))
	if err != nil {
		return nil, nil, nil, err
	}
	return
}
Example #2
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 := cmat.MulVec(a, want)
	got, err := lu.Solve(false, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)

	// Then solve conjugate-transpose system.
	b = cmat.MulVec(cmat.H(a), want)
	got, err = lu.Solve(true, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Example #3
0
func ExampleCholFact_Solve() {
	// A = V' V, with V = [1, 1; 2, 1]
	v := cmat.NewRows([][]complex128{
		{1, 1},
		{2, 1},
	})
	a := cmat.Mul(cmat.H(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 := []complex128{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.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
}
Example #4
0
func underDetProb(m, n int) (a *cmat.Mat, b, x []complex128, 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 := SolveHerm(cmat.Mul(a, cmat.H(a)), b)
	if err != nil {
		return nil, nil, nil, err
	}
	// x <- A' y
	x = cmat.MulVec(cmat.H(a), y)
	return
}
Example #5
0
func TestSolvePosDef(t *testing.T) {
	n := 100
	// Random symmetric positive definite matrix.
	a := randMat(2*n, n)
	a = cmat.Mul(cmat.H(a), a)
	// Random vector.
	want := randVec(n)
	b := cmat.MulVec(a, want)

	got, err := SolvePosDef(a, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Example #6
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 conjugate transpose.
	qr, err := QR(cmat.H(a))
	if err != nil {
		t.Fatal(err)
	}
	got, err := qr.Solve(true, b)
	if err != nil {
		t.Fatal(err)
	}
	testSliceEq(t, want, got)
}
Example #7
0
func TestLDLFact_Solve(t *testing.T) {
	n := 100
	// Random symmetric matrix.
	a := randMat(n, n)
	a = cmat.Plus(a, cmat.H(a))
	// Random vector.
	want := randVec(n)
	b := cmat.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)
}
Example #8
0
func TestCholFact_Solve(t *testing.T) {
	n := 100
	// Random symmetric positive definite matrix.
	a := randMat(2*n, n)
	a = cmat.Mul(cmat.H(a), a)
	// Random vector.
	want := randVec(n)
	b := cmat.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)
}
Example #9
0
func TestCholFact_Inverse(t *testing.T) {
	n := 100
	// Random symmetric positive definite matrix.
	a := randMat(2*n, n)
	a = cmat.Mul(cmat.H(a), a)

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

	b, err := chol.Inv()
	if err != nil {
		t.Fatal(err)
	}
	right := cmat.Mul(a, b)
	testMatEq(t, cmat.I(n), right)
	left := cmat.Mul(a, b)
	testMatEq(t, cmat.I(n), left)
}