Skip to content

Instantly share code, notes, and snippets.

@Jerrynet
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save Jerrynet/a5e4990958dea9e260ee to your computer and use it in GitHub Desktop.

Select an option

Save Jerrynet/a5e4990958dea9e260ee to your computer and use it in GitHub Desktop.
Swift Example: LinearAlgebra(LA Object) in Accelerate Framework
import Accelerate
//Perform a 3x3 matrix production, axb
var a:[Double] = [1,1,0, 0,1,0, 0,0,1]
var b:[Double] = [1,2,3, 4,5,6, 7,8,9]
//Swift cannot understand LA constants, need to cast them
var aObj:la_object_t = la_matrix_from_double_buffer(&a, 3, 3, 3, la_hint_t(LA_NO_HINT), la_attribute_t(LA_DEFAULT_ATTRIBUTES))
var bObj:la_object_t = la_matrix_from_double_buffer(&b, 3, 3, 3, la_hint_t(LA_NO_HINT), la_attribute_t(LA_DEFAULT_ATTRIBUTES))
//Perform matrix production, this will not calculate results immediately.
//Please see "Accelerate Framework" video from WWDC 2014
var s:la_object_t = la_matrix_product(aObj, bObj)
//Calculate result and send it to the variable "result"
var result:[Double] = [Double](count:9, repeatedValue: 0)
var res = la_matrix_to_double_buffer(&result, 3, s)
//If no error occurred, print result
if Int32(res) == LA_SUCCESS {
println("ok")
println(result)
}else{
println("wrong")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment