예제 #1
0
func _TestRankSmall(t *testing.T) {
	bM := 5
	bN := 5
	//bP := 5
	Adata := [][]float64{
		[]float64{1.0, 1.0, 1.0, 1.0, 1.0},
		[]float64{2.0, 2.0, 2.0, 2.0, 2.0},
		[]float64{3.0, 3.0, 3.0, 3.0, 3.0},
		[]float64{4.0, 4.0, 4.0, 4.0, 4.0},
		[]float64{5.0, 5.0, 5.0, 5.0, 5.0}}

	A := matrix.FloatMatrixFromTable(Adata, matrix.RowOrder)
	A0 := matrix.FloatMatrixFromTable(Adata, matrix.RowOrder)
	X := matrix.FloatVector([]float64{1.0, 2.0, 3.0, 4.0, 5.0})
	Y := matrix.FloatWithValue(bN, 1, 2.0)

	Ar := A.FloatArray()
	Xr := X.FloatArray()
	Yr := Y.FloatArray()

	blas.GerFloat(X, Y, A0, 1.0)

	DRankMV(Ar, Xr, Yr, 1.0, A.LeadingIndex(), 1, 1, 0, bN, 0, bM, 4, 4)
	ok := A0.AllClose(A)
	t.Logf("A0 == A1: %v\n", ok)
	if !ok {
		t.Logf("blas ger:\n%v\n", A0)
		t.Logf("A1: \n%v\n", A)
	}
}
예제 #2
0
func TestTrsmUnblk(t *testing.T) {
	//bN := 7
	Udata3 := [][]float64{
		[]float64{2.0, 2.0, 2.0},
		[]float64{0.0, 3.0, 3.0},
		[]float64{0.0, 0.0, 4.0}}
	U3 := matrix.FloatMatrixFromTable(Udata3, matrix.RowOrder)
	_ = U3

	Ldata3 := [][]float64{
		[]float64{1.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 0.0},
		[]float64{1.0, 2.0, 3.0}}
	L3 := matrix.FloatMatrixFromTable(Ldata3, matrix.RowOrder)
	_ = L3

	bN := 10
	nP := 7
	nb := 4
	L := matrix.FloatNormalSymmetric(bN, matrix.Lower)

	//t.Logf("-- TRSM-UPPER, TRANS, RIGHT, NON_UNIT --")
	//trsmSolve(t, U3, UPPER|TRANSA|RIGHT, false, 2, 0)
	//t.Logf("-- TRSM-UPPER, TRANS, RIGHT, UNIT --")
	//trsmSolve(t, U3, UPPER|TRANSA|UNIT|RIGHT, false, 2, 0)

	t.Logf("-- UNBLK TRSM-LOWER, TRANS, RIGHT, NON-UNIT --")
	trsmSolve(t, L, LOWER|TRANSA|RIGHT, false, nP, 0)
	t.Logf("-- BLK   TRSM-LOWER, TRANS, RIGHT, NON-UNIT --")
	trsmSolve(t, L, LOWER|TRANSA|RIGHT, false, nP, nb)

}
예제 #3
0
func _TestSyrk2Small(t *testing.T) {
	//bN := 7
	Udata3 := [][]float64{
		[]float64{2.0, 2.0, 2.0},
		[]float64{0.0, 3.0, 3.0},
		[]float64{0.0, 0.0, 4.0}}

	Udata := [][]float64{
		[]float64{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0},
		[]float64{0.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0},
		[]float64{0.0, 0.0, 3.0, 3.0, 3.0, 3.0, 3.0},
		[]float64{0.0, 0.0, 0.0, 4.0, 4.0, 4.0, 4.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 5.0, 5.0, 5.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 0.0, 6.0, 6.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.0}}
	U := matrix.FloatMatrixFromTable(Udata, matrix.RowOrder)
	U3 := matrix.FloatMatrixFromTable(Udata3, matrix.RowOrder)
	_ = U
	_ = U3

	Ldata3 := [][]float64{
		[]float64{1.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 0.0},
		[]float64{1.0, 2.0, 3.0}}

	Ldata := [][]float64{
		[]float64{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}}
	L := matrix.FloatMatrixFromTable(Ldata, matrix.RowOrder)
	L3 := matrix.FloatMatrixFromTable(Ldata3, matrix.RowOrder)
	_ = L
	_ = L3

	Adata := [][]float64{
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}}
	Bdata := [][]float64{
		[]float64{7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0},
		[]float64{7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0}}
	_ = Bdata
	A := matrix.FloatMatrixFromTable(Adata)
	//B := matrix.FloatMatrixFromTable(Bdata);
	B := matrix.FloatNormal(7, 2)
	t.Logf("-- SYR2K UPPER --")
	syrk2Test(t, U.Copy(), A, B, UPPER, 4, 2)
	t.Logf("-- SYR2K LOWER --")
	syrk2Test(t, L.Copy(), A, B, LOWER, 4, 2)
	t.Logf("-- SYR2K UPPER, TRANSA --")
	//t.Logf("A: \n%v\n", A.Transpose())
	syrk2Test(t, U.Copy(), A.Transpose(), B.Transpose(), UPPER|TRANSA, 4, 2)
	t.Logf("-- SYR2K LOWER, TRANS --")
	syrk2Test(t, L.Copy(), A.Transpose(), B.Transpose(), LOWER|TRANSA, 4, 2)
}
예제 #4
0
func _TestBKpivot1U(t *testing.T) {
	Ldata := [][]float64{
		[]float64{7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0},
		[]float64{1.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0},
		[]float64{1.0, 2.0, 5.0, 4.0, 3.0, 5.0, 1.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0},
		[]float64{1.0, 5.0, 3.0, 4.0, 3.0, 2.0, 1.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 2.0, 1.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0}}

	Bdata := [][]float64{
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0}}

	A := matrix.FloatMatrixFromTable(Ldata, matrix.RowOrder)
	X := matrix.FloatMatrixFromTable(Bdata, matrix.RowOrder)
	N := A.Rows()
	B := matrix.FloatZeros(N, 2)
	MultSym(B, A, X, 1.0, 0.0, UPPER|LEFT)
	//t.Logf("initial B:\n%v\n", B)
	//N := 8
	//A := matrix.FloatUniformSymmetric(N)

	W := matrix.FloatWithValue(A.Rows(), 5, 0.0)

	nb := 0
	ipiv := make([]int, N, N)
	L, _ := DecomposeBK(A.Copy(), W, ipiv, UPPER, nb)
	t.Logf("ipiv: %v\n", ipiv)
	t.Logf("L:\n%v\n", L)

	nb = 4
	//ipiv0 := make([]int, N, N)
	//L0, _ := DecomposeBK(A.Copy(), W, ipiv0, LOWER, nb)
	//t.Logf("ipiv: %v\n", ipiv0)
	//t.Logf("L:\n%v\n", L0)
	//B0 := B.Copy()
	//SolveBK(B0, L0, ipiv0, LOWER)
	//t.Logf("B0:\n%v\n", B0)

	ipiv2 := make([]int32, N, N)
	lapack.SytrfFloat(A, ipiv2, linalg.OptUpper)
	t.Logf("ipiv2: %v\n", ipiv2)
	t.Logf("lapack A:\n%v\n", A)
	//lapack.Sytrs(A, B, ipiv2, linalg.OptLower)
	//t.Logf("lapack B:\n%v\n", B)
	//t.Logf("B == B0: %v\n", B.AllClose(B0))
}
예제 #5
0
파일: testsocp.go 프로젝트: hrautila/go.opt
func main() {
	flag.Parse()

	gdata0 := [][]float64{
		[]float64{12., 13., 12.},
		[]float64{6., -3., -12.},
		[]float64{-5., -5., 6.}}

	gdata1 := [][]float64{
		[]float64{3., 3., -1., 1.},
		[]float64{-6., -6., -9., 19.},
		[]float64{10., -2., -2., -3.}}

	c := matrix.FloatVector([]float64{-2.0, 1.0, 5.0})
	g0 := matrix.FloatMatrixFromTable(gdata0, matrix.ColumnOrder)
	g1 := matrix.FloatMatrixFromTable(gdata1, matrix.ColumnOrder)
	Ghq := sets.FloatSetNew("Gq", "hq")
	Ghq.Append("Gq", g0, g1)

	h0 := matrix.FloatVector([]float64{-12.0, -3.0, -2.0})
	h1 := matrix.FloatVector([]float64{27.0, 0.0, 3.0, -42.0})
	Ghq.Append("hq", h0, h1)

	var Gl, hl, A, b *matrix.FloatMatrix = nil, nil, nil, nil
	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}

	sol, err := cvx.Socp(c, Gl, hl, A, b, Ghq, &solopts, nil, nil)
	fmt.Printf("status: %v\n", err)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		for i, m := range sol.Result.At("sq") {
			fmt.Printf("sq[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		for i, m := range sol.Result.At("zq") {
			fmt.Printf("zq[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		sq0 := sol.Result.At("sq")[0]
		sq1 := sol.Result.At("sq")[1]
		zq0 := sol.Result.At("zq")[0]
		zq1 := sol.Result.At("zq")[1]
		check(x, sq0, sq1, zq0, zq1)
	}
}
예제 #6
0
func _TestTrmmUnblkSmall(t *testing.T) {

	U := matrix.FloatMatrixFromTable(upper7, matrix.RowOrder)
	U3 := matrix.FloatMatrixFromTable(upper3, matrix.RowOrder)
	_ = U
	_ = U3

	L := matrix.FloatMatrixFromTable(lower7, matrix.RowOrder)
	L3 := matrix.FloatMatrixFromTable(lower3, matrix.RowOrder)
	_ = L

	t.Logf("-- TRMM-UPPER, NON-UNIT ---")
	fail(t, trmmTest(t, U3, UPPER, 0))
	t.Logf("-- TRMM-UPPER, UNIT ---")
	fail(t, trmmTest(t, U3, UPPER|UNIT, 0))
	t.Logf("-- TRMM-UPPER, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, U3, UPPER|TRANSA, 0))
	t.Logf("-- TRMM-UPPER, UNIT, TRANSA ---")
	fail(t, trmmTest(t, U3, UPPER|TRANSA|UNIT, 0))
	t.Logf("-- TRMM-LOWER, NON-UNIT ---")
	fail(t, trmmTest(t, L3, LOWER, 0))
	t.Logf("-- TRMM-LOWER, UNIT ---")
	fail(t, trmmTest(t, L3, LOWER|UNIT, 0))
	t.Logf("-- TRMM-LOWER, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, L3, LOWER|TRANSA, 0))
	t.Logf("-- TRMM-LOWER, UNIT, TRANSA ---")
	fail(t, trmmTest(t, L3, LOWER|TRANSA|UNIT, 0))

	t.Logf("-- TRMM-UPPER, NON-UNIT, RIGHT ---")
	fail(t, trmmTest(t, U3, UPPER|RIGHT, 0))
	t.Logf("-- TRMM-UPPER, UNIT, RIGHT ---")
	fail(t, trmmTest(t, U3, UPPER|UNIT|RIGHT, 0))

	t.Logf("-- TRMM-LOWER, NON-UNIT, RIGHT ---")
	fail(t, trmmTest(t, L3, LOWER|RIGHT, 0))
	t.Logf("-- TRMM-LOWER, UNIT, RIGHT ---")
	fail(t, trmmTest(t, L3, LOWER|UNIT|RIGHT, 0))

	t.Logf("-- TRMM-UPPER, NON-UNIT, RIGHT, TRANSA ---")
	fail(t, trmmTest(t, U3, UPPER|RIGHT|TRANSA, 0))
	t.Logf("-- TRMM-UPPER, UNIT, RIGHT, TRANSA ---")
	fail(t, trmmTest(t, U3, UPPER|UNIT|RIGHT|TRANSA, 0))

	t.Logf("-- TRMM-LOWER, NON-UNIT, RIGHT, TRANSA ---")
	fail(t, trmmTest(t, L3, LOWER|RIGHT|TRANSA, 0))
	t.Logf("-- TRMM-LOWER, UNIT, RIGHT, TRANSA ---")
	fail(t, trmmTest(t, L3, LOWER|UNIT|RIGHT|TRANSA, 0))

}
예제 #7
0
func TestTrmvSmall(t *testing.T) {

	L := matrix.FloatMatrixFromTable(lower5, matrix.RowOrder)
	U := L.Transpose()
	nb := 0

	t.Logf("-- TRMV, LOWER --")
	trmvTest(t, L, LOWER, nb)
	t.Logf("-- TRMV, UPPER --")
	trmvTest(t, U, UPPER, nb)

	t.Logf("-- TRMV, LOWER, UNIT --")
	trmvTest(t, L, LOWER|UNIT, nb)
	t.Logf("-- TRMV, UPPER, UNIT --")
	trmvTest(t, U, UPPER|UNIT, nb)

	t.Logf("-- TRMV, LOWER, TRANS --")
	trmvTest(t, L, LOWER|TRANS, nb)
	t.Logf("-- TRMV, UPPER, TRANS --")
	trmvTest(t, U, UPPER|TRANS, nb)

	t.Logf("-- TRMV, LOWER, TRANS, UNIT --")
	trmvTest(t, L, LOWER|UNIT|TRANS, nb)
	t.Logf("-- TRMV, UPPER, TRANS, UNIT --")
	trmvTest(t, U, UPPER|UNIT|TRANS, nb)
}
예제 #8
0
func _TestViewUpdate(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)
	N := A.Rows()

	// simple LU decomposition without pivoting
	var A11, a10, a01, a00 matrix.FloatMatrix
	for k := 1; k < N; k++ {
		a00.SubMatrixOf(A, k-1, k-1, 1, 1)
		a01.SubMatrixOf(A, k-1, k, 1, A.Cols()-k)
		a10.SubMatrixOf(A, k, k-1, A.Rows()-k, 1)
		A11.SubMatrixOf(A, k, k)
		//t.Logf("A11: %v  a01: %v\n", A11, a01)
		a10.Scale(1.0 / a00.Float())
		MVRankUpdate(&A11, &a10, &a01, -1.0)
	}

	Ld := TriLU(A.Copy())
	Ud := TriU(A)
	t.Logf("Ld:\n%v\nUd:\n%v\n", Ld, Ud)
	An := matrix.FloatZeros(N, N)
	Mult(An, Ld, Ud, 1.0, 1.0, NOTRANS)
	t.Logf("A == Ld*Ud: %v\n", An.AllClose(An))
}
예제 #9
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))
}
예제 #10
0
func _TestSolveBlockedSmall(t *testing.T) {

	L := matrix.FloatMatrixFromTable(lower5, matrix.RowOrder)
	N := L.Rows()
	nb := 4

	U := L.Transpose()
	X0 := matrix.FloatWithValue(L.Rows(), 1, 1.0)
	X1 := X0.Copy()
	xsum := 0.0
	for i := 0; i < N; i++ {
		xsum += float64(i)
		X0.Add(xsum, i)
		X1.Add(xsum, -(i + 1))
	}

	t.Logf("-- SOLVE LOWER, NON-UNIT ---\n")
	solveMVTest(t, L, X0.Copy(), LOWER, N, nb)
	t.Logf("-- SOLVE UPPER, NON-UNIT ---\n")
	solveMVTest(t, U, X1.Copy(), UPPER, N, nb)

	t.Logf("-- SOLVE LOWER, UNIT ---\n")
	solveMVTest(t, L, X0.Copy(), LOWER|UNIT, N, nb)
	t.Logf("-- SOLVE UPPER, UNIT ---\n")
	solveMVTest(t, U, X1.Copy(), UPPER|UNIT, N, nb)
}
예제 #11
0
func _TestMultSymmLowerSmall(t *testing.T) {
	//bM := 5
	bN := 7
	bP := 7
	Adata := [][]float64{
		[]float64{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}}

	A := matrix.FloatMatrixFromTable(Adata, matrix.RowOrder)
	B := matrix.FloatNormal(bN, bP)
	C0 := matrix.FloatZeros(bN, bP)
	C1 := matrix.FloatZeros(bN, bP)

	Ar := A.FloatArray()
	Br := B.FloatArray()
	C1r := C1.FloatArray()

	blas.SymmFloat(A, B, C0, 1.0, 1.0, linalg.OptLower, linalg.OptRight)

	DMultSymm(C1r, Ar, Br, 1.0, 1.0, LOWER|RIGHT, bN, A.LeadingIndex(), bN,
		bN, 0, bP, 0, bN, 2, 2, 2)
	ok := C0.AllClose(C1)
	t.Logf("C0 == C1: %v\n", ok)
	if !ok {
		t.Logf("A=\n%v\n", A)
		t.Logf("blas: C=A*B\n%v\n", C0)
		t.Logf("C1: C1 = A*X\n%v\n", C1)
	}
}
예제 #12
0
func TestQRSmal(t *testing.T) {
	data := [][]float64{
		[]float64{12.0, -51.0, 4.0},
		[]float64{6.0, 167.0, -68.0},
		[]float64{-4.0, 24.0, -41.0}}

	A := matrix.FloatMatrixFromTable(data, matrix.RowOrder)
	T := matrix.FloatZeros(A.Cols(), A.Cols())
	T0 := T.Copy()

	M := A.Rows()
	//N := A.Cols()
	Tau := matrix.FloatZeros(M, 1)
	X, _ := DecomposeQR(A.Copy(), Tau, nil, 0)
	t.Logf("A\n%v\n", A)
	t.Logf("X\n%v\n", X)
	t.Logf("Tau\n%v\n", Tau)

	Tau0 := matrix.FloatZeros(M, 1)
	lapack.Geqrf(A, Tau0)
	t.Logf("lapack X\n%v\n", A)
	t.Logf("lapack Tau\n%v\n", Tau0)

	unblkQRBlockReflector(X, Tau, T)
	t.Logf("T:\n%v\n", T)

	V := TriLU(X.Copy())
	lapack.LarftFloat(V, Tau, T0)
	t.Logf("T0:\n%v\n", T0)

}
예제 #13
0
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	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}}

	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 := At.Times(A)
	q := At.Times(b).Scale(-1.0)

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

	var solopts cvx.SolverOptions
	solopts.MaxIter = 20
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.ConeQp(P, q, G, h, nil, nil, dims, &solopts, nil)
	if err == nil {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("Optimal\n")
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	}

}
예제 #14
0
func _TestBKSolve(t *testing.T) {
	Ldata := [][]float64{
		[]float64{1.0, 2.0, 3.0, 4.0},
		[]float64{2.0, 2.0, 3.0, 4.0},
		[]float64{3.0, 3.0, 3.0, 4.0},
		[]float64{4.0, 4.0, 4.0, 4.0}}
	Xdata := [][]float64{
		[]float64{1.0, 2.0},
		[]float64{1.0, 2.0},
		[]float64{1.0, 2.0},
		[]float64{1.0, 2.0}}

	A := matrix.FloatMatrixFromTable(Ldata, matrix.RowOrder)
	X := matrix.FloatMatrixFromTable(Xdata, matrix.RowOrder)
	N := A.Rows()
	B := matrix.FloatZeros(N, 2)
	Mult(B, A, X, 1.0, 0.0, NOTRANS)
	S := matrix.FloatZeros(N, 2)
	MultSym(S, A, X, 1.0, 0.0, LOWER|LEFT)
	t.Logf("B:\n%v\n", B)
	t.Logf("S:\n%v\n", S)
	//N := 8
	//A := matrix.FloatUniformSymmetric(N)
	nb := 0

	W := matrix.FloatWithValue(A.Rows(), 5, 0.0)

	ipiv := make([]int, N, N)
	L, _ := DecomposeBK(A.Copy(), W, ipiv, LOWER, nb)
	t.Logf("ipiv: %v\n", ipiv)
	t.Logf("L:\n%v\n", L)
	B0 := B.Copy()
	SolveBK(B0, L, ipiv, LOWER)
	t.Logf("B0:\n%v\n", B0)

	ipiv2 := make([]int32, N, N)
	lapack.Sytrf(A, ipiv2, linalg.OptLower)
	t.Logf("ipiv2: %v\n", ipiv2)
	t.Logf("lapack A:\n%v\n", A)
	lapack.Sytrs(A, B, ipiv2, linalg.OptLower)
	t.Logf("lapack B:\n%v\n", B)
}
예제 #15
0
func _TestTrmmBlkSmall(t *testing.T) {
	//bN := 7
	Udata := [][]float64{
		[]float64{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0},
		[]float64{0.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0},
		[]float64{0.0, 0.0, 3.0, 3.0, 3.0, 3.0, 3.0},
		[]float64{0.0, 0.0, 0.0, 4.0, 4.0, 4.0, 4.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 5.0, 5.0, 5.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 0.0, 6.0, 6.0},
		[]float64{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.0}}
	U := matrix.FloatMatrixFromTable(Udata, matrix.RowOrder)
	_ = U

	Ldata := [][]float64{
		[]float64{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0},
		[]float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}}
	L := matrix.FloatMatrixFromTable(Ldata, matrix.RowOrder)
	_ = L

	t.Logf("-- TRMM-UPPER, NON-UNIT ---")
	fail(t, trmmTest(t, U, UPPER, 2))
	t.Logf("-- TRMM-UPPER, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, U, UPPER|TRANSA, 2))
	t.Logf("-- TRMM-LOWER, NON-UNIT ---")
	fail(t, trmmTest(t, L, LOWER, 2))
	t.Logf("-- TRMM-LOWER, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, L, LOWER|TRANSA, 2))
	t.Logf("-- TRMM-UPPER, RIGHT, NON-UNIT ---")
	fail(t, trmmTest(t, U, UPPER|RIGHT, 2))
	t.Logf("-- TRMM-UPPER, RIGHT, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, U, UPPER|RIGHT|TRANSA, 2))
	t.Logf("-- TRMM-LOWER, RIGHT, NON-UNIT ---")
	fail(t, trmmTest(t, U, LOWER|RIGHT, 2))
	t.Logf("-- TRMM-LOWER, RIGHT, NON-UNIT, TRANSA ---")
	fail(t, trmmTest(t, U, LOWER|RIGHT|TRANSA, 2))
}
예제 #16
0
func main() {

	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	gdata := [][]float64{
		[]float64{16., 7., 24., -8., 8., -1., 0., -1., 0., 0., 7.,
			-5., 1., -5., 1., -7., 1., -7., -4.},
		[]float64{-14., 2., 7., -13., -18., 3., 0., 0., -1., 0., 3.,
			13., -6., 13., 12., -10., -6., -10., -28.},
		[]float64{5., 0., -15., 12., -6., 17., 0., 0., 0., -1., 9.,
			6., -6., 6., -7., -7., -6., -7., -11.}}

	hdata := []float64{-3., 5., 12., -2., -14., -13., 10., 0., 0., 0., 68.,
		-30., -19., -30., 99., 23., -19., 23., 10.}

	c := matrix.FloatVector([]float64{-6., -4., -5.})
	G := matrix.FloatMatrixFromTable(gdata)
	h := matrix.FloatVector(hdata)

	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{2})
	dims.Set("q", []int{4, 4})
	dims.Set("s", []int{3})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.ConeLp(c, G, h, nil, nil, dims, &solopts, nil, nil)
	if err == nil {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("Optimal\n")
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	} else {
		fmt.Printf("status: %s\n", err)
	}
}
예제 #17
0
파일: testgp.go 프로젝트: hrautila/go.opt
func main() {
	flag.Parse()

	aflr := 1000.0
	awall := 100.0
	alpha := 0.5
	beta := 2.0
	gamma := 0.5
	delta := 2.0

	fdata := [][]float64{
		[]float64{-1.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 0.0},
		[]float64{-1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, -1.0},
		[]float64{-1.0, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0, 1.0}}

	gdata := []float64{1.0, 2.0 / awall, 2.0 / awall, 1.0 / aflr, alpha, 1.0 / beta, gamma, 1.0 / delta}

	g := matrix.FloatNew(8, 1, gdata).Log()
	F := matrix.FloatMatrixFromTable(fdata)
	K := []int{1, 2, 1, 1, 1, 1, 1}

	var solopts cvx.SolverOptions
	solopts.MaxIter = 40
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.7f")
	}
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.Gp(K, F, g, nil, nil, nil, nil, &solopts)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		r := matrix.Exp(x)
		h := r.GetIndex(0)
		w := r.GetIndex(1)
		d := r.GetIndex(2)
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("\n h = %f,  w = %f, d = %f.\n", h, w, d)
		check(x)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
예제 #18
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))
}
예제 #19
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))
}
예제 #20
0
func _TestBK2U(t *testing.T) {
	Bdata := [][]float64{
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0},
		[]float64{10.0, 20.0}}

	N := 7

	A0 := matrix.FloatNormal(N, N)
	A := matrix.FloatZeros(N, N)
	// A is symmetric, posivite definite
	Mult(A, A0, A0, 1.0, 1.0, TRANSB)

	X := matrix.FloatMatrixFromTable(Bdata, matrix.RowOrder)
	B := matrix.FloatZeros(N, 2)
	MultSym(B, A, X, 1.0, 0.0, LOWER|LEFT)
	t.Logf("initial B:\n%v\n", B)

	nb := 0
	W := matrix.FloatWithValue(A.Rows(), 5, 1.0)
	A.SetAt(4, 1, A.GetAt(4, 1)+1.0)
	A.SetAt(1, 4, A.GetAt(4, 1))

	ipiv := make([]int, N, N)
	L, _ := DecomposeBK(A.Copy(), W, ipiv, LOWER, nb)
	t.Logf("ipiv: %v\n", ipiv)
	t.Logf("L:\n%v\n", L)

	ipiv0 := make([]int, N, N)
	nb = 4
	L0, _ := DecomposeBK(A.Copy(), W, ipiv0, LOWER, nb)
	t.Logf("ipiv: %v\n", ipiv0)
	t.Logf("L:\n%v\n", L0)
	B0 := B.Copy()
	SolveBK(B0, L0, ipiv0, LOWER)
	t.Logf("B0:\n%v\n", B0)

	ipiv2 := make([]int32, N, N)
	lapack.Sytrf(A, ipiv2, linalg.OptLower)
	t.Logf("ipiv2: %v\n", ipiv2)
	t.Logf("lapack A:\n%v\n", A)
	lapack.Sytrs(A, B, ipiv2, linalg.OptLower)
	t.Logf("lapack B:\n%v\n", B)
	t.Logf("B == B0: %v\n", B.AllClose(B0))
}
예제 #21
0
func TestAcent(t *testing.T) {
	// matrix string in row order presentation
	Adata := [][]float64{
		[]float64{-7.44e-01, 1.11e-01, 1.29e+00, 2.62e+00, -1.82e+00},
		[]float64{4.59e-01, 7.06e-01, 3.16e-01, -1.06e-01, 7.80e-01},
		[]float64{-2.95e-02, -2.22e-01, -2.07e-01, -9.11e-01, -3.92e-01},
		[]float64{-7.75e-01, 1.03e-01, -1.22e+00, -5.74e-01, -3.32e-01},
		[]float64{-1.80e+00, 1.24e+00, -2.61e+00, -9.31e-01, -6.38e-01}}

	bdata := []float64{
		8.38e-01, 9.92e-01, 9.56e-01, 6.14e-01, 6.56e-01,
		3.57e-01, 6.36e-01, 5.08e-01, 8.81e-03, 7.08e-02}

	// these are solution obtained from running cvxopt acent.py with above data
	solData := []float64{-11.59728373909344512, -1.35196389161339936,
		7.21894899350256303, -3.29159917142051528, 4.90454147385329176}

	ntData := []float64{
		1.5163484265903457, 1.2433928210771914, 1.0562922103520955, 0.8816246051011607,
		0.7271128861543598, 0.42725003346248974, 0.0816777301914883, 0.0005458037072843131,
		1.6259980735305693e-10}

	b := matrix.FloatVector(bdata)
	Al := matrix.FloatMatrixFromTable(Adata, matrix.RowOrder)
	Au := matrix.Scale(Al, -1.0)
	A := matrix.FloatZeros(2*Al.Rows(), Al.Cols())
	A.SetSubMatrix(0, 0, Al)
	A.SetSubMatrix(Al.Rows(), 0, Au)

	x, nt, err := acent(A, b, 10)
	if err != nil {
		t.Logf("Acent error: %s", err)
		t.Fail()
	}
	solref := matrix.FloatVector(solData)
	ntref := matrix.FloatVector(ntData)
	soldf := matrix.Minus(x, solref)
	ntdf := matrix.Minus(matrix.FloatVector(nt), ntref)
	solNrm := blas.Nrm2Float(soldf)
	ntNrm := blas.Nrm2Float(ntdf)
	t.Logf("x  [diff=%.2e]:\n%v\n", solNrm, x)
	t.Logf("nt [diff=%.2e]:\n%v\n", ntNrm, nt)

	if solNrm > TOL {
		t.Log("solution deviates too much from expected\n")
		t.Fail()
	}
}
예제 #22
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)))
}
예제 #23
0
파일: gp_test.go 프로젝트: hrautila/cvx
// The small GP of section 9.3 (Geometric programming).
func TestGp(t *testing.T) {

	xref := []float64{1.06032641296944741, 1.75347359157296845, 2.44603683900611868}

	aflr := 1000.0
	awall := 100.0
	alpha := 0.5
	beta := 2.0
	gamma := 0.5
	delta := 2.0

	fdata := [][]float64{
		[]float64{-1.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 0.0},
		[]float64{-1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, -1.0},
		[]float64{-1.0, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0, 1.0}}

	gdata := []float64{1.0, 2.0 / awall, 2.0 / awall, 1.0 / aflr, alpha, 1.0 / beta, gamma, 1.0 / delta}

	g := matrix.FloatNew(8, 1, gdata).Log()
	F := matrix.FloatMatrixFromTable(fdata)
	K := []int{1, 2, 1, 1, 1, 1, 1}

	var solopts SolverOptions
	solopts.MaxIter = 40
	solopts.ShowProgress = false
	solopts.KKTSolverName = "ldl"
	sol, err := Gp(K, F, g, nil, nil, nil, nil, &solopts)
	if sol != nil && sol.Status == Optimal {
		x := sol.Result.At("x")[0]
		r := matrix.Exp(x)
		h := r.GetIndex(0)
		w := r.GetIndex(1)
		d := r.GetIndex(2)
		t.Logf("x=\n%v\n", x.ToString("%.9f"))
		t.Logf("h = %f,  w = %f, d = %f.\n", h, w, d)
		xe, _ := nrmError(matrix.FloatVector(xref), x)
		if xe > TOL {
			t.Logf("x differs [%.3e] from exepted too much.", xe)
			t.Fail()
		}
	} else {
		t.Logf("status: %v\n", err)
		t.Fail()
	}
}
예제 #24
0
func _TestUpdate(t *testing.T) {
	Tdata := [][]float64{
		[]float64{1.37e+00, 3.77e-01},
		[]float64{0.00e+00, 1.39e+00}}

	C1data := [][]float64{
		[]float64{2.54e-01, 9.77e-01, 8.01e-01, 9.08e-02},
		[]float64{2.82e-01, 7.43e-02, 7.30e-01, 4.93e-01}}

	C2data := [][]float64{
		[]float64{7.89e-01, 2.22e-01, 1.83e-01, 9.27e-01},
		[]float64{3.62e-01, 6.81e-01, 4.28e-01, 9.55e-01},
		[]float64{8.81e-01, 2.42e-01, 8.97e-01, 3.48e-01},
		[]float64{2.97e-01, 3.12e-01, 6.83e-01, 6.91e-01},
		[]float64{8.94e-01, 9.33e-01, 9.79e-01, 7.11e-01},
		[]float64{9.75e-02, 7.42e-01, 9.22e-01, 5.64e-01}}

	Y1data := [][]float64{
		[]float64{-1.41e+00, -1.11e+00},
		[]float64{1.46e-02, -7.04e-01}}

	Y2data := [][]float64{
		[]float64{8.19e-02, 2.62e-02},
		[]float64{3.14e-01, -2.57e-02},
		[]float64{5.05e-01, -3.73e-01},
		[]float64{4.11e-02, 2.09e-01},
		[]float64{3.08e-01, -1.34e-01},
		[]float64{3.06e-02, 4.86e-01}}

	Wdata := [][]float64{
		[]float64{1.62e+00, 3.81e-01},
		[]float64{2.28e+00, 1.01e+00},
		[]float64{2.42e+00, 1.84e+00},
		[]float64{1.24e+00, 1.30e+00}}

	T := matrix.FloatMatrixFromTable(Tdata, matrix.RowOrder)
	C1 := matrix.FloatMatrixFromTable(C1data, matrix.RowOrder)
	Y1 := matrix.FloatMatrixFromTable(Y1data, matrix.RowOrder)
	C2 := matrix.FloatMatrixFromTable(C2data, matrix.RowOrder)
	Y2 := matrix.FloatMatrixFromTable(Y2data, matrix.RowOrder)
	W := matrix.FloatMatrixFromTable(Wdata, matrix.RowOrder)
	_, _, _ = C2, Y2, W
	_, _, _ = T, C1, Y1

	C1b := C1.Copy()
	C2b := C2.Copy()
	update(t, Y1, Y2, C1, C2, T, W)
	t.Logf("C1:\n%v\n", C1)
	updateBlas(t, Y1, Y2, C1b, C2b, T, W)
	t.Logf("C1b:\n%v\n", C1b)

}
예제 #25
0
파일: testlp.go 프로젝트: hrautila/go.opt
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	gdata := [][]float64{
		[]float64{2.0, 1.0, -1.0, 0.0},
		[]float64{1.0, 2.0, 0.0, -1.0}}

	c := matrix.FloatVector([]float64{-4.0, -5.0})
	G := matrix.FloatMatrixFromTable(gdata, matrix.ColumnOrder)
	h := matrix.FloatVector([]float64{3.0, 3.0, 0.0, 0.0})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.Lp(c, G, h, nil, nil, &solopts, nil, nil)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
예제 #26
0
파일: testcp.go 프로젝트: hrautila/go.opt
func acenter() *matrix.FloatMatrix {

	F := &acenterProg{3, 1}

	gdata := [][]float64{
		[]float64{0., -1., 0., 0., -21., -11., 0., -11., 10., 8., 0., 8., 5.},
		[]float64{0., 0., -1., 0., 0., 10., 16., 10., -10., -10., 16., -10., 3.},
		[]float64{0., 0., 0., -1., -5., 2., -17., 2., -6., 8., -17., -7., 6.}}

	G := matrix.FloatMatrixFromTable(gdata)
	h := matrix.FloatVector(
		[]float64{1.0, 0.0, 0.0, 0.0, 20., 10., 40., 10., 80., 10., 40., 10., 15.})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 40
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}

	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{0})
	dims.Set("q", []int{4})
	dims.Set("s", []int{3})

	var err error
	var sol *cvx.Solution

	sol, err = cvx.Cp(F, G, h, nil, nil, dims, &solopts)
	if err == nil && sol.Status == cvx.Optimal {
		return sol.Result.At("x")[0]
	} else {
		fmt.Printf("result: %v\n", err)
	}
	return nil
}
예제 #27
0
파일: cp_test.go 프로젝트: hrautila/cvx
// The analytic centering with cone constraints example of section 9.1
// (Problems with nonlinear objectives).
func TestCp(t *testing.T) {

	xref := []float64{0.41132359189354400, 0.55884774432611484, -0.72007090016957931}

	F := &acenterProg{3, 1}

	gdata := [][]float64{
		[]float64{0., -1., 0., 0., -21., -11., 0., -11., 10., 8., 0., 8., 5.},
		[]float64{0., 0., -1., 0., 0., 10., 16., 10., -10., -10., 16., -10., 3.},
		[]float64{0., 0., 0., -1., -5., 2., -17., 2., -6., 8., -17., -7., 6.}}

	G := matrix.FloatMatrixFromTable(gdata)
	h := matrix.FloatVector(
		[]float64{1.0, 0.0, 0.0, 0.0, 20., 10., 40., 10., 80., 10., 40., 10., 15.})

	var solopts SolverOptions
	solopts.MaxIter = 40
	solopts.ShowProgress = false

	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{0})
	dims.Set("q", []int{4})
	dims.Set("s", []int{3})

	sol, err := Cp(F, G, h, nil, nil, dims, &solopts)
	if err == nil && sol.Status == Optimal {
		x := sol.Result.At("x")[0]
		t.Logf("x = \n%v\n", x.ToString("%.9f"))
		xe, _ := nrmError(matrix.FloatVector(xref), x)
		if xe > TOL {
			t.Logf("x differs [%.3e] from exepted too much.", xe)
			t.Fail()
		}
	} else {
		t.Logf("result: %v\n", err)
		t.Fail()
	}
}
예제 #28
0
파일: testqp.go 프로젝트: hrautila/go.opt
func main() {

	Sdata := [][]float64{
		[]float64{4e-2, 6e-3, -4e-3, 0.0},
		[]float64{6e-3, 1e-2, 0.0, 0.0},
		[]float64{-4e-3, 0.0, 2.5e-3, 0.0},
		[]float64{0.0, 0.0, 0.0, 0.0}}

	pbar := matrix.FloatVector([]float64{.12, .10, .07, .03})
	S := matrix.FloatMatrixFromTable(Sdata)
	n := pbar.Rows()
	G := matrix.FloatDiagonal(n, -1.0)
	h := matrix.FloatZeros(n, 1)
	A := matrix.FloatWithValue(1, n, 1.0)
	b := matrix.FloatNew(1, 1, []float64{1.0})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true

	mu := 1.0
	Smu := matrix.Scale(S, mu)
	pbarNeg := matrix.Scale(pbar, -1.0)
	fmt.Printf("Smu=\n%v\n", Smu.String())
	fmt.Printf("-pbar=\n%v\n", pbarNeg.String())

	sol, err := cvx.Qp(Smu, pbarNeg, G, h, A, b, &solopts, nil)

	fmt.Printf("status: %v\n", err)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		ret := blas.DotFloat(x, pbar)
		risk := math.Sqrt(blas.DotFloat(x, S.Times(x)))
		fmt.Printf("ret=%.3f, risk=%.3f\n", ret, risk)
		fmt.Printf("x=\n%v\n", x)
	}
}
예제 #29
0
func _TestBlkQRUT(t *testing.T) {
	data := [][]float64{
		[]float64{12.0, -51.0, 4.0},
		[]float64{6.0, 167.0, -68.0},
		[]float64{-4.0, 24.0, -41.0}}

	A := matrix.FloatMatrixFromTable(data, matrix.RowOrder)

	M := A.Rows()
	N := A.Cols()
	var d matrix.FloatMatrix
	//nb := 0
	T := matrix.FloatZeros(N, N)
	Q := matrix.FloatZeros(M, M)
	X, _ := DecomposeQRUT(A.Copy(), T)
	t.Logf("A\n%v\n", A)
	Y := TriLU(X.Copy())
	R := TriU(X.Copy())
	t.Logf("R\n%v\n", R)
	t.Logf("Y\n%v\n", Y)
	t.Logf("T\n%v\n", T)
	_ = Q
	_ = d
}
예제 #30
0
func TestMultMVTransASmall(t *testing.T) {
	data6 := [][]float64{
		[]float64{-1.59e+00, 6.56e-02, 2.14e-01, 6.79e-01, 2.93e-01, 5.24e-01},
		[]float64{4.28e-01, 1.57e-01, 3.81e-01, 2.19e-01, 2.97e-01, 2.83e-02},
		[]float64{3.02e-01, 9.70e-02, 3.18e-01, 2.03e-01, 7.53e-01, 1.58e-01},
		[]float64{1.99e-01, 3.01e-01, 4.69e-01, 3.61e-01, 2.07e-01, 6.07e-01},
		[]float64{1.93e-01, 5.15e-01, 2.83e-01, 5.71e-01, 8.65e-01, 9.75e-01},
		[]float64{3.13e-01, 8.14e-01, 2.93e-01, 8.62e-01, 6.97e-01, 7.95e-02}}
	data5 := [][]float64{
		[]float64{1.57e-01, 3.81e-01, 2.19e-01, 2.97e-01, 2.83e-02},
		[]float64{9.70e-02, 3.18e-01, 2.03e-01, 7.53e-01, 1.58e-01},
		[]float64{3.01e-01, 4.69e-01, 3.61e-01, 2.07e-01, 6.07e-01},
		[]float64{5.15e-01, 2.83e-01, 5.71e-01, 8.65e-01, 9.75e-01},
		[]float64{8.14e-01, 2.93e-01, 8.62e-01, 6.97e-01, 7.95e-02}}
	data2 := []float64{4.28e-01, 3.02e-01, 1.99e-01, 1.93e-01, 3.13e-01}

	bM := 5
	bN := 4
	nb := 2
	//A := matrix.FloatNormal(bN, bM)
	//X := matrix.FloatWithValue(bN, 1, 1.0)

	A := matrix.FloatMatrixFromTable(data5, matrix.RowOrder)
	X := matrix.FloatNew(5, 1, data2)
	bM = A.Rows()
	bN = A.Cols()
	Ym := matrix.FloatZeros(3, bM)
	Y1 := matrix.FloatZeros(bM, 1)
	Y0 := matrix.FloatZeros(bM, 1)

	Ar := A.FloatArray()
	Xr := X.FloatArray()
	Y1r := Y1.FloatArray()

	blas.GemvFloat(A, X, Y0, 1.0, 1.0, linalg.OptTrans)

	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, 1, A.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok := Y0.AllClose(Y1)
	t.Logf("Y0 == Y1: %v\n", ok)
	if ok || !ok {
		t.Logf("blas: Y=A.T*X\n%v\n", Y0)
		t.Logf("Y1: Y1 = A*X\n%v\n", Y1)
	}

	// zero Y0, Y1
	Y0.Scale(0.0)
	Y1.Scale(0.0)

	// test with matrix view; A is view
	var A0 matrix.FloatMatrix
	A6 := matrix.FloatMatrixFromTable(data6, matrix.RowOrder)
	A0.SubMatrixOf(A6, 1, 1)

	blas.GemvFloat(&A0, X, Y0, 1.0, 1.0, linalg.OptTrans)

	Ar = A0.FloatArray()
	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, 1, A0.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok = Y0.AllClose(Y1)
	t.Logf("lda>rows: Y0 == Y1: %v\n", ok)
	if ok || !ok {
		t.Logf("blas: Y=A.T*X\n%v\n", Y0)
		t.Logf("Y1: Y1 = A*X\n%v\n", Y1)
	}

	// Y is view too.
	Y1.SubMatrixOf(Ym, 0, 0, 1, bM)
	Y1r = Y1.FloatArray()
	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, Y1.LeadingIndex(), A0.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok = Y0.AllClose(Y1.Transpose())
	t.Logf("Y0 == Y1 row: %v\n", ok)
	t.Logf("row Y1: %v\n", Y1)
}