1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swiftで行列の計算

Last updated at Posted at 2022-12-16

はじめに

Swiftでsimdの計算に初めて取り掛かる場合、まずはじめに以下の記事を読むことをお勧めします。
https://developer.apple.com/documentation/accelerate/working_with_matrices

スクリーンショット 2022-12-16 0.15.32.png

記事にもある通り、列優先の命名規則になります。

計算の確認ツール

Matrix Multiplicationを計算手順の思い出し & 計算結果の確認使用します。

列ベクトル

列ベクトルが一般的だと思いますが、行ベクトルで生成、計算することも可能です。

スクリーンショット 2022-09-22 23.05.45.png

simd.swift
import simd

let vector = simd_float4(-0.8, 0, 0, 1)
// 4x4の変換行列(列優先)
let transformA = simd_float4x4([
    simd_float4(-0.07, 0, -0.9, 0),
    simd_float4(0, 0.9, 0, 0),
    simd_float4(0.9, 0, -0.07, 0),
    simd_float4(-0.8, 0.01, -1.58, 1.0)
])

let rowResult1 = simd_mul(transformA, vector)
print(rowResult1)
// > SIMD4<Float>(-0.744, 0.01, -0.8600001, 1.0)

行ベクトル

スクリーンショット 2022-09-22 18.24.05.png

引数にrowsを指定することで行優先で作成することができます。

simd.swift
import simd

let vector = simd_float4(-0.8, 0, 0, 1)

// 4x4の変換行列(行優先)
let transformB = simd_float4x4(rows: [
    simd_float4(-0.07, 0, -0.9, 0),
    simd_float4(0, 0.9, 0, 0),
    simd_float4(0.9, 0, -0.07, 0),
    simd_float4(-0.8, 0.01, -1.58, 1.0)
])

let rowResult2 = simd_mul(vector, transformB)
print(rowResult1)
// > SIMD4<Float>(-0.744, 0.01, -0.8600001, 1.0)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?