예제 #1
0
 /**
  * Creates a new Matrix filled with values sampled from a Gaussian distribution with mean 0 and
  * variance 1.
  *
  * @param numRows The number of rows.
  * @param numColumns The number of columns.
  * @param random The random number generator to use.
  * @return A new Matrix with the given size and values sampled from a standard Gaussian.
  */
 public MatrixType createGaussianRandom(
     final int numRows, final int numColumns, final Random random) {
   final MatrixType result = this.createMatrix(numRows, numColumns);
   for (int i = 0; i < numRows; i++) {
     for (int j = 0; j < numColumns; j++) {
       result.set(i, j, random.nextGaussian());
     }
   }
   return result;
 }
예제 #2
0
 /**
  * Creates a matrix with the given initial value.
  *
  * @param numRows The number of rows. Cannot be negative.
  * @param numColumns The number of columns. Cannot be negative.
  * @param initialValue The initial value to set all elements to.
  * @return A Matrix of the given size initialized with the given initial value.
  */
 public MatrixType createMatrix(
     final int numRows, final int numColumns, final double initialValue) {
   final MatrixType result = this.createMatrix(numRows, numColumns);
   if (initialValue != 0.0) {
     for (int i = 0; i < numRows; i++) {
       for (int j = 0; j < numColumns; j++) {
         result.setElement(i, j, initialValue);
       }
     }
   }
   return result;
 }
예제 #3
0
  /**
   * Creates a new Matrix of the given size with random values for the entries, uniformly
   * distributed between the given minimum and maximum values.
   *
   * @param numRows The number of rows in the Matrix.
   * @param numColumns The number of columns in the Matrix.
   * @param min The minimum range of the uniform distribution.
   * @param max The maximum range of the uniform distribution.
   * @param random The random number generator.
   * @return Matrix with random values for the entries, uniformly distributed between the given
   *     minimum and maximum values.
   */
  public MatrixType createUniformRandom(
      int numRows, int numColumns, double min, double max, Random random) {
    MatrixType m = this.createMatrix(numRows, numColumns);
    for (int i = 0; i < m.getNumRows(); i++) {
      for (int j = 0; j < m.getNumColumns(); j++) {
        double uniform = random.nextDouble();
        double value = ((max - min) * uniform) + min;
        m.setElement(i, j, value);
      }
    }

    return m;
  }
예제 #4
0
  /**
   * Creates a new square matrix whose number of rows and columns match the dimensionality of the
   * given vector. It also places the values of the vector on the diagonal of the matrix.
   *
   * @param diagonal The vector of diagonal values.
   * @return A new, square matrix with the diagonal elements equal to the elements of the given
   *     vector.
   */
  public MatrixType createDiagonal(final Vectorizable diagonal) {
    final Vector vector = diagonal.convertToVector();
    final int dimensionality = vector.getDimensionality();

    // Create the matrix.
    final MatrixType result = this.createMatrix(dimensionality, dimensionality);

    // Set the diagonal values.
    for (VectorEntry entry : vector) {
      final int i = entry.getIndex();
      result.setElement(i, i, entry.getValue());
    }
    return result;
  }
예제 #5
0
  /**
   * Creates a new matrix by copying the given set of column vectors.
   *
   * @param columns The column vectors to create a matrix from. Must all be the same dimensionality.
   * @return A new matrix whose columns are equal to the given set of columns.
   */
  public MatrixType copyColumnVectors(final Collection<? extends Vectorizable> columns) {
    // Create the matrix.
    final int numRows = VectorUtil.safeGetDimensionality(CollectionUtil.getFirst(columns));
    final int numColumns = columns.size();
    final MatrixType result = this.createMatrix(numRows, numColumns);

    // Fill in the matrix with the columns.
    int columnIndex = 0;
    for (Vectorizable column : columns) {
      result.setColumn(columnIndex, column.convertToVector());
      columnIndex++;
    }

    return result;
  }
예제 #6
0
  /**
   * Creates a new matrix by copying the given set of row vectors.
   *
   * @param rows The row vectors to create a matrix from. Must all be the same dimensionality.
   * @return A new matrix whose rows are equal to the given set of rows.
   */
  public MatrixType copyRowVectors(final Collection<? extends Vectorizable> rows) {
    // Create the matrix.
    final int numRows = rows.size();
    final int numColumns = VectorUtil.safeGetDimensionality(CollectionUtil.getFirst(rows));
    final MatrixType result = this.createMatrix(numRows, numColumns);

    // Fill in the matrix with the rows.
    int rowIndex = 0;
    for (Vectorizable row : rows) {
      result.setRow(rowIndex, row.convertToVector());
      rowIndex++;
    }

    return result;
  }
예제 #7
0
 public void setSelectedDisplayOption(MatrixType[] options, boolean control) {
   if (control) {
     MatrixType originalMatrixType = (MatrixType) displayOptionComboBox.getSelectedItem();
     displayOptionComboBox.setModel(new DefaultComboBoxModel<MatrixType>(options));
     int indx = 0;
     for (int i = 0; i < displayOptionComboBox.getItemCount(); i++) {
       if (originalMatrixType.equals(displayOptionComboBox.getItemAt(i))) {
         indx = i;
         break;
       }
     }
     displayOptionComboBox.setSelectedIndex(indx);
   } else {
     displayOptionComboBox.setModel(new DefaultComboBoxModel<MatrixType>(options));
     displayOptionComboBox.setSelectedIndex(0);
   }
 }
예제 #8
0
  /**
   * Copies the values from the array into the Matrix
   *
   * @param values Values to copy
   * @return Matrix with same dimension and values as "values"
   */
  public MatrixType copyArray(double[][] values) {
    int M = values.length;
    int N = 0;
    if (M > 0) {
      N = values[0].length;
    }

    MatrixType m = this.createMatrix(M, N);

    for (int i = 0; i < M; i++) {
      if (values[i].length != N) {
        throw new IllegalArgumentException("Array columns are not same size!");
      }
      for (int j = 0; j < N; j++) {
        m.setElement(i, j, values[i][j]);
      }
    }

    return m;
  }
예제 #9
0
 /**
  * Creates a Matrix with ones (1) on the diagonal, and zeros (0) elsewhere
  *
  * @param numRows number of rows in the Matrix
  * @param numColumns number of columns in the Matrix
  * @return Identity Matrix with ones on the diagonal and zeros elsewhere
  */
 public MatrixType createIdentity(int numRows, int numColumns) {
   MatrixType m = this.createMatrix(numRows, numColumns);
   m.identity();
   return m;
 }