Exemple #1
0
func MakeData(M, N, P int, randomData, diagonal bool) (A, B, C *matrix.FloatMatrix) {

	if diagonal && M != N {
		diagonal = false
		fmt.Printf("cannot make diagonal if B.rows != B.cols\n")
	}

	if randomData {
		A = matrix.FloatNormal(M, P)
		if diagonal {
			d := matrix.FloatNormal(P, 1)
			B := matrix.FloatDiagonal(P, 0.0)
			B.SetIndexesFromArray(d.FloatArray(), matrix.DiagonalIndexes(B)...)
		} else {
			B = matrix.FloatNormal(P, N)
		}
	} else {
		A = matrix.FloatWithValue(M, P, 1.0)
		if diagonal {
			B = matrix.FloatDiagonal(P, 1.0)
		} else {
			B = matrix.FloatWithValue(P, N, 1.0)
		}
	}
	C = matrix.FloatZeros(M, N)
	return
}
Exemple #2
0
func TestVectors(t *testing.T) {
	a := matrix.FloatWithValue(1, 5, 1.0)
	b := matrix.FloatWithValue(1, 5, 2.0)
	c := matrix.FloatWithValue(5, 1, 2.0)
	ar := a.FloatArray()
	br := b.FloatArray()
	cr := c.FloatArray()
	v := DDot(ar, br, 1.0, a.LeadingIndex(), b.LeadingIndex(), a.NumElements())
	t.Logf("a*b = %.1f\n", v)
	v = DDot(cr, br, 1.0, 1, b.LeadingIndex(), c.NumElements())
	t.Logf("c*b = %.1f\n", v)
	v = DNorm2(br, 1, b.NumElements())
	t.Logf("norm2(b) = %.1f\n", v)

	b0 := matrix.FloatNew(1, 5, []float64{-1.0, 0.0, 2.0, -3.0, 5.0})
	br = b0.FloatArray()
	ix := DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)

	b0 = matrix.FloatNew(1, 5, []float64{-8.0, 0.0, 2.0, -3.0, 5.0})
	br = b0.FloatArray()
	ix = DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)

	b0 = matrix.FloatNew(1, 5, []float64{-8.0, 0.0, 2.0, -9.0, 5.0})
	br = b0.FloatArray()
	ix = DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)
}
Exemple #3
0
func trmmTest(t *testing.T, A *matrix.FloatMatrix, flags Flags, nb int) bool {
	var B0 *matrix.FloatMatrix
	N := A.Cols()
	S := 0
	E := A.Cols()
	side := linalg.OptLeft
	if flags&RIGHT != 0 {
		B0 = matrix.FloatWithValue(2, A.Rows(), 2.0)
		side = linalg.OptRight
		E = B0.Rows()
	} else {
		B0 = matrix.FloatWithValue(A.Rows(), 2, 2.0)
		E = B0.Cols()
	}
	B1 := B0.Copy()

	trans := linalg.OptNoTrans
	if flags&TRANSA != 0 {
		trans = linalg.OptTransA
	}
	diag := linalg.OptNonUnit
	if flags&UNIT != 0 {
		diag = linalg.OptUnit
	}
	uplo := linalg.OptUpper
	if flags&LOWER != 0 {
		uplo = linalg.OptLower
	}

	blas.TrmmFloat(A, B0, 1.0, uplo, diag, trans, side)
	if A.Rows() < 8 {
		//t.Logf("..A\n%v\n", A)
		t.Logf("  BLAS B0:\n%v\n", B0)
	}

	Ar := A.FloatArray()
	Br := B1.FloatArray()
	if nb != 0 {
		DTrmmBlk(Br, Ar, 1.0, flags, B1.LeadingIndex(), A.LeadingIndex(),
			N, S, E, nb)
	} else {
		DTrmmUnblk(Br, Ar, 1.0, flags, B1.LeadingIndex(), A.LeadingIndex(),
			N, S, E, 0)
	}
	result := B0.AllClose(B1)
	t.Logf("   B0 == B1: %v\n", result)
	if A.Rows() < 8 {
		t.Logf("  DTrmm B1:\n%v\n", B1)
	}
	return result
}
Exemple #4
0
func trmvTest(t *testing.T, A *matrix.FloatMatrix, flags Flags, nb int) bool {
	N := A.Cols()
	//S := 0
	//E := A.Cols()
	X0 := matrix.FloatWithValue(A.Rows(), 1, 2.0)
	X1 := X0.Copy()

	trans := linalg.OptNoTrans
	if flags&TRANS != 0 {
		trans = linalg.OptTrans
	}
	diag := linalg.OptNonUnit
	if flags&UNIT != 0 {
		diag = linalg.OptUnit
	}
	uplo := linalg.OptUpper
	if flags&LOWER != 0 {
		uplo = linalg.OptLower
	}

	blas.TrmvFloat(A, X0, uplo, diag, trans)

	Ar := A.FloatArray()
	Xr := X1.FloatArray()
	if nb == 0 {
		DTrimvUnblkMV(Xr, Ar, flags, 1, A.LeadingIndex(), N)
	}
	result := X0.AllClose(X1)
	t.Logf("   X0 == X1: %v\n", result)
	if !result && A.Rows() < 8 {
		t.Logf("  BLAS TRMV X0:\n%v\n", X0)
		t.Logf("  DTrmv X1:\n%v\n", X1)
	}
	return result
}
Exemple #5
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)
}
Exemple #6
0
func _TestLDLnoPiv(t *testing.T) {
	N := 42
	nb := 8

	A0 := matrix.FloatUniform(N, N)
	A := matrix.FloatZeros(N, N)
	Mult(A, A0, A0, 1.0, 1.0, TRANSB)

	B := matrix.FloatNormal(A.Rows(), 2)
	w := matrix.FloatWithValue(A.Rows(), 2, 1.0)

	// B0 = A*B
	B0 := B.Copy()

	nb = 2
	L, _ := DecomposeLDLnoPiv(A.Copy(), w, LOWER, nb)
	Mult(B0, A, B, 1.0, 0.0, NOTRANS)
	SolveLDLnoPiv(B0, L, LOWER)
	t.Logf("L*D*L.T: ||B - A*X||_1: %e\n", NormP(B0.Minus(B), NORM_ONE))

	U, _ := DecomposeLDLnoPiv(A.Copy(), w, UPPER, nb)
	Mult(B0, A, B, 1.0, 0.0, NOTRANS)
	SolveLDLnoPiv(B0, U, UPPER)
	t.Logf("U*D*U.T: ||B - A*X||_1: %e\n", NormP(B0.Minus(B), NORM_ONE))

}
Exemple #7
0
func _TestMultMVTransA(t *testing.T) {
	bM := 1000 * M
	bN := 1000 * N
	A := matrix.FloatNormal(bN, bM)
	X := matrix.FloatWithValue(bN, 1, 1.0)
	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, 4, 4)
	ok := Y0.AllClose(Y1)
	t.Logf("Y0 == Y1: %v\n", ok)
	if !ok {
		var y1, y0 matrix.FloatMatrix
		Y1.SubMatrix(&y1, 0, 0, 5, 1)
		t.Logf("Y1[0:5]:\n%v\n", y1)
		Y0.SubMatrix(&y0, 0, 0, 5, 1)
		t.Logf("Y0[0:5]:\n%v\n", y0)
	}
}
Exemple #8
0
func _TestMultSymmSmall(t *testing.T) {
	//bM := 5
	bN := 7
	bP := 7
	Adata := [][]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}}

	A := matrix.FloatMatrixFromTable(Adata, matrix.RowOrder)
	B := matrix.FloatWithValue(bN, bP, 2.0)
	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.OptUpper, linalg.OptRight)

	DMultSymm(C1r, Ar, Br, 1.0, 1.0, UPPER|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)
	}
}
Exemple #9
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)
	}
}
Exemple #10
0
func TestLDLlower(t *testing.T) {
	/*
	   Ldata := [][]float64{
	    []float64{7.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
	    []float64{7.0, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0},
	    []float64{7.0, 6.0, 5.0, 0.0, 0.0, 0.0, 0.0},
	    []float64{7.0, 6.0, 5.0, 4.0, 0.0, 0.0, 0.0},
	    []float64{7.0, 6.0, 5.0, 4.0, 6.0, 0.0, 0.0},
	    []float64{7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 0.0},
	    []float64{7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0}}
	   A := matrix.FloatMatrixFromTable(Ldata, matrix.RowOrder)
	   N := A.Rows()
	*/
	N := 7
	nb := 0

	A0 := matrix.FloatUniform(N, N)
	A := matrix.FloatZeros(N, N)
	Mult(A, A0, A0, 1.0, 1.0, TRANSB)

	B := matrix.FloatNormal(A.Rows(), 2)
	B0 := B.Copy()
	B1 := B.Copy()
	Mult(B0, A, B, 1.0, 0.0, NOTRANS)
	_, _, _ = B0, B1, A0

	ipiv := make([]int, N, N)
	L, _ := DecomposeLDL(A.Copy(), nil, ipiv, LOWER, 0)
	//t.Logf("unblk: ipiv = %v\n", ipiv)
	//t.Logf("unblk: L\n%v\n", L)

	ApplyRowPivots(B, ipiv, FORWARD)
	MultTrm(B, L, 1.0, LOWER|UNIT|TRANSA)
	MultDiag(B, L, LEFT)
	MultTrm(B, L, 1.0, LOWER|UNIT)
	ApplyRowPivots(B0, ipiv, FORWARD)
	t.Logf(" unblk: L*D*L.T %d pivots: ||A*B - L*D*L.T*B||_1: %e\n",
		NumPivots(ipiv), NormP(B.Minus(B0), NORM_ONE))
	t.Logf("pivots: %v\n", ipiv)

	nb = 4
	w := matrix.FloatWithValue(A.Rows(), nb, 1.0)
	L, _ = DecomposeLDL(A.Copy(), w, ipiv, LOWER, nb)
	//t.Logf("blk: ipiv = %v\n", ipiv)
	//t.Logf("blk: L\n%v\n", L)

	// B2 = A*B1 == A*B
	B2 := B1.Copy()
	Mult(B2, A, B1, 1.0, 0.0, NOTRANS)

	ApplyRowPivots(B1, ipiv, FORWARD)
	MultTrm(B1, L, 1.0, LOWER|UNIT|TRANSA)
	MultDiag(B1, L, LEFT)
	MultTrm(B1, L, 1.0, LOWER|UNIT)
	ApplyRowPivots(B2, ipiv, FORWARD)
	t.Logf("   blk: L*D*L.T %d pivots: ||A*B - L*D*L.T*B||_1: %e\n",
		NumPivots(ipiv), NormP(B2.Minus(B1), NORM_ONE))
	t.Logf("pivots: %v\n", ipiv)
}
Exemple #11
0
func _TestRank(t *testing.T) {
	bM := M * 100
	bN := N * 100
	//bP := 5

	A := matrix.FloatWithValue(bM, bN, 1.0)
	A0 := matrix.FloatWithValue(bM, bN, 1.0)
	X := matrix.FloatNormal(bM, 1)
	Y := matrix.FloatNormal(bN, 1)

	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)
	t.Logf("GER A0 == A1: %v\n", A0.AllClose(A))
}
Exemple #12
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))
}
Exemple #13
0
func _TestMultSyr2Small(t *testing.T) {
	bN := 7
	//A := matrix.FloatNormal(bN, bN)
	//B := matrix.FloatNormal(bN, bP)
	//A := matrix.FloatWithValue(bM, bP, 1.0)
	X := matrix.FloatWithValue(bN, 1, 1.0)
	Y := matrix.FloatWithValue(bN, 1, 1.0)
	C0 := matrix.FloatZeros(bN, bN)
	C1 := matrix.FloatZeros(bN, bN)
	for i := 0; i < bN; i++ {
		X.Add(1.0+float64(i), i)
		Y.Add(2.0+float64(i), i)
	}
	t.Logf("X=\n%v\nY=\n%v\n", X, Y)

	Xr := X.FloatArray()
	Yr := Y.FloatArray()
	C1r := C1.FloatArray()

	blas.Syr2Float(X, Y, C0, 1.0, linalg.OptUpper)

	DSymmRank2MV(C1r, Xr, Yr, 1.0, UPPER, C1.LeadingIndex(), 1, 1, 0, bN, 4)
	ok := C0.AllClose(C1)
	t.Logf("UPPER C0 == C1: %v\n", ok)
	if !ok {
		t.Logf("C1: C1 = A*X\n%v\n", C1)
		t.Logf("blas: C0\n%v\n", C0)
	}

	blas.Syr2Float(X, Y, C0, 1.0, linalg.OptLower)

	DSymmRank2MV(C1r, Xr, Yr, 1.0, LOWER, C1.LeadingIndex(), 1, 1, 0, bN, 4)
	ok = C0.AllClose(C1)
	t.Logf("LOWER C0 == C1: %v\n", ok)
	if !ok {
		t.Logf("blas: C0\n%v\n", C0)
		t.Logf("C1: C1 = A*X\n%v\n", C1)
	}
}
Exemple #14
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))
}
Exemple #15
0
func _TestSolveRandom(t *testing.T) {
	bN := 22
	nb := 4
	L := matrix.FloatNormalSymmetric(bN, matrix.Lower)
	U := L.Transpose()
	X0 := matrix.FloatWithValue(L.Rows(), 1, 1.0)

	t.Logf("-- BLOCKED SOLVE LOWER NON-UNIT ---\n")
	solveMVTest(t, L, X0.Copy(), LOWER, bN, nb)
	t.Logf("-- BLOCKED SOLVE LOWER UNIT ---\n")
	solveMVTest(t, L, X0.Copy(), LOWER|UNIT, bN, nb)
	t.Logf("-- BLOCKED SOLVE UPPER NON-UNIT ---\n")
	solveMVTest(t, U, X0.Copy(), UPPER, bN, nb)
	t.Logf("-- BLOCKED SOLVE UPPER UNIT ---\n")
	solveMVTest(t, U, X0.Copy(), UPPER|UNIT, bN, nb)
}
Exemple #16
0
func main() {
	flag.Parse()

	x := floorplan(matrix.FloatWithValue(5, 1, 100.0))
	if x != nil {
		W := x.GetIndex(0)
		H := x.GetIndex(1)
		xs := matrix.FloatVector(x.FloatArray()[2:7])
		ys := matrix.FloatVector(x.FloatArray()[7:12])
		ws := matrix.FloatVector(x.FloatArray()[12:17])
		hs := matrix.FloatVector(x.FloatArray()[17:])
		fmt.Printf("W = %.5f, H = %.5f\n", W, H)
		fmt.Printf("x = \n%v\n", xs.ToString("%.5f"))
		fmt.Printf("y = \n%v\n", ys.ToString("%.5f"))
		fmt.Printf("w = \n%v\n", ws.ToString("%.5f"))
		fmt.Printf("h = \n%v\n", hs.ToString("%.5f"))
		check(x)
	}
}
Exemple #17
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)
}
Exemple #18
0
func _TestMultTransASmall(t *testing.T) {
	bM := 7
	bN := 7
	bP := 7
	D := matrix.FloatNormal(bM, bP)
	E := matrix.FloatNormal(bP, bN)
	C0 := matrix.FloatWithValue(bM, bN, 0.0)
	C1 := C0.Copy()
	Dt := D.Transpose()

	Dr := Dt.FloatArray()
	Er := E.FloatArray()
	C1r := C1.FloatArray()
	blas.GemmFloat(Dt, E, C0, 1.0, 1.0, linalg.OptTransA)
	t.Logf("blas: C=D*E\n%v\n", C0)

	DMult(C1r, Dr, Er, 1.0, 1.0, TRANSA, bM, bM, bP, bP, 0, bN, 0, bM, 4, 4, 4)
	t.Logf("C0 == C1: %v\n", C0.AllClose(C1))
	t.Logf("C1: C1=D*E\n%v\n", C1)
}
Exemple #19
0
func _TestMultSmall(t *testing.T) {
	bM := 6
	bN := 6
	bP := 6
	D := matrix.FloatNormal(bM, bP)
	E := matrix.FloatNormal(bP, bN)
	C0 := matrix.FloatWithValue(bM, bN, 1.0)
	C1 := C0.Copy()

	Dr := D.FloatArray()
	Er := E.FloatArray()
	C1r := C1.FloatArray()

	blas.GemmFloat(D, E, C0, 1.0, 1.0)
	t.Logf("blas: C=D*E\n%v\n", C0)

	DMult(C1r, Dr, Er, 1.0, 1.0, NOTRANS, bM, bM, bP, bP, 0, bN, 0, bM, 4, 4, 4)
	t.Logf("C0 == C1: %v\n", C0.AllClose(C1))
	t.Logf("C1: C1=D*E\n%v\n", C1)
}
Exemple #20
0
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)
	}
}
Exemple #21
0
// not run
func _TestPanic(t *testing.T) {
	PanicOnError(true)
	X := matrix.FloatWithValue(10, 1, 1.0)
	ScalFloat(X, 2.0, &linalg.IOpt{"offset", -1})
}
Exemple #22
0
func mcsdp(w *matrix.FloatMatrix) (*Solution, error) {
	//
	// Returns solution x, z to
	//
	//    (primal)  minimize    sum(x)
	//              subject to  w + diag(x) >= 0
	//
	//    (dual)    maximize    -tr(w*z)
	//              subject to  diag(z) = 1
	//                          z >= 0.
	//
	n := w.Rows()
	G := &matrixFs{n}

	cngrnc := func(r, x *matrix.FloatMatrix, alpha float64) (err error) {
		// Congruence transformation
		//
		//    x := alpha * r'*x*r.
		//
		// r and x are square matrices.
		//
		err = nil

		// tx = matrix(x, (n,n)) is copying and reshaping
		// scale diagonal of x by 1/2, (x is (n,n))
		tx := x.Copy()
		matrix.Reshape(tx, n, n)
		tx.Diag().Scale(0.5)

		// a := tril(x)*r
		// (python: a = +r is really making a copy of r)
		a := r.Copy()

		err = blas.TrmmFloat(tx, a, 1.0, linalg.OptLeft)

		// x := alpha*(a*r' + r*a')
		err = blas.Syr2kFloat(r, a, tx, alpha, 0.0, linalg.OptTrans)

		// x[:] = tx[:]
		tx.CopyTo(x)
		return
	}

	Fkkt := func(W *sets.FloatMatrixSet) (KKTFunc, error) {

		//    Solve
		//                  -diag(z)                           = bx
		//        -diag(x) - inv(rti*rti') * z * inv(rti*rti') = bs
		//
		//    On entry, x and z contain bx and bs.
		//    On exit, they contain the solution, with z scaled
		//    (inv(rti)'*z*inv(rti) is returned instead of z).
		//
		//    We first solve
		//
		//        ((rti*rti') .* (rti*rti')) * x = bx - diag(t*bs*t)
		//
		//    and take z  = -rti' * (diag(x) + bs) * rti.

		var err error = nil
		rti := W.At("rti")[0]

		// t = rti*rti' as a nonsymmetric matrix.
		t := matrix.FloatZeros(n, n)
		err = blas.GemmFloat(rti, rti, t, 1.0, 0.0, linalg.OptTransB)
		if err != nil {
			return nil, err
		}

		// Cholesky factorization of tsq = t.*t.
		tsq := matrix.Mul(t, t)
		err = lapack.Potrf(tsq)
		if err != nil {
			return nil, err
		}

		f := func(x, y, z *matrix.FloatMatrix) (err error) {
			// tbst := t * zs * t = t * bs * t
			tbst := z.Copy()
			matrix.Reshape(tbst, n, n)
			cngrnc(t, tbst, 1.0)

			// x := x - diag(tbst) = bx - diag(rti*rti' * bs * rti*rti')
			diag := tbst.Diag().Transpose()
			x.Minus(diag)

			// x := (t.*t)^{-1} * x = (t.*t)^{-1} * (bx - diag(t*bs*t))
			err = lapack.Potrs(tsq, x)

			// z := z + diag(x) = bs + diag(x)
			// z, x are really column vectors here
			z.AddIndexes(matrix.MakeIndexSet(0, n*n, n+1), x.FloatArray())

			// z := -rti' * z * rti = -rti' * (diag(x) + bs) * rti
			cngrnc(rti, z, -1.0)
			return nil
		}
		return f, nil
	}

	c := matrix.FloatWithValue(n, 1, 1.0)

	// initial feasible x: x = 1.0 - min(lmbda(w))
	lmbda := matrix.FloatZeros(n, 1)
	wp := w.Copy()
	lapack.Syevx(wp, lmbda, nil, 0.0, nil, []int{1, 1}, linalg.OptRangeInt)
	x0 := matrix.FloatZeros(n, 1).Add(-lmbda.GetAt(0, 0) + 1.0)
	s0 := w.Copy()
	s0.Diag().Plus(x0.Transpose())
	matrix.Reshape(s0, n*n, 1)

	// initial feasible z is identity
	z0 := matrix.FloatIdentity(n)
	matrix.Reshape(z0, n*n, 1)

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

	primalstart := sets.FloatSetNew("x", "s")
	dualstart := sets.FloatSetNew("z")
	primalstart.Set("x", x0)
	primalstart.Set("s", s0)
	dualstart.Set("z", z0)

	var solopts SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = false
	h := w.Copy()
	matrix.Reshape(h, h.NumElements(), 1)
	return ConeLpCustomMatrix(c, G, h, nil, nil, dims, Fkkt, &solopts, primalstart, dualstart)
}
Exemple #23
0
func main() {
	m := 6
	Vdata := [][]float64{
		[]float64{1.0, -1.0, -2.0, -2.0, 0.0, 1.5, 1.0},
		[]float64{1.0, 2.0, 1.0, -1.0, -2.0, -1.0, 1.0}}

	V := matrix.FloatMatrixFromTable(Vdata, matrix.RowOrder)

	// V[1, :m] - V[1,1:]
	a0 := matrix.Minus(V.GetSubMatrix(1, 0, 1, m), V.GetSubMatrix(1, 1, 1))
	// V[0, :m] - V[0,1:]
	a1 := matrix.Minus(V.GetSubMatrix(0, 0, 1, m), V.GetSubMatrix(0, 1, 1))
	A0, _ := matrix.FloatMatrixStacked(matrix.StackDown, a0.Scale(-1.0), a1)
	A0 = A0.Transpose()
	b0 := matrix.Mul(A0, V.GetSubMatrix(0, 0, 2, m).Transpose())
	b0 = matrix.Times(b0, matrix.FloatWithValue(2, 1, 1.0))

	A := make([]*matrix.FloatMatrix, 0)
	b := make([]*matrix.FloatMatrix, 0)
	A = append(A, A0)
	b = append(b, b0)

	// List of symbols
	C := make([]*matrix.FloatMatrix, 0)
	C = append(C, matrix.FloatZeros(2, 1))
	var row *matrix.FloatMatrix = nil
	for k := 0; k < m; k++ {
		row = A0.GetRow(k, row)
		nrm := blas.Nrm2Float(row)
		row.Scale(2.0 * b0.GetIndex(k) / (nrm * nrm))
		C = append(C, row.Transpose())
	}

	// Voronoi set around C[1]
	A1 := matrix.FloatZeros(3, 2)
	A1.SetSubMatrix(0, 0, A0.GetSubMatrix(0, 0, 1).Scale(-1.0))
	A1.SetSubMatrix(1, 0, matrix.Minus(C[m], C[1]).Transpose())
	A1.SetSubMatrix(2, 0, matrix.Minus(C[2], C[1]).Transpose())
	b1 := matrix.FloatZeros(3, 1)
	b1.SetIndex(0, -b0.GetIndex(0))
	v := matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[m], C[1])).Float() * 0.5
	b1.SetIndex(1, v)
	v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[2], C[1])).Float() * 0.5
	b1.SetIndex(2, v)
	A = append(A, A1)
	b = append(b, b1)

	// Voronoi set around C[2] ... C[5]
	for k := 2; k < 6; k++ {
		A1 = matrix.FloatZeros(3, 2)
		A1.SetSubMatrix(0, 0, A0.GetSubMatrix(k-1, 0, 1).Scale(-1.0))
		A1.SetSubMatrix(1, 0, matrix.Minus(C[k-1], C[k]).Transpose())
		A1.SetSubMatrix(2, 0, matrix.Minus(C[k+1], C[k]).Transpose())
		b1 = matrix.FloatZeros(3, 1)
		b1.SetIndex(0, -b0.GetIndex(k-1))
		v := matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[k-1], C[k])).Float() * 0.5
		b1.SetIndex(1, v)
		v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[k+1], C[k])).Float() * 0.5
		b1.SetIndex(2, v)
		A = append(A, A1)
		b = append(b, b1)
	}

	// Voronoi set around C[6]
	A1 = matrix.FloatZeros(3, 2)
	A1.SetSubMatrix(0, 0, A0.GetSubMatrix(5, 0, 1).Scale(-1.0))
	A1.SetSubMatrix(1, 0, matrix.Minus(C[1], C[6]).Transpose())
	A1.SetSubMatrix(2, 0, matrix.Minus(C[5], C[6]).Transpose())
	b1 = matrix.FloatZeros(3, 1)
	b1.SetIndex(0, -b0.GetIndex(5))
	v = matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[1], C[6])).Float() * 0.5
	b1.SetIndex(1, v)
	v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[5], C[6])).Float() * 0.5
	b1.SetIndex(2, v)

	A = append(A, A1)
	b = append(b, b1)

	P := matrix.FloatIdentity(2)
	q := matrix.FloatZeros(2, 1)
	solopts := &cvx.SolverOptions{ShowProgress: false, MaxIter: 30}
	ovals := make([]float64, 0)
	for k := 1; k < 7; k++ {
		sol, err := cvx.Qp(P, q, A[k], b[k], nil, nil, solopts, nil)
		_ = err
		x := sol.Result.At("x")[0]
		ovals = append(ovals, math.Pow(blas.Nrm2Float(x), 2.0))
	}

	optvals := matrix.FloatVector(ovals)
	//fmt.Printf("optvals=\n%v\n", optvals)

	rangeFunc := func(n int) []float64 {
		r := make([]float64, 0)
		for i := 0; i < n; i++ {
			r = append(r, float64(i))
		}
		return r
	}

	nopts := 200
	sigmas := matrix.FloatVector(rangeFunc(nopts))
	sigmas.Scale((0.5 - 0.2) / float64(nopts)).Add(0.2)

	bndsVal := func(sigma float64) float64 {
		// 1.0 - sum(exp( -optvals/(2*sigma**2)))
		return 1.0 - matrix.Exp(matrix.Scale(optvals, -1.0/(2*sigma*sigma))).Sum()
	}
	bnds := matrix.FloatZeros(sigmas.NumElements(), 1)
	for j, v := range sigmas.FloatArray() {
		bnds.SetIndex(j, bndsVal(v))
	}
	plotData("plot.png", sigmas.FloatArray(), bnds.FloatArray())
}
Exemple #24
0
func trsmSolve(t *testing.T, A *matrix.FloatMatrix, flags Flags, rand bool, nrhs, nb int) bool {
	var B0 *matrix.FloatMatrix
	side := linalg.OptLeft
	trans := linalg.OptNoTrans
	N := A.Cols()
	S := 0
	E := A.Rows()
	_ = S
	_ = E
	if flags&RIGHT != 0 {
		if rand {
			B0 = matrix.FloatNormal(nrhs, A.Rows())
		} else {
			B0 = matrix.FloatWithValue(nrhs, A.Rows(), 2.0)
		}
		side = linalg.OptRight
		E = B0.Rows()
	} else {
		if rand {
			B0 = matrix.FloatNormal(A.Rows(), nrhs)
		} else {
			B0 = matrix.FloatWithValue(A.Rows(), nrhs, 2.0)
		}
		E = B0.Cols()
	}
	B1 := B0.Copy()
	diag := linalg.OptNonUnit
	if flags&UNIT != 0 {
		diag = linalg.OptUnit
	}
	uplo := linalg.OptUpper
	if flags&LOWER != 0 {
		uplo = linalg.OptLower
	}
	if flags&TRANSA != 0 {
		trans = linalg.OptTransA
	}
	blas.TrsmFloat(A, B0, 1.0, uplo, diag, side, trans)

	Ar := A.FloatArray()
	Br := B1.FloatArray()
	if nb == 0 || nb == N {
		DSolveUnblk(Br, Ar, 1.0, flags, B1.LeadingIndex(), A.LeadingIndex(), N, S, E)
	} else {
		DSolveBlk(Br, Ar, 1.0, flags, B1.LeadingIndex(), A.LeadingIndex(), N, S, E, nb)
	}
	result := B1.AllClose(B0)
	t.Logf("B1 == B0: %v\n", result)
	if !result {
		if nrhs < 10 {
			t.Logf("blas: B0\n%v\n", B0)
			t.Logf("B1:\n%v\n", B1)
		} else {
			b0 := B0.FloatArray()
			b1 := B1.FloatArray()
			for k := 0; k < len(b0); k++ {
				if !isClose(b0[k], b1[k]) {
					t.Logf("first divergences at %d ... col %d, row %d\n", k, k/B0.Rows(), k%B0.Rows())
					break
				}
			}
		}
	}
	return result
}