Ejemplo n.º 1
0
        /**
         * <p>
         * If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returned
         * </p>
         *
         * @see CommonOps_DDRM#extractDiag(DMatrixRMaj, DMatrixRMaj)
         * @return Diagonal elements inside a vector or a square matrix with the same diagonal elements.
         */
        public T diag <T>()
            where T : SimpleMatrix <W>
        {
            T diag;

            if (bits() == 64)
            {
                if (MatrixFeatures_DDRM.isVector(mat))
                {
                    int N = Math.Max(mat.NumCols, mat.NumRows);
                    diag = (T)createMatrix(N, N, mat.Type);
                    CommonOps_DDRM.diag((DMatrixRMaj)diag.mat, N, ((DMatrixRMaj)mat).data);
                }
                else
                {
                    int N = Math.Min(mat.NumCols, mat.NumRows);
                    diag = (T)createMatrix(N, 1, mat.Type);
                    CommonOps_DDRM.extractDiag((DMatrixRMaj)mat, (DMatrixRMaj)diag.mat);
                }
            }
            else
            {
                if (MatrixFeatures_DDRM.isVector(mat))
                {
                    int N = Math.Max(mat.NumCols, mat.NumRows);
                    diag = (T)createMatrix(N, N, mat.Type);
                    CommonOps_DDRM.diag((DMatrixRMaj)diag.mat, N, ((DMatrixRMaj)mat).data);
                }
                else
                {
                    int N = Math.Min(mat.NumCols, mat.NumRows);
                    diag = (T)createMatrix(N, 1, mat.Type);
                    CommonOps_DDRM.extractDiag((DMatrixRMaj)mat, (DMatrixRMaj)diag.mat);
                }
                //if (MatrixFeatures_FDRM.isVector(mat))
                //{
                //	int N = Math.Max(mat.NumCols, mat.NumRows);
                //	diag = createMatrix(N, N, mat.Type);
                //	CommonOps_FDRM.diag((FMatrixRMaj)diag.mat, N, ((FMatrixRMaj)mat).data);
                //}
                //else
                //{
                //	int N = Math.min(mat.NumCols, mat.NumRows);
                //	diag = createMatrix(N, 1, mat.Type);
                //	CommonOps_FDRM.extractDiag((FMatrixRMaj)mat, (FMatrixRMaj)diag.mat);
                //}
            }

            return(diag);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// If a vector then a square matrix is returned if a matrix then a vector of diagonal ements is returned.
        /// </summary>
        /// <returns>Diagonal elements inside a vector or a square matrix with the same diagonal elements.</returns>
        /// <see cref="CommonOps_DDRM.extractDiag(DMatrixRMaj, DMatrixRMaj)"/>
        public SimpleMatrixD diag()
        {
            SimpleMatrixD diag;

            if (MatrixFeatures_DDRM.isVector(mat))
            {
                int N = Math.Max(mat.getNumCols(), mat.getNumRows());
                diag = createMatrix(N, N);
                var dm = diag.getMatrix();
                CommonOps_DDRM.diag(dm, N, mat.data);
            }
            else
            {
                int N = Math.Min(mat.getNumCols(), mat.getNumRows());
                diag = createMatrix(N, 1);
                var dm = diag.getMatrix();
                CommonOps_DDRM.extractDiag(mat, dm);
            }

            return(diag);
        }