Beispiel #1
0
        /// <summary>
        /// Sparse matrix multiplication, C = A*B
        /// </summary>
        /// <param name="other">column-compressed matrix</param>
        /// <returns>C = A*B, null on error</returns>
        public CompressedColumnStorage <double> Multiply(CompressedColumnStorage <double> other)
        {
            int m = this.nrows;
            int n = other.ColumnCount;

            int anz = this.NonZerosCount;
            int bnz = other.NonZerosCount;

            int p, j, nz = 0;

            int[]    cp, ci, bp, bi;
            double[] bx, cx;

            // check inputs
            if (other == null)
            {
                throw new Exception();
            }
            if (this.ColumnCount != other.RowCount)
            {
                throw new MatrixException();
            }

            bp = other.ColumnPointers;
            bi = other.RowIndices;
            bx = other.Values;

            // Workspace
            var w = new int[m];
            var x = new double[m];

            var result = new CompressedColumnStorage(m, n, anz + bnz);

            cp = result.ColumnPointers;
            for (j = 0; j < n; j++)
            {
                if (nz + m > result.Values.Length && !result.Resize(2 * (result.Values.Length) + m))
                {
                    throw new Exception(); // out of memory
                }
                ci    = result.RowIndices;
                cx    = result.Values; // C.i and C.x may be reallocated
                cp[j] = nz;            // column j of C starts here
                for (p = bp[j]; p < bp[j + 1]; p++)
                {
                    nz = this.Scatter(bi[p], bx[p], w, x, j + 1, result, nz);
                }

                for (p = cp[j]; p < nz; p++)
                {
                    cx[p] = x[ci[p]];
                }
            }
            cp[n] = nz;       // finalize the last column of C
            result.Resize(0); // remove extra space from C
            result.SortIndices();

            return(result); // success
        }
Beispiel #2
0
        /// <summary>
        /// Sums two matrices.
        /// </summary>
        /// <param name="alpha">Scalar factor for (this).</param>
        /// <param name="beta">Scalar factor for (other).</param>
        /// <param name="other">The matrix added to this instance.</param>
        /// <param name="result">Contains the sum: alpha*(this) + beta*(other).</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 void Add(double alpha, double beta, CompressedColumnStorage <double> other,
                        CompressedColumnStorage <double> result)
        {
            int p, j, nz = 0;

            int m = this.nrows;
            int n = this.ncols;

            // check inputs
            if (m != other.RowCount || n != other.ColumnCount)
            {
                throw new ArgumentException(); // TODO: ex
            }

            var bi = other.ColumnPointers;
            var bx = other.Values;

            int anz = this.ColumnPointers[ncols];
            int bnz = bi[n];

            // 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;

            // Remove extra space
            result.Resize(0);
            result.SortIndices();
        }