Example #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)
}
Example #2
0
// test: min || B - A*X ||
func TestLeastSquaresQR(t *testing.T) {
	M := 811
	N := 723
	K := 311
	nb := 32
	conf := gomas.NewConf()
	conf.LB = nb

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

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

	W := lapackd.Workspace(lapackd.QRFactorWork(A, conf))
	err := lapackd.QRFactor(A, tau, W, conf)
	if err != nil {
		t.Logf("DecomposeQR: %v\n", err)
	}

	// B' = A.-1*B
	err = lapackd.QRSolve(B, A, tau, W, gomas.NONE, conf)
	if err != nil {
		t.Logf("SolveQR: %v\n", err)
	}

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

	X.SubMatrix(B, 0, 0, N, 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*X - B0|| ) ||_1: %e\n", M, N, nrm)
}