Exemplo n.º 1
0
  /**
   * Construct from DoubleMatrix2D type
   *
   * @param aMatrix is a DoubleMatrix2D
   */
  public SparseCoordinateFormatMatrix(final DoubleMatrix2D aMatrix) {
    Validate.notNull(aMatrix);

    // get number of elements
    _els = aMatrix.getNumberOfElements();

    // tmp arrays, in case we get in a fully populated matrix, intelligent design upstream should
    // ensure that this is overkill!
    double[] valuesTmp = new double[_els];
    int[] xTmp = new int[_els];
    int[] yTmp = new int[_els];

    // we need unwind the array aMatrix into coordinate form
    int localmaxEntriesInARow;
    _maxEntriesInARow = -1; // set max entries in a column negative, so that maximiser will work
    int ptr = 0;
    for (int i = 0; i < aMatrix.getNumberOfRows(); i++) {
      localmaxEntriesInARow = 0;
      for (int j = 0; j < aMatrix.getNumberOfColumns(); j++) {
        if (Double.doubleToLongBits(aMatrix.getEntry(i, j)) != 0L) {
          xTmp[ptr] = j;
          yTmp[ptr] = i;
          valuesTmp[ptr] = aMatrix.getEntry(i, j);
          ptr++;
          localmaxEntriesInARow++;
        }
      }
      if (localmaxEntriesInARow > _maxEntriesInARow) {
        _maxEntriesInARow = localmaxEntriesInARow;
      }
    }

    _values = Arrays.copyOfRange(valuesTmp, 0, ptr);
    _x = Arrays.copyOfRange(xTmp, 0, ptr);
    _y = Arrays.copyOfRange(yTmp, 0, ptr);
    _rows = aMatrix.getNumberOfRows();
    _cols = aMatrix.getNumberOfColumns();
  }