Returns the sums of columns.

@param matrix The matrix of which the sums of columns to be computed
@return A double array of column sums
342958
public static double[] colSums (RealMatrix matrix) {
        // Declare and initialize the return value:
        double[] retval = new double[matrix.getColumnDimension()];

        // Iterate over columns and compute totals:
        for (int col = 0; col < matrix.getColumnDimension(); col++) {
            for (int row = 0; row < matrix.getRowDimension(); row++) {
                retval[col] += matrix.getEntry(row, col);
            }
        }

        // Done, return col sums:
        return retval;
    }
495810
public Matrix getMatrix(int[] r, int[] c)
    {
        Matrix X = new Matrix(r.length, c.length);
        double[][] B = X.getArray();
        try
        {
            for (int i = 0; i < r.length; i++)
            {
                for (int j = 0; j < c.length; j++)
                {
                    B[i][j] = A[r[i]][c[j]];
                }
            }
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            throw new ArrayIndexOutOfBoundsException("Submatrix indices");
        }
        return X;
    }
139809
public static double dotInnerColumns( DMatrixSparseCSC A , int colA , DMatrixSparseCSC B , int colB ,
                                          IGrowArray gw , DGrowArray gx)
    {
        if( A.numRows != B.numRows )
            throw new IllegalArgumentException("Number of rows must match.");

        int w[] = adjust(gw,A.numRows);
        Arrays.fill(w,0,A.numRows,-1);
        double x[] = adjust(gx,A.numRows);

        int length = 0;

        int idx0 = A.col_idx[colA];
        int idx1 = A.col_idx[colA+1];
        for (int i = idx0; i < idx1; i++) {
            int row = A.nz_rows[i];
            x[length] = A.nz_values[i];
            w[row] = length++;
        }

        double dot = 0;

        idx0 = B.col_idx[colB];
        idx1 = B.col_idx[colB+1];
        for (int i = idx0; i < idx1; i++) {
            int row = B.nz_rows[i];
            if( w[row] != -1 ) {
                dot += x[w[row]]*B.nz_values[i];
            }
        }

        return dot;
    }
201353
public Matrix plus(final Matrix B)
    {
        final Matrix A = this;

        if ((B.m_rows != A.m_rows) || (B.m_columns != A.m_columns))
        {
            throw new GeometryException("Illegal matrix dimensions");
        }
        final Matrix C = new Matrix(m_rows, m_columns);

        for (int i = 0; i < m_rows; i++)
        {
            for (int j = 0; j < m_columns; j++)
            {
                C.m_data[i][j] = A.m_data[i][j] + B.m_data[i][j];
            }
        }
        return C;
    }
896034
public static final DoubleMatrix1D diagonalMatrixMult(DoubleMatrix1D diagonalM, DoubleMatrix1D vector){
		int n = diagonalM.size();
		DoubleMatrix1D ret = DoubleFactory1D.dense.make(n);
		for(int i=0; i<n; i++){
			ret.setQuick(i, diagonalM.getQuick(i) * vector.getQuick(i));
		}
		return ret;
	}
819394
public static DoubleMatrix multiply(DoubleMatrix m1, DoubleMatrix m2)
    {
        if (m1.cols != m2.rows)
        {
            throw new IllegalArgumentException("Matrices not comfortable");
        }
        int m = m1.rows;
        int n = m1.cols;
        int p = m2.cols;
        ItemSupplier s1 = m1.supplier;
        ItemSupplier s2 = m2.supplier;
        DoubleMatrix mr = DoubleMatrix.getInstance(m, p);
        ItemConsumer c = mr.consumer;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < p; j++)
            {
                double s = 0;
                for (int r = 0; r < n; r++)
                {
                    s += s1.get(i, r) * s2.get(r, j);
                }
                c.set(i, j, s);
            }
        }
        return mr;
    }
1285072
@Override
	public DoubleMatrix1D[] getMatrixScalingFactors(DoubleMatrix2D A){
		DoubleFactory1D F1 = DoubleFactory1D.dense;
		Algebra ALG = Algebra.DEFAULT;
		int r = A.rows();
		int c = A.columns();
		DoubleMatrix1D D1 = F1.make(r, 1);
		DoubleMatrix1D D2 = F1.make(c, 1);
		DoubleMatrix2D AK = A.copy();
		DoubleMatrix1D DR = F1.make(r, 1);
		DoubleMatrix1D DC = F1.make(c, 1);
		DoubleMatrix1D DRInv = F1.make(r);
		DoubleMatrix1D DCInv = F1.make(c);
		log.debug("eps  : " + eps);
		int maxIteration = 50;
		for(int k=0; k<=maxIteration; k++){
			double normR = -Double.MAX_VALUE;
			double normC = -Double.MAX_VALUE;
			for(int i=0; i<r; i++){
				double dri = ALG.normInfinity(AK.viewRow(i));
				DR.setQuick(i, Math.sqrt(dri));
				DRInv.setQuick(i, 1./Math.sqrt(dri));
				normR = Math.max(normR, Math.abs(1-dri));
			}
			for(int j=0; j<c; j++){
				double dci = ALG.normInfinity(AK.viewColumn(j));
				DC.setQuick(j, Math.sqrt(dci));
				DCInv.setQuick(j, 1./Math.sqrt(dci));
				normC = Math.max(normC, Math.abs(1-dci));
			}
			
			log.debug("normR: " + normR);
			log.debug("normC: " + normC);
			if(normR < eps && normC < eps){
				break;
			}
			
			//D1 = ALG.mult(D1, DRInv);
			for(int i=0; i<r; i++){
				double prevD1I = D1.getQuick(i);
				double newD1I = prevD1I * DRInv.getQuick(i);
				D1.setQuick(i, newD1I);
			}
			//D2 = ALG.mult(D2, DCInv);
			for(int j=0; j<c; j++){
				double prevD2J = D2.getQuick(j);
				double newD2J = prevD2J * DCInv.getQuick(j);
				D2.setQuick(j, newD2J);
			}
			//log.debug("D1: " + ArrayUtils.toString(D1.toArray()));
			//log.debug("D2: " + ArrayUtils.toString(D2.toArray()));
			
			if(k==maxIteration){
				log.warn("max iteration reached");
			}
			
			//AK = ALG.mult(DRInv, ALG.mult(AK, DCInv));
			AK = ColtUtils.diagonalMatrixMult(DRInv, AK, DCInv);
		}
		
		return new DoubleMatrix1D[]{D1, D2};
	}
920803
public static double[][] getMatrix(final double[][] m1, final int[] r, final int[] c) {
    final int rowdim = r.length, coldim = c.length;
    final double[][] X = new double[rowdim][coldim];
    for(int i = 0; i < rowdim; i++) {
      final double[] rowM = m1[r[i]], rowX = X[i];
      for(int j = 0; j < coldim; j++) {
        rowX[j] = rowM[c[j]];
      }
    }
    return X;
  }
439547
public static double sumOfProducts(double[]... nums) {
        if (nums == null || nums.length < 1)
            return 0;
        double sum = 0;

        for (int i = 0; i < nums.length; i++) {
            /* The ith column for all of the rows */
            double[] column = column(i, nums);
            sum += times(column);

        }
        return sum;
    }
1294034
public void mul(IMatrix b, IMatrix result) {
        if ((b == null) || (columns != b.rows)) return;

        if ((result.rows != rows) || (result.columns != b.columns)) result.reshape(rows, b.columns);
        int i, j, k;
        double realsum, imagsum;
        for (i = 0; i < rows; i++)
            for (k = 0; k < b.columns; k++) {
                realsum = 0;
                imagsum = 0;
                for (j = 0; j < columns; j++) {
                    realsum += realmatrix[i][j] * b.realmatrix[j][k] - imagmatrix[i][j] * b.imagmatrix[j][k];
                    imagsum += realmatrix[i][j] * b.imagmatrix[j][k] + imagmatrix[i][j] * b.realmatrix[j][k];
                }
                result.realmatrix[i][k] = realsum;
                result.imagmatrix[i][k] = imagsum;
            }
    }
552089
public static int[][] computeFloydAPSP(double costMatrix[][]) {
        int nrow = costMatrix.length;
        int[][] distMatrix = new int[nrow][nrow];
        //logger.debug("Matrix size: " + n);
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < nrow; j++) {
                if (costMatrix[i][j] == 0) {
                    distMatrix[i][j] = 0;
                } else {
                    distMatrix[i][j] = 1;
                }
            }
        }
        return computeFloydAPSP(distMatrix);
    }
997905
public static double[] rowSums(double[][] data) {
        double[] x = new double[data.length];

        for (int i = 0; i < x.length; i++) {
            x[i] = sum(data[i]);
        }

        return x;
    }
1329253
public void getColumn(int j, double[] array, int offset)
    {
        int m = rows;
        for (int i=0;i<m;i++)
        {
            array[i+offset] = get(i, j);
        }
    }
702284
public double[] getRow(int row) {
    double[] rowArray = new double[getColumnCount()];
    for (int i = 0; i < getColumnCount(); i++) {
      rowArray[i] = get(row, i);
    }
    return rowArray;
  }
365400
public static double max(double[][] matrix) {
        double m = Double.NEGATIVE_INFINITY;

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

        return m;
    }
594405
public DoubleMatrix multiply(double c)
    {
        DoubleMatrix clone = clone();
        clone.scalarMultiply(c);
        return clone;
    }
950094
public DoubleVector getColumnVector(int column) {
        checkIndices(0, column);
        rowReadLock.lock();
        DoubleVector values = new DenseVector(rows.get());
        for (int row = 0; row < rows.get(); ++row) {
            double value = get(row, column);
            if (value != 0d)
                values.set(row, value);
        }
        rowReadLock.unlock();
        return values;
    }