Beispiel #1
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)]
}
Beispiel #2
0
func ExampleQRFact_Solve() {
	a := cmat.NewRows([][]complex128{
		{4, 2},
		{1, 1},
		{2, 0},
	})
	b_over := []complex128{6, 7, 4}
	b_under := []complex128{39, 19}

	qr, err := QR(a)
	if err != nil {
		fmt.Println(err)
		return
	}

	x_over, err := qr.Solve(false, b_over)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x_over, 'f', 3))

	x_under, err := qr.Solve(true, b_under)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x_under, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
	// [(8.000+0.000i) (3.000+0.000i) (2.000+0.000i)]
}
Beispiel #3
0
func ExampleSolveHerm() {
	a := cmat.NewRows([][]complex128{
		{1, 2},
		{2, -3},
	})
	// x = [1; 2]
	// b = A x = [5; -4]
	b := []complex128{5, -4}

	x, err := SolveHerm(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
}
Beispiel #4
0
func ExampleSolveSquare() {
	a := cmat.NewRows([][]complex128{
		{-1, 2},
		{3, 1},
	})
	// x = [1; 2]
	// b = A x = [3; 5]
	b := []complex128{3, 5}

	x, err := SolveSquare(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
}
Beispiel #5
0
func ExampleSolve_underdetermined() {
	// Find minimum-norm solution to
	//	x     + z = 6,
	//	    y + z = 9.
	a := cmat.NewRows([][]complex128{
		{1, 0, 1},
		{0, 1, 1},
	})
	b := []complex128{6, 9}

	x, err := Solve(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (4.000+0.000i) (5.000+0.000i)]
}
Beispiel #6
0
func ExampleSolve_overdetermined() {
	// Find minimum-error solution to
	//	x     = 3,
	//	    y = 6,
	//	x + y = 3.
	a := cmat.NewRows([][]complex128{
		{1, 0},
		{0, 1},
		{1, 1},
	})
	b := []complex128{3, 6, 3}

	x, err := Solve(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (4.000+0.000i)]
}
Beispiel #7
0
func ExampleLDLFact_Solve() {
	a := cmat.NewRows([][]complex128{
		{1, 2},
		{2, -3},
	})
	// x = [1; 2]
	// b = A x = [5; -4]
	b := []complex128{5, -4}

	ldl, err := LDL(a)
	if err != nil {
		fmt.Println(err)
		return
	}
	x, err := ldl.Solve(b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
}
Beispiel #8
0
func ExampleLUFact_Solve() {
	a := cmat.NewRows([][]complex128{
		{-1, 2},
		{3, 1},
	})
	// x = [1; 2]
	// b = A x = [3; 5]
	b := []complex128{3, 5}

	lu, err := LU(a)
	if err != nil {
		fmt.Println(err)
		return
	}
	x, err := lu.Solve(false, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(formatSlice(x, 'f', 3))
	// Output:
	// [(1.000+0.000i) (2.000+0.000i)]
}