对数组中的 2 个连续元素求和

Sum 2 consecutive elements in an array

我有一个指向浮点数组的指针:arr = [a0, a1, a2, a3, ..., an]。 我希望结果是:result = [a0+a1, a0+a1, a2+a3, a2+a3, a4+a5, a4+a5, ...]。 现在我用 map() 函数来做:

let multiArrayValue: MLMultiArray = someMulityArray
let pointer = (multiArrayValue.dataPointer).bindMemory(to: Float.self, capacity: multiArrayValue.count)
let sums = (0..<multiArrayValue.count/2).map { (index) -> [Float] in
                let sum = pointer[index * 2] + pointer[index * 2 + 1]
                return [sum, sum]
            }.flatMap { [=10=] }

如何使用 Accelerate 框架高效地做到这一点?

编辑: 我设法得到 res = [a0+a1, a2+a3, a4+a5, ..., an+an]:

let k = multiArrayValue.count/2
let n = vDSP_Length(k)
var res = [Float](repeating: 0, count: k)
vDSP_vadd(&pointer, vDSP_Stride(2),
          &pointer+1, vDSP_Stride(2),
          &res, vDSP_Stride(1),
          n)

所以现在剩下的问题是如何用Accelerate得到重复值:[a1, a2, a3, ... an] => [a1, a1, a2, a2, ..., an, an]

这个问题的解决方案分两步完成。这两个步骤的关键是玩大步。首先只计算总和向量:

let k = multiArrayValue.count/2
let n = vDSP_Length(k)
var sums = [Float](repeating: 0, count: k)
vDSP_vadd(&pointer, vDSP_Stride(2),
          &pointer+1, vDSP_Stride(2),
          &sums, vDSP_Stride(1),
          n)

第二步是求重复和:

var resSparse = [Float](repeating: 0.0, count: k * 2)
vDSP_vmax(pointerOpt, 2, &sums + 1, 2, &resSparse, 2, k)
var res = [Float](repeating: 0.0, count: k * 2)
catlas_saxpby(k * 2 - 1, 1.0, &resSparse, 1, 1.0, &res + 1, 1)
catlas_saxpby(k * 2, 1.0, &resSparse, 1, 1.0, &res, 1)