Given the histogram of columns compute the col_idx for the matrix.  nz_length is automatically set and
nz_values will grow if needed.
@param histogram histogram of column values in the sparse matrix. modified, see above.
966610
public void histogramToStructure(int histogram[] ) {
        col_idx[0] = 0;
        int index = 0;
        for (int i = 1; i <= numCols; i++) {
            col_idx[i] = index += histogram[i-1];
        }
        nz_length = index;
        growMaxLength( nz_length , false);
        if( col_idx[numCols] != nz_length )
            throw new RuntimeException("Egads");
    }
833134
def set_col_name(self, index, name):
        
        javabridge.call(self.jobject, "setColName", "(ILjava/lang/String;)V", index, name)
1204835
private static Matrix readDenseTextMatrix(File matrix, Type matrixType,
                                              boolean transposeOnRead) 
            throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(matrix));

        // unknown number of rows, so do a quick scan to determine it
        int rows = 0;
        int cols = -1;
        for (String line = null; (line = br.readLine()) != null; rows++) {
            // Create a scanner to parse out the values from the current line of
            // the file.
            Scanner s = new Scanner(line);
            s.useLocale(Locale.US);
            int vals = 0;
            // Count how many columns the row has
            for (; s.hasNextDouble(); vals++, s.nextDouble())
                ;

            // Base case if the number of columns has not been set
            if (cols == -1) 
                cols = vals;
            // Otherwise, ensure that the number of values in the current row
            // matches the number of values seen in the first
            else {
                if (cols != vals) {
                    throw new MatrixIOException("line " + (rows + 1) + 
                        " contains an inconsistent number of columns. " +
                        cols + " columns expected versus " + vals + " found.");
                }
            }
        }
        br.close();

        if (MATRIX_IO_LOGGER.isLoggable(Level.FINE)) {
            MATRIX_IO_LOGGER.fine("reading in text matrix with " + rows  +
                                  " rows and " + cols + " cols");
        }

        // Once the matrix has had its dimensions determined, re-open the file
        // to load the data into a Matrix instance.  Use a Scanner to parse the
        // text for us.
        Scanner scanner = new Scanner(matrix);
        scanner.useLocale(Locale.US);
        Matrix m = (transposeOnRead)
            ? Matrices.create(cols, rows, matrixType)
            : Matrices.create(rows, cols, matrixType);
        
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                double d = scanner.nextDouble();
                if (transposeOnRead)
                    m.set(col, row, d);
                else
                    m.set(row, col, d);
            }
        }
        scanner.close();
        
        return m;
    }
350775
public static void addColTimes(double[][] matrix, int diag, int fromRow, int col, double factor) {
		int rows = matrix.length;
		double[] r = null;
		for (int row = fromRow; row < rows; row++) {
			r = matrix[row];
			r[col] -= factor * r[diag];
		}
	}
1138019
public static void transpose(DMatrixSparseCSC A , DMatrixSparseCSC C , @Nullable IGrowArray gw ) {
        int []work = adjust(gw,A.numRows,A.numRows);
        C.reshape(A.numCols,A.numRows,A.nz_length);

        // compute the histogram for each row in 'a'
        int idx0 = A.col_idx[0];
        for (int j = 1; j <= A.numCols; j++) {
            int idx1 = A.col_idx[j];
            for (int i = idx0; i < idx1; i++) {
                if( A.nz_rows.length <= i)
                    throw new RuntimeException("Egads");
                work[A.nz_rows[i]]++;
            }
            idx0 = idx1;
        }

        // construct col_idx in the transposed matrix
        C.histogramToStructure(work);
        System.arraycopy(C.col_idx,0,work,0,C.numCols);

        // fill in the row indexes
        idx0 = A.col_idx[0];
        for (int j = 1; j <= A.numCols; j++) {
            int col = j-1;
            int idx1 = A.col_idx[j];
            for (int i = idx0; i < idx1; i++) {
                int row = A.nz_rows[i];
                int index = work[row]++;
                C.nz_rows[index] = col;
                C.nz_values[index] = A.nz_values[i];
            }
            idx0 = idx1;
        }
    }
814837
private static int linearSearch(int[] histogramLevels, int value) {
		for (int i = 0; i < histogramLevels.length; i++) {
			if (value < histogramLevels[i]) {
				return i;
			}
		}
		return histogramLevels.length; //
	}
252827
public static ColumnMajorSparseMatrix fromMatrixMarket(InputStream is) throws IOException {
        return Matrix.fromMatrixMarket(is).to(Matrices.SPARSE_COLUMN_MAJOR);
    }
1233400
void computeHistogram(int c_x, int c_y, double sigma) {
		int r = (int)Math.ceil(sigma * sigmaEnlarge);

		// specify the area being sampled
		bound.x0 = c_x - r;
		bound.y0 = c_y - r;
		bound.x1 = c_x + r + 1;
		bound.y1 = c_y + r + 1;

		ImageGray rawDX = derivX.getImage();
		ImageGray rawDY = derivY.getImage();

		// make sure it is contained in the image bounds
		BoofMiscOps.boundRectangleInside(rawDX,bound);

		// clear the histogram
		Arrays.fill(histogramMag,0);
		Arrays.fill(histogramX,0);
		Arrays.fill(histogramY,0);

		// construct the histogram
		for( int y = bound.y0; y < bound.y1; y++ ) {
			// iterate through the raw array for speed
			int indexDX = rawDX.startIndex + y*rawDX.stride + bound.x0;
			int indexDY = rawDY.startIndex + y*rawDY.stride + bound.x0;

			for( int x = bound.x0; x < bound.x1; x++ ) {
				float dx = derivX.getF(indexDX++);
				float dy = derivY.getF(indexDY++);

				// edge intensity and angle
				double magnitude = Math.sqrt(dx*dx + dy*dy);
				double theta = UtilAngle.domain2PI(Math.atan2(dy,dx));

				// weight from gaussian
				double weight = computeWeight( x-c_x, y-c_y , sigma );

				// histogram index
				int h = (int)(theta / histAngleBin) % histogramMag.length;

				// update the histogram
				histogramMag[h] += magnitude*weight;
				histogramX[h] += dx*weight;
				histogramY[h] += dy*weight;
			}
		}
	}
1087845
private int getIndex(int row, int column) {
        int i = no.uib.cipr.matrix.sparse.Arrays.binarySearch(rowIndex, row,
                columnPointer[column], columnPointer[column + 1]);

        if (i >= 0 && rowIndex[i] == row)
            return i;
        else
            throw new IndexOutOfBoundsException("Entry (" + (row + 1) + ", "
                    + (column + 1) + ") is not in the matrix structure");
    }
1093058
@Override public DTree.Split scoreMSE( int col ) {
    final int nbins = nbins();
    assert nbins > 1;

    // Store indices from sort to determine group split later
    Integer idx[] = new Integer[nbins];
    for(int b = 0; b < nbins; b++) idx[b] = b;

    // Sort predictor levels in ascending order of mean response within each bin
    if(_isInt == 2 && _step == 1.0f && nbins >= 4 && _doGrpSplit) {
      final double[] means = new double[nbins];
      System.arraycopy(_sums, 0, means, 0, nbins);
      Arrays.sort(idx, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
          return ((Double)means[o1]).compareTo(means[o2]);
        }
      });
    }

    // Compute mean/var for cumulative bins from 0 to nbins inclusive.
    double sums0[] = MemoryManager.malloc8d(nbins+1);
    double ssqs0[] = MemoryManager.malloc8d(nbins+1);
    long     ns0[] = MemoryManager.malloc8 (nbins+1);
    for( int b=1; b<=nbins; b++ ) {
      double m0 = sums0[b-1],  m1 = _sums[b-1];
      double s0 = ssqs0[b-1],  s1 = _ssqs[b-1];
      long   k0 = ns0  [b-1],  k1 = _bins[b-1];
      if( k0==0 && k1==0 ) continue;
      sums0[b] = m0+m1;
      ssqs0[b] = s0+s1;
      ns0  [b] = k0+k1;
    }
    long tot = ns0[nbins];
    // If we see zero variance, we must have a constant response in this
    // column.  Normally this situation is cut out before we even try to split, but we might
    // have NA's in THIS column...
    if( ssqs0[nbins]*tot - sums0[nbins]*sums0[nbins] == 0 ) {
      assert isConstantResponse(); return null;
    }

    // Compute mean/var for cumulative bins from nbins to 0 inclusive.
    double sums1[] = MemoryManager.malloc8d(nbins+1);
    double ssqs1[] = MemoryManager.malloc8d(nbins+1);
    long     ns1[] = MemoryManager.malloc8 (nbins+1);
    for( int b=nbins-1; b>=0; b-- ) {
      double m0 = sums1[b+1],  m1 = _sums[b];
      double s0 = ssqs1[b+1],  s1 = _ssqs[b];
      long   k0 = ns1  [b+1],  k1 = _bins[b];
      if( k0==0 && k1==0 ) continue;
      sums1[b] = m0+m1;
      ssqs1[b] = s0+s1;
      ns1  [b] = k0+k1;
      assert ns0[b]+ns1[b]==tot;
    }

    // Now roll the split-point across the bins.  There are 2 ways to do this:
    // split left/right based on being less than some value, or being equal/
    // not-equal to some value.  Equal/not-equal makes sense for catagoricals
    // but both splits could work for any integral datatype.  Do the less-than
    // splits first.
    int best=0;                         // The no-split
    double best_se0=Double.MAX_VALUE;   // Best squared error
    double best_se1=Double.MAX_VALUE;   // Best squared error
    byte equal=0;                // Ranged check
    for( int b=1; b<=nbins-1; b++ ) {
      if( _bins[b] == 0 ) continue; // Ignore empty splits
      if( ns0[b] < _min_rows ) continue;
      if( ns1[b] < _min_rows ) break; // ns1 shrinks at the higher bin#s, so if it fails once it fails always
      // We're making an unbiased estimator, so that MSE==Var.
      // Then Squared Error = MSE*N = Var*N
      //                    = (ssqs/N - mean^2)*N
      //                    = ssqs - N*mean^2
      //                    = ssqs - N*(sum/N)(sum/N)
      //                    = ssqs - sum^2/N
      double se0 = ssqs0[b] - sums0[b]*sums0[b]/ns0[b];
      double se1 = ssqs1[b] - sums1[b]*sums1[b]/ns1[b];
      if( (se0+se1 < best_se0+best_se1) || // Strictly less error?
          // Or tied MSE, then pick split towards middle bins
          (se0+se1 == best_se0+best_se1 &&
           Math.abs(b -(nbins>>1)) < Math.abs(best-(nbins>>1))) ) {
        best_se0 = se0;   best_se1 = se1;
        best = b;
      }
    }

    // If the min==max, we can also try an equality-based split
    if( _isInt > 0 && _step == 1.0f &&    // For any integral (not float) column
        _maxEx-_min > 2 ) { // Also need more than 2 (boolean) choices to actually try a new split pattern
      for( int b=1; b<=nbins-1; b++ ) {
        if( _bins[b] < _min_rows ) continue; // Ignore small bin
        long N =        ns0[b] + ns1[b+1];
        double sums = sums0[b]+sums1[b+1];
        double ssqs = ssqs0[b]+ssqs1[b+1];
        if( N < _min_rows ) continue;
        double si =  ssqs    -  sums   * sums   /   N    ; // Left+right, excluding 'b'
        double sx = _ssqs[b] - _sums[b]*_sums[b]/_bins[b]; // Just 'b'
        if( si+sx < best_se0+best_se1 ) { // Strictly less error?
          best_se0 = si;   best_se1 = sx;
          best = b;        equal = 1; // Equality check
        }
      }
    }

    if( best==0 ) return null;  // No place to split
    assert best > 0 : "Must actually pick a split "+best;
    long   n0 = equal == 0 ?   ns0[best] :   ns0[best]+  ns1[best+1];
    long   n1 = equal == 0 ?   ns1[best] : _bins[best]              ;
    double p0 = equal == 0 ? sums0[best] : sums0[best]+sums1[best+1];
    double p1 = equal == 0 ? sums1[best] : _sums[best]              ;

    // For categorical predictors, set bits for levels grouped to right of split
    Utils.IcedBitSet bs = null;
    if(_isInt == 2 && _step == 1.0f && nbins >= 4 && _doGrpSplit) {
      // Small cats: always use 4B to store and prepend offset # of zeros at front
      // Big cats: save offset and store only nbins # of bits that are left after trimming
      int offset = (int)_min;
      if(_maxEx <= 32) {
        equal = 2;
        bs = new Utils.IcedBitSet(32);
        for(int i = best; i < nbins; i++)
          bs.set(idx[i] + offset);
      } else {
        equal = 3;
        bs = new Utils.IcedBitSet(nbins, offset);
        for(int i = best; i < nbins; i++)
          bs.set(idx[i]);
      }
    }

    return new DTree.Split(col,best,bs,equal,best_se0,best_se1,n0,n1,p0/n0,p1/n1);
  }
92613
public static void addColTimes(Matrix matrix, long diag, long fromRow, long col, double factor) {
		long rows = matrix.getRowCount();
		for (long row = fromRow; row < rows; row++) {
			matrix.setAsDouble(
					matrix.getAsDouble(row, col) - factor * matrix.getAsDouble(row, diag), row, col);
		}
	}
493272
public void setColumn(ColumnVector cv, int c)
        throws MatrixException
    {
        if ((c < 0) || (c >= nCols)) {
            throw new MatrixException(MatrixException.INVALID_INDEX);
        }
        if (nRows != cv.nRows) {
            throw new MatrixException(
                                MatrixException.INVALID_DIMENSIONS);
        }

        for (int r = 0; r < nRows; ++r) {
            this.values[r][c] = cv.values[r][0];
        }
    }
51111
@Override
  public int[] columns(String[] names) {
    int[] idxs = new int[_strs.length];
    for (int i = 0; i < _strs.length; i++) {
      int idx = idxs[i] = water.util.ArrayUtils.find(names, _strs[i]);
      if (idx == -1) throw new IllegalArgumentException("Column " + _strs[i] + " not found");
    }
    return idxs;
  }
41625
public static void histogram( GrayS16 input , int minValue , int histogram[] ) {
		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplImageStatistics_MT.histogram(input,minValue,histogram);
		} else {
			ImplImageStatistics.histogram(input,minValue,histogram);
		}
	}
566432
public static void histogram( GrayS32 input , int minValue , int histogram[] ) {
		if( BoofConcurrency.USE_CONCURRENT ) {
			ImplImageStatistics_MT.histogram(input,minValue,histogram);
		} else {
			ImplImageStatistics.histogram(input,minValue,histogram);
		}
	}
624393
public static boolean checkIndicesSorted( DMatrixSparseCSC A ) {
        for (int j = 0; j < A.numCols; j++) {
            int idx0 = A.col_idx[j];
            int idx1 = A.col_idx[j+1];

            if( idx0 != idx1 && A.nz_rows[idx0] >= A.numRows )
                return false;

            for (int i = idx0+1; i < idx1; i++) {
                int row = A.nz_rows[i];
                if( A.nz_rows[i-1] >= row)
                    return false;
                if( row >= A.numRows )
                    return false;
            }
        }
        return true;
    }
1253285
public List<Column<?>> columns(int... columnIndices) {
        List<Column<?>> cols = new ArrayList<>(columnIndices.length);
        for (int i : columnIndices) {
            cols.add(column(i));
        }
        return cols;
    }