Swift中的方程组求解 [英] Solving System of Equations in Swift

查看:131
本文介绍了Swift中的方程组求解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Swift中复制"\"(反斜杠)运算符在Matlab中的作用.

I want to replicate in Swift what the "\" (backslash) operator does in Matlab.

从本质上讲,它求解矩阵形式的一组方程以给出系数(解).

Essentially, it solves a set of equations in Matrix form to give coefficients (solution).

矩阵A给出如下:

 1     0     0     0     0     0     0     0     0     0
 1     4     1     0     0     0     0     0     0     0
 0     1     4     1     0     0     0     0     0     0
 0     0     1     4     1     0     0     0     0     0
 0     0     0     1     4     1     0     0     0     0
 0     0     0     0     1     4     1     0     0     0
 0     0     0     0     0     1     4     1     0     0
 0     0     0     0     0     0     1     4     1     0
 0     0     0     0     0     0     0     1     4     1
 0     0     0     0     0     0     0     0     0     1 

矩阵b给出如下:

 0
-15
-15
-3
-3
45
-12
-6
 0
 0

在Matlab中,操作cj = A \ b给出:

Operation cj = A\b in Matlab gives:

0
-2.9185
-3.3259
1.2220
-4.5620
14.0262
-6.5427
0.1447
-0.0362
0

如何在Swift中实现上述功能?

How do I implement the above in Swift?

快速等效的矩阵/数组是:

The swift equivalent Matrices/Arrays are:

let A = [[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]]

let b = [0, -15, -15, -3, -3, 45, -12, -6, 0, 0]

任何帮助将不胜感激.

Any help would be greatly appreciated.

推荐答案

我对MATLAB不太了解,但是A\b似乎返回了AX = B的解决方案.为此,可以在iOS/macOS中的LinearAlgebra框架(Accelerate.vecLib.LinearAlgebra)中使用la_solve.

I do not know much about MATLAB, but A\b seems to return the solution of AX=B. For such purpose, you can use la_solve in LinearAlgebra framework (Accelerate.vecLib.LinearAlgebra) in iOS/macOS.

import Foundation
import Accelerate.vecLib.LinearAlgebra

let 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]
let matA = la_matrix_from_double_buffer(A, 10, 10, 10, la_hint_t(LA_NO_HINT), la_attribute_t(LA_DEFAULT_ATTRIBUTES))

let b: [Double] = [
    0, -15, -15, -3, -3, 45, -12, -6, 0, 0
]
let vecB = la_matrix_from_double_buffer(b, 10, 1, 1, la_hint_t(LA_NO_HINT), la_attribute_t(LA_DEFAULT_ATTRIBUTES))

let vecCj = la_solve(matA, vecB)
var cj: [Double] = Array(repeating: 0.0, count: 10)

let status = la_matrix_to_double_buffer(&cj, 1, vecCj)
if status == la_status_t(LA_SUCCESS) {
    print(cj) //->[0.0, -2.9185349611542728, -3.3258601553829079, 1.2219755826859044, -4.5620421753607099, 14.026193118756936, -6.5427302996670358, 0.14472807991120964, -0.036182019977802411, 0.0]
} else {
    print("Failure: \(status)")
}

结果似乎与您的结果相同(精确度除外).

The result seems to be the same (other than the precision) as yours.

这篇关于Swift中的方程组求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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