Beispiel #1
0
// test: min ||X|| s.t A.T*X = B
func TestSolveQR(t *testing.T) {
	M := 799
	N := 711
	K := 241
	nb := 32
	conf := gomas.NewConf()
	conf.LB = nb

	tau := cmat.NewMatrix(N, 1)
	A := cmat.NewMatrix(M, N)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)
	A0 := cmat.NewCopy(A)
	B0 := cmat.NewMatrix(M, K)
	B0.SetFrom(src)
	B := cmat.NewCopy(B0)

	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	lapackd.QRFactor(A, tau, W, conf)

	lapackd.QRSolve(B, A, tau, W, gomas.TRANS, conf)

	var Bmin cmat.FloatMatrix
	Bmin.SubMatrix(B0, 0, 0, N, K)
	blasd.Mult(&Bmin, A0, B, 1.0, -1.0, gomas.TRANSA, conf)

	nrm := lapackd.NormP(&Bmin, lapackd.NORM_ONE)
	t.Logf("M=%d, N=%d ||B - A.T*X||_1: %e\n", M, N, nrm)
}
Beispiel #2
0
func TestDTrmmLower(t *testing.T) {
	N := 563
	K := 171
	nofail := true

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(N, K)
	B0 := cmat.NewMatrix(N, K)
	C := cmat.NewMatrix(N, K)

	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatNormSource()

	A.SetFrom(zeromean, cmat.LOWER)
	B.SetFrom(ones)
	B0.SetFrom(ones)
	// B = A*B
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT)
	blasd.Mult(C, A, B0, 1.0, 0.0, gomas.NONE)
	ok := C.AllClose(B)
	nofail = nofail && ok
	t.Logf("trmm(B, A, L|L|N) == gemm(C, TriL(A), B)   : %v\n", ok)

	B.SetFrom(ones)
	// B = A.T*B
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT|gomas.TRANSA)
	blasd.Mult(C, A, B0, 1.0, 0.0, gomas.TRANSA)
	ok = C.AllClose(B)
	nofail = nofail && ok
	t.Logf("trmm(B, A, L|L|T) == gemm(C, TriL(A).T, B) : %v\n", ok)
}
Beispiel #3
0
func __TestZeroQLSweep(t *testing.T) {

	N := 5
	D0 := cmat.NewMatrix(1, N)
	E0 := cmat.NewMatrix(1, N-1)
	D1 := cmat.NewMatrix(1, N)
	E1 := cmat.NewMatrix(1, N-1)

	for k := 0; k < N; k++ {
		D0.SetAt(N-k-1, float64(k+1))
		D1.SetAt(N-k-1, float64(k+1))
		if k < N-1 {
			E0.SetAt(k, 1.0)
			E1.SetAt(k, 1.0)
		}
	}

	f := D0.GetAt(4)
	g := E0.GetAt(3)
	lapackd.BDQLsweep(D0, E0, f, g)
	lapackd.BDQLzero(D1, E1)
	t.Logf("D0.std :%v\n", D0)
	t.Logf("D1.zero:%v\n", D1)
	t.Logf("E0.std :%v\n", E0)
	t.Logf("E1.zero:%v\n", E1)
}
Beispiel #4
0
func __TestSweep(t *testing.T) {

	N := 5
	D0 := cmat.NewMatrix(1, N)
	E0 := cmat.NewMatrix(1, N-1)
	D1 := cmat.NewMatrix(1, N)
	E1 := cmat.NewMatrix(1, N-1)

	for k := 0; k < N; k++ {
		D0.SetAt(k, float64(k+1))
		D1.SetAt(N-1-k, float64(k+1))
		if k < N-1 {
			E0.SetAt(k, 1.0)
			E1.SetAt(k, 1.0)
		}
	}

	f := 0.5
	g := -0.45
	lapackd.BDQRsweep(D0, E0, f, g)
	lapackd.BDQLsweep(D1, E1, f, g)
	t.Logf("D0:%v\n", D0)
	t.Logf("D1:%v\n", D1)
	t.Logf("E0:%v\n", E0)
	t.Logf("E1:%v\n", E1)
}
Beispiel #5
0
// test: min || B - A.T*X ||
func TestLeastSquaresLQ(t *testing.T) {
	M := 723
	N := 811
	K := 273
	nb := 32
	conf := gomas.NewConf()
	conf.LB = nb

	tau := cmat.NewMatrix(M, 1)
	A := cmat.NewMatrix(M, N)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)
	B0 := cmat.NewMatrix(M, K)
	B0.SetFrom(src)
	B := cmat.NewMatrix(N, K)

	// B = A.T*B0
	blasd.Mult(B, A, B0, 1.0, 0.0, gomas.TRANSA, conf)

	W := lapackd.Workspace(lapackd.LQFactorWork(A, conf))
	lapackd.LQFactor(A, tau, W, conf)

	// B' = A.-1*B
	lapackd.LQSolve(B, A, tau, W, gomas.TRANS, conf)

	// expect B[0:M,0:K] == B0[0:M,0:K], B[M:N,0:K] == 0
	var X cmat.FloatMatrix

	X.SubMatrix(B, 0, 0, M, K)
	blasd.Plus(&X, B0, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(&X, lapackd.NORM_ONE)

	t.Logf("M=%d, N=%d  ||B0 - min( ||A.T*X - B0|| ) ||_1: %e\n", M, N, nrm)
}
Beispiel #6
0
func TestBlockedDecomposeCHOL(t *testing.T) {
	N := 119
	nb := 16

	conf := gomas.NewConf()
	conf.LB = nb

	Z := cmat.NewMatrix(N, N)
	AL := cmat.NewMatrix(N, N)
	AU := cmat.NewMatrix(N, N)

	unitrand := cmat.NewFloatUniformSource()
	Z.SetFrom(unitrand)

	blasd.Mult(AL, Z, Z, 1.0, 0.0, gomas.TRANSB)
	AU.Copy(AL)

	eu := lapackd.CHOLFactor(AU, gomas.UPPER, conf)
	el := lapackd.CHOLFactor(AL, gomas.LOWER, conf)
	_, _ = eu, el

	Z.Transpose(AU)
	if N < 10 {
		t.Logf("AU.T:\n%v\n", Z)
		t.Logf("AL:\n%v\n", AL)
	}
	ok := AL.AllClose(Z)
	t.Logf("Decompose(AL) == Decompose(AU).T: %v\n", ok)
}
Beispiel #7
0
// m > n: A[m,n], I[m,m] --> A == I*A == Q*Q.T*A
func TestQRTMultLeftIdent(t *testing.T) {
	M := 411
	N := 399
	nb := 16
	A := cmat.NewMatrix(M, N)
	T := cmat.NewMatrix(nb, N)

	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)
	A0 := cmat.NewCopy(A)
	C := cmat.NewCopy(A)
	conf := gomas.NewConf()
	conf.LB = nb
	//t.Logf("A0:\n%v\n", A0)

	// QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRTFactorWork(A, conf))
	lapackd.QRTFactor(A, T, W, conf)
	//t.Logf("T:\n%v\n", T)

	// C = Q.T*A
	W = lapackd.Workspace(lapackd.QRTMultWork(C, T, gomas.LEFT, conf))
	lapackd.QRTMult(C, A, T, W, gomas.LEFT|gomas.TRANS, conf)

	// C = Q*C == Q*Q.T*A
	lapackd.QRTMult(C, A, T, W, gomas.LEFT, conf)
	//t.Logf("A*Q*Q.T:\n%v\n", C)

	// A = A - Q*Q.T*A
	blasd.Plus(A0, C, 1.0, -1.0, gomas.NONE)
	// ||A - Q*Q.T*A||_1
	nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
	t.Logf("M=%d,N=%d  ||A - Q*Q.T*A||_1: %e\n", M, N, nrm)
}
Beispiel #8
0
// test that unblocked and blocked QRT are equal
func TestDecomposeQRT(t *testing.T) {
	M := 615
	N := 591
	nb := 16

	conf := gomas.NewConf()
	conf.LB = nb

	A := cmat.NewMatrix(M, N)
	T := cmat.NewMatrix(nb, N)
	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)

	A0 := cmat.NewCopy(A)
	T0 := cmat.NewMatrix(N, N)

	// blocked: QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRTFactorWork(A, conf))
	lapackd.QRTFactor(A, T, W, conf)

	conf.LB = 0
	lapackd.QRTFactor(A0, T0, W, conf)

	ok := A.AllClose(A0)
	t.Logf("blk.DecomposeQRT(A) == unblk.DecomposeQRT(A): %v\n", ok)

}
Beispiel #9
0
func TestDSyr1(t *testing.T) {

	const N = 911

	A := cmat.NewMatrix(N, N)
	X := cmat.NewMatrix(N, 1)
	B := cmat.NewMatrix(N, N)

	//ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatUniformSource(0.5, 2.0)

	A.SetFrom(zeromean, cmat.LOWER)
	X.SetFrom(zeromean)
	B.Copy(A)

	// B = A*B
	blasd.MVUpdate(B, X, X, 1.0)
	cmat.TriL(B, cmat.NONE)
	blasd.MVUpdateSym(A, X, 1.0, gomas.LOWER)
	ok := B.AllClose(A)
	t.Logf("MVUpdateSym(A, X, L) == TriL(MVUpdate(A, X, X)) : %v\n", ok)

	A.SetFrom(zeromean, cmat.UPPER)
	cmat.TriU(A, cmat.NONE)
	B.Copy(A)
	blasd.MVUpdate(B, X, X, 1.0)
	cmat.TriU(B, cmat.NONE)
	blasd.MVUpdateSym(A, X, 1.0, gomas.UPPER)
	ok = B.AllClose(A)
	t.Logf("MVUpdateSym(A, X, U) == TriU(MVUpdate(A, X, X)) : %v\n", ok)
}
Beispiel #10
0
func TestDTrmmUnitUpper(t *testing.T) {
	var d cmat.FloatMatrix
	N := 563
	K := 171

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(N, K)
	B0 := cmat.NewMatrix(N, K)
	C := cmat.NewMatrix(N, K)

	zeros := cmat.NewFloatConstSource(0.0)
	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatNormSource()

	A.SetFrom(zeromean, cmat.UPPER|cmat.UNIT)
	B.SetFrom(ones)
	B0.SetFrom(ones)
	// B = A*B
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.UNIT)
	d.Diag(A).SetFrom(ones)
	blasd.Mult(C, A, B0, 1.0, 0.0, gomas.NONE)
	ok := C.AllClose(B)
	t.Logf("trmm(B, A, L|U|N|U) == gemm(C, TriUU(A), B)   : %v\n", ok)

	B.SetFrom(ones)
	// B = A.T*B
	d.Diag(A).SetFrom(zeros)
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.TRANSA|gomas.UNIT)
	d.Diag(A).SetFrom(ones)
	blasd.Mult(C, A, B0, 1.0, 0.0, gomas.TRANSA)
	ok = C.AllClose(B)
	t.Logf("trmm(B, A, L|U|T|U) == gemm(C, TriUU(A).T, B) : %v\n", ok)
}
Beispiel #11
0
func TestSolveBKLowerBig(t *testing.T) {
	N := 427
	normsrc := cmat.NewFloatNormSource(5.0, 10.0)
	A := cmat.NewMatrix(N, N)
	A.SetFrom(normsrc, cmat.LOWER)

	X := cmat.NewMatrix(N, 2)
	X.SetFrom(normsrc)
	B := cmat.NewCopy(X)
	blasd.MultSym(B, A, X, 1.0, 0.0, gomas.LOWER|gomas.LEFT)

	ipiv := lapackd.NewPivots(N)

	conf := gomas.NewConf()
	conf.LB = 16
	W := lapackd.Workspace(lapackd.BKFactorWork(A, conf))
	lapackd.BKFactor(A, W, ipiv, gomas.LOWER, conf)

	lapackd.BKSolve(B, A, ipiv, gomas.LOWER, conf)
	ok := B.AllClose(X)
	t.Logf("N=%d unblk.BK(X) == A.-1*B : %v\n", N, ok)
	blasd.Plus(B, X, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(B, lapackd.NORM_ONE)
	t.Logf("  ||X - A.-1*B||_1: %.4e\n", nrm)

}
Beispiel #12
0
func TestDTrmmUnitUpperRight(t *testing.T) {
	var d cmat.FloatMatrix
	N := 563
	K := 171

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(K, N)
	B0 := cmat.NewMatrix(K, N)
	C := cmat.NewMatrix(K, N)

	zeros := cmat.NewFloatConstSource(0.0)
	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatNormSource()

	A.SetFrom(zeromean, cmat.UPPER|cmat.UNIT)
	B.SetFrom(ones)
	B0.SetFrom(ones)
	// B = B*A
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.RIGHT|gomas.UNIT)
	d.Diag(A).SetFrom(ones)
	blasd.Mult(C, B0, A, 1.0, 0.0, gomas.NONE)
	ok := C.AllClose(B)
	t.Logf("trmm(B, A, R|U|N|U) == gemm(C, B, TriUU(A))   : %v\n", ok)

	B.SetFrom(ones)
	// B = B*A.T
	d.SetFrom(zeros)
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.RIGHT|gomas.TRANSA|gomas.UNIT)
	d.SetFrom(ones)
	blasd.Mult(C, B0, A, 1.0, 0.0, gomas.TRANSB)
	ok = C.AllClose(B)
	t.Logf("trmm(B, A, R|U|T|U) == gemm(C, B, TriUU(A).T) : %v\n", ok)
}
Beispiel #13
0
// QR decompose A, then compute ||A - Q*R||_1, should be small
func TestUnblkQRMultLeft(t *testing.T) {
	M := 711
	N := 593
	A := cmat.NewMatrix(M, N)
	tau := cmat.NewMatrix(N, 1)

	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)
	A0 := cmat.NewCopy(A)

	conf := gomas.NewConf()
	conf.LB = 0

	// QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	lapackd.QRFactor(A, tau, W, conf)

	// C = TriU(QR) = R
	C := cmat.TriU(cmat.NewCopy(A), cmat.NONE)

	// C = Q*C
	W = lapackd.Workspace(lapackd.QRMultWork(C, gomas.LEFT, conf))
	err := lapackd.QRMult(C, A, tau, W, gomas.LEFT, conf)
	if err != nil {
		t.Logf("err: %v\n", err)
	}

	// A = A - QR
	blasd.Plus(A0, C, 1.0, -1.0, gomas.NONE)
	// ||A - Q*R||_1
	nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
	t.Logf("M=%d,N=%d  ||A - Q*R||_1: %e\n", M, N, nrm)
}
Beispiel #14
0
func TestDTrsm1(t *testing.T) {
	nofail := true

	const N = 31
	const K = 4

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(N, K)
	B0 := cmat.NewMatrix(N, K)

	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatUniformSource(1.0, -0.5)

	A.SetFrom(zeromean, cmat.LOWER)
	B.SetFrom(ones)
	B0.Copy(B)
	// B = A*B
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT)
	blasd.SolveTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT)
	ok := B0.AllClose(B)
	nofail = nofail && ok
	t.Logf("B == trsm(trmm(B, A, L|L|N), A, L|L|N) : %v\n", ok)
	if !ok {
		t.Logf("B|B0:\n%v\n", cmat.NewJoin(cmat.AUGMENT, B, B0))
	}

	B.Copy(B0)
	// B = A.T*B
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT|gomas.TRANSA)
	blasd.SolveTrm(B, A, 1.0, gomas.LOWER|gomas.LEFT|gomas.TRANSA)
	ok = B0.AllClose(B)
	nofail = nofail && ok
	t.Logf("B == trsm(trmm(B, A, L|L|T), A, L|L|T) : %v\n", ok)
}
Beispiel #15
0
// test that unblocked QR and QRT are equal
func TestQRFactor(t *testing.T) {
	M := 411
	N := 375
	nb := 16

	conf := gomas.NewConf()
	conf.LB = nb

	A := cmat.NewMatrix(M, N)
	//W := cmat.NewMatrix(N, nb)
	tau := cmat.NewMatrix(N, 1)
	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)

	A0 := cmat.NewCopy(A)
	tau0 := cmat.NewCopy(tau)

	// blocked: QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	lapackd.QRFactor(A, tau, W, conf)

	conf.LB = 0
	lapackd.QRFactor(A0, tau0, W, conf)

	ok := A.AllClose(A0)
	t.Logf("blk.QRFactor(A) == unblk.QRFactor(A): %v\n", ok)

	ok = tau0.AllClose(tau)
	t.Logf("blk QR.tau == unblk QR.tau: %v\n", ok)
}
Beispiel #16
0
// m > n: A[m,n], I[m,m] --> A.T == A.T*I == A.T*Q*Q.T
func TestBlockedQRMultRightIdent(t *testing.T) {
	M := 511
	N := 489
	A := cmat.NewMatrix(M, N)
	C := cmat.NewMatrix(N, M)
	tau := cmat.NewMatrix(N, 1)

	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)
	A0 := cmat.NewCopy(A)
	C.Transpose(A)
	conf := gomas.NewConf()
	conf.LB = 32

	// QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	lapackd.QRFactor(A, tau, W, conf)

	// C = A.T*Q
	W = lapackd.Workspace(lapackd.QRMultWork(C, gomas.RIGHT, conf))
	lapackd.QRMult(C, A, tau, W, gomas.RIGHT, conf)

	// C = C*Q.T == A.T*Q*Q.T
	lapackd.QRMult(C, A, tau, W, gomas.RIGHT|gomas.TRANS, conf)
	//t.Logf("A*Q*Q.T:\n%v\n", C)

	// A = A - (A.T*Q*Q.T).T
	blasd.Plus(A0, C, 1.0, -1.0, gomas.TRANSB)
	// ||A - (A.T*Q*Q.T).T||_1
	nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
	t.Logf("M=%d,N=%d  ||A - (A.T*Q*Q.T).T||_1: %e\n", M, N, nrm)
}
Beispiel #17
0
// Simple and slow LQ decomposition with Givens rotations
func TestGivensLQ(t *testing.T) {
	var d cmat.FloatMatrix
	M := 149
	N := 167
	A := cmat.NewMatrix(M, N)
	A1 := cmat.NewCopy(A)

	ones := cmat.NewFloatConstSource(1.0)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)
	A0 := cmat.NewCopy(A)

	Qt := cmat.NewMatrix(N, N)
	d.Diag(Qt)
	d.SetFrom(ones)

	// R = G(n)...G(2)G(1)*A; Q = G(1).T*G(2).T...G(n).T ;  Q.T = G(n)...G(2)G(1)
	for i := 0; i < M; i++ {
		// zero elements right of diagonal
		for j := N - 2; j >= i; j-- {
			c, s, r := lapackd.ComputeGivens(A.Get(i, j), A.Get(i, j+1))
			A.Set(i, j, r)
			A.Set(i, j+1, 0.0)
			// apply rotation to this column starting from row i+1
			lapackd.ApplyGivensRight(A, j, j+1, i+1, M-i-1, c, s)
			// update Qt = G(k)*Qt
			lapackd.ApplyGivensRight(Qt, j, j+1, 0, N, c, s)
		}
	}
	// A = L*Q
	blasd.Mult(A1, A, Qt, 1.0, 0.0, gomas.TRANSB)
	blasd.Plus(A0, A1, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
	t.Logf("M=%d, N=%d ||A - L*G(1)..G(n)||_1: %e\n", M, N, nrm)
}
Beispiel #18
0
func TestRowPivot(t *testing.T) {
	N := 7
	K := 3
	B := cmat.NewMatrix(N, K)
	B0 := cmat.NewMatrix(N, K)
	ipv := lapackd.NewPivots(7)
	for k, _ := range ipv {
		ipv[k] = k + 1
	}
	ipv[0] = 3
	ipv[3] = 7
	ipv[5] = 7
	t.Logf("pivots: %v\n", ipv)
	rowsetter := func(i, j int, v float64) float64 {
		return float64(i + 1)
	}
	B.Map(&cmat.FloatEvaluator{rowsetter})
	B0.Copy(B)

	lapackd.ApplyRowPivots(B, ipv, lapackd.FORWARD)
	t.Logf("pivot forward ...\n")
	lapackd.ApplyRowPivots(B, ipv, lapackd.BACKWARD)
	t.Logf("pivot backward ...\n")
	ok := B.AllClose(B0)
	t.Logf("result is original: %v\n", ok)
}
Beispiel #19
0
func TestTriRedUpper(t *testing.T) {
	N := 843
	nb := 48
	conf := gomas.NewConf()
	conf.LB = 0

	A := cmat.NewMatrix(N, N)
	tau := cmat.NewMatrix(N, 1)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src, cmat.UPPER)
	A1 := cmat.NewCopy(A)
	tau1 := cmat.NewCopy(tau)
	_ = A1

	W := lapackd.Workspace(N)
	W1 := lapackd.Workspace(N * nb)

	lapackd.TRDReduce(A, tau, W, gomas.UPPER, conf)

	conf.LB = nb
	lapackd.TRDReduce(A1, tau1, W1, gomas.UPPER, conf)

	blasd.Plus(A, A1, -1.0, 1.0, gomas.NONE)
	nrm := lapackd.NormP(A, lapackd.NORM_ONE)
	t.Logf("N=%d, ||unblk.Trired(A) - blk.Trired(A)||_1: %e\n", N, nrm)
	blasd.Axpy(tau, tau1, -1.0)
	nrm = blasd.Nrm2(tau)
	t.Logf("   ||unblk.Trired(tau) - blk.Trired(tau)||_1: %e\n", nrm)
}
Beispiel #20
0
func TestDTrms2(t *testing.T) {
	const N = 31
	const K = 4

	nofail := true

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(K, N)
	B0 := cmat.NewMatrix(K, N)

	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatUniformSource(1.0, -0.5)

	A.SetFrom(zeromean, cmat.LOWER)
	B.SetFrom(ones)
	B0.Copy(B)

	// B = B*A
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT)
	blasd.SolveTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT)
	ok := B0.AllClose(B)
	nofail = nofail && ok
	t.Logf("B == trsm(trmm(B, A, R|L|N), A, R|L|N) : %v\n", ok)

	B.Copy(B0)
	// B = B*A.T
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT|gomas.TRANSA)
	blasd.SolveTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT|gomas.TRANSA)
	ok = B0.AllClose(B)
	nofail = nofail && ok
	t.Logf("B == trsm(trmm(B, A, R|L|T), A, R|L|T) : %v\n", ok)
}
Beispiel #21
0
// QR decompose A, then compute ||A - (R.T*Q.T).T||_1, should be small
func TestUnblkQRMultRight(t *testing.T) {
	M := 711
	N := 593
	A := cmat.NewMatrix(M, N)
	C := cmat.NewMatrix(N, M)
	tau := cmat.NewMatrix(N, 1)

	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)
	A0 := cmat.NewCopy(A)

	conf := gomas.NewConf()
	conf.LB = 0

	// QR = A = Q*R
	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	lapackd.QRFactor(A, tau, W, conf)

	// C = transpose(TriU(QR)) = R.T
	C.Transpose(cmat.TriU(cmat.NewCopy(A), cmat.NONE))

	// C = C*Q.T = R.T*Q.T
	W = lapackd.Workspace(lapackd.QRMultWork(C, gomas.RIGHT, conf))
	err := lapackd.QRMult(C, A, tau, W, gomas.RIGHT|gomas.TRANS, conf)
	if err != nil {
		t.Logf("err: %v\n", err)
	}

	// A = A - QR
	blasd.Plus(A0, C, 1.0, -1.0, gomas.TRANSB)
	// ||A - Q*R||_1
	nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
	t.Logf("M=%d,N=%d  ||A - (R.T*Q.T).T||_1: %e\n", M, N, nrm)
}
Beispiel #22
0
func TestDTrsm3(t *testing.T) {
	const N = 31
	const K = 4

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(N, K)
	B0 := cmat.NewMatrix(N, K)

	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatUniformSource(1.0, -0.5)

	A.SetFrom(zeromean, cmat.UPPER)
	B.SetFrom(ones)
	B0.Copy(B)
	// B = A*B
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT)
	blasd.SolveTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT)
	ok := B0.AllClose(B)
	t.Logf("B == trsm(trmm(B, A, L|U|N), A, L|U|N) : %v\n", ok)

	B.Copy(B0)
	// B = A.T*B
	blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.TRANSA)
	blasd.SolveTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.TRANSA)
	ok = B0.AllClose(B)
	t.Logf("B == trsm(trmm(B, A, L|U|T), A, L|U|T) : %v\n", ok)
}
Beispiel #23
0
func TestDTrmmLowerRight(t *testing.T) {
	N := 563
	K := 171
	nofail := true

	A := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(K, N)
	B0 := cmat.NewMatrix(K, N)
	C := cmat.NewMatrix(K, N)

	ones := cmat.NewFloatConstSource(1.0)
	zeromean := cmat.NewFloatNormSource()

	A.SetFrom(zeromean, cmat.LOWER)
	B.SetFrom(ones)
	B0.SetFrom(ones)
	// B = B*A
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT)
	blasd.Mult(C, B0, A, 1.0, 0.0, gomas.NONE)
	ok := C.AllClose(B)
	nofail = nofail && ok
	t.Logf("trmm(B, A, R|L|N) == gemm(C, B, TriL(A))   : %v\n", ok)

	B.SetFrom(ones)
	// B = B*A.T
	blasd.MultTrm(B, A, 1.0, gomas.LOWER|gomas.RIGHT|gomas.TRANSA)
	blasd.Mult(C, B0, A, 1.0, 0.0, gomas.TRANSB)
	ok = C.AllClose(B)
	nofail = nofail && ok
	t.Logf("trmm(B, A, R|L|T) == gemm(C, B, TriL(A).T) : %v\n", ok)
}
Beispiel #24
0
// test: unblk.ReduceHess(A) == blk.ReduceHess(A)
func TestReduceHess(t *testing.T) {
	N := 375
	nb := 16

	conf := gomas.NewConf()
	conf.LB = nb

	A := cmat.NewMatrix(N, N)
	tau := cmat.NewMatrix(N, 1)
	zeromean := cmat.NewFloatNormSource()
	A.SetFrom(zeromean)

	A0 := cmat.NewCopy(A)
	tau0 := cmat.NewCopy(tau)

	// blocked reduction
	W := lapackd.Workspace(lapackd.HessReduceWork(A, conf))
	lapackd.HessReduce(A, tau, W, conf)

	// unblocked reduction
	conf.LB = 0
	lapackd.HessReduce(A0, tau0, W, conf)

	ok := A.AllClose(A0)
	t.Logf("blk.ReduceHess(A) == unblk.ReduceHess(A): %v\n", ok)

	ok = tau0.AllClose(tau)
	t.Logf("blk HessQ.tau == unblk HessQ.tau: %v\n", ok)

	// ||A - A0||_1
	blasd.Plus(A, A0, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(A, lapackd.NORM_ONE)
	t.Logf("||H - H0||_1: %e\n", nrm)
}
Beispiel #25
0
func TestLQFactor(t *testing.T) {
	M := 611
	N := 715
	nb := 32
	conf := gomas.NewConf()

	A := cmat.NewMatrix(M, N)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)
	tau := cmat.NewMatrix(M, 1)

	A1 := cmat.NewCopy(A)
	tau1 := cmat.NewCopy(tau)

	conf.LB = 0
	W := cmat.NewMatrix(M+N, 1)
	lapackd.LQFactor(A, tau, W, conf)

	conf.LB = nb
	W1 := lapackd.Workspace(lapackd.LQFactorWork(A1, conf))
	lapackd.LQFactor(A1, tau1, W1, conf)

	blasd.Plus(A1, A, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(A1, lapackd.NORM_ONE)
	t.Logf("M=%d, N=%d ||blk.LQ(A) - unblk.LQ(A)||_1: %e\n", M, N, nrm)
}
Beispiel #26
0
func test_bdsvd(N, flags, kind int, verbose bool, t *testing.T) {
	var At, sD, sE, tmp cmat.FloatMatrix

	uplo := "upper"
	offdiag := 1
	if flags&gomas.LOWER != 0 {
		offdiag = -1
		uplo = "lower"
	}
	A0 := cmat.NewMatrix(N, N)
	desc := setDiagonals(A0, offdiag, kind)
	At.SubMatrix(A0, 0, 0, N, N)
	sD.Diag(A0, 0)
	sE.Diag(A0, offdiag)
	D := cmat.NewCopy(&sD)
	E := cmat.NewCopy(&sE)

	// unit singular vectors
	U := cmat.NewMatrix(N, N)
	sD.Diag(U, 0)
	sD.Add(1.0)

	V := cmat.NewMatrix(N, N)
	sD.Diag(V, 0)
	sD.Add(1.0)

	W := cmat.NewMatrix(4*N, 1)
	C := cmat.NewMatrix(N, N)

	lapackd.BDSvd(D, E, U, V, W, flags|gomas.WANTU|gomas.WANTV)

	blasd.Mult(C, U, U, 1.0, 0.0, gomas.TRANSA)
	sD.Diag(C)
	sD.Add(-1.0)
	nrmu := lapackd.NormP(C, lapackd.NORM_ONE)

	blasd.Mult(C, V, V, 1.0, 0.0, gomas.TRANSB)
	sD.Add(-1.0)
	nrmv := lapackd.NormP(C, lapackd.NORM_ONE)

	blasd.Mult(C, U, A0, 1.0, 0.0, gomas.TRANSA)
	blasd.Mult(&At, C, V, 1.0, 0.0, gomas.TRANSB)
	if verbose && N < 10 {
		t.Logf("D:\n%v\n", asRow(&tmp, D))
		t.Logf("U:\n%v\n", U)
		t.Logf("V:\n%v\n", V)
		t.Logf("U.T*A*V\n%v\n", &At)
	}
	sD.Diag(&At)
	blasd.Axpy(&sD, D, -1.0)
	nrma := lapackd.NormP(&At, lapackd.NORM_ONE)

	t.Logf("N=%d [%s,%s] ||U.T*A*V - bdsvd(A)||_1: %e\n", N, uplo, desc, nrma)
	t.Logf("  ||I - U.T*U||_1: %e\n", nrmu)
	t.Logf("  ||I - V.T*V||_1: %e\n", nrmv)
}
Beispiel #27
0
// test: C = C*Q.T
func TestQLMultRightTrans(t *testing.T) {
	var d, di0, di1 cmat.FloatMatrix
	M := 891
	N := 853
	lb := 36
	conf := gomas.NewConf()

	A := cmat.NewMatrix(M, N)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)

	C0 := cmat.NewMatrix(N, M)
	d.Diag(C0, M-N)
	ones := cmat.NewFloatConstSource(1.0)
	d.SetFrom(ones)
	C1 := cmat.NewCopy(C0)

	I0 := cmat.NewMatrix(N, N)
	I1 := cmat.NewCopy(I0)
	di0.Diag(I0)
	di1.Diag(I1)

	tau := cmat.NewMatrix(N, 1)
	W := cmat.NewMatrix(lb*(M+N), 1)

	conf.LB = lb
	lapackd.QLFactor(A, tau, W, conf)

	conf.LB = 0
	lapackd.QLMult(C0, A, tau, W, gomas.RIGHT|gomas.TRANS, conf)
	// I = Q*Q.T - I
	blasd.Mult(I0, C0, C0, 1.0, 0.0, gomas.TRANSB, conf)
	blasd.Add(&di0, -1.0)
	n0 := lapackd.NormP(I0, lapackd.NORM_ONE)

	conf.LB = lb
	lapackd.QLMult(C1, A, tau, W, gomas.RIGHT|gomas.TRANS, conf)
	// I = Q*Q.T - I
	blasd.Mult(I1, C1, C1, 1.0, 0.0, gomas.TRANSB, conf)
	blasd.Add(&di1, -1.0)
	n1 := lapackd.NormP(I1, lapackd.NORM_ONE)

	if N < 10 {
		t.Logf("unblk C0*Q:\n%v\n", C0)
		t.Logf("blk. C2*Q:\n%v\n", C1)
	}
	blasd.Plus(C0, C1, 1.0, -1.0, gomas.NONE)
	n2 := lapackd.NormP(C0, lapackd.NORM_ONE)

	t.Logf("M=%d, N=%d ||unblk.QLMult(C) - blk.QLMult(C)||_1: %e\n", M, N, n2)
	t.Logf("unblk M=%d, N=%d ||I - Q*Q.T||_1: %e\n", M, N, n0)
	t.Logf("blk   M=%d, N=%d ||I - Q*Q.T||_1: %e\n", M, N, n1)
}
Beispiel #28
0
func TestUpperCHOL(t *testing.T) {
	N := 311
	K := 43
	nb := 0

	conf := gomas.NewConf()
	conf.LB = nb

	Z := cmat.NewMatrix(N, N)
	A := cmat.NewMatrix(N, N)
	A0 := cmat.NewMatrix(N, N)
	B := cmat.NewMatrix(N, K)
	X := cmat.NewMatrix(N, K)

	unitrand := cmat.NewFloatUniformSource()
	Z.SetFrom(unitrand)

	blasd.Mult(A, Z, Z, 1.0, 0.0, gomas.TRANSB)
	A0.Copy(A)

	B.SetFrom(unitrand)
	X.Copy(B)

	// A = chol(A) = U.T*U
	t.Logf("Unblocked version: nb=%d\n", conf.LB)
	lapackd.CHOLFactor(A, gomas.UPPER, conf)
	// X = A.-1*B = U.-1*(U.-T*B)
	lapackd.CHOLSolve(X, A, gomas.UPPER)
	// B = B - A*X
	blasd.Mult(B, A0, X, -1.0, 1.0, gomas.NONE)
	// ||B - A*X||_1
	nrm := lapackd.NormP(B, lapackd.NORM_ONE)
	t.Logf("N=%d:  ||B - A*X||_1: %e\n", N, nrm)

	// A = chol(A) = U.T*U
	A.Copy(A0)
	B.SetFrom(unitrand)
	X.Copy(B)
	conf.LB = 16
	t.Logf("Blocked version: nb=%d\n", conf.LB)
	lapackd.CHOLFactor(A, gomas.UPPER, conf)
	// X = A.-1*B = U.-1*(U.-T*B)
	lapackd.CHOLSolve(X, A, gomas.UPPER)
	// B = B - A*X
	blasd.Mult(B, A0, X, -1.0, 1.0, gomas.NONE)
	// ||B - A*X||_1
	nrm = lapackd.NormP(B, lapackd.NORM_ONE)
	t.Logf("N=%d:  ||B - A*X||_1: %e\n", N, nrm)

}
Beispiel #29
0
func TestQLBuildwithK(t *testing.T) {
	var dc cmat.FloatMatrix
	M := 711
	N := 707
	K := 691
	lb := 36
	conf := gomas.NewConf()

	A := cmat.NewMatrix(M, N)
	src := cmat.NewFloatNormSource()
	A.SetFrom(src)
	tau := cmat.NewMatrix(N, 1)

	W := cmat.NewMatrix(M+N, 1)
	C := cmat.NewMatrix(N, N)

	conf.LB = lb
	lapackd.QLFactor(A, tau, W, conf)
	A1 := cmat.NewCopy(A)

	conf.LB = 0
	lapackd.QLBuild(A, tau, W, K, conf)
	blasd.Mult(C, A, A, 1.0, 0.0, gomas.TRANSA, conf)
	dc.Diag(C)
	blasd.Add(&dc, -1.0)
	if N < 10 {
		t.Logf("unblk.QLBuild Q:\n%v\n", A)
		t.Logf("unblk.QLBuild Q.T*Q:\n%v\n", C)
	}
	n0 := lapackd.NormP(C, lapackd.NORM_ONE)

	conf.LB = lb
	W1 := lapackd.Workspace(lapackd.QLBuildWork(A1, conf))
	lapackd.QLBuild(A1, tau, W1, K, conf)
	if N < 10 {
		t.Logf("blk.QLBuild Q:\n%v\n", A1)
	}
	// compute: I - Q.T*Q
	blasd.Mult(C, A1, A1, 1.0, 0.0, gomas.TRANSA, conf)
	blasd.Add(&dc, -1.0)
	n1 := lapackd.NormP(C, lapackd.NORM_ONE)

	blasd.Plus(A, A1, 1.0, -1.0, gomas.NONE)
	n2 := lapackd.NormP(A, lapackd.NORM_ONE)

	t.Logf("M=%d, N=%d, K=%d ||unblk.QLBuild(A) - blk.QLBuild(A)||_1 :%e\n", M, N, K, n2)
	t.Logf("unblk M=%d, N=%d, K=%d ||Q.T*Q - I||_1 : %e\n", M, N, K, n0)
	t.Logf("blk   M=%d, N=%d, K=%d ||Q.T*Q - I||_1 : %e\n", M, N, K, n1)
}
Beispiel #30
0
func TestTrdMultUpper(t *testing.T) {
	var dt, et, da, ea cmat.FloatMatrix
	N := 843
	nb := 48
	conf := gomas.NewConf()
	conf.LB = nb

	A := cmat.NewMatrix(N, N)
	tau := cmat.NewMatrix(N, 1)
	src := cmat.NewFloatNormSource()
	// create symmetric matrix
	A.SetFrom(src, cmat.SYMM)
	A0 := cmat.NewCopy(A)

	W := lapackd.Workspace(lapackd.TRDReduceWork(A, conf))
	lapackd.TRDReduce(A, tau, W, gomas.UPPER, conf)

	// make tridiagonal matrix T
	T0 := cmat.NewMatrix(N, N)
	dt.Diag(T0)
	da.Diag(A)
	blasd.Copy(&dt, &da)

	ea.Diag(A, 1)
	et.Diag(T0, 1)
	blasd.Copy(&et, &ea)
	et.Diag(T0, -1)
	blasd.Copy(&et, &ea)
	T1 := cmat.NewCopy(T0)

	// compute Q*T*Q.T (unblocked)
	conf.LB = 0
	lapackd.TRDMult(T0, A, tau, W, gomas.LEFT|gomas.UPPER, conf)
	lapackd.TRDMult(T0, A, tau, W, gomas.RIGHT|gomas.TRANS|gomas.UPPER, conf)

	blasd.Plus(T0, A0, 1.0, -1.0, gomas.NONE)
	nrm := lapackd.NormP(T0, lapackd.NORM_ONE)
	t.Logf("N=%d, unblk.||A - Q*T*Q.T||_1: %e\n", N, nrm)

	// compute Q*T*Q.T (blocked)
	conf.LB = nb
	W = lapackd.Workspace(lapackd.TRDMultWork(A, gomas.LEFT|gomas.UPPER, conf))
	lapackd.TRDMult(T1, A, tau, W, gomas.LEFT|gomas.UPPER, conf)
	lapackd.TRDMult(T1, A, tau, W, gomas.RIGHT|gomas.TRANS|gomas.UPPER, conf)

	blasd.Plus(T1, A0, 1.0, -1.0, gomas.NONE)
	nrm = lapackd.NormP(T1, lapackd.NORM_ONE)
	t.Logf("N=%d,   blk.||A - Q*T*Q.T||_1: %e\n", N, nrm)
}