/** * Builds up a consistently random (same seed every time) sparse matrix, with sometimes repeated * rows. * * @param numRows * @param nonNullRows * @param numCols * @param entriesPerRow * @param entryMean * @return */ public static Matrix randomSequentialAccessSparseMatrix( int numRows, int nonNullRows, int numCols, int entriesPerRow, double entryMean) { SparseRowMatrix m = new SparseRowMatrix(new int[] {numRows, numCols}); double n = 0; Random r = new Random(1234L); for (int i = 0; i < nonNullRows; i++) { SequentialAccessSparseVector v = new SequentialAccessSparseVector(numCols); for (int j = 0; j < entriesPerRow; j++) { int col = r.nextInt(numCols); double val = r.nextGaussian(); v.set(col, val * entryMean); } int c = r.nextInt(numRows); if (r.nextBoolean() || numRows == nonNullRows) { m.assignRow(numRows == nonNullRows ? i : c, v); } else { Vector other = m.getRow(r.nextInt(numRows)); if (other != null && other.getLengthSquared() > 0) { m.assignRow(c, other.clone()); } } n += m.getRow(c).getLengthSquared(); } return m; }
public void observe(Vector x) { setS0(getS0() + 1); if (getS1() == null) { setS1(x.clone()); } else { getS1().assign(x, Functions.PLUS); } Vector x2 = x.times(x); if (getS2() == null) { setS2(x2); } else { getS2().assign(x2, Functions.PLUS); } }
@Override public void observe(VectorWritable v) { Vector x = v.get(); s0++; if (s1 == null) { s1 = x.clone(); } else { s1 = s1.plus(x); } if (s2 == null) { s2 = x.times(x); } else { s2 = s2.plus(x.times(x)); } }
@Override public long write(Iterable<Vector> iterable, long maxDocs) throws IOException { for (Vector point : iterable) { if (recNum >= maxDocs) { break; } if (point.getClass() == NamedVector.class) { NamedVector v = (NamedVector) point.clone(); } if (point != null) { writer.append(new LongWritable(recNum++), new VectorWritable(point)); } } return recNum; }