Computes the eigenvalue decomposition of the given matrix

@param A
Matrix to factorize. Overwritten on return
@return The current eigenvalue decomposition
@throws NotConvergedException
519789
public SymmBandEVD factor(UpperSymmBandMatrix A)
            throws NotConvergedException {
        if (uplo != UpLo.Upper)
            throw new IllegalArgumentException(
                    "Eigenvalue computer configured for upper-symmetrical matrices");

        return factor(A, A.getData(), A.numSuperDiagonals());
    }
1028725
private static EigenPair[] processDecomposition(EigenvalueDecomposition evd) {
    double[] eigenvalues = evd.getRealEigenvalues();
    double[][] eigenvectors = evd.getV();

    EigenPair[] eigenPairs = new EigenPair[eigenvalues.length];
    for(int i = 0; i < eigenvalues.length; i++) {
      double e = Math.abs(eigenvalues[i]);
      double[] v = VMath.getCol(eigenvectors, i);
      eigenPairs[i] = new EigenPair(v, e);
    }
    Arrays.sort(eigenPairs, Comparator.reverseOrder());
    return eigenPairs;
  }
799143
public static boolean isRowsLinearIndependent( DMatrixRMaj A )
    {
        // LU decomposition
        LUDecomposition<DMatrixRMaj> lu = DecompositionFactory_DDRM.lu(A.numRows,A.numCols);
        if( lu.inputModified() )
            A = A.copy();

        if( !lu.decompose(A))
            throw new RuntimeException("Decompositon failed?");

        // if they are linearly independent it should not be singular
        return !lu.isSingular();
    }
1041629
public static boolean decomposeSafe(DecompositionInterface<ZMatrixRMaj> decomposition, ZMatrixRMaj a) {

        if( decomposition.inputModified() ) {
            a = a.copy();
        }
        return decomposition.decompose(a);
    }
989132
public static final DoubleMatrix2D diagonalMatrixMult(final DoubleMatrix1D diagonalU, DoubleMatrix2D A, final DoubleMatrix1D diagonalV){
		int r = A.rows();
		int c = A.columns();
		final DoubleMatrix2D ret;
		if (A instanceof SparseDoubleMatrix2D) {
			ret = DoubleFactory2D.sparse.make(r, c);
			A.forEachNonZero(new IntIntDoubleFunction() {
				@Override
				public double apply(int i, int j, double aij) {
					ret.setQuick(i, j, aij * diagonalU.getQuick(i) * diagonalV.getQuick(j));
					return aij;
				}
			});
		} else {
			ret = DoubleFactory2D.dense.make(r, c);
			for (int i = 0; i < r; i++) {
				for (int j = 0; j < c; j++) {
					ret.setQuick(i, j, A.getQuick(i, j) * diagonalU.getQuick(i)	* diagonalV.getQuick(j));
				}
			}
		}

		return ret;
	}
1123424
@Override
    public boolean decompose(DMatrixRBlock A) {
        if( A.numCols != A.numRows )
            throw new IllegalArgumentException("A must be square");

        this.T = A;

        if( lower )
            return decomposeLower();
        else
            return decomposeUpper();
    }
895786
public static DenseMatrix matrix(double[][] A) {
        if (nlmatrixZeros != null) {
            try {
                return (DenseMatrix) nlmatrixArray2D.newInstance((Object) A);
            } catch (Exception e) {
                logger.error("Failed to call NLMatrix(double[][]): {}", e);
            }
        }

        return new JMatrix(A);
    }
1007250
public static double[][] transpose(double[][] A) {
        int m = A.length;
        int n = A[0].length;

        double[][] matrix = new double[n][m];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[j][i] = A[i][j];
            }
        }

        return matrix;
    }
120302
public static double elementSum( DMatrixSparseCSC A ) {
        if( A.nz_length == 0)
            return 0;

        double sum = 0;
        for(int i = 0; i < A.nz_length; i++ ) {
            sum += A.nz_values[i];
        }

        return sum;
    }
1144392
@Override
    public DMatrixRMaj getA() {
        if( A.data.length < numRows*numCols ) {
            A = new DMatrixRMaj(numRows,numCols);
        }
        A.reshape(numRows,numCols, false);
        CommonOps_DDRM.mult(Q,R,A);

        return A;
    }
860019
public void decompose( DMatrixRMaj A ) {

        this.QR = A.copy();

        int N = Math.min(A.numCols,A.numRows);

        gammas = new double[ A.numCols ];

        DMatrixRMaj A_small = new DMatrixRMaj(A.numRows,A.numCols);
        DMatrixRMaj A_mod = new DMatrixRMaj(A.numRows,A.numCols);
        DMatrixRMaj v = new DMatrixRMaj(A.numRows,1);
        DMatrixRMaj Q_k = new DMatrixRMaj(A.numRows,A.numRows);

        for( int i = 0; i < N; i++ ) {
            // reshape temporary variables
            A_small.reshape(QR.numRows-i,QR.numCols-i);
            v.reshape(A_small.numRows,1);
            Q_k.reshape(v.getNumElements(),v.getNumElements());

            // use extract matrix to get the column that is to be zeroed
            CommonOps_DDRM.extract(QR,i,QR.numRows,i,i+1,v,0,0);

            double max = CommonOps_DDRM.elementMaxAbs(v);

            if( max > 0 && v.getNumElements() > 1 ) {
                // normalize to reduce overflow issues
                CommonOps_DDRM.divide(v,max);

                // compute the magnitude of the vector
                double tau = NormOps_DDRM.normF(v);

                if( v.get(0) < 0 )
                    tau *= -1.0;

                double u_0 = v.get(0) + tau;
                double gamma = u_0/tau;

                CommonOps_DDRM.divide(v,u_0);
                v.set(0,1.0);

                // extract the submatrix of A which is being operated on
                CommonOps_DDRM.extract(QR,i,QR.numRows,i,QR.numCols,A_small,0,0);

                // A = (I - &gamma;*u*u<sup>T</sup>)A
                CommonOps_DDRM.setIdentity(Q_k);
                CommonOps_DDRM.multAddTransB(-gamma,v,v,Q_k);
                CommonOps_DDRM.mult(Q_k,A_small,A_mod);

                // save the results
                CommonOps_DDRM.insert(A_mod, QR, i,i);
                CommonOps_DDRM.insert(v, QR, i,i);
                QR.unsafe_set(i,i,-tau*max);

                // save gamma for recomputing Q later on
                gammas[i] = gamma;
            }
        }
    }
732999
public static DenseCholesky factorize(Matrix A) {
        return new DenseCholesky(A.numRows(), true)
                .factor(new UpperSPDDenseMatrix(A));
    }
938346
public static double[] singularValues( DMatrixRMaj A ) {
        SingularValueDecomposition_F64<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows,A.numCols,false,true,true);

        if( svd.inputModified() ) {
            A = A.copy();
        }
        if( !svd.decompose(A)) {
            throw new RuntimeException("SVD Failed!");
        }

        double sv[] = svd.getSingularValues();
        Arrays.sort(sv,0,svd.numberOfSingularValues());

        // change the ordering to ascending
        for (int i = 0; i < sv.length/2; i++) {
            double tmp = sv[i];
            sv[i] = sv[sv.length-i-1];
            sv[sv.length-i-1] = tmp;
        }

        return sv;
    }
228844
public static DoubleMatrix[] fullSVD(DoubleMatrix A) {
        int m = A.rows;
        int n = A.columns;

        DoubleMatrix U = new DoubleMatrix(m, m);
        DoubleMatrix S = new DoubleMatrix(min(m, n));
        DoubleMatrix V = new DoubleMatrix(n, n);

        int info = NativeBlas.dgesvd('A', 'A', m, n, A.dup().data, 0, m, S.data, 0, U.data, 0, m, V.data, 0, n);

        if (info > 0) {
          throw new LapackConvergenceException("GESVD", info + " superdiagonals of an intermediate bidiagonal form failed to converge.");
        }

        return new DoubleMatrix[]{U, S, V.transpose()};
    }
320578
public static int rank( DMatrixRMaj A  ) {
        SingularValueDecomposition_F64<DMatrixRMaj> svd = DecompositionFactory_DDRM.svd(A.numRows,A.numCols,false,true,true);

        if( svd.inputModified() ) {
            A = A.copy();
        }
        if( !svd.decompose(A)) {
            throw new RuntimeException("SVD Failed!");
        }

        int N = svd.numberOfSingularValues();
        double sv[] = svd.getSingularValues();

        double threshold = singularThreshold(sv,N);
        int count = 0;
        for (int i = 0; i < sv.length; i++) {
            if( sv[i] >= threshold ) {
                count++;
            }
        }
        return count;
    }
1320460
public static double elementDiagMaxMagnitude2(ZMatrixRMaj a) {
        final int size = Math.min(a.numRows,a.numCols);

        int rowStride = a.getRowStride();
        double max = 0;
        for( int i = 0; i < size; i++ ) {
            int index = i*rowStride + i*2;

            double real = a.data[index];
            double imaginary = a.data[index+1];

            double m = real*real + imaginary*imaginary;

            if( m > max ) {
                max = m;
            }
        }

        return max;
    }
1153322
public static DoubleMatrix2D subdiagonalMultiply(final DoubleMatrix2D A, final DoubleMatrix2D B){
		final int r = A.rows();
		final int rc = A.columns();
		final int c = B.columns();

		if(r != c){
			throw new IllegalArgumentException("The result must be square");
		}

		boolean useSparsity = A instanceof SparseDoubleMatrix2D;
		DoubleFactory2D F2 = (useSparsity)? DoubleFactory2D.sparse : DoubleFactory2D.dense; 
		final DoubleMatrix2D ret = F2.make(r, c);

		if(useSparsity){
			IntIntDoubleFunction myFunct = new IntIntDoubleFunction() {
				@Override
				public double apply(int t, int s, double pts) {
					int i = t;
					for (int j = 0; j < i + 1; j++) {
						ret.setQuick(i, j, ret.getQuick(i, j) + pts * B.getQuick(s, j));
					}
					return pts;
				}
			};

			//view A row by row
			A.forEachNonZero(myFunct);
		}else{
			for (int i = 0; i < r; i++) {
				for (int j = 0; j < i + 1; j++) {
					double s = 0;
					for (int k = 0; k < rc; k++) {
						s += A.getQuick(i, k) * B.getQuick(k, j);
					}
					ret.setQuick(i, j, s);
				}
			}
		}

		return ret;
	}