/** * Constructs a sampler with diagonal scale matrix. * @param diagonal The diagonal elements of the scale matrix. */ public MultiNormal(Vector diagonal) { this(new DiagonalMatrix(diagonal), null); }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize()); }
@Override public Matrix times(Matrix other) { return timesRight(other); }
@Test public void testBasics() { DiagonalMatrix a = new DiagonalMatrix(new double[]{1, 2, 3, 4}); assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 2, 3, 4})).norm(1), 1.0e-10); assertEquals(0, a.viewPart(0, 3, 0, 3).viewDiagonal().minus( new DenseVector(new double[]{1, 2, 3})).norm(1), 1.0e-10); assertEquals(4, a.get(3, 3), 1.0e-10); Matrix m = new DenseMatrix(4, 4); m.assign(a); assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate( Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); m = new DenseMatrix(new double[][]{{1, 2, 3, 4}, {5, 6, 7, 8}}); assertEquals(100, a.timesLeft(m).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); assertEquals(100, a.times(m.transpose()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10); }
d.set(i, i * i); DiagonalMatrix m = new DiagonalMatrix(d); Assert.assertFalse(m.viewRow(0).isDense()); Assert.assertFalse(m.viewColumn(0).isDense()); assertEquals(i * i, m.viewRow(i).zSum(), 0); assertEquals(i * i, m.viewRow(i).get(i), 0); assertEquals(i * i, m.viewColumn(i).zSum(), 0); assertEquals(i * i, m.viewColumn(i).get(i), 0); Iterator<Vector.Element> ix = m.viewRow(7).nonZeroes().iterator(); assertTrue(ix.hasNext()); Vector.Element r = ix.next(); assertFalse(ix.hasNext()); assertEquals(0, m.viewRow(5).get(3), 0); assertEquals(0, m.viewColumn(8).get(3), 0); m.viewRow(3).set(3, 1); assertEquals(1, m.get(3, 3), 0); for (Vector.Element element : m.viewRow(6).all()) { if (element.index() == 6) { assertEquals(36, element.get(), 0);
/** * Constructs a sampler with diagonal scale matrix and (potentially) * non-zero mean. * @param diagonal The scale matrix's principal diagonal. * @param mean The desired mean. Set to null if zero mean is desired. */ public MultiNormal(Vector diagonal, Vector mean) { this(new DiagonalMatrix(diagonal), mean); }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize()); }
@Override public Matrix times(Matrix other) { return timesRight(other); }
@Override protected Matrix matrixLike(int rows, int columns) { return new DiagonalMatrix(rows, columns); }
/** * Return an empty matrix of the same underlying class as the receiver * * @return a Matrix */ @Override public Matrix like() { return new SparseRowMatrix(rowSize(), columnSize()); }
public static DiagonalMatrix identity(int size) { return new DiagonalMatrix(1, size); }
public MultiNormal(double radius, Vector mean) { this(new DiagonalMatrix(radius, mean.size()), mean); }
private static Matrix lowRankMatrix() { Matrix u = new RandomTrinaryMatrix(1, 20, 4, false); Matrix d = new DiagonalMatrix(new double[]{5, 3, 1, 0.5}); Matrix v = new RandomTrinaryMatrix(2, 23, 4, false); return u.times(d).times(v.transpose()); } }
@Test(timeout=50000) public void testTimesOtherSparseEfficiency() { Random raw = RandomUtils.getRandom(); Gamma gen = new Gamma(0.1, 0.1, raw); // build a sequential sparse matrix and a diagonal matrix and multiply them Matrix x = new SparseRowMatrix(1000, 2000, false); for (int i = 0; i < 1000; i++) { int[] values = new int[1000]; for (int k = 0; k < 1000; k++) { int j = (int) Math.min(1000, gen.nextDouble()); values[j]++; } for (int j = 0; j < 1000; j++) { if (values[j] > 0) { x.set(i, j, values[j]); } } } Vector d = new DenseVector(2000).assign(Functions.random()); Matrix y = new DiagonalMatrix(d); long t0 = System.nanoTime(); Matrix z = x.times(y); double elapsedTime = (System.nanoTime() - t0) * 1e-6; System.out.printf("done in %.1f ms\n", elapsedTime); for (MatrixSlice row : z) { for (Vector.Element element : row.nonZeroes()) { assertEquals(x.get(row.index(), element.index()) * d.get(element.index()), element.get(), 1e-12); } } }
@Test public void testSingularValues() { Matrix A = lowRankMatrix(); SequentialBigSvd s = new SequentialBigSvd(A, 8); SingularValueDecomposition svd = new SingularValueDecomposition(A); Vector reference = new DenseVector(svd.getSingularValues()).viewPart(0, 8); assertEquals(reference, s.getSingularValues()); assertEquals(A, s.getU().times(new DiagonalMatrix(s.getSingularValues())).times(s.getV().transpose())); }
/** * Constructs a sampler with diagonal scale matrix and (potentially) * non-zero mean. * @param diagonal The scale matrix's principal diagonal. * @param mean The desired mean. Set to null if zero mean is desired. */ public MultiNormal(Vector diagonal, Vector mean) { this(new DiagonalMatrix(diagonal), mean); }
public enum Matricies { DIAGONAL_MATRIX(new DiagonalMatrix()), OTHER_MATRIX(new OtherMatrix(4, 4)); private MainMatrix value; // each value of the enum has a value which is an instance of a matrix Matricies(MainMatrix val) { // enum constructor to set instance of matrix value = val; } public MainMatrix getValue() { return value; } }
/** * Constructs a sampler with diagonal scale matrix. * @param diagonal The diagonal elements of the scale matrix. */ public MultiNormal(Vector diagonal) { this(new DiagonalMatrix(diagonal), null); }
@Override protected Matrix matrixLike(int rows, int columns) { return new DiagonalMatrix(rows, columns); }
public class MatrixImplFactory { IMatrixImpl createMatrix(double[] diagonal) { return new DiagonalMatrix(diagonal); } IMatrixImpl createMatrix(int rows, int cols) { return new FullMatrix(rows, cols); } }