Contents
  1. 1. vDSP
  2. 2. simd

A =

1 2 3
4 5 6
7 8 9

B =

1
2
3

A X B 使用 iOS 自带的矩阵运算

vDSP

#import <Accelerate/Accelerate.h>

float matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

float rawA[3] = {1, 2, 3};

float resultA[3];

vDSP_mmul(&matrix[0][0], 1, &rawA[0], 1, &resultA[0], 1, 3, 1, 3);

NSLog(@"%f %f %f", resultA[0], resultA[1], resultA[2]);

vDSP_mmul 函数有点丑,这么多参数。

  • 第一个是矩阵 A 的首地址
  • 第二个是步长 1
  • 第三个是矩阵 B 的首地址
  • 第四个是步长 1
  • 第五个是结果矩阵 result 的首地址
  • 第六个是步长 1
  • 第七个是矩阵 A 的行数
  • 第八个是矩阵 B 的列数
  • 第九个是结果矩阵的列数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    Single-precision real M-by-P input matrix.
    __IA
    Stride for A.
    __B
    Single-precision real P-by-N input matrix.
    __IB
    Stride for B.
    __C
    Single-precision real M-by-N result matrix.
    __IC
    Stride for C.
    __M
    The number of rows in matrices A and C.
    __N
    The number of columns in matrices B and C.
    __P
    The number of columns in matrix A and the number of rows in matrix B

运行一下得到结果

14.000000 32.000000 50.000000

simd

#import <simd/simd.h>

simd_float3x3 smatrix = {
    simd_make_float3(1, 2, 3),
    simd_make_float3(4, 5, 6),
    simd_make_float3(7, 8, 9)
};
simd_float3 svector = simd_make_float3(1, 2, 3);

simd_float3 sresult = simd_mul(smatrix, svector);
NSLog(@"%f %f %f", sresult.x, sresult.y, sresult.z);

运行一下结果
30.000000 36.000000 42.000000

结果是错误的,因为 simd_float3 是 3x1 的向量也就是 [1, 2, 3] 的转置。

simd_float3 的定义比较神奇,typedef __attribute__((__ext_vector_type__(3))) float simd_float3;
虽然是 float 却能用 .x .y .z 访问三个分量

Contents
  1. 1. vDSP
  2. 2. simd