Print a matrix stored as a SparseMatrixStructure

97 views Asked by At

I am working with Apple's Accelerate framework, using Sparse Matrix operations.

I have created a sparse matrix thus:

    var  values: Array<Double> = [0.125, 0.08, -0.01, 0.405, -0.02, 0.005]

    var rowIndices: [Int32] = [0, 1, 2,     // Column 0
                               3, 4,     // Column 1
                               5 ]           // Column 3
     
    var columnStarts = [0, 3, 5, 6]

let structure: SparseMatrixStructure = rowIndices.withUnsafeMutableBufferPointer { rowIndicesPtr in
        columnStarts.withUnsafeMutableBufferPointer { columnStartsPtr in
            var attributes = SparseAttributes_t()
            attributes.triangle = SparseLowerTriangle
            attributes.kind = SparseSymmetric
            
            return SparseMatrixStructure(
                rowCount: 3,
                columnCount: 3,
                columnStarts: columnStartsPtr.baseAddress!,
                rowIndices: rowIndicesPtr.baseAddress!,
                attributes: attributes,
                blockSize: 1
            )
        }

I am making a call to factorise the matrix:

 let llt: SparseOpaqueFactorization_Double = values.withUnsafeMutableBufferPointer { valuesPtr in
        let a = SparseMatrix_Double(
            structure: structure,
            data: valuesPtr.baseAddress!
        )
        
        return SparseFactor(SparseFactorizationCholesky, a)
    }

I really need to check what the result is of the factorisation - i.e. print out the result. Can't see any way of even getting the result, let alone print it out.

Can anyone help me understand how to retrieve the result and print it?

1

There are 1 answers

0
Tomm P On

Solved this myself eventually. I used:

let subfactors = SparseCreateSubfactor(SparseSubfactorL, llt)

to extract a Subfactors object from the factorisation, then multiplied the subfactors by the identity matrix to get at the factorisation.