Exemplo n.º 1
0
        public static SimpleMatrix <T> identity(int width, Type type)
        {
            SimpleMatrix <T> ret = new SimpleMatrix <T>(width, width, type);

            if (type == typeof(DMatrixRMaj))
            {
                CommonOps_DDRM.setIdentity(ret.mat as DMatrixRMaj);
            }
            else if (type == typeof(FMatrixRMaj))
            {
                CommonOps_FDRM.setIdentity(ret.mat as FMatrixRMaj);
            }
            else
            {
                throw new InvalidOperationException("Matrix type must be DMatrixRMaj or FMatrixRMaj.");
            }

            return(ret);
        }
Exemplo n.º 2
0
        public SimpleSVD(Matrix mat, bool compact)
        {
            this.mat  = mat;
            this.is64 = mat is DMatrixRMaj;
            if (is64)
            {
                DMatrixRMaj m = (DMatrixRMaj)mat;
                svd = (SingularValueDecomposition <T>)DecompositionFactory_DDRM.svd(m.numRows, m.numCols, true, true, compact);
            }
            else
            {
                FMatrixRMaj m = (FMatrixRMaj)mat;
                svd = (SingularValueDecomposition <T>)DecompositionFactory_FDRM.svd(m.numRows, m.numCols, true, true, compact);
            }

            if (!svd.decompose((T)mat))
            {
                throw new InvalidOperationException("Decomposition failed");
            }
            U = SimpleMatrix <T> .wrap(svd.getU(null, false));

            W = SimpleMatrix <T> .wrap(svd.getW(null));

            V = SimpleMatrix <T> .wrap(svd.getV(null, false));

            // order singular values from largest to smallest
            if (is64)
            {
                var um = U.getMatrix() as DMatrixRMaj;
                var wm = W.getMatrix() as DMatrixRMaj;
                var vm = V.getMatrix() as DMatrixRMaj;
                SingularOps_DDRM.descendingOrder(um, false, wm, vm, false);
                tol = SingularOps_DDRM.singularThreshold((SingularValueDecomposition_F64 <DMatrixRMaj>)svd);
            }
            else
            {
                var um = U.getMatrix() as FMatrixRMaj;
                var wm = W.getMatrix() as FMatrixRMaj;
                var vm = V.getMatrix() as FMatrixRMaj;
                SingularOps_FDRM.descendingOrder(um, false, wm, vm, false);
                tol = SingularOps_FDRM.singularThreshold((SingularValueDecomposition_F32 <FMatrixRMaj>)svd);
            }
        }
Exemplo n.º 3
0
        public static SimpleMatrix <T> diag(Type type, double[] vals)
        {
            T m;

            if (type == typeof(DMatrixRMaj))
            {
                m = CommonOps_DDRM.diag(vals) as T;
            }
            else
            {
                float[] f = new float[vals.Length];
                for (int i = 0; i < f.Length; i++)
                {
                    f[i] = (float)vals[i];
                }
                m = CommonOps_FDRM.diag(f) as T;
            }
            SimpleMatrix <T> ret = wrap(m);

            return(ret);
        }
Exemplo n.º 4
0
 /**
  * Creats a new SimpleMatrix which is identical to the original.
  *
  * @param orig The matrix which is to be copied. Not modified.
  */
 public SimpleMatrix(SimpleMatrix <T> orig)
 {
     setMatrix((T)orig.mat.copy());
 }
Exemplo n.º 5
0
        /**
         * <p>Converts the block matrix into a SimpleMatrix.</p>
         *
         * @param A Block matrix that is being converted.  Not modified.
         * @return Equivalent SimpleMatrix.
         */
        public static SimpleMatrix <T> convertSimple <T>(DMatrixRBlock A) where T : class, Matrix
        {
            var B = ConvertDMatrixStruct.convert(A, null) as T;

            return(SimpleMatrix <T> .wrap(B));
        }