func TestMultQT(t *testing.T) { M := 60 N := 40 K := 30 nb := 12 A := matrix.FloatUniform(M, N) B := matrix.FloatUniform(M, K) W := matrix.FloatZeros(N, nb) T := matrix.FloatZeros(N, N) // QR = Q*R QR, err := DecomposeQRT(A.Copy(), T, W, nb) if err != nil { t.Logf("decompose-err: %v\n", err) } // compute: B - Q*Q.T*B = 0 // X = Q*Q.T*B X := B.Copy() MultQT(X, QR, T, W, LEFT|TRANS, nb) MultQT(X, QR, T, W, LEFT, nb) B.Minus(X) // ||B - Q*Q.T*B||_1 nrm := NormP(B, NORM_ONE) t.Logf("||B - Q*Q.T*B||_1: %e\n", nrm) }
func TestSolveLeastSquaresQRT(t *testing.T) { M := 60 N := 40 K := 30 nb := 12 A := matrix.FloatUniform(M, N) B := matrix.FloatZeros(M, K) X0 := matrix.FloatUniform(N, K) // B = A*X0 Mult(B, A, X0, 1.0, 0.0, NOTRANS) W := matrix.FloatZeros(N, nb) T := matrix.FloatZeros(N, N) QR, err := DecomposeQRT(A, T, W, nb) if err != nil { t.Logf("decompose error: %v\n", err) } // B' = A.-1*B err = SolveQRT(B, QR, T, W, NOTRANS, nb) // expect B[0:N, 0:K] == X0, B[N:M, 0:K] == 0.0 var Xref matrix.FloatMatrix Bref := matrix.FloatZeros(M, K) Bref.SubMatrix(&Xref, 0, 0, N, K) Xref.Plus(X0) Bref.Minus(B) t.Logf("\nmin ||B - A*X0||\n\twhere B = A*X0\n") t.Logf("||B - A*X0||_1 ~~ 0.0: %e\n", NormP(Bref, NORM_ONE)) }
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)) }
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) }
func _TestSwap(t *testing.T) { A := matrix.FloatUniform(5, 5) t.Logf("A:\n%v\n", A) swapRows(A, 0, 4) t.Logf("A: 0,4 swapped\n%v\n", A) swapRows(A, 4, 0) t.Logf("A: 4,0 swapped again\n%v\n", A) }
func TestLU(t *testing.T) { N := 60 K := 30 nb := 12 A := matrix.FloatUniform(N, N) B := matrix.FloatUniform(N, K) X := B.Copy() piv := make([]int, N, N) // R = lu(A) = P*L*U R, _ := DecomposeLU(A.Copy(), piv, nb) // X = A.-1*B = U.-1*(L.-1*B) SolveLU(X, R, piv, NONE) // B = B - A*X Mult(B, A, X, -1.0, 1.0, NONE) nrm := NormP(B, NORM_ONE) t.Logf("||B - A*X||_1: %e\n", nrm) }
func TestLowerCHOL(t *testing.T) { N := 60 K := 30 nb := 16 Z := matrix.FloatUniform(N, N) A := matrix.Times(Z, Z.Transpose()) B := matrix.FloatUniform(N, K) X := B.Copy() // R = chol(A) = L*L.T R, _ := DecomposeCHOL(A.Copy(), LOWER, nb) // X = A.-1*B = L.-T*(L.-1*B) SolveCHOL(X, R, LOWER) // B = B - A*X Mult(B, A, X, -1.0, 1.0, NONE) nrm := NormP(B, NORM_ONE) t.Logf("||B - A*X||_1: %e\n", nrm) }
func TestUpperCHOL(t *testing.T) { N := 60 K := 30 nb := 16 Z := matrix.FloatUniform(N, N) A := matrix.Times(Z, Z.Transpose()) B := matrix.FloatUniform(N, K) X := B.Copy() // R = chol(A) = U.T*U R, _ := DecomposeCHOL(TriU(A.Copy()), UPPER, nb) // X = A.-1*B = U.-1*(U.-T*B) SolveCHOL(X, R, UPPER) // B = B - A*X Mult(B, A, X, -1.0, 1.0, NONE) // ||B - A*X||_1 nrm := NormP(B, NORM_ONE) t.Logf("||B - A*X||_1: %e\n", nrm) }
func TestSolveMinQRT(t *testing.T) { var QR *matrix.FloatMatrix var err error N := 40 K := 30 nb := 12 A := matrix.FloatUniform(N, N) B0 := matrix.FloatUniform(N, K) W := matrix.FloatZeros(N, nb) T := matrix.FloatZeros(N, N) QR, err = DecomposeQRT(A.Copy(), T, W, nb) if err != nil { t.Logf("decompose error: %v\n", err) } // B' = A.-1*B B := B0.Copy() err = SolveQRT(B, QR, T, W, TRANS, nb) Mult(B0, A, B, -1.0, 1.0, TRANSA) t.Logf("\nmin ||X||\n\ts.t. A.T*X = B\n\twhere A is N-by-N, B is N-by-K\n") t.Logf("||B - A.T*X||_1 ~~ 0.0: %e\n", NormP(B0, NORM_ONE)) }
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)) */ }
func TestDecomposeQRT(t *testing.T) { M := 60 N := 40 nb := 12 A := matrix.FloatUniform(M, N) W := matrix.FloatZeros(N, N) T := matrix.FloatZeros(N, N) // QR = Q*R QR, _ := DecomposeQRT(A.Copy(), T, W, nb) // A2 = Q*R A2 := TriU(QR.Copy()) MultQT(A2, QR, T, W, LEFT, 0) A.Minus(A2) // ||A - Q*R||_1 nrm := NormP(A, NORM_ONE) t.Logf("||A - Q*R||_1: %e\n", nrm) }
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) }
func TestQRBlk(t *testing.T) { M := 8 N := 6 nb := 2 A := matrix.FloatUniform(M, N) Tau := matrix.FloatZeros(M, 1) Tz := matrix.FloatZeros(N, N) Tx := matrix.FloatZeros(N, N) DecomposeBlockSize(0) Z, _ := DecomposeQRT(A.Copy(), Tz, nil, 0) _ = Z DecomposeBlockSize(nb) //X, _ := DecomposeQR(A.Copy(), Tau, nil) X, _ := DecomposeQRT(A.Copy(), Tx, nil, nb) Tau0 := matrix.FloatZeros(M, 1) A0 := A.Copy() lapack.Geqrf(A0, Tau0) ok := X.AllClose(A0) var dx matrix.FloatMatrix dx.DiagOf(Tx) //okt := Tau.AllClose(Tau0) okt := true _ = Tau t.Logf("lapack QR == DecomposeQR: %v\n", ok && okt) if !ok || !okt || true { t.Logf("A0: %d, %d, %d\n", A0.Rows(), A0.Cols(), A0.LeadingIndex()) t.Logf("A\n%v\n", A) t.Logf("X\n%v\n", X) t.Logf("Tz\n%v\n", Tz) t.Logf("Tx\n%v\n", Tx) t.Logf("Tau\n%v\n", &dx) t.Logf("lapack X\n%v\n", A0) t.Logf("lapack Tau\n%v\n", Tau0) } }
func TestQRT(t *testing.T) { M := 6 N := 5 var Tau matrix.FloatMatrix A := matrix.FloatUniform(M, N) T := matrix.FloatZeros(A.Cols(), A.Cols()) T0 := T.Copy() X, _ := DecomposeQRT(A.Copy(), T, nil, 0) Tau.DiagOf(T) Tau0 := matrix.FloatZeros(M, 1) A0 := A.Copy() lapack.Geqrf(A0, Tau0) ok := X.AllClose(A0) okt := Tau.AllClose(Tau0) t.Logf("lapack QR == DecomposeQR: %v\n", ok && okt) if !ok || !okt { t.Logf("A0: %d, %d, %d\n", A0.Rows(), A0.Cols(), A0.LeadingIndex()) t.Logf("A\n%v\n", A) t.Logf("X\n%v\n", X) t.Logf("Tau\n%v\n", &Tau) t.Logf("lapack X\n%v\n", A0) t.Logf("lapack Tau\n%v\n", Tau0) } // build block reflectors //unblkQRBlockReflector(X, Tau, T) V := TriLU(A0.Copy()) lapack.LarftFloat(V, Tau0, T0) ok = T0.AllClose(T) t.Logf("lapack.dlarft == QRBlockReflector: %v\n", ok) if !ok { t.Logf("T:\n%v\n", T) t.Logf("lapack T0:\n%v\n", T0) } }