Exemplo n.º 1
0
func _TestMultMV(t *testing.T) {
	bM := 100 * M
	bN := 100 * N
	A := matrix.FloatNormal(bM, bN)
	X := matrix.FloatNormal(bN, 1)
	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)

	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, NOTRANS, 1, A.LeadingIndex(), 1, 0, bN, 0, bM, 32, 32)
	t.Logf("Y0 == Y1: %v\n", Y0.AllClose(Y1))
	/*
	   if ! Y0.AllClose(Y1) {
	       y0 := Y0.SubMatrix(0, 0, 2, 1)
	       y1 := Y1.SubMatrix(0, 0, 2, 1)
	       t.Logf("y0=\n%v\n", y0)
	       t.Logf("y1=\n%v\n", y1)
	   }
	*/
}
Exemplo n.º 2
0
func CTestGemv(m, n, p int) (fnc func(), A, X, Y *matrix.FloatMatrix) {
	A = matrix.FloatNormal(m, n)
	X = matrix.FloatNormal(n, 1)
	Y = matrix.FloatZeros(m, 1)
	fnc = func() {
		blas.GemvFloat(A, X, Y, 1.0, 1.0)
	}
	return
}
Exemplo n.º 3
0
func TestTemplate(m, n, p int) (fnc func(), A, X, Y *matrix.FloatMatrix) {
	A = matrix.FloatNormal(m, n)
	X = matrix.FloatNormal(n, 1)
	Y = matrix.FloatZeros(m, 1)
	fnc = func() {
		// test core here
	}
	return
}
Exemplo n.º 4
0
func TestTemplate(m, n, p int) (fnc func(), A, B, C *matrix.FloatMatrix) {
	A = matrix.FloatNormal(m, p)
	B = matrix.FloatNormal(p, n)
	C = matrix.FloatZeros(m, n)
	fnc = func() {
		// test core here
	}
	return
}
Exemplo n.º 5
0
func MMTestMultTransAB(m, n, p int) (fnc func(), A, B, C *matrix.FloatMatrix) {
	A = matrix.FloatNormal(p, m)
	B = matrix.FloatNormal(n, p)
	C = matrix.FloatZeros(m, n)
	fnc = func() {
		matops.Mult(C, A, B, 1.0, 1.0, matops.TRANSA|matops.TRANSB)
	}
	return
}
Exemplo n.º 6
0
func MMTestMult(m, n, p int) (fnc func(), A, B, C *matrix.FloatMatrix) {
	A = matrix.FloatNormal(m, p)
	B = matrix.FloatNormal(p, n)
	C = matrix.FloatZeros(m, n)
	fnc = func() {
		matops.Mult(C, A, B, 1.0, 1.0, matops.NOTRANS)
	}
	return
}
Exemplo n.º 7
0
func CTestMVMultTransA(m, n, p int) (fnc func(), A, X, Y *matrix.FloatMatrix) {
	A = matrix.FloatNormal(n, m)
	X = matrix.FloatNormal(n, 1)
	Y = matrix.FloatZeros(m, 1)
	fnc = func() {
		matops.MVMultTransA(Y, A, X, 1.0, 1.0)
	}
	return
}
Exemplo n.º 8
0
func CTestGemvTransA(m, n, p int) (fnc func(), A, X, Y *matrix.FloatMatrix) {
	A = matrix.FloatNormal(n, m)
	X = matrix.FloatNormal(n, 1)
	Y = matrix.FloatZeros(m, 1)
	A = A.Transpose()
	fnc = func() {
		blas.GemvFloat(A, X, Y, 1.0, 1.0, linalg.OptTrans)
	}
	return
}
Exemplo n.º 9
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)
	}
}
Exemplo n.º 10
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)
	}
}
Exemplo n.º 11
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))

}
Exemplo n.º 12
0
func TestUpdateTrmMV(t *testing.T) {
	//bM := 5
	bN := 8
	//bP := 4
	nb := 4
	X := matrix.FloatNormal(bN, 1)
	//B := matrix.FloatNormal(bP, bN)
	Y := X.Copy()
	C0 := matrix.FloatZeros(bN, bN)
	C2 := matrix.FloatZeros(bN, bN)
	C1 := matrix.FloatZeros(bN, bN)

	Xr := X.FloatArray()
	Yr := Y.FloatArray()
	C1r := C1.FloatArray()
	C0r := C0.FloatArray()
	C2r := C2.FloatArray()

	// no transpose
	DRankMV(C1r, Xr, Yr, 1.0, C1.LeadingIndex(), 1, 1,
		0, bN, 0, bN, nb, nb)
	DTrmUpdMV(C0r, Xr, Yr, 1.0, LOWER, C0.LeadingIndex(), 1, 1,
		0, bN, nb)
	DTrmUpdMV(C2r, Xr, Yr, 1.0, UPPER, C2.LeadingIndex(), 1, 1,
		0, bN, nb)

	t.Logf("C1:\n%v\nC0:\n%v\nC2:\n%v\n", C1, C0, C2)
	// C0 == C2.T
	t.Logf("C0 == C2.T: %v\n", C0.AllClose(C2.Transpose()))
	// C1 == C1 - C2 + C0.T
	Cn := matrix.Minus(C1, C2)
	Cn.Plus(C0.Transpose())
	t.Logf("C1 == C1 - C2 + C0.T: %v\n", Cn.AllClose(C1))

}
Exemplo n.º 13
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)
}
Exemplo n.º 14
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)
}
Exemplo n.º 15
0
func CTestBlasUp(m, n, p int) (fnc func(), A, B, C *matrix.FloatMatrix) {
	A = matrix.FloatNormalSymmetric(m, matrix.Lower)
	B = matrix.FloatNormal(m, n)
	C = matrix.FloatZeros(m, n)
	fnc = func() {
		blas.SymmFloat(A, B, C, 1.0, 1.0, linalg.OptUpper)
	}
	return fnc, A, B, C
}
Exemplo n.º 16
0
func CTestSymmLower(m, n, p int) (fnc func(), A, B, C *matrix.FloatMatrix) {
	A = matrix.FloatNormalSymmetric(m, matrix.Upper)
	B = matrix.FloatNormal(m, n)
	C = matrix.FloatZeros(m, n)
	fnc = func() {
		matops.MultSym(C, A, B, 1.0, 1.0, matops.LEFT|matops.LOWER)
	}
	return fnc, A, B, C
}
Exemplo n.º 17
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))
}
Exemplo n.º 18
0
func _TestMultTransABig(t *testing.T) {
	bM := 100*M + 3
	bN := 100*N + 3
	bP := 100*P + 3
	D := matrix.FloatNormal(bM, bP)
	E := matrix.FloatNormal(bP, bN)
	C0 := matrix.FloatZeros(bM, bN)
	C1 := matrix.FloatZeros(bM, bN)
	Dt := D.Transpose()

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

	blas.GemmFloat(Dt, E, C0, 1.0, 1.0, linalg.OptTransA)

	DMult(C1r, Dr, Er, 1.0, 1.0, TRANSA, bM, bM, bP, bP, 0, bN, 0, bM, 32, 32, 32)
	t.Logf("C0 == C1: %v\n", C0.AllClose(C1))
}
Exemplo n.º 19
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)
}
Exemplo n.º 20
0
func _TestMultBig(t *testing.T) {
	bM := 100*M + 3
	bN := 100*N + 3
	bP := 100*P + 3
	D := matrix.FloatNormal(bM, bP)
	E := matrix.FloatNormal(bP, bN)
	C0 := matrix.FloatZeros(bM, bN)
	C1 := matrix.FloatZeros(bM, bN)

	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, 32, 32, 32)
	res := C0.AllClose(C1)
	t.Logf("C0 == C1: %v\n", res)
}
Exemplo n.º 21
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)
}
Exemplo n.º 22
0
func _TestMultTransABSmall(t *testing.T) {
	bM := 7
	bN := 7
	bP := 7
	D := matrix.FloatNormal(bM, bP)
	E := matrix.FloatNormal(bP, bN)
	C0 := matrix.FloatZeros(bM, bN)
	C1 := matrix.FloatZeros(bM, bN)
	Dt := D.Transpose()
	Et := E.Transpose()

	Dr := Dt.FloatArray()
	Er := Et.FloatArray()
	C1r := C1.FloatArray()

	blas.GemmFloat(Dt, Et, C0, 1.0, 1.0, linalg.OptTransA, linalg.OptTransB)
	t.Logf("blas: C=D.T*E.T\n%v\n", C0)

	DMult(C1r, Dr, Er, 1.0, 1.0, TRANSA|TRANSB, 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.T*E.T\n%v\n", C1)
}
Exemplo n.º 23
0
func _TestBK2(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))
}
Exemplo n.º 24
0
func TestLDLSolve(t *testing.T) {
	N := 7
	nb := 4
	K := 6
	A0 := matrix.FloatUniform(N, N)
	A := matrix.FloatZeros(N, N)
	Mult(A, A0, A0, 1.0, 1.0, TRANSB)

	X0 := matrix.FloatNormal(A.Rows(), K)
	B0 := matrix.FloatZeros(X0.Size())
	Mult(B0, A, X0, 1.0, 0.0, NOTRANS)
	B := B0.Copy()
	_ = B

	w := matrix.FloatZeros(A.Rows(), nb)
	ipiv := make([]int, N, N)
	L, _ := DecomposeLDL(A.Copy(), w, ipiv, LOWER, nb)

	ipiv0 := make([]int, N, N)
	L0, _ := DecomposeLDL(A.Copy(), w, ipiv0, LOWER, 0)

	t.Logf("L:\n%v\n", L)
	t.Logf("ipiv: %v\n", ipiv)
	t.Logf("L0:\n%v\n", L0)
	t.Logf("ipiv0: %v\n", ipiv0)
	t.Logf("L == L0: %v", L.AllClose(L0))

	// B = A*X0; solve and B should be X0
	/*
	   SolveLDL(B, L, ipiv, LOWER)
	   B.Minus(X0)
	   t.Logf("L*D*L.T: ||A*X - B||_1: %e\n", NormP(B, NORM_ONE))
	   t.Logf("ipiv: %v\n", ipiv)
	*/

	/*
	   B = B0.Copy()
	   U, _ := DecomposeLDL(A.Copy(), w, ipiv, UPPER, nb)
	   // B = A*X0; solve and B should be X0
	   SolveLDL(B, U, ipiv, UPPER)
	   B.Minus(X0)
	   t.Logf("U*D*U.T: ||A*X - B||_1: %e\n", NormP(B, NORM_ONE))
	*/
}
Exemplo n.º 25
0
func _TestMultSymmLower(t *testing.T) {
	//bM := 5
	bN := 100 * N
	bP := 100 * P
	A := matrix.FloatNormalSymmetric(bN, matrix.Lower)
	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)

	DMultSymm(C1r, Ar, Br, 1.0, 1.0, LOWER|LEFT, bN, A.LeadingIndex(), bN,
		bN, 0, bP, 0, bN, 32, 32, 32)
	t.Logf("C0 == C1: %v\n", C0.AllClose(C1))
}
Exemplo n.º 26
0
func _TestLDLupper(t *testing.T) {
	N := 8
	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)

	ipiv := make([]int, N, N)
	U, _ := DecomposeLDL(A.Copy(), nil, ipiv, UPPER, 0)

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

	nb = 4
	w := matrix.FloatZeros(A.Rows(), nb)
	U, _ = DecomposeLDL(A.Copy(), w, ipiv, UPPER, nb)
	// B2 = A*B1 == A*B
	B2 := B1.Copy()
	Mult(B2, A, B1, 1.0, 0.0, NOTRANS)

	ApplyRowPivots(B1, ipiv, BACKWARD)
	MultTrm(B1, U, 1.0, UPPER|UNIT|TRANSA)
	MultDiag(B1, U, LEFT)
	MultTrm(B1, U, 1.0, UPPER|UNIT)
	ApplyRowPivots(B2, ipiv, BACKWARD)
	t.Logf("   blk: U*D*U.T %d pivots: ||A*B - U*D*U.T*B||_1: %e\n",
		NumPivots(ipiv), NormP(B2.Minus(B1), NORM_ONE))
	t.Logf("pivots: %v\n", ipiv)
}
Exemplo n.º 27
0
func _TestMultMVSmall(t *testing.T) {
	bM := 5
	bN := 4
	A := matrix.FloatNormal(bM, bN)
	X := matrix.FloatVector([]float64{1.0, 2.0, 3.0, 4.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)

	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, NOTRANS, 1, A.LeadingIndex(), 1, 0, bN, 0, bM, 4, 4)
	ok := Y0.AllClose(Y1)
	t.Logf("Y0 == Y1: %v\n", ok)
	if !ok {
		t.Logf("blas: Y=A*X\n%v\n", Y0)
		t.Logf("Y1: Y1 = A*X\n%v\n", Y1)
	}
}
Exemplo n.º 28
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
}
Exemplo n.º 29
0
func TestUpdateTrmLower(t *testing.T) {
	//bM := 5
	bN := 8
	bP := 4
	nb := 4
	A := matrix.FloatNormal(bN, bP)
	//B := matrix.FloatNormal(bP, bN)
	B := A.Transpose()
	C0 := matrix.FloatZeros(bN, bN)
	C2 := matrix.FloatZeros(bN, bN)
	C1 := matrix.FloatZeros(bN, bN)

	Ar := A.FloatArray()
	Br := B.FloatArray()
	C1r := C1.FloatArray()
	C0r := C0.FloatArray()
	C2r := C2.FloatArray()

	// no transpose
	DMult(C1r, Ar, Br, 2.0, 1.0, NOTRANS, C1.LeadingIndex(), A.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, 0, bN, nb, nb, nb)
	DTrmUpdBlk(C0r, Ar, Br, 2.0, 1.0, LOWER, C0.LeadingIndex(), A.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, nb, nb)
	DTrmUpdBlk(C2r, Ar, Br, 2.0, 1.0, UPPER, C2.LeadingIndex(), A.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, nb, nb)

	//t.Logf("C1:\n%v\nC0:\n%v\nC2:\n%v\n", C1, C0, C2)
	// C0 == C2.T
	t.Logf("C0 == C2.T: %v\n", C0.AllClose(C2.Transpose()))
	// C1 == C1 - C2 + C0.T
	Cn := matrix.Minus(C1, C2)
	Cn.Plus(C0.Transpose())
	t.Logf("C1 == C1 - C2 + C0.T: %v\n", Cn.AllClose(C1))

	// B == A.T
	DMult(C1r, Ar, Ar, 2.0, 0.0, TRANSB, C1.LeadingIndex(), A.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, 0, bN, nb, nb, nb)
	DTrmUpdBlk(C0r, Ar, Ar, 2.0, 0.0, LOWER|TRANSB, C0.LeadingIndex(), A.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, nb, nb)
	DTrmUpdBlk(C2r, Ar, Ar, 2.0, 0.0, UPPER|TRANSB, C2.LeadingIndex(), A.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, nb, nb)

	//t.Logf("TRANSB:\nC1:\n%v\nC0:\n%v\nC2:\n%v\n", C1, C0, C2)
	// C0 == C2.T
	t.Logf("B.T: C0 == C2.T: %v\n", C0.AllClose(C2.Transpose()))
	// C1 == C1 - C2 + C0.T
	Cn = matrix.Minus(C1, C2)
	Cn.Plus(C0.Transpose())
	t.Logf("B.T: C1 == C1 - C2 + C0.T: %v\n", Cn.AllClose(C1))

	// A == B.T
	DMult(C1r, Br, Br, 2.0, 0.0, TRANSA, C1.LeadingIndex(), B.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, 0, bN, nb, nb, nb)
	DTrmUpdBlk(C0r, Br, Br, 2.0, 0.0, LOWER|TRANSA, C0.LeadingIndex(), B.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, nb, nb)
	DTrmUpdBlk(C2r, Br, Br, 2.0, 0.0, UPPER|TRANSA, C2.LeadingIndex(), B.LeadingIndex(), B.LeadingIndex(),
		bP, 0, bN, nb, nb)

	//t.Logf("TRANSA:\nC1:\n%v\nC0:\n%v\nC2:\n%v\n", C1, C0, C2)
	// C0 == C2.T
	t.Logf("A.T: C0 == C2.T: %v\n", C0.AllClose(C2.Transpose()))
	// C1 == C1 - C2 + C0.T
	Cn = matrix.Minus(C1, C2)
	Cn.Plus(C0.Transpose())
	t.Logf("A.T: C1 == C1 - C2 + C0.T: %v\n", Cn.AllClose(C1))

	// A == B.T, B == A.T
	DMult(C1r, Br, Ar, 2.0, 0.0, TRANSA|TRANSB, C1.LeadingIndex(), B.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, 0, bN, nb, nb, nb)
	DTrmUpdBlk(C0r, Br, Ar, 2.0, 0.0, LOWER|TRANSA|TRANSB, C0.LeadingIndex(), B.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, nb, nb)
	DTrmUpdBlk(C2r, Br, Ar, 2.0, 0.0, UPPER|TRANSA|TRANSB, C2.LeadingIndex(), B.LeadingIndex(), A.LeadingIndex(),
		bP, 0, bN, nb, nb)

	//t.Logf("TRANSA|TRANSB:\nC1:\n%v\nC0:\n%v\nC2:\n%v\n", C1, C0, C2)
	// C0 == C2.T
	t.Logf("A.T, B.T: C0 == C2.T: %v\n", C0.AllClose(C2.Transpose()))
	// C1 == C1 - C2 + C0.T
	Cn = matrix.Minus(C1, C2)
	Cn.Plus(C0.Transpose())
	t.Logf("A.T, B.T: C1 == C1 - C2 + C0.T: %v\n", Cn.AllClose(C1))

}