swift SIMD。用于对小矢量和矩阵执行计算的模块。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift SIMD。用于对小矢量和矩阵执行计算的模块。相关的知识,希望对你有一定的参考价值。

import simd // importing module

let arr = [2.0, 2.0]
// initializing simd
let vector = simd_double2(x: arr[0], y:arr[1]) // initializing
let normalized_vector = simd_normalize(vector) // normalized vector
let gfd : Double = normalized_vector.x // fetching values

import simd // importing module

let arr = [2.0, 2.0]
// initializing simd
let vector = simd_double2(x: arr[0], y:arr[1]) // initializing
let normalized_vector = simd_normalize(vector) // normalized vector
let gfd : Double = normalized_vector.x // fetching values
let transposed_matrix = simd_double3x3(rows: rows).transpose //transpose of matrix

//*** Multiply vectors -----------
import simd
let rows = [
    simd_double3(2,      0, 0),
    simd_double3(     1, 2, 0),
    simd_double3(     0,      0, 1)
]
let RotationMatrix = simd_double3x3(rows: rows)
let positionVector = simd_double3(x: 3, y: 2, z: 1)
print(RotationMatrix)
print(RotationMatrix*positionVector)
//------------------------------***

//*** Cross multiplication of vectors---------
let two = simd_double3(x: 3, y: 2, z: 12)
let three = simd_double3(x: 14, y: 22, z: 10)
print(cross(two, three))
//-----------------------------------------***

//*** converting matrix into string----------
let RotationMatrix = simd_double3x3(rows: rows)
var rotationMatrix_string = ""
for i in 0...2{
    for col in [RotationMatrix.columns.0, RotationMatrix.columns.1, RotationMatrix.columns.2]
    {
        rotationMatrix_string += String(col[i]) + ", "
    }
    rotationMatrix_string += "\n"
}

rotationMatrix_string
print(rotationMatrix_string)
//---------------------------------------*****


//finding angle between two vectors
import simd
var vector1 = float3(0.0, 1.0, 0.0) // VECTOR 1
var vector2 = float3(1.0, 1.0, 0.0) // VECTOR 2
var dotProduct = dot(vector1, vector2)
var a = dotProduct / (length(vector1) * length(vector2))
a = Float(round(1000*a)/1000)
let angle = acos(a)*(360.0/3.14) // angle is in degrees

//

以上是关于swift SIMD。用于对小矢量和矩阵执行计算的模块。的主要内容,如果未能解决你的问题,请参考以下文章

如何使用内部函数 C++ 将 3 个加法和 1 个乘法转换为矢量化 SIMD

Swift 中的向量 SIMD 类型

是否可以使用 SIMD 指令进行 3x3 矩阵求逆?

使用 Rcpp 和 OpenMP 在 R 中多线程和 SIMD 矢量化 Mandelbrot

SIMD加速计算矩阵(组成原理实验5)

使用矢量化 C++ 的矩阵乘法