Swift中的方程组求解

编程入门 行业动态 更新时间:2024-10-11 03:23:05
本文介绍了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中的方程组求解

本文发布于:2023-11-30 06:10:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方程组   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!