AlternateLeastSquaresSolver { protected Matrix createRiIiMaybeTransposed(Vector ratingVector) { Preconditions.checkArgument(ratingVector.isSequentialAccess()); Matrix RiIiMaybeTransposed = new DenseMatrix(ratingVector.getNumNondefaultElements(), 1); Iterator<Vector.Element> ratingsIterator = ratingVector.iterateNonZero(); int index = 0; while (ratingsIterator.hasNext()) { Vector.Element elem = ratingsIterator.next(); RiIiMaybeTransposed.setQuick(index++, 0, elem.get()); } return RiIiMaybeTransposed; }  Vector solve(List<Vector> featureVectors, Vector ratingVector, double lambda, int numFeatures);  }
@Test public void createRiIiMaybeTransposed() { Vector ratings = new SequentialAccessSparseVector(3); ratings.setQuick(1, 1.0); ratings.setQuick(3, 3.0); ratings.setQuick(5, 5.0); Matrix riIiMaybeTransposed = solver.createRiIiMaybeTransposed(ratings); assertEquals(1, riIiMaybeTransposed.numCols(), 1); assertEquals(3, riIiMaybeTransposed.numRows(), 3); assertEquals(1.0, riIiMaybeTransposed.getQuick(0, 0), EPSILON); assertEquals(3.0, riIiMaybeTransposed.getQuick(1, 0), EPSILON); assertEquals(5.0, riIiMaybeTransposed.getQuick(2, 0), EPSILON); }
@Test public void createRiIiMaybeTransposedExceptionOnNonSequentialVector() { Vector ratings = new RandomAccessSparseVector(3); ratings.setQuick(1, 1.0); ratings.setQuick(3, 3.0); ratings.setQuick(5, 5.0); try { solver.createRiIiMaybeTransposed(ratings); fail(); } catch (IllegalArgumentException e) {} }