Contents

SparseMultiply(_:_:_:)

Performs the multiply operation Y = AX on a sparse matrix of double-precision, floating-point values.

Declaration

func SparseMultiply(_ A: SparseMatrix_Double, _ X: DenseMatrix_Double, _ Y: DenseMatrix_Double)

Parameters

  • A:

    The sparse matrix A in Y = AX.

  • X:

    The dense matrix X in Y = AX.

  • Y:

    The dense matrix Y in Y = AX.

Discussion

Use this function to multiply a sparse matrix by a dense matrix. The following equation is an example of a matrix-matrix multiplication where the first matrix is sparse:

[Image]

Call SparseMultiply(_:_:_:) to calculate the result.

let rowCount = Int32(4)
let columnCount = Int32(4)
let blockCount = 4
let blockSize = UInt8(1)
let rowIndices: [Int32] = [0, 3, 0, 3]
let columnIndices: [Int32] = [0, 0, 3, 3]
let data = [1.0, 4.0, 13.0, 16.0]

/// The _A_ in _Y=AX_.
let A = SparseConvertFromCoordinate(rowCount, columnCount,
                                    blockCount, blockSize,
                                    SparseAttributes_t(),
                                    rowIndices, columnIndices,
                                    data)
defer {
    SparseCleanup(A)
}

/// The values for _X_ in _Y=AX_.
var xValues = [10.0, -1.0, -1.0, 10.0,
               100.0, -1.0, -1.0, 100.0]

/// The values for _Y_ in _Y=AX_.
let yValues = [Double](unsafeUninitializedCapacity: xValues.count) {
    resultBuffer, count in
    
    xValues.withUnsafeMutableBufferPointer { denseMatrixPtr in
        /// The _X_ in _Y=AX_.
        let X = DenseMatrix_Double(rowCount: 4,
                                   columnCount: 2,
                                   columnStride: 4,
                                   attributes: SparseAttributes_t(),
                                   data: denseMatrixPtr.baseAddress!)
        
        /// The _Y_ in _Y=AX_.
        let Y = DenseMatrix_Double(rowCount: 4,
                                   columnCount: 2,
                                   columnStride: 4,
                                   attributes: SparseAttributes_t(),
                                   data: resultBuffer.baseAddress!)
        
        SparseMultiply(A, X, Y)
    }
    
    count = xValues.count
}

// On return, `yValues` contains:
//      `[ 140.0, 0.0, 0.0,  200.0,
//        1400.0, 0.0, 0.0, 2000.0]`

See Also

Multiplication functions