/// <summary> /// Construct packed versions of input matrices, and then use sparse row/column dot /// to compute elements of output matrix. This is faster. But still relatively expensive. /// </summary> void multiply_fast(SymmetricSparseMatrix M2in, ref SymmetricSparseMatrix Rin, bool bParallel) { int N = Rows; if (M2in.Rows != N) { throw new Exception("SymmetricSparseMatrix.Multiply: matrices have incompatible dimensions"); } if (Rin == null) { Rin = new SymmetricSparseMatrix(); } SymmetricSparseMatrix R = Rin; // require alias for use in lambda below PackedSparseMatrix M = new PackedSparseMatrix(this); M.Sort(); PackedSparseMatrix M2 = new PackedSparseMatrix(M2in, true); M2.Sort(); // Parallel variant is vastly faster, uses spinlock to control access to R if (bParallel) { // goddamn SpinLock is in .Net 4 //SpinLock spin = new SpinLock(); gParallel.ForEach(Interval1i.Range(N), (r1i) => { for (int c2i = r1i; c2i < N; c2i++) { double v = M.DotRowColumn(r1i, c2i, M2); if (Math.Abs(v) > math.MathUtil.ZeroTolerance) { //bool taken = false; //spin.Enter(ref taken); //Debug.Assert(taken); //R[r1i, c2i] = v; //spin.Exit(); lock (R) { R[r1i, c2i] = v; } } } }); } else { for (int r1i = 0; r1i < N; r1i++) { for (int c2i = r1i; c2i < N; c2i++) { double v = M.DotRowColumn(r1i, c2i, M2); if (Math.Abs(v) > math.MathUtil.ZeroTolerance) { R[r1i, c2i] = v; } } } } }
public SymmetricSparseMatrix Multiply(SymmetricSparseMatrix M2) { SymmetricSparseMatrix R = new SymmetricSparseMatrix(); Multiply(M2, ref R); return(R); }
public void Multiply(SymmetricSparseMatrix M2, ref SymmetricSparseMatrix R, bool bParallel = true) { // testing code //multiply_slow(M2, ref R); //SymmetricSparseMatrix R2 = new SymmetricSparseMatrix(); //multiply_fast(M2, ref R2); //Debug.Assert(R.EpsilonEqual(R2)); multiply_fast(M2, ref R, bParallel); }
public PackedSparseMatrix(SymmetricSparseMatrix m, bool bTranspose = false) { int numRows = (bTranspose) ? m.Columns : m.Rows; Columns = (bTranspose) ? m.Columns : m.Rows; Rows = new nonzero[numRows][]; int[] counts = new int[numRows]; foreach (Index2i ij in m.NonZeroIndices()) { counts[ij.a]++; if (ij.a != ij.b) { counts[ij.b]++; } } for (int k = 0; k < numRows; ++k) { Rows[k] = new nonzero[counts[k]]; } int[] accum = new int[numRows]; foreach (KeyValuePair <Index2i, double> ijv in m.NonZeros()) { int i = ijv.Key.a, j = ijv.Key.b; if (bTranspose) { int tmp = i; i = j; j = tmp; } int k = accum[i]++; Rows[i][k].j = j; Rows[i][k].d = ijv.Value; if (i != j) { k = accum[j]++; Rows[j][k].j = i; Rows[j][k].d = ijv.Value; } } for (int k = 0; k < numRows; ++k) { Debug.Assert(accum[k] == counts[k]); } Sorted = false; }
// returns this*this (requires less memory) public SymmetricSparseMatrix Square(bool bParallel = true) { SymmetricSparseMatrix R = new SymmetricSparseMatrix(); PackedSparseMatrix M = new PackedSparseMatrix(this); M.Sort(); // Parallel variant is vastly faster, uses spinlock to control access to R if (bParallel) { // goddamn SpinLock is in .Net 4 //SpinLock spin = new SpinLock(); gParallel.ForEach(Interval1i.Range(N), (r1i) => { for (int c2i = r1i; c2i < N; c2i++) { double v = M.DotRowColumn(r1i, c2i, M); if (Math.Abs(v) > math.MathUtil.ZeroTolerance) { //bool taken = false; //spin.Enter(ref taken); //Debug.Assert(taken); //R[r1i, c2i] = v; //spin.Exit(); lock (R) { R[r1i, c2i] = v; } } } }); } else { for (int r1i = 0; r1i < N; r1i++) { for (int c2i = r1i; c2i < N; c2i++) { double v = M.DotRowColumn(r1i, c2i, M); if (Math.Abs(v) > math.MathUtil.ZeroTolerance) { R[r1i, c2i] = v; } } } } return(R); }
public bool EpsilonEqual(SymmetricSparseMatrix B, double eps = math.MathUtil.Epsilon) { foreach (var val in d) { if (Math.Abs(B[val.Key.a, val.Key.b] - val.Value) > eps) { return(false); } } foreach (var val in B.d) { if (Math.Abs(this[val.Key.a, val.Key.b] - val.Value) > eps) { return(false); } } return(true); }
/// <summary> /// directly multiply the matrices. This is very slow as the matrix gets /// larger because we are iterating over all indices to find nonzeros /// </summary> void multiply_slow(SymmetricSparseMatrix M2, ref SymmetricSparseMatrix R) { // this multiply is probably not ideal.... int N = Rows; if (M2.Rows != N) { throw new Exception("SymmetricSparseMatrix.Multiply: matrices have incompatible dimensions"); } if (R == null) { R = new SymmetricSparseMatrix(); } List <mval> row = new List <mval>(128); for (int r1i = 0; r1i < N; r1i++) { row.Clear(); this.get_row_nonzeros(r1i, row); int rN = row.Count; for (int c2i = r1i; c2i < N; c2i++) { double v = 0; // would it be faster to convert cols to mval lists?? for (int ri = 0; ri < rN; ++ri) { int k = row[ri].k; v += row[ri].v * M2[k, c2i]; } if (Math.Abs(v) > math.MathUtil.ZeroTolerance) { R[r1i, c2i] = v; } } } }
public SymmetricSparseMatrix(SymmetricSparseMatrix m) { N = m.N; d = new Dictionary <Index2i, double>(m.d); }