/// <inheritdoc /> public override void Add(double alpha, double beta, CompressedColumnStorage <double> other, CompressedColumnStorage <double> result) { if (other == null) { throw new ArgumentNullException(nameof(other)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } int p, j, nz = 0; int m = this.rows; int n = this.columns; // check inputs if (m != other.RowCount || n != other.ColumnCount) { throw new ArgumentException(Resources.MatrixDimensions, nameof(other)); } // Workspace var w = new int[m]; var x = new double[m]; // Allocate result: (anz + bnz) is an upper bound var ci = result.ColumnPointers; var cj = result.RowIndices; var cx = result.Values; for (j = 0; j < n; j++) { ci[j] = nz; // column j of C starts here nz = this.Scatter(j, alpha, w, x, j + 1, result, nz); // alpha*A(:,j) nz = other.Scatter(j, beta, w, x, j + 1, result, nz); // beta*B(:,j) for (p = ci[j]; p < nz; p++) { cx[p] = x[cj[p]]; } } // Finalize the last column ci[n] = nz; if (AutoTrimStorage) { // Remove extra space. result.Resize(0); } Helper.SortIndices(result); }
/// <summary> /// Adds two matrices, C = alpha*A + beta*B, where A is current instance. /// </summary> /// <param name="alpha">Scalar factor for A, current instance.</param> /// <param name="beta">Scalar factor for B, other instance.</param> /// <param name="other">The matrix added to this instance.</param> /// <param name="result">Contains the sum.</param> /// <remarks> /// The (result) matrix has to be fully initialized and provide enough space for /// the nonzero entries of the sum. An upper bound is the sum of the nonzeros count /// of (this) and (other). /// </remarks> public override void Add(Complex alpha, Complex beta, CompressedColumnStorage <Complex> other, CompressedColumnStorage <Complex> result) { int p, j, nz = 0; int m = this.rowCount; int n = this.columnCount; // check inputs if (m != other.RowCount || n != other.ColumnCount) { throw new ArgumentException(Resources.MatrixDimensions); } // Workspace var w = new int[m]; var x = new Complex[m]; // Allocate result: (anz + bnz) is an upper bound var ci = result.ColumnPointers; var cj = result.RowIndices; var cx = result.Values; for (j = 0; j < n; j++) { ci[j] = nz; // column j of C starts here nz = this.Scatter(j, alpha, w, x, j + 1, result, nz); // alpha*A(:,j) nz = other.Scatter(j, beta, w, x, j + 1, result, nz); // beta*B(:,j) for (p = ci[j]; p < nz; p++) { cx[p] = x[cj[p]]; } } // Finalize the last column ci[n] = nz; // Remove extra space result.Resize(0); result.SortIndices(); }