Exemplo n.º 1
0
func _TestBlkCHOLUpLo(t *testing.T) {
	N := 10
	nb := 4
	L := matrix.FloatUniformSymmetric(N, matrix.Lower)
	A := matrix.Times(L, L.Transpose())
	DecomposeCHOL(A, LOWER, nb)
	Ld := TriL(A)
	ok := L.AllClose(Ld)
	t.Logf("result L == Ld: %v\n", ok)
	if !ok {
		t.Logf("L:\n%v\n", L)
		t.Logf("Ld:\n%v\n", Ld)
	}

	U := matrix.FloatUniformSymmetric(N, matrix.Upper)
	A = matrix.Times(U.Transpose(), U)
	DecomposeCHOL(A, UPPER, nb)
	Ud := TriU(A)
	ok = U.AllClose(Ud)
	t.Logf("result U == Ud: %v\n", ok)

	//lapack.Potrf(A0)
	//t.Logf("lapack result:\n%v\n", A0)
	//t.Logf("A == A0: %v\n", A0.AllClose(A))
}
Exemplo n.º 2
0
func _TestCHOLUpLo(t *testing.T) {
	N := 10
	L := matrix.FloatUniformSymmetric(N, matrix.Lower)
	A := matrix.Times(L, L.Transpose())
	DecomposeCHOL(A, LOWER, 0)
	Ld := TriL(A)
	t.Logf("result L == Ld: %v\n", L.AllClose(Ld))

	U := matrix.FloatUniformSymmetric(N, matrix.Upper)
	A = matrix.Times(U.Transpose(), U)
	DecomposeCHOL(A, UPPER, 0)
	Ud := TriU(A)
	t.Logf("result U == Ud: %v\n", U.AllClose(Ud))
}
Exemplo n.º 3
0
func _TestUnblkLUnoPiv(t *testing.T) {
	N := 6
	L := matrix.FloatUniformSymmetric(N, matrix.Lower)
	U := matrix.FloatUniformSymmetric(N, matrix.Upper)
	// Set L diagonal to 1.0
	L.Diag().SetIndexes(1.0)

	A := matrix.Times(L, U)
	t.Logf("A\n%v\n", A)
	DecomposeBlockSize(0)
	R, _ := DecomposeLUnoPiv(A.Copy(), 0)
	Ld := TriLU(R.Copy())
	Ud := TriU(R)
	t.Logf("A == L*U: %v\n", A.AllClose(matrix.Times(Ld, Ud)))
}
Exemplo n.º 4
0
func _TestLU3x3Piv(t *testing.T) {
	Adata2 := [][]float64{
		[]float64{3.0, 2.0, 2.0},
		[]float64{6.0, 4.0, 1.0},
		[]float64{4.0, 6.0, 3.0},
	}
	A := matrix.FloatMatrixFromTable(Adata2, matrix.RowOrder)
	piv := make([]int, A.Rows())
	piv0 := make([]int32, A.Rows())
	A0 := A.Copy()
	t.Logf("start A\n%v\n", A)
	DecomposeBlockSize(0)
	DecomposeLU(A, piv, 0)
	Ld := TriLU(A.Copy())
	Ud := TriU(A.Copy())
	t.Logf("A\n%v\n", A)
	t.Logf("Ld:\n%v\n", Ld)
	t.Logf("Ud:\n%v\n", Ud)
	t.Logf("piv: %v\n", piv)
	t.Logf("result:\n%v\n", matrix.Times(Ld, Ud))
	//t.Logf("A == L*U: %v\n", A0.AllClose(matrix.Times(Ld, Ud)))
	lapack.Getrf(A0, piv0)
	t.Logf("lapack result: piv0 %v\n%v\n", piv0, A0)
	t.Logf("A == A0: %v\n", A0.AllClose(A))
}
Exemplo n.º 5
0
func _TestLU2x2NoPiv(t *testing.T) {
	Adata2 := [][]float64{
		[]float64{4.0, 3.0},
		[]float64{6.0, 3.0}}

	A := matrix.FloatMatrixFromTable(Adata2, matrix.RowOrder)
	DecomposeBlockSize(0)
	DecomposeLUnoPiv(A, 0)
	t.Logf("A\n%v\n", A)
	Ld := TriLU(A.Copy())
	Ud := TriU(A)
	t.Logf("L*U\n%v\n", matrix.Times(Ld, Ud))
}
Exemplo n.º 6
0
func _TestCHOL3x3(t *testing.T) {
	Ldata2 := [][]float64{
		[]float64{3.0, 0.0, 0.0},
		[]float64{6.0, 4.0, 0.0},
		[]float64{4.0, 6.0, 3.0},
	}
	L := matrix.FloatMatrixFromTable(Ldata2, matrix.RowOrder)
	A := matrix.Times(L, L.Transpose())
	DecomposeBlockSize(0)
	DecomposeCHOL(A, LOWER, 0)
	Ld := TriL(A.Copy())
	t.Logf("Ld:\n%v\n", Ld)
	t.Logf("result L == Ld: %v\n", L.AllClose(Ld))
}
Exemplo n.º 7
0
func _TestLU3x3NoPiv(t *testing.T) {
	Adata2 := [][]float64{
		[]float64{4.0, 2.0, 2.0},
		[]float64{6.0, 4.0, 2.0},
		[]float64{4.0, 6.0, 1.0},
	}

	A := matrix.FloatMatrixFromTable(Adata2, matrix.RowOrder)
	A0 := A.Copy()
	DecomposeBlockSize(0)
	DecomposeLUnoPiv(A, 0)
	t.Logf("A\n%v\n", A)
	Ld := TriLU(A.Copy())
	Ud := TriU(A.Copy())
	t.Logf("A == L*U: %v\n", A0.AllClose(matrix.Times(Ld, Ud)))
}
Exemplo n.º 8
0
func _TestBlkLUPiv(t *testing.T) {
	N := 10
	nb := 4
	L := matrix.FloatUniformSymmetric(N, matrix.Lower)
	U := matrix.FloatUniformSymmetric(N, matrix.Upper)
	// Set L diagonal to 1.0
	L.Diag().SetIndexes(1.0)

	A := matrix.Times(L, U)
	A0 := A.Copy()
	piv := make([]int, N, N)
	DecomposeBlockSize(nb)
	R, _ := DecomposeLU(A.Copy(), piv, 0)
	t.Logf("piv: %v\n", piv)

	piv0 := make([]int32, N, N)
	lapack.Getrf(A0, piv0)
	t.Logf("lapack result: piv0 %v\n", piv0)
	t.Logf("R == A0: %v\n", A0.AllClose(R))
}
Exemplo n.º 9
0
func TestLowerCHOL(t *testing.T) {
	N := 60
	K := 30
	nb := 16
	Z := matrix.FloatUniform(N, N)
	A := matrix.Times(Z, Z.Transpose())
	B := matrix.FloatUniform(N, K)
	X := B.Copy()

	// R = chol(A) = L*L.T
	R, _ := DecomposeCHOL(A.Copy(), LOWER, nb)

	// X = A.-1*B = L.-T*(L.-1*B)
	SolveCHOL(X, R, LOWER)

	// B = B - A*X
	Mult(B, A, X, -1.0, 1.0, NONE)

	nrm := NormP(B, NORM_ONE)
	t.Logf("||B - A*X||_1: %e\n", nrm)
}
Exemplo n.º 10
0
func TestUpperCHOL(t *testing.T) {
	N := 60
	K := 30
	nb := 16
	Z := matrix.FloatUniform(N, N)
	A := matrix.Times(Z, Z.Transpose())
	B := matrix.FloatUniform(N, K)
	X := B.Copy()

	// R = chol(A) = U.T*U
	R, _ := DecomposeCHOL(TriU(A.Copy()), UPPER, nb)

	// X = A.-1*B = U.-1*(U.-T*B)
	SolveCHOL(X, R, UPPER)

	// B = B - A*X
	Mult(B, A, X, -1.0, 1.0, NONE)

	// ||B - A*X||_1
	nrm := NormP(B, NORM_ONE)
	t.Logf("||B - A*X||_1: %e\n", nrm)
}
Exemplo n.º 11
0
func TestConeQp(t *testing.T) {
	adata := [][]float64{
		[]float64{0.3, -0.4, -0.2, -0.4, 1.3},
		[]float64{0.6, 1.2, -1.7, 0.3, -0.3},
		[]float64{-0.3, 0.0, 0.6, -1.2, -2.0}}

	// reference values from cvxopt coneqp.py
	xref := []float64{0.72558318685981904, 0.61806264311119252, 0.30253527966423444}
	sref := []float64{
		0.72558318685981904, 0.61806264311119263,
		0.30253527966423449, 1.00000000000041678,
		-0.72558318686012169, -0.61806264311145032,
		-0.30253527966436067}
	zref := []float64{
		0.00000003332583626, 0.00000005116586239,
		0.00000009993673262, 0.56869648433154019,
		0.41264857754144563, 0.35149286573190930,
		0.17201618570052318}

	A := matrix.FloatMatrixFromTable(adata, matrix.ColumnOrder)
	b := matrix.FloatVector([]float64{1.5, 0.0, -1.2, -0.7, 0.0})

	_, n := A.Size()
	N := n + 1 + n

	h := matrix.FloatZeros(N, 1)
	h.SetIndex(n, 1.0)

	I0 := matrix.FloatDiagonal(n, -1.0)
	I1 := matrix.FloatIdentity(n)
	G, _ := matrix.FloatMatrixStacked(matrix.StackDown, I0, matrix.FloatZeros(1, n), I1)

	At := A.Transpose()
	P := matrix.Times(At, A)
	q := matrix.Times(At, b).Scale(-1.0)

	dims := sets.DSetNew("l", "q", "s")
	dims.Set("l", []int{n})
	dims.Set("q", []int{n + 1})

	var solopts SolverOptions
	solopts.MaxIter = 10
	solopts.ShowProgress = false
	sol, err := ConeQp(P, q, G, h, nil, nil, dims, &solopts, nil)
	if err == nil {
		fail := false
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		t.Logf("Optimal\n")
		t.Logf("x=\n%v\n", x.ToString("%.9f"))
		t.Logf("s=\n%v\n", s.ToString("%.9f"))
		t.Logf("z=\n%v\n", z.ToString("%.9f"))
		xe, _ := nrmError(matrix.FloatVector(xref), x)
		if xe > TOL {
			t.Logf("x differs [%.3e] from exepted too much.", xe)
			fail = true
		}
		se, _ := nrmError(matrix.FloatVector(sref), s)
		if se > TOL {
			t.Logf("s differs [%.3e] from exepted too much.", se)
			fail = true
		}
		ze, _ := nrmError(matrix.FloatVector(zref), z)
		if ze > TOL {
			t.Logf("z differs [%.3e] from exepted too much.", ze)
			fail = true
		}
		if fail {
			t.Fail()
		}
	}

}
Exemplo n.º 12
0
// Computes analytic center of A*x <= b with A m by n of rank n.
// We assume that b > 0 and the feasible set is bounded.
func acent(A, b *matrix.FloatMatrix, niters int) (x *matrix.FloatMatrix, ntdecrs []float64, err error) {

	err = nil
	if niters <= 0 {
		niters = MAXITERS
	}
	ntdecrs = make([]float64, 0, niters)

	if A.Rows() != b.Rows() {
		return nil, nil, errors.New("A.Rows() != b.Rows()")
	}

	m, n := A.Size()
	x = matrix.FloatZeros(n, 1)
	H := matrix.FloatZeros(n, n)
	// Helper m*n matrix
	Dmn := matrix.FloatZeros(m, n)

	for i := 0; i < niters; i++ {

		// Gradient is g = A^T * (1.0/(b - A*x)). d = 1.0/(b - A*x)
		// d is m*1 matrix, g is n*1 matrix
		d := matrix.Minus(b, matrix.Times(A, x)).Inv()
		g := matrix.Times(A.Transpose(), d)

		// Hessian is H = A^T * diag(1./(b-A*x))^2 * A.
		// in the original python code expression d[:,n*[0]] creates
		// a m*n matrix where each column is copy of column 0.
		// We do it here manually.
		for i := 0; i < n; i++ {
			Dmn.SetColumn(i, d)
		}

		// Function mul creates element wise product of matrices.
		Asc := matrix.Mul(Dmn, A)
		blas.SyrkFloat(Asc, H, 1.0, 0.0, linalg.OptTrans)

		// Newton step is v = H^-1 * g.
		v := matrix.Scale(g, -1.0)
		PosvFloat(H, v)

		// Directional derivative and Newton decrement.
		lam := blas.DotFloat(g, v)
		ntdecrs = append(ntdecrs, math.Sqrt(-lam))
		if ntdecrs[len(ntdecrs)-1] < TOL {
			return x, ntdecrs, err
		}

		// Backtracking line search.
		// y = d .* A*v
		y := matrix.Mul(d, matrix.Times(A, v))
		step := 1.0
		for 1-step*y.Max() < 0 {
			step *= BETA
		}

	search:
		for {
			// t = -step*y + 1 [e.g. t = 1 - step*y]
			t := matrix.Scale(y, -step).Add(1.0)

			// ts = sum(log(1-step*y))
			ts := t.Log().Sum()
			if -ts < ALPHA*step*lam {
				break search
			}
			step *= BETA
		}
		v.Scale(step)
		x.Plus(v)
	}
	// no solution !!
	err = errors.New(fmt.Sprintf("Iteration %d exhausted\n", niters))
	return x, ntdecrs, err
}