public class MatriceSparsa { private int m, n; private VettoreSparso e[]; public MatriceSparsa(int m, int n) { e = new VettoreSparso [n+1]; for(int j=1; j<=n; j++) e[j] = new VettoreSparso(m); this.m = m; this.n = n; } public double get(int i, int j) { return e[j].get(i); } public void put(int i, int j, double x) { e[j].put(i, x); } public VettoreSparso getCol(int j) { VettoreSparso c = new VettoreSparso(m); for(Integer i : e[j].indices()) c.put(i, get(i, j)); return c; } public MatriceSparsa plus(MatriceSparsa that) { MatriceSparsa a = new MatriceSparsa(m, n); for(int j = 1; j <= n; j++) a.e[j] = this.getCol(j).plus(that.getCol(j)); return a; } public String toString() { String s = ""; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) s += get(i, j) + " "; s += "\n"; } return s; } public int nRows() { return m; } public int nCols() { return n; } public VettoreSparso getRow(int i) { VettoreSparso r = new VettoreSparso(n); for(int j=1; j<=n; j++) if(get(i, j) != 0) r.put(j, get(i, j)); return r; } public MatriceSparsa transpose() { MatriceSparsa a = new MatriceSparsa(n, m); for(int i=1; i<=m; i++) a.e[i] = this.getRow(i); return a; } public static VettoreSparso multiply(MatriceSparsa A, VettoreSparso v) { int m = A.nRows(), n = A.nCols(); MatriceSparsa At = A.transpose(); VettoreSparso r = new VettoreSparso(m); for(int i=1; i<=m; i++) { double s = At.getCol(i).dot(v); if(s != 0) r.put(i, s); } return r; } }