/**
  *
  * @param computeVectors
  * @param tol Tolerance for a matrix being symmetric
  */
 public SwitchingEigenDecomposition_FDRM(int matrixSize, bool computeVectors, float tol)
 {
     symmetricAlg        = DecompositionFactory_FDRM.eig(matrixSize, computeVectors, true);
     generalAlg          = DecompositionFactory_FDRM.eig(matrixSize, computeVectors, false);
     this.computeVectors = computeVectors;
     this.tol            = tol;
 }
Ejemplo n.º 2
0
        /**
         * <p>
         * The condition p = 2 number of a matrix is used to measure the sensitivity of the linear
         * system <b>Ax=b</b>.  A value near one indicates that it is a well conditioned matrix.<br>
         * <br>
         * &kappa;<sub>2</sub> = ||A||<sub>2</sub>||A<sup>-1</sup>||<sub>2</sub>
         * </p>
         * <p>
         * This is also known as the spectral condition number.
         * </p>
         *
         * @param A The matrix.
         * @return The condition number.
         */
        public static float conditionP2(FMatrixRMaj A)
        {
            SingularValueDecomposition_F32 <FMatrixRMaj> svd =
                DecompositionFactory_FDRM.svd(A.numRows, A.numCols, false, false, true);

            svd.decompose(A);

            float[] singularValues = svd.getSingularValues();

            int n = SingularOps_FDRM.rank(svd, UtilEjml.TEST_F32);

            if (n == 0)
            {
                return(0);
            }

            float smallest = float.MaxValue;
            float largest  = float.MinValue;

            foreach (float s in singularValues)
            {
                if (s < smallest)
                {
                    smallest = s;
                }
                if (s > largest)
                {
                    largest = s;
                }
            }

            return(largest / smallest);
        }
Ejemplo n.º 3
0
        /**
         * <p>
         * Checks to see if the matrix is positive semidefinite:
         * </p>
         * <p>
         * x<sup>T</sup> A x &ge; 0<br>
         * for all x where x is a non-zero vector and A is a symmetric matrix.
         * </p>
         *
         * @param A square symmetric matrix. Not modified.
         *
         * @return True if it is positive semidefinite and false if it is not.
         */
        public static bool isPositiveSemidefinite(FMatrixRMaj A)
        {
            if (!isSquare(A))
            {
                return(false);
            }

            EigenDecomposition_F32 <FMatrixRMaj> eig = DecompositionFactory_FDRM.eig(A.numCols, false);

            if (eig.inputModified())
            {
                A = (FMatrixRMaj)A.copy();
            }
            eig.decompose(A);

            for (int i = 0; i < A.numRows; i++)
            {
                Complex_F32 v = eig.getEigenvalue(i);

                if (v.getReal() < 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /**
         * <p>
         * Computes the induced p = 2 matrix norm, which is the largest singular value.
         * </p>
         *
         * @param A Matrix. Not modified.
         * @return The norm.
         */
        public static float inducedP2(FMatrixRMaj A)
        {
            SingularValueDecomposition_F32 <FMatrixRMaj> svd =
                DecompositionFactory_FDRM.svd(A.numRows, A.numCols, false, false, true);

            if (!svd.decompose(A))
            {
                throw new InvalidOperationException("Decomposition failed");
            }

            float[] singularValues = svd.getSingularValues();

            // the largest singular value is the induced p2 norm
            return(UtilEjml.max(singularValues, 0, singularValues.Length));
        }
Ejemplo n.º 5
0
        /**
         * Computes the nullity of a matrix using the specified tolerance.
         *
         * @param A Matrix whose rank is to be calculated.  Not modified.
         * @param threshold The numerical threshold used to determine a singular value.
         * @return The matrix's nullity.
         */
        public static int nullity(FMatrixRMaj A, float threshold)
        {
            SingularValueDecomposition_F32 <FMatrixRMaj> svd =
                DecompositionFactory_FDRM.svd(A.numRows, A.numCols, false, false, true);

            if (svd.inputModified())
            {
                A = (FMatrixRMaj)A.copy();
            }

            if (!svd.decompose(A))
            {
                throw new InvalidOperationException("Decomposition failed");
            }

            return(SingularOps_FDRM.nullity(svd, threshold));
        }
Ejemplo n.º 6
0
        public SimpleEVD(T mat)
        {
            this.mat  = mat;
            this.is64 = mat is DMatrixRMaj;

            if (is64)
            {
                eig = (EigenDecomposition <T>)DecompositionFactory_DDRM.eig(mat.getNumCols(), true);
            }
            else
            {
                eig = (EigenDecomposition <T>)DecompositionFactory_FDRM.eig(mat.getNumCols(), true);
            }
            if (!eig.decompose((T)mat))
            {
                throw new InvalidOperationException("Eigenvalue Decomposition failed");
            }
        }
Ejemplo n.º 7
0
        /**
         * Checks to see if the rows of the provided matrix are linearly independent.
         *
         * @param A Matrix whose rows are being tested for linear independence.
         * @return true if linearly independent and false otherwise.
         */
        public static bool isRowsLinearIndependent(FMatrixRMaj A)
        {
            // LU decomposition
            LUDecomposition <FMatrixRMaj> lu = DecompositionFactory_FDRM.lu(A.numRows, A.numCols);

            if (lu.inputModified())
            {
                A = (FMatrixRMaj)A.copy();
            }

            if (!lu.decompose(A))
            {
                throw new InvalidOperationException("Decompositon failed?");
            }

            // if they are linearly independent it should not be singular
            return(!lu.isSingular());
        }
Ejemplo n.º 8
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);
            }
        }
 public SymmetricQRAlgorithmDecomposition_FDRM(bool computeVectors)
     : this(DecompositionFactory_FDRM.tridiagonal(0), computeVectors)
 {
 }
Ejemplo n.º 10
0
 /**
  * Creates a new solver targeted at the specified matrix size.
  *
  * @param maxRows The expected largest matrix it might have to process.  Can be larger.
  * @param maxCols The expected largest matrix it might have to process.  Can be larger.
  */
 public SolvePseudoInverseSvd_FDRM(int maxRows, int maxCols)
 {
     svd = DecompositionFactory_FDRM.svd(maxRows, maxCols, true, true, true);
 }