Exemple #1
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]
}
Exemple #2
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
}
Exemple #3
0
func ExampleQRFact_Solve() {
	a := mat.NewRows([][]float64{
		{4, 2},
		{1, 1},
		{2, 0},
	})
	b_over := []float64{6, 7, 4}
	b_under := []float64{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.Printf("%.6g\n", x_over)

	x_under, err := qr.Solve(true, b_under)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g\n", x_under)
	// Output:
	// [1 2]
	// [8 3 2]
}
Exemple #4
0
// Find minimum-norm solution to
//	x     + z = 6,
//	    y + z = 9.
func ExampleSolve_underdetermined() {
	a := mat.NewRows([][]float64{
		{1, 0, 1},
		{0, 1, 1},
	})
	b := []float64{6, 9}

	x, err := Solve(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g\n", x)
	// Output:
	// [1 4 5]
}
Exemple #5
0
func ExampleEigSymm() {
	a := mat.NewRows([][]float64{
		{7, -2, 0},
		{-2, 6, -2},
		{0, -2, 5},
	})

	_, d, err := EigSymm(a)
	if err != nil {
		fmt.Println(err)
		return
	}
	sort.Sort(sort.Float64Slice(d))
	fmt.Printf("%.6g\n", d)
	// Output:
	// [3 6 9]
}
Exemple #6
0
func ExampleSolveSymm() {
	a := mat.NewRows([][]float64{
		{1, 2},
		{2, -3},
	})
	// x = [1; 2]
	// b = A x = [5; -4]
	b := []float64{5, -4}

	x, err := SolveSymm(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g", x)
	// Output:
	// [1 2]
}
Exemple #7
0
func ExampleSolveSquare() {
	a := mat.NewRows([][]float64{
		{-1, 2},
		{3, 1},
	})
	// x = [1; 2]
	// b = A x = [3; 5]
	b := []float64{3, 5}

	x, err := SolveSquare(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g", x)
	// Output:
	// [1 2]
}
Exemple #8
0
func ExampleSolveFullRank_underdetermined() {
	// Find minimum-norm solution to
	//	x     + z = 6,
	//	    y + z = 9.
	a := mat.NewRows([][]float64{
		{1, 0, 1},
		{0, 1, 1},
	})
	b := []float64{6, 9}

	x, err := SolveFullRank(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g", x)
	// Output:
	// [1 4 5]
}
Exemple #9
0
func ExampleSolveFullRank_overdetermined() {
	// Find minimum-error solution to
	//	x     = 3,
	//	    y = 6,
	//	x + y = 3.
	a := mat.NewRows([][]float64{
		{1, 0},
		{0, 1},
		{1, 1},
	})
	b := []float64{3, 6, 3}

	x, err := SolveFullRank(a, b)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%.6g", x)
	// Output:
	// [1 4]
}
Exemple #10
0
func ExampleLDLFact_Solve() {
	a := mat.NewRows([][]float64{
		{1, 2},
		{2, -3},
	})
	// x = [1; 2]
	// b = A x = [5; -4]
	b := []float64{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.Printf("%.6g", x)
	// Output:
	// [1 2]
}
Exemple #11
0
func ExampleLUFact_Solve() {
	a := mat.NewRows([][]float64{
		{-1, 2},
		{3, 1},
	})
	// x = [1; 2]
	// b = A x = [3; 5]
	b := []float64{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.Printf("%.6g", x)
	// Output:
	// [1 2]
}