Esempio n. 1
0
 /*
  * Matrix mult but with min-plus, and iterative. Each min-plus operation
  * that changes the path inserts it into a new queue
  */
 public SparseMatrix apsp() {
   SparseMatrix shortestPaths = new SparseMatrix(this);
   SparseMatrix currentPairs = new SparseMatrix(this.rowDim, this.colDim);
   SparseMatrix newPairs = new SparseMatrix(this.rowDim, this.colDim);
   newPairs = new SparseMatrix(this);
   for (int d = 0; d < this.rowDim; d++) {
     shortestPaths.set(d, d, 0.0);
   }
   for (int d = 0; d < this.rowDim; d++) {
     newPairs.set(d, d, 0.0);
   }
   while (!newPairs.isEmpty()) {
     currentPairs = new SparseMatrix(newPairs);
     newPairs = new SparseMatrix(this.rowDim, this.colDim);
     for (int r : currentPairs.rows) {
       Counter row = currentPairs.getRow(r);
       for (int c : row.keySet()) {
         Counter oRow = this.getRow(c);
         for (int oc : oRow.keySet()) {
           double pathLength = currentPairs.get(r, c) + oRow.get(oc);
           if (pathLength < shortestPaths.getPath(r, oc)) {
             newPairs.set(r, oc, pathLength);
             shortestPaths.set(r, oc, pathLength);
           }
         }
       }
     }
   }
   return shortestPaths;
 }
Esempio n. 2
0
 public static Counter ConstrainedEig(SparseMatrix mat, SparseMatrix orthogMat) {
   double trimEps = 0;
   int iterLimit = 10000;
   long start;
   long end;
   Counter vector = new Counter();
   int vecLen = mat.colDim;
   for (int i = 0; i < vecLen; i++) {
     //			vector.add(i, 1.0/Math.sqrt(vecLen));
     vector.add(i, Math.random() - 0.5);
   }
   Counter oldVector = new Counter();
   //		vector = mat.multiply(vector);
   //		for(int r: orthogMat.rows){
   //			Counter orthogRow = orthogMat.getRow(r);
   //			vector.orthogonalize(orthogRow);
   ////			System.out.println(vector.dot(orthogRow));
   //		}
   double norm = 0;
   double sim = 0;
   double diff = 1.0;
   double diffNeg = 1.0;
   int t = 0;
   start = System.currentTimeMillis();
   while (diff > Math.pow(10.0, -16.0) && diffNeg > Math.pow(10.0, -16.0) && t < iterLimit) {
     t += 1;
     //			vector.trimKeys(trimEps);
     oldVector = new Counter(vector);
     vector = mat.multiply(vector);
     for (int r : orthogMat.rows) {
       Counter orthogRow = orthogMat.getRow(r);
       //				System.out.println("before: "+vector.dot(orthogRow));
       vector.orthogonalize(orthogRow);
       //				System.out.println("after: "+vector.dot(orthogRow));
     }
     norm = 0;
     norm = vector.norm();
     vector.multiply(1.0 / norm);
     diff = 0;
     diffNeg = 0;
     Set<Integer> vecOldUnion = vector.concreteKeySet();
     vecOldUnion.addAll(oldVector.concreteKeySet());
     for (int i : vecOldUnion) {
       //			for(int i = 0; i < mat.colDim; i++){
       diff += (oldVector.get(i) - vector.get(i)) * (oldVector.get(i) - vector.get(i));
       diffNeg += (oldVector.get(i) + vector.get(i)) * (oldVector.get(i) + vector.get(i));
     }
     sim = vector.dot(oldVector);
     //			System.out.println(diff+" "+diffNeg+" "+sim+" "+norm+"
     // "+vector.dot(orthogMat.getRow(0)));
     //			System.out.println(vector.toString());
     // System.out.println(oldVector.toString());
   }
   System.out.println(norm + " " + orthogMat.rows.size() + " " + sim);
   //		System.out.println(mat.toStringValues());
   end = System.currentTimeMillis();
   System.out.println("Time: " + (end - start) + " iterations: " + t);
   return vector;
 }
Esempio n. 3
0
 public void removeEntries(SparseMatrix redundant) {
   for (int r : redundant.getRows()) {
     Counter row = redundant.getRow(r);
     for (int c : row.keySet()) {
       this.remove(r, c);
     }
   }
 }
Esempio n. 4
0
 public SparseMatrix add(SparseMatrix other) {
   SparseMatrix sum = new SparseMatrix(this.rowDim, this.colDim);
   sum = new SparseMatrix(this);
   for (int r : other.rows) {
     sum.addRow(r, other.getRow(r));
   }
   return sum;
 }
Esempio n. 5
0
 public SparseMatrix multiply(SparseMatrix other) {
   SparseMatrix mult = new SparseMatrix(this.rowDim, other.colDim);
   for (int r : rows) {
     // System.out.println("multiplying row: "+ r);
     Counter row = this.getRow(r);
     // for(int c: other.cols){
     // Counter col = other.getCol(c);
     // double dotProd = row.dot(col);
     // System.out.println(row.toString()+" "+col.toString()+" "+dotProd);
     // mult.set(r, c, dotProd);
     // }
     for (int c : row.keySet()) {
       Counter oRow = other.getRow(c);
       for (int oc : oRow.keySet()) {
         mult.add(r, oc, row.get(c) * oRow.get(oc));
       }
     }
   }
   return mult;
 }