Computes the rank of a matrix.
@memberof Helpers
@param {Matrix2|Matrix3|Matrix4} M The matrix to get the rank of.
@param {number} EPS The numerical tolerance for an element to be zero.
173548
function getRank (M, EPS) {
  const n = M.dimension;
  const { R } = qrDecomposition(M);
  // TODO: this is a bad way of doing this probably
  R.thresholdEntriesToZero(100 * EPS);
  let rank = 0;
  rrefInPlace(R);
  for (let i = 0; i < n; ++i) {
    if (isRowNonzero(R, i)) {
      rank++;
    }
  }
  return rank;
}
602042
function calculateColumnAverage(inputMatrix)
{
	var rows = inputMatrix.rows();
	
	var averages = new Array();
	for(var i = 1; i <= inputMatrix.cols(); i++)
	{
		var sum = 0;
		for(var j = 1; j <= inputMatrix.rows(); j++)
		{
			sum += inputMatrix.e(j, i);
		}
		averages[i-1] = sum/rows;
	}
	
	return $V(averages);
}
985030
public static final double[] getZYZEuler(Matrix m) {
		double m22 = m.get(2,2);
		double rY = Math.toDegrees(Math.acos(m22));
		double rZ1, rZ2;
		if (m22 > .999d || m22 < -.999d) {
			rZ1 = Math.toDegrees(Math.atan2(m.get(1,0),  m.get(1,1)));
			rZ2 = 0;
		} else {
			rZ1 = Math.toDegrees(Math.atan2(m.get(2,1), -m.get(2,0)));
			rZ2 = Math.toDegrees(Math.atan2(m.get(1,2),  m.get(0,2)));
		}
		return new double[] {rZ1,rY,rZ2};
	}
1176828
public static void setMatrix(final double[][] m1, final int r0, final int r1, final int[] c, final double[][] m2) {
    assert r0 <= r1 : ERR_INVALID_RANGE;
    assert r1 <= m1.length : ERR_MATRIX_DIMENSIONS;
    for(int i = r0; i < r1; i++) {
      final double[] row1 = m1[i], row2 = m2[i - r0];
      for(int j = 0; j < c.length; j++) {
        row1[c[j]] = row2[j];
      }
    }
  }
1037486
function(startRow, endRow, startCol, endCol) {
            	var x = [];
            	
            	if(endRow == 0)
            	    endRow = this.rows();
            	
            	if(endCol == 0)
            	    endCol = this.cols();

            	for(i = startRow; i <= endRow; i++) {
            	    var row = [];

            	    for(j = startCol; j <= endCol; j++) {
            		row.push(this.e(i, j));
            	    }

            	    x.push(row);
            	}

            	return $M(x);
                }
1132385
public static void symmRankNMinus_L(int blockLength ,
                                        DSubmatrixD1 A , DSubmatrixD1 B )
    {
        int widthB = B.col1-B.col0;
        if( widthB > blockLength )
            throw new IllegalArgumentException("Width of B cannot be greater than the block length");

        int N = B.row1-B.row0;

        if( A.col1-A.col0 != N )
            throw new IllegalArgumentException("A does not have the expected number of columns based on B's height");
        if( A.row1-A.row0 != N )
            throw new IllegalArgumentException("A does not have the expected number of rows based on B's height");

        for( int i = B.row0; i < B.row1; i += blockLength ) {


            int heightB_i = Math.min(blockLength,B.row1-i);
            int indexB_i = i*B.original.numCols + heightB_i*B.col0;

            int rowA = i-B.row0+A.row0;
            int heightA = Math.min( blockLength , A.row1 - rowA);

            for( int j = B.row0; j <= i; j += blockLength ) {
                
                int widthB_j = Math.min(blockLength,B.row1-j);

                int indexA = rowA * A.original.numCols + (j-B.row0+A.col0)*heightA;
                int indexB_j = j*B.original.numCols + widthB_j*B.col0;

                if( i == j ) {
                    multTransBBlockMinus_L( B.original.data,A.original.data,
                            indexB_i,indexB_j,indexA,widthB,heightB_i,widthB_j);
                } else {
                    multTransBBlockMinus( B.original.data,A.original.data,
                            indexB_i,indexB_j,indexA,widthB,heightB_i,widthB_j);
                }
            }
        }
    }
1119986
def matrix_rank(model):
    
    s_matrix, _, _ = con_helpers.stoichiometry_matrix(
        model.metabolites, model.reactions
    )
    return con_helpers.rank(s_matrix)
662235
private SimpleMatrix computeHZero( DMatrixRMaj F , Point3D_F64 e2 ,
									   SimpleMatrix H ) {

		Vector3D_F64 v = new Vector3D_F64(.1,0.5,.2);

		// need to make sure M is not singular for this technique to work
		SimpleMatrix P = SimpleMatrix.wrap(MultiViewOps.fundamentalToProjective(F, e2, v, 1));

		SimpleMatrix M = P.extractMatrix(0, 3, 0, 3);

		return H.mult(M);
	}
1197598
public static double elementMin( DMatrix2x2 a ) {
        double min = a.a11;
        if( a.a12 < min ) min = a.a12;
        if( a.a21 < min ) min = a.a21;
        if( a.a22 < min ) min = a.a22;

        return min;
    }
401939
public static DMatrixRMaj concatRowsMulti(DMatrixRMaj ...m ) {
        int rows = 0;
        int cols = 0;

        for (int i = 0; i < m.length; i++) {
            rows += m[i].numRows;
            cols = Math.max(cols,m[i].numCols);
        }
        DMatrixRMaj R = new DMatrixRMaj(rows,cols);

        int row = 0;
        for (int i = 0; i < m.length; i++) {
            insert(m[i],R,row,0);
            row += m[i].numRows;
        }

        return R;
    }
331753
public static double elementMin( DMatrix3x3 a ) {
        double min = a.a11;
        if( a.a12 < min ) min = a.a12;
        if( a.a13 < min ) min = a.a13;
        if( a.a21 < min ) min = a.a21;
        if( a.a22 < min ) min = a.a22;
        if( a.a23 < min ) min = a.a23;
        if( a.a31 < min ) min = a.a31;
        if( a.a32 < min ) min = a.a32;
        if( a.a33 < min ) min = a.a33;

        return min;
    }
116888
public static void symmRankTranA_sub(DMatrixRMaj a , DMatrixRMaj c ,
                                         int startIndexC )
    {
        // TODO update so that it doesn't modify/read parts that it shouldn't
        final double dataA[] = a.data;
        final double dataC[] = c.data;

//        for( int i = 0; i < a.numCols; i++ ) {
//            for( int k = 0; k < a.numRows; k++ ) {
//                double valA = dataA[k*a.numCols+i];
//
//                for( int j = i; j < a.numCols; j++ ) {
//                    dataC[startIndexC+i*c.numCols+j] -= valA * dataA[k*a.numCols+j];
//                }
//            }
//        }

        final int strideC = c.numCols + 1;
        for( int i = 0; i < a.numCols; i++ ) {
            int indexA = i;
            int endR = a.numCols;

            for( int k = 0; k < a.numRows; k++ , indexA += a.numCols , endR += a.numCols) {
                int indexC = startIndexC;
                final double valA = dataA[indexA];
                int indexR = indexA;

                while( indexR < endR ) {
                    dataC[indexC++] -= valA * dataA[indexR++];
                }
            }
            startIndexC += strideC;
        }

    }
1045744
private static function countSolutions(Matrix $M): int
    {
        $number_of_solutions = 0;
        // There are solutions to be found.
        $more_solutions = true;
        $m = $M->getM();
        // We will count the number of rows with all zeros, starting at the bottom.
        for ($i = $m - 1; $i >= 0 && $more_solutions; $i--) {
            // Every row of zeros is a degree of freedom (a solution) with that eigenvalue
            if ($M->getRow($i) == array_fill(0, $m, 0)) {
                $number_of_solutions++;
            } else {
                 // Once we find a row with nonzero values, there are no more.
                 $more_solutions = false;
            }
        }
        return $number_of_solutions;
    }
672067
function(matrix) {
    var M = matrix.elements || matrix;
    if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
    var T = this.dup(), cols = T.elements[0].length;
    var ni = T.elements.length, ki = ni, i, nj, kj = M[0].length, j;
    if (ni != M.length) { return null; }
    do { i = ki - ni;
      nj = kj;
      do { j = kj - nj;
        T.elements[i][cols + j] = M[i][j];
      } while (--nj);
    } while (--ni);
    return T;
  }
770261
public static void setMatrix(final double[][] m1, final int r0, final int r1, final int c0, final int c1, final double[][] m2) {
    assert r0 <= r1 && c0 <= c1 : ERR_INVALID_RANGE;
    assert r1 <= m1.length && c1 <= m1[0].length : ERR_MATRIX_DIMENSIONS;
    final int coldim = c1 - c0;
    for(int i = r0; i < r1; i++) {
      System.arraycopy(m2[i - r0], 0, m1[i], c0, coldim);
    }
  }
615456
function(matrix) {
    var M = matrix.elements || matrix;
    if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
    // this.columns should equal matrix.rows
    return (this.elements[0].length == M.length);
  }
1075958
function printMatrix3 (m) {
  const tStr = m.elements.map(formatPrintNumber);
  const matrixString = `
  +-                         -+
  | ${tStr[0]}  ${tStr[3]}  ${tStr[6]} |
  | ${tStr[1]}  ${tStr[4]}  ${tStr[7]} |
  | ${tStr[2]}  ${tStr[5]}  ${tStr[8]} |
  +-                         -+`;
  return matrixString;
}