Esempio n. 1
0
        public static object SVD(double[,] Data)
        {
            DenseMatrix data = new DenseMatrix(Data);
            int         nRows = data.RowCount, nCols = data.ColumnCount;

            MathNet.Numerics.LinearAlgebra.Double.Factorization.Svd svd = data.Svd(true);

            Matrix <double> u  = svd.U();   // m x m
            Matrix <double> w  = svd.W();   // m x n, diagonal
            Matrix <double> vt = svd.VT();  // n x n, transpose

            object[,] ret = new object[Math.Max(nRows, nCols), nRows + 1 + nCols + 1 + nCols];
            for (int i = 0; i < ret.GetLength(0); ++i)
            {
                for (int j = 0; j < ret.GetLength(1); ++j)
                {
                    ret[i, j] = "";
                }
            }

            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < nRows; ++j)
                {
                    ret[i, j] = u[i, j];
                }
            }

            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < nCols; ++j)
                {
                    ret[i, nRows + 1 + j] = w[i, j];
                }
            }

            for (int i = 0; i < nCols; ++i)
            {
                for (int j = 0; j < nCols; ++j)
                {
                    ret[i, nRows + 1 + nCols + 1 + j] = vt[j, i];                   // Re-transpose, so that they're in the expected format.
                }
            }

            return(ret);
        }