我试着在两个向量R^n之间做一个叉积,有没有什么方法可以用最优化的方法来做?
我看了加速图书馆,但仍然找不到任何东西
最佳答案
当n和n=0, 1, 3或7时,交叉积可以存在于Rn中。
来源:http://www.math.csusb.edu/faculty/pmclough/CP.pdf
所以不,你肯定找不到这样的图书馆。如果你是指元素,你可以使用加速。下面是一个简短的测试:
import Accelerate
let n = 10_000_000
let a = (0..<n).map{ _ in Double(arc4random()) / Double(UInt32.max) }
let b = (0..<n).map{ _ in Double(arc4random()) / Double(UInt32.max) }
print("A: [\(a.prefix(10).map{ "\($0)" }.joinWithSeparator(", ")), ...]")
print("B: [\(b.prefix(10).map{ "\($0)" }.joinWithSeparator(", ")), ...]")
var result = [Double](count: n, repeatedValue: 0)
let start = mach_absolute_time()
vDSP_vmulD(a, 1, b, 1, &result, 1, UInt(n))
let stop = mach_absolute_time()
let time = Double(stop - start) / Double(NSEC_PER_SEC)
print("Time: \(time) for \(n) elements")
print("Result: [\(result.prefix(10).map{ "\($0)" }.joinWithSeparator(", ")), ...]")
输出:
A: [0.269752697849123, 0.851672558312228, 0.0668649589798564, 0.0955562389212559, 0.255900985620893, 0.93693982901446, 0.085282990495973, 0.732230591525377, 0.588338787804437, 0.952581417968632, ...]
B: [0.750105029379508, 0.0454008649209051, 0.863010750120275, 0.308104009904923, 0.700024090637459, 0.327355608653127, 0.679469040520366, 0.666848364208557, 0.0567599588671606, 0.623293806245386, ...]
Time: 0.024393279 for 10000000 elements
Result: [0.202342855345318, 0.0386666707767751, 0.0577051784059674, 0.0294412603830718, 0.179136854752495, 0.306712507998386, 0.0579471517250063, 0.488286772182162, 0.0333940853957349, 0.593738097764296, ...]
1000万元素0.024秒相当快
关于ios - Swift iOS中的跨产品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38127835/