code: private Matrix scaleMatrix(Matrix matrix) {
          // Scale every data point such that it has a dot product of 1 with
          // itself.  This will make further calculations easier since the dot
          // product distrubutes when the cosine similarity does not.
          if (matrix instanceof SparseMatrix) {
              List<SparseDoubleVector> scaledVectors =
                  new ArrayList<SparseDoubleVector>(matrix.rows());
              SparseMatrix sm = (SparseMatrix) matrix;
              for (int r = 0; r < matrix.rows(); ++r) {
                  SparseDoubleVector v = sm.getRowVector(r);
                  scaledVectors.add(new ScaledSparseDoubleVector(
                              v, 1/v.magnitude()));
              }
              return Matrices.asSparseMatrix(scaledVectors);
          } else {
              List<DoubleVector> scaledVectors = 
                  new ArrayList<DoubleVector>(matrix.rows());
              for (int r = 0; r < matrix.rows(); ++r) {
                  DoubleVector v = matrix.getRowVector(r);
                  scaledVectors.add(new ScaledDoubleVector(v, 1/v.magnitude()));
              }
              return Matrices.asMatrix(scaledVectors);
          }
      }
docs: Returns a scaled {@link Matrix}, where each row is the unit
  magnitude version of the corresponding row vector in {@link matrix}.
  This is required so that dot product computations over a large number of
  data points can be distributed, wherease the cosine similarity cannot be.