public void SVDMatrixTest() { double[,] Arr1 = new double[3, 3] { { 4, -1, 1 }, { -1, 4.25, 2.75 }, { 1, 2.75, 3.5 } }; double[,] Arr2 = new double[4, 4] { { 1, 1, 0, 3 }, { 2, 1, -1, 1 }, { 3, -1, -1, 2 }, { -1, 2, 3, -1 } }; double[,] Arr3 = new double[2, 3] { { 3, 1, 1 }, { -1, 3, 1 } }; double[,] Arr4 = new double[4, 4] { { 3, 1, 0, 1 }, { 1, 3, 1, 1 }, { 0, 1, 3, 1 }, { 1, 1, 1, 3 } }; MMatrix A = new MMatrix(Arr2); MMatrix B = new MMatrix(); string s = ""; try { //A = MMatrix.RandomMatrix(size,0,255); A.FactorSVD(); s = "Factor SVD:\n"; s += "\nMatrix A=\n" + A.PrintToString(); s += "\nU=\n" + (new MMatrix(A.SVD_U)).PrintToString(); s += "\nS=\n" + (new MMatrix(A.SVD_S)).PrintToString(); s += "\nVt=\n" + (new MMatrix(A.SVD_Vt)).PrintToString(); B = (new MMatrix(A.SVD_U)) * (new MMatrix(A.SVD_S)) * (new MMatrix(A.SVD_Vt)); s += "\nA=USV*\n" + B.PrintToString(); this.RichText_Display(s); OutputMatrix = A; } catch (MMatrixException exp) { MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void SVD(ref MMatrix mat, int newrank) { if (newrank >= Math.Min(mat.row, mat.col)) return; mat.FactorSVD(); //MMatrix.FactorSVD_LDLt(mat, mat.row); MMatrix U = new MMatrix(mat.SVD_U, mat.row, newrank); MMatrix S = new MMatrix(mat.SVD_S, newrank, newrank); MMatrix Vt = new MMatrix(mat.SVD_Vt, newrank, mat.col); mat = U * S * Vt; }