Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created August 17, 2014 17:45
Show Gist options
  • Save chriseidhof/12601c41aba7fa77debf to your computer and use it in GitHub Desktop.
Save chriseidhof/12601c41aba7fa77debf to your computer and use it in GitHub Desktop.
Accelerate-wrapper
//
// main.swift
// Accelerate
//
// Created by Chris Eidhof on 17/08/14.
// Copyright (c) 2014 Chris Eidhof. All rights reserved.
//
import Foundation
import Accelerate
println("Hello, World!")
enum AccelerateUnaryOperation {
case Sqrt
case Ceil
}
enum AccelerateBinaryOperation {
case Add
}
func map(input: [Double], operation: AccelerateUnaryOperation) -> [Double] {
var results = [Double](count:input.count, repeatedValue:0.0)
let count = [Int32(input.count)]
switch operation {
case .Sqrt:
vvsqrt(&results, input, count)
case .Ceil:
vvceil(&results, input, count)
}
return results
}
func zipWith(left: [Double], right: [Double], operation: AccelerateBinaryOperation) -> [Double] {
assert(left.count == right.count, "Expected arrays of the same length, instead got arrays of two different lengths")
var results = [Double](count:left.count, repeatedValue:0.0)
let count = UInt(left.count)
switch operation {
case .Add:
vDSP_vaddD(left, 1, right, 1, &results, 1, count)
}
return results
}
println(map([4.0, 3.0, 16.0],.Sqrt))
println(zipWith([4.0, 3.0, 16.0], [4.0, 3.0, 16.0], .Add))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment