使用dgtsv_或sgtsv_求解A * X = B类型的方程 [英] Solve Equations Of Type A*X = B Using dgtsv_ or sgtsv_

查看:216
本文介绍了使用dgtsv_或sgtsv_求解A * X = B类型的方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决以下类型的线性方程组:SWIFT中的A * X = B.

I am trying to solve a system of linear equations of the type: A*X = B in SWIFT.

我已经能够使用基于LU分解的算法来执行此操作,该算法消耗O(N ^ 2)内存.

I have been able to do this using LU factorization based algorithm that consumes O(N^2) memory.

由于我的数组通常很大(10000个样本或更多),因此我正在查看LAPACK,它具有一些特定于三对角矩阵的函数,这些函数仅占用O(N)的存储空间和更有效率.

Since my arrays are generally big (10000 samples and more), I am looking at LAPACK that has some functions specific to tridiagonal matrices which consumes only O(N) memory space & are more efficient.

http://www. netlib.org/lapack/explore-html-3.4.2/d4/d62/group__double_g_tsolve.html#

基本上,我希望使用上面的dgtsv_或sgtsv_函数求解方程.但是我找不到任何例子.

Essentially, I am looking to solve the equations using dgtsv_ or sgtsv_ functions above. But there are no examples I can find.

由于我是SWIFT的新手,所以我努力传递该函数要求的8个输入参数.哪里有例子?

As I am fairly new to SWIFT, I am struggling to pass the 8 input parameters the function asks for. Is there an example somewhere?

我粘贴到我的工作代码下面(使用LU分解).

I paste below my working code (using LU factorization).

import Accelerate

func solve( A:[Double], _ B:[Double] ) -> [Double] {

var inMatrix:[Double] = A

var solution:[Double] = B

// Get the dimensions of the matrix. An NxN matrix has N^2
// elements, so sqrt( N^2 ) will return N, the dimension
var N:__CLPK_integer = __CLPK_integer( sqrt( Double( A.count ) ) )

// Number of columns on the RHS
var NRHS:__CLPK_integer = 1

// Leading dimension of A and B
var LDA:__CLPK_integer = N

var LDB:__CLPK_integer = N

// Initialize some arrays for the dgetrf_(), and dgetri_() functions
var pivots:[__CLPK_integer] = [__CLPK_integer](repeating: 0, count: Int(N))

var error: __CLPK_integer   = 0

// Perform LU factorization
dgetrf_(&N, &N, &inMatrix, &N, &pivots, &error)

// Calculate solution from LU factorization
_ = "T".withCString {
    dgetrs_( UnsafeMutablePointer(mutating: $0), &N, &NRHS, &inMatrix, &LDA, &pivots, &solution, &LDB, &error )
}
return solution
  }


  //Call the function
  var A: [Double] = [
      1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
      1.0, 4.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
      0.0, 1.0, 4.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
      0.0, 0.0, 1.0, 4.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
      0.0, 0.0, 0.0, 1.0, 4.0, 1.0, 0.0, 0.0, 0.0, 0.0,
      0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 1.0, 0.0, 0.0, 0.0,
      0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 1.0, 0.0, 0.0,
      0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 1.0, 0.0,
      0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 4.0, 1.0,
      0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]

  var b: [Double] = [0, -15, -15, -3, -3, 45, -12, -6, 0, 0]

  var cj = solve(A: A, b)

  print( cj ) // --> [0.0, -2.9185349611542728, -3.3258601553829079,   1.2219755826859044, -4.5620421753607099, 14.026193118756936, -6.5427302996670358, 0.14472807991120964, -0.036182019977802411, 0.0]
  //Call the function


  //TRY LAPACK (need examples to get above solution)
  let xx = dgtsv_(<#T##__n:    UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__nrhs: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__dl: UnsafeMutablePointer<__CLPK_doublereal>!##UnsafeMutablePointer<__CLPK_doublereal>!#>, <#T##__d__: UnsafeMutablePointer<__CLPK_doublereal>!##UnsafeMutablePointer<__CLPK_doublereal>!#>, <#T##__du: UnsafeMutablePointer<__CLPK_doublereal>!##UnsafeMutablePointer<__CLPK_doublereal>!#>, <#T##__b: UnsafeMutablePointer<__CLPK_doublereal>!##UnsafeMutablePointer<__CLPK_doublereal>!#>, <#T##__ldb: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__info: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>)

  let xx2 = sgtsv_(<#T##__n: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__nrhs: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__dl: UnsafeMutablePointer<__CLPK_real>!##UnsafeMutablePointer<__CLPK_real>!#>, <#T##__d__: UnsafeMutablePointer<__CLPK_real>!##UnsafeMutablePointer<__CLPK_real>!#>, <#T##__du: UnsafeMutablePointer<__CLPK_real>!##UnsafeMutablePointer<__CLPK_real>!#>, <#T##__b: UnsafeMutablePointer<__CLPK_real>!##UnsafeMutablePointer<__CLPK_real>!#>, <#T##__ldb: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>, <#T##__info: UnsafeMutablePointer<__CLPK_integer>!##UnsafeMutablePointer<__CLPK_integer>!#>)
  //TRY LAPACK (need examples to get above solution)

推荐答案

dgtsv_() expects the lower/main/upper diagonal of the tri-diagonal matrix as separate arguments. You can pass the address of variable arrays with &.

所有整数参数都是__CLPK_integerInt32的地址 变量.

All integer parameters are addresses of __CLPK_integer aka Int32 variables.

右侧的矢量b被解决方案x覆盖为 A x = b公式.描述A的三个向量被覆盖 同样,因此您可能想要复制原始数据.

The right-hand side vector b is overwritten with the solution x to the A x = b equation. The three vectors describing A are overwritten as well, so you might want to make copies of the original data.

示例:

import Swift
import Accelerate

var mainDiagA = [ 1.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 1.0 ]
var upperDiagA = [ 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]
var lowerDiagA = [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0 ]

var b = [0.0, -15.0, -15.0, -3.0, -3.0, 45.0, -12.0, -6.0, 0.0, 0.0 ]

var n = Int32(mainDiagA.count) // Order of matrix A
var nrhs = Int32(1) // Number of right-hand sides
var info = Int32(0) // Result code

dgtsv_(&n, &nrhs, &lowerDiagA, &mainDiagA, &upperDiagA, &b, &n, &info)
if info == 0 { // success
    print(b)
    // [0.0, -2.9185349611542732, -3.3258601553829075, 1.2219755826859044, -4.5620421753607099, 14.026193118756938, -6.5427302996670367, 0.14472807991120964, -0.036182019977802411, 0.0]

}

这篇关于使用dgtsv_或sgtsv_求解A * X = B类型的方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆