Compute the sum of a matrix.

@param mat Matrix
@return Sum of all elements
328140
private long sumMatrix(int[][] mat) {
    long ret = 0;
    for(int i = 0; i < mat.length; i++) {
      final int[] row = mat[i];
      for(int j = 0; j < row.length; j++) {
        ret += row[j];
      }
    }
    return ret;
  }
846311
public Vector3f mulProject(Matrix4fc mat, Vector3f dest) {
        float invW = 1.0f / (mat.m03() * x + mat.m13() * y + mat.m23() * z + mat.m33());
        float rx = (mat.m00() * x + mat.m10() * y + mat.m20() * z + mat.m30()) * invW;
        float ry = (mat.m01() * x + mat.m11() * y + mat.m21() * z + mat.m31()) * invW;
        float rz = (mat.m02() * x + mat.m12() * y + mat.m22() * z + mat.m32()) * invW;
        dest.x = rx;
        dest.y = ry;
        dest.z = rz;
        return dest;
    }
1124646
public static double[][] plusEquals(final double[][] m1, final double[][] m2) {
    final int rowdim = m1.length, coldim = getColumnDimensionality(m1);
    assert getRowDimensionality(m1) == getRowDimensionality(m2) && coldim == getColumnDimensionality(m2) : ERR_MATRIX_DIMENSIONS;
    for(int i = 0; i < rowdim; i++) {
      final double[] row1 = m1[i], row2 = m2[i];
      for(int j = 0; j < coldim; j++) {
        row1[j] += row2[j];
      }
    }
    return m1;
  }
456285
protected double multiply(double[][] mat, double[] in, double[] out) {
    double l = 0.;
    // Matrix multiplication:
    for(int d1 = 0; d1 < in.length; d1++) {
      final double[] row = mat[d1];
      double t = 0.;
      for(int d2 = 0; d2 < in.length; d2++) {
        t += row[d2] * in[d2];
      }
      out[d1] = t;
      l += t * t;
    }
    return l > 0 ? FastMath.sqrt(l) : 0.;
  }
677079
public static void transposeConjugate( ZMatrixRMaj mat ) {
        if( mat.numCols == mat.numRows ){
            TransposeAlgs_ZDRM.squareConjugate(mat);
        } else {
            ZMatrixRMaj b = new ZMatrixRMaj(mat.numCols,mat.numRows);
            transposeConjugate(mat, b);
            mat.reshape(b.numRows, b.numCols);
            mat.set(b);
        }
    }
1100843
private static int sum(final int[] sizes) {
        int sum = 0;
        for (int i = sizes.length - 1; i >= 0; i--) {
            sum += sizes[i];
        }
        return sum;
    }
1074861
public static double sum(DoubleVector v) {
        double sum = 0;
        if (v instanceof SparseVector) {
            for (int nz : ((SparseVector)v).getNonZeroIndices())
                sum += v.get(nz);
        }
        else {
            int len = v.length();
            for (int i = 0; i < len; ++i)
                sum += v.get(i);
        }
        return sum;
    }
382899
public static int min(int[][] matrix) {
    int min = Integer.MAX_VALUE;
    for (int[] row : matrix) {
      for (int elem : row) {
        min = Math.min(min, elem);
      }
    }
    return min;
  }
1078142
public static double min(double[][] matrix) {
        double m = Double.POSITIVE_INFINITY;

        for (double[] x : matrix) {
            for (double y : x) {
                if (m > y) {
                    m = y;
                }
            }
        }

        return m;
    }
137744
public Matrix applyEntropyTransformations(Matrix mat) {
        int n = mat.columns();
        int m = mat.rows();
        for (int col_num = 0; col_num < n; col_num++) {
            double col_total = 0.0;
            for (int row_num = 0; row_num < m; row_num++) {
                col_total += mat.get(row_num,col_num);
            }
            //System.err.println("coltotal: " + col_total);
            if (col_total == 0.0) 
                continue;

            double entropy = 0.0;
            for (int row_num = 0; row_num < m; row_num++) {
                double p = mat.get(row_num,col_num)/col_total;
                //System.err.print(p + " ");
                if (p==0.0)
                    continue;
                entropy += p * Math.log10(p);
            }
            //System.err.println("entropy: " + entropy);
            entropy *= -1;
            double w = 1 - entropy/Math.log10(m);
            //System.err.println("w: " + w);
            for (int row_num = 0; row_num < m; row_num++) {
                mat.set(row_num, col_num, w*Math.log10(mat.get(row_num, col_num) + 1.0));
            }
        }
        return mat;
    }
178939
public double getSum() {
        double sum = s.zero();
        for (int c = 0; c < this.values.length; c++) {
            sum = s.plus(sum, values[c]);
        }
        return sum;
    }
1334511
public double compute( DMatrix1Row mat ) {
        if( width != mat.numCols || width != mat.numRows ) {
            throw new RuntimeException("Unexpected matrix dimension");
        }

        // make sure everything is in the proper state before it starts
        initStructures();

//        System.arraycopy(mat.data,0,minorMatrix[0],0,mat.data.length);

        int level = 0;
        while( true ) {
            int levelWidth = width-level;
            int levelIndex = levelIndexes[level];

            if( levelIndex == levelWidth ) {
                if( level == 0 ) {
                    return levelResults[0];
                }
                int prevLevelIndex = levelIndexes[level-1]++;

                double val = mat.get((level-1)*width+levelRemoved[level-1]);
                if( prevLevelIndex % 2 == 0 ) {
                    levelResults[level-1] += val * levelResults[level];
                } else {
                    levelResults[level-1] -= val * levelResults[level];
                }

                putIntoOpen(level-1);

                levelResults[level] = 0;
                levelIndexes[level] = 0;
                level--;
            } else {
                int excluded = openRemove( levelIndex );

                levelRemoved[level] = excluded;

                if( levelWidth == minWidth ) {
                    createMinor(mat);
                    double subresult = mat.get(level*width+levelRemoved[level]);

                    subresult *= UnrolledDeterminantFromMinor_DDRM.det(tempMat);

                    if( levelIndex % 2 == 0 ) {
                        levelResults[level] += subresult;
                    } else {
                        levelResults[level] -= subresult;
                    }

                    // put it back into the list
                    putIntoOpen(level);
                    levelIndexes[level]++;
                } else {
                    level++;
                }
            }
        }
    }
1280143
public Matrix subtract(Matrix B)
    {
        Matrix toReturn = getThisSideMatrix(B);
        toReturn.mutableSubtract(1.0, B);
        return toReturn;
    }
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;
    }
992175
public static void byColumn(Matrix m) {
        for (int i = 0; i < m.columns(); ++i) {
            double colSum = 0;
            for (int j = 0; j < m.rows(); ++j)
                colSum += m.get(j,i);

            // Skip columns whose sume is zero.
            if (colSum == 0) 
                continue;
            for (int j = 0; j < m.rows(); ++j)
                m.set(j,i, m.get(j,i) / colSum);
        }
    }
63714
public static void multLowerTranB( DMatrixRMaj mat ) {
        int m = mat.numCols;
        double[] L = mat.data;
        for( int i = 0; i < m; i++ ) {
            for( int j = m-1; j >= i; j-- ) {
                double val = 0;
                for( int k = 0; k <= i; k++ ) {
                    val += L[ i*m + k] * L[ j*m + k ];
                }
                L[ i*m + j ] = val;
            }
        }
        // copy the results into the lower portion
        for( int i = 0; i < m; i++ ) {
            for (int j = 0; j < i; j++) {
                L[i*m+j] = L[j*m+i];
            }
        }
    }
1298126
public static Matrix average(Matrix m, Dimension dim) {
        Matrix averageMatrix = null;

        if (dim == Dimension.ALL) {
            // Compute the average of all values in the matrix.
            double average = 0;
            for (int i = 0; i < m.rows(); ++i) {
                for (int j = 0; j < m.columns(); ++j)
                    average += m.get(i, j);
            }

            averageMatrix = new ArrayMatrix(1, 1);
            average = average / (double) (m.rows() * m.columns());
            averageMatrix.set(1, 1, average);
        } else if (dim == Dimension.ROW) {
            // Compute the average of each row in the matrix.
            averageMatrix = new ArrayMatrix(m.rows(), 1);
            for (int i = 0; i < m.rows(); ++i) {
                double average = 0;
                for (int j = 0; j < m.columns(); ++j)
                    average += m.get(i, j);

                average = average / (double) m.columns();
                averageMatrix.set(i, 0, average);
            }
        } else if (dim == Dimension.COLUMN) {
            // Compute the average of each column in the matrix.
            averageMatrix = new ArrayMatrix(1, m.columns());
            for (int i = 0; i < m.rows(); ++i) {
                for (int j = 0; j < m.columns(); ++j) {
                    double newValue = m.get(i, j) + averageMatrix.get(0, j);
                    averageMatrix.set(0, j, newValue);
                }
            }

            for (int i = 0; i < m.columns(); ++i) {
                double average = averageMatrix.get(0, i);
                average = average / (double) m.rows();
                averageMatrix.set(0, i, average);
            }
        }
        return averageMatrix;
    }