/** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); }
/** {@inheritDoc} * @throws NumberIsTooLargeException if {@code row != column} and value is non-zero. */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException, NumberIsTooLargeException { if (row == column) { MatrixUtils.checkRowIndex(this, row); data[row] = value; } else { ensureZero(value); } }
/** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { return multiply(new DiagonalMatrix(v, false)).getDataRef(); }
/** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public DiagonalMatrix multiply(final DiagonalMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] * m.data[i]; } return new DiagonalMatrix(outData, false); }
/** * Computes the inverse of this diagonal matrix. * * @param threshold Singularity threshold. * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse(double threshold) throws SingularMatrixException { if (isSingular(threshold)) { throw new SingularMatrixException(); } final double[] result = new double[data.length]; for (int i = 0; i < data.length; i++) { result[i] = 1.0 / data[i]; } return new DiagonalMatrix(result, false); }
@Override public void consume(List<Datum> records) throws Exception { if (!hasConsumed) { n = records.get(0).metrics().getDimension(); mean = new ArrayRealVector(n); covV = new ArrayRealVector(n, 1d/n); covM = new DiagonalMatrix(covV.toArray()); mnd = new MultivariateNormalDistribution(mean.toArray(), covM.getData()); mnd.reseedRandomGenerator(randomSeed); randomProjectionMatrix = new BlockRealMatrix(mnd.sample(k)); hasConsumed = true; } for (Datum d: records){ metricVector = d.metrics(); transformedVector = randomProjectionMatrix.operate(metricVector); output.add(new Datum(d,transformedVector)); } }
/** * Computes the inverse of this diagonal matrix. * <p> * Note: this method will use a singularity threshold of 0, * use {@link #inverse(double)} if a different threshold is needed. * * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse() throws SingularMatrixException { return inverse(0); }
/** {@inheritDoc} */ @Override public double[][] getData() { final int dim = getRowDimension(); final double[][] out = new double[dim][dim]; for (int i = 0; i < dim; i++) { out[i][i] = data[i]; } return out; }
/** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ @Override public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { if (m instanceof DiagonalMatrix) { return multiply((DiagonalMatrix) m); } else { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = m.getRowDimension(); final int nCols = m.getColumnDimension(); final double[][] product = new double[nRows][nCols]; for (int r = 0; r < nRows; r++) { for (int c = 0; c < nCols; c++) { product[r][c] = data[r] * m.getEntry(r, c); } } return new Array2DRowRealMatrix(product, false); } }
/** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix subtract(final DiagonalMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] - m.data[i]; } return new DiagonalMatrix(outData, false); }
/** * Computes the inverse of this diagonal matrix. * * @param threshold Singularity threshold. * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse(double threshold) throws SingularMatrixException { if (isSingular(threshold)) { throw new SingularMatrixException(); } final double[] result = new double[data.length]; for (int i = 0; i < data.length; i++) { result[i] = 1.0 / data[i]; } return new DiagonalMatrix(result, false); }
return ((DiagonalMatrix) matrix).inverse(threshold); } else { QRDecomposition decomposition = new QRDecomposition(matrix, threshold);
/** {@inheritDoc} */ @Override public double[][] getData() { final int dim = getRowDimension(); final double[][] out = new double[dim][dim]; for (int i = 0; i < dim; i++) { out[i][i] = data[i]; } return out; }
/** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ @Override public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { if (m instanceof DiagonalMatrix) { return multiply((DiagonalMatrix) m); } else { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = m.getRowDimension(); final int nCols = m.getColumnDimension(); final double[][] product = new double[nRows][nCols]; for (int r = 0; r < nRows; r++) { for (int c = 0; c < nCols; c++) { product[r][c] = data[r] * m.getEntry(r, c); } } return new Array2DRowRealMatrix(product, false); } }
/** {@inheritDoc} */ @Override public RealMatrix copy() { return new DiagonalMatrix(data); }
/** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix add(final DiagonalMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] + m.data[i]; } return new DiagonalMatrix(outData, false); }
/** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { return multiply(new DiagonalMatrix(v, false)).getDataRef(); }
/** * Computes the inverse of this diagonal matrix. * * @param threshold Singularity threshold. * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse(double threshold) throws SingularMatrixException { if (isSingular(threshold)) { throw new SingularMatrixException(); } final double[] result = new double[data.length]; for (int i = 0; i < data.length; i++) { result[i] = 1.0 / data[i]; } return new DiagonalMatrix(result, false); }
/** {@inheritDoc} * @throws NumberIsTooLargeException if {@code row != column} and increment is non-zero. */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException, NumberIsTooLargeException { if (row == column) { MatrixUtils.checkRowIndex(this, row); data[row] += increment; } else { ensureZero(increment); } }
/** * Computes the inverse of this diagonal matrix. * <p> * Note: this method will use a singularity threshold of 0, * use {@link #inverse(double)} if a different threshold is needed. * * @return the inverse of {@code m} * @throws SingularMatrixException if the matrix is singular * @since 3.3 */ public DiagonalMatrix inverse() throws SingularMatrixException { return inverse(0); }