Ejemplo n.º 1
0
 public static double[] rowSums(IMatrix m) {
   int size = Math.max(m.nCols(), m.nRows());
   double toret[] = new double[size];
   for (int i = 0; i < size; i++) toret[i] = 0;
   for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) toret[i] += m.get(i, j);
   return toret;
 }
Ejemplo n.º 2
0
 /**
  * Matrix transpose
  *
  * @param m
  * @return
  */
 public static IMatrix transpose(IMatrix m) {
   IMatrix g = new GuavaMatrix();
   for (Cell<Integer, Integer, Double> k : m.getValueRange()) {
     int li = k.getRowKey();
     int ri = k.getColumnKey();
     g.set(ri, li, m.get(li, ri));
   }
   return g;
 }
Ejemplo n.º 3
0
 /**
  * Matrix divide
  *
  * @param left
  * @param r
  * @return
  */
 public static IMatrix div(IMatrix left, double r) {
   IMatrix g = new GuavaMatrix();
   for (Cell<Integer, Integer, Double> l : left.getValueRange()) {
     int li = l.getRowKey();
     int ri = l.getColumnKey();
     g.incr(li, ri, (left.get(li, ri) / r));
   }
   return g;
 }
Ejemplo n.º 4
0
 public static IMatrix regularizedLaplacianMatrix(IMatrix m) {
   IMatrix tmp = MatrixOp.prod(m, MatrixOp.transpose(m));
   int size = tmp.nCols();
   IMatrix i = diagonal(1, size);
   IMatrix d = diagonal(rowSums(tmp));
   IMatrix laplacian = new GuavaMatrix();
   for (int ii = 0; ii < size; ii++)
     for (int ji = 0; ji < size; ji++)
       laplacian.set(
           ii, ji, (d.get(ii, ji) - tmp.get(ii, ji)) / Math.sqrt(d.get(ii, ii) * d.get(ji, ji)));
   return laplacian;
 }
Ejemplo n.º 5
0
 /**
  * MAtrix difference
  *
  * @param left
  * @param right
  * @return
  */
 public static IMatrix diff(IMatrix left, IMatrix right) {
   Set<Cell<Integer, Integer, Double>> iter = left.getValueRange();
   iter.addAll(right.getValueRange());
   IMatrix g = new GuavaMatrix();
   for (Cell<Integer, Integer, Double> x : iter) {
     g.set(
         x.getRowKey(),
         x.getColumnKey(),
         left.get(x.getRowKey(), x.getColumnKey()) - right.get(x.getRowKey(), x.getColumnKey()));
   }
   return g;
 }
Ejemplo n.º 6
0
 /**
  * Matrix product
  *
  * @param left
  * @param right
  * @return
  */
 public static IMatrix prod(IMatrix left, IMatrix right) {
   IMatrix g = new GuavaMatrix();
   double size = (left.getValueRange().size() * right.getValueRange().size());
   System.out.println(size + " ~~~ ");
   double i = 0;
   for (Cell<Integer, Integer, Double> l : left.getValueRange()) {
     int li = (Integer) l.getColumnKey();
     for (Cell<Integer, Integer, Double> r : right.getValueRange()) {
       int ri = (Integer) r.getRowKey();
       if (li == ri) {
         double lv = (Double) left.get(l.getRowKey(), l.getColumnKey());
         double rv = (Double) right.get(r.getRowKey(), r.getColumnKey());
         // System.out.println(lv+" "+rv+" "+lv*rv);
         g.incr((Integer) l.getRowKey(), (Integer) r.getColumnKey(), lv * rv);
       }
       i++;
       // updateProgress((double) i / (double) size);
     }
   }
   System.out.println("Done!");
   return g;
 }
Ejemplo n.º 7
0
 /**
  * Matrix divide
  *
  * @param left
  * @param right
  * @return
  */
 public static IMatrix div(IMatrix left, IMatrix right) {
   IMatrix g = new GuavaMatrix();
   for (Cell<Integer, Integer, Double> l : left.getValueRange()) {
     for (Cell<Integer, Integer, Double> r : right.getValueRange()) {
       int li = l.getColumnKey();
       int ri = l.getRowKey();
       if (li == ri) {
         double lv = (Double) left.get(li, ri);
         double rv = (Double) right.get(r.getRowKey(), r.getColumnKey());
         // System.out.println(lv+" "+rv+" "+lv*rv);
         g.incr((Integer) l.getRowKey(), (Integer) r.getColumnKey(), lv / rv);
       }
     }
   }
   return g;
 }
Ejemplo n.º 8
0
 public static IMatrix diagonal(double val, int size) {
   IMatrix tmp = new GuavaMatrix();
   for (int i = 0; i < size; i++) tmp.set(i, i, val);
   return tmp;
 }
Ejemplo n.º 9
0
 public static IMatrix diagonal(double... d) {
   IMatrix tmp = new GuavaMatrix();
   for (int i = 0; i < d.length; i++) tmp.set(i, i, d[i]);
   return tmp;
 }