@Override public Vector divide(double x) { if (x == 1.0) { return clone(); } Vector result = createOptimizedCopy(); for (Element element : result.nonZeroes()) { element.set(element.get() / x); } return result; }
@SuppressWarnings("CloneDoesntCallSuperClone") @Override public Vector clone() { Vector v = new DenseVector(size()); v.assign(this, Functions.PLUS); return v; }
@Override public Vector times(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } if (this.getNumNondefaultElements() <= that.getNumNondefaultElements()) { return createOptimizedCopy(this).assign(that, Functions.MULT); } else { return createOptimizedCopy(that).assign(this, Functions.MULT); } }
public SequentialBigSvd(Matrix A, int p) { // Y = A * \Omega y = A.times(new RandomTrinaryMatrix(A.columnSize(), p)); // R'R = Y' Y cd1 = new CholeskyDecomposition(y.transpose().times(y)); // B = Q" A = (Y R^{-1} )' A b = cd1.solveRight(y).transpose().times(A); // L L' = B B' cd2 = new CholeskyDecomposition(b.times(b.transpose())); // U_0 D V_0' = L svd = new SingularValueDecomposition(cd2.getL()); }
private static void assertVectorEquals(Vector expected, Vector actual, double epsilon) { assertEquals(expected.size(), actual.size()); for (Element x : expected.all()) { assertEquals(x.get(), actual.get(x.index()), epsilon); } }
public static void checkOrthogonal(Matrix m) { Matrix mTm = m.transpose().times(m); Matrix id = new DenseMatrix(mTm.numRows(),mTm.numRows()); for (int i = 0; i < mTm.numRows(); i++) { id.set(i, i, 1); } assertEquals(0, Algebra.getNorm(mTm.minus(id)), NORM_TOLERANCE); }
@Override public Matrix assignRow(int row, Vector other) { if (columnSize() != other.size()) { throw new CardinalityException(columnSize(), other.size()); } if (row < 0 || row >= rowSize()) { throw new IndexException(row, rowSize()); } for (int col = 0; col < columnSize(); col++) { values[row][col] = other.getQuick(col); } return this; }
@Override public Vector plus(Vector that) { if (size != that.size()) { throw new CardinalityException(size, that.size()); } return createOptimizedCopy().assign(that, Functions.PLUS); }
@Override public void setQuick(int row, int column, double value) { if (columnVectors[column] == null) { columnVectors[column] = new RandomAccessSparseVector(rowSize()); } columnVectors[column].setQuick(row, value); }
public Matrix getV() { // V = (B' inv(L')) V_0 return cd2.solveRight(b.transpose()).times(svd.getV()); } }
@Override public Matrix matrixFactory(double[][] values) { Matrix base = new DenseMatrix(values); // for general tests, we just make a scrambled matrix and fill it // with the standard data. Then we can test the details of the // row and/or column swapping separately. PivotedMatrix pm = new PivotedMatrix(base.like()); pm.swap(0, 1); pm.swapRows(1, 2); pm.assign(base); return pm; }
@Override public Vector viewColumn(int column) { if (column < 0 || column >= columnSize()) { throw new IndexException(column, columnSize()); } return matrix.viewColumn(column + offset[COL]).viewPart(offset[ROW], rowSize()); }
private static void doTestMatrixWritableEquals(Matrix m) throws IOException { Writable matrixWritable = new MatrixWritable(m); MatrixWritable matrixWritable2 = new MatrixWritable(); writeAndRead(matrixWritable, matrixWritable2); Matrix m2 = matrixWritable2.get(); compareMatrices(m, m2); doCheckBindings(m2.getRowLabelBindings()); doCheckBindings(m2.getColumnLabelBindings()); }
@Override public Matrix like(int rows, int columns) { if (denseLike) return new DenseMatrix(rows, columns); else return new SparseMatrix(rows, columns); }
@Override public double dot(Vector x) { if (size != x.size()) { throw new CardinalityException(size, x.size()); } if (this == x) { return getLengthSquared(); } return aggregate(x, Functions.PLUS, Functions.MULT); }
@SuppressWarnings("CloneDoesntCallSuperClone") @Override public SequentialAccessSparseVector clone() { return new SequentialAccessSparseVector(size(), values.clone()); }
/** * Perform a merge sort on a range of a long array using a specified ordering. * @param array the array. * @param start the first index. * @param end the last index (exclusive). * @param comp the comparator object. */ public static void mergeSort(long[] array, int start, int end, LongComparator comp) { checkBounds(array.length, start, end); long[] out = Arrays.copyOf(array, array.length); mergeSort(out, array, start, end, comp); }
@Override public Vector viewPart(int offset, int length) { if (offset < 0) { throw new IndexException(offset, size()); } if (offset + length > size()) { throw new IndexException(offset + length, size()); } return new DenseVectorView(vector, offset + this.offset, length); } }
/** * Sort the specified range of an array of object that implement the Comparable * interface. * @param <T> The type of object. * @param array the array. * @param start the first index. * @param end the last index (exclusive). */ public static <T extends Comparable<? super T>> void quickSort(T[] array, int start, int end) { quickSort(array, start, end, new ComparableAdaptor<T>()); }
/** * Perform a merge sort on a range of a float array using a specified ordering. * @param array the array. * @param start the first index. * @param end the last index (exclusive). * @param comp the comparator object. */ public static void mergeSort(float[] array, int start, int end, FloatComparator comp) { checkBounds(array.length, start, end); float[] out = Arrays.copyOf(array, array.length); mergeSort(out, array, start, end, comp); }