void DoMultiplySparse(SparseMatrix other, SparseMatrix result) { result.Clear(); var ax = _storage.Values; var ap = _storage.RowPointers; var ai = _storage.ColumnIndices; var bx = other._storage.Values; var bp = other._storage.RowPointers; var bi = other._storage.ColumnIndices; int rows = RowCount; int cols = other.ColumnCount; int[] cp = result._storage.RowPointers; var marker = new int[cols]; for (int ib = 0; ib < cols; ib++) { marker[ib] = -1; } int count = 0; for (int i = 0; i < rows; i++) { // For each row of A for (int j = ap[i]; j < ap[i + 1]; j++) { // Row number to be added int a = ai[j]; for (int k = bp[a]; k < bp[a + 1]; k++) { int b = bi[k]; if (marker[b] != i) { marker[b] = i; count++; } } } // Record non-zero count. cp[i + 1] = count; } var ci = new int[count]; var cx = new double[count]; for (int ib = 0; ib < cols; ib++) { marker[ib] = -1; } count = 0; for (int i = 0; i < rows; i++) { int rowStart = cp[i]; for (int j = ap[i]; j < ap[i + 1]; j++) { int a = ai[j]; double aEntry = ax[j]; for (int k = bp[a]; k < bp[a + 1]; k++) { int b = bi[k]; double bEntry = bx[k]; if (marker[b] < rowStart) { marker[b] = count; ci[marker[b]] = b; cx[marker[b]] = aEntry * bEntry; count++; } else { cx[marker[b]] += aEntry * bEntry; } } } } result._storage.Values = cx; result._storage.ColumnIndices = ci; result._storage.Normalize(); }
private void DoMultiplySparse(SparseMatrix other, SparseMatrix result) { result.Clear(); var ax = _storage.Values; var ap = _storage.RowPointers; var ai = _storage.ColumnIndices; var bx = other._storage.Values; var bp = other._storage.RowPointers; var bi = other._storage.ColumnIndices; var rows = RowCount; var cols = other.ColumnCount; var cp = result._storage.RowPointers; var marker = new int[cols]; for (var ib = 0; ib < cols; ib++) { marker[ib] = -1; } var count = 0; for (var i = 0; i < rows; i++) { // For each row of A for (var j = ap[i]; j < ap[i + 1]; j++) { // Row number to be added var a = ai[j]; for (var k = bp[a]; k < bp[a + 1]; k++) { var b = bi[k]; if (marker[b] != i) { marker[b] = i; count++; } } } // Record non-zero count. cp[i + 1] = count; } var ci = new int[count]; var cx = new double[count]; for (var ib = 0; ib < cols; ib++) { marker[ib] = -1; } count = 0; for (var i = 0; i < rows; i++) { var rowStart = cp[i]; for (var j = ap[i]; j < ap[i + 1]; j++) { var a = ai[j]; var aEntry = ax[j]; for (var k = bp[a]; k < bp[a + 1]; k++) { var b = bi[k]; var bEntry = bx[k]; if (marker[b] < rowStart) { marker[b] = count; ci[marker[b]] = b; cx[marker[b]] = aEntry * bEntry; count++; } else { cx[marker[b]] += aEntry * bEntry; } } } } result._storage.Values = cx; result._storage.ColumnIndices = ci; result._storage.Normalize(); }