首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Accelerate从SparseOpaqueFactorization中获取/提取因子分解

使用Accelerate从SparseOpaqueFactorization中获取/提取因子分解
EN

Stack Overflow用户
提问于 2021-03-25 16:26:43
回答 1查看 24关注 0票数 0

我正在使用Apples Swift / Accelerate框架编写一些线性代数算法。所有工作和求解的Ax =b方程都会产生正确的结果(此代码来自apple示例)。

我希望能够从LLT分解中提取

代码语言:javascript
复制
SparseOpaqueFactorization_Double

对象。但是似乎没有任何方法来提取(打印)因子分解。有没有人知道从SparseOpaqueFactorization_Double对象中提取分解矩阵的方法?

代码语言:javascript
复制
import Foundation
import Accelerate

print("Hello, World!")

// Example of a symmetric sparse matrix, empty cells represent zeros.

var rowIndices: [Int32] = [0, 1, 3,   // Column 0
                           1, 2, 3,   // Column 1
                            2,        // col 2
                            3]        // Col 3
 

// note that the Matrix representation is the upper triangular
// here. Since the matrix is symmetric, no need to store the lower
// triangular.

 var values: [Double]  = [10.0, 1.0 ,      2.5,          // Column 0
                            12.0, -0.3, 1.1,        // Column 1
                                  9.5,              // Col 2
                                       6.0 ]        // Column 3

var columnStarts = [0,      // Column 0
                    3,      // Column 1
                    6, 7,   // Column 2
                    8]      // col 3

var attributes = SparseAttributes_t()
attributes.triangle = SparseLowerTriangle
attributes.kind = SparseSymmetric

let structure = SparseMatrixStructure(rowCount: 4,
                                      columnCount: 4,
                                      columnStarts: &columnStarts,
                                      rowIndices: &rowIndices,
                                      attributes: attributes,
                                      blockSize: 1)


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

    return SparseFactor(SparseFactorizationCholesky, a)
}


var bValues = [ 2.20, 2.85, 2.79, 2.87 ]
var xValues = [ 0.00, 0.00, 0.00, 0.00 ]

bValues.withUnsafeMutableBufferPointer { bPtr in
    xValues.withUnsafeMutableBufferPointer { xPtr in
        
        let b = DenseVector_Double(
            count: 4,
            data: bPtr.baseAddress!
        )
        let x = DenseVector_Double(
            count: 4,
            data: xPtr.baseAddress!
        )

        SparseSolve(llt, b, x)
    }
}
for val in xValues {
    print("x = " +  String(format: "%.2f", val), terminator: " ")
    
}
print("")
print("Success")
EN

回答 1

Stack Overflow用户

发布于 2021-03-27 18:36:27

好了,经过对苹果快速标题的大量研究,我已经解决了这个问题。

有一个名为的加速API调用

代码语言:javascript
复制
public func SparseCreateSubfactor(_ subfactor: SparseSubfactor_t, _ Factor: SparseOpaqueFactorization_Double) -> SparseOpaqueSubfactor_Double

它返回这个SparceOpaqueSubfactor_类型。这可以在矩阵乘法中使用,以产生一个“透明”的结果(即,您可以使用/打印/查看的矩阵)。因此,我将乔列斯基分解的下三角部分的SubFactor乘以单位矩阵,以提取因子。真是太棒了!

代码语言:javascript
复制
    let subfactors = SparseCreateSubfactor(SparseSubfactorL, llt)
    
    var identValues = generateIdentity(n)
    ppm(identValues)
    
    let sparseAs = SparseAttributes_t(transpose: false,
                                      triangle: SparseUpperTriangle,
                                      kind: SparseOrdinary,
                                      _reserved: 0,
                                      _allocatedBySparse: false)
    
    let identity_m = DenseMatrix_Double(rowCount: Int32(n),
                                        columnCount: Int32(n),
                                        columnStride: Int32(n),
                                        attributes: sparseAs,
                                        data: &identValues)
    
    
    SparseMultiply(subfactors, identity_m) // Output is in identity_m after the call

我写了一个小函数来生成我在上面的代码中使用的单位矩阵:

代码语言:javascript
复制
    func generateIdentity(_ dimension: Int) -> [Double] {
    
    var iden = Array<Double>()
    
    for i in 0...dimension - 1 {
        for j in 0...dimension - 1 {
            if i == j {
                iden.append(1.0)
                
            } else {
                iden.append(0.0)
            }
            
        }
        
        
    }

    return iden
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66795632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档