2

I need to multiply 2 matrices in swift, so in order to use accelerate I need 2 arrays with type double. The issue is, the two arrays that I need to multiply are type UInt32. Is there anyway to convert a UInt32 array to a double array?

var UInt32TestArray: [UInt32] = [1,4,2,3]
var Int32TestArray: [Double] = [Double](UInt32) //Doesn't work
Ethan Rappaport
  • 417
  • 1
  • 6
  • 14
  • 3
    Suggest not capitalizing variables names - they look like type names. And, naming a `[Double]` type as `Int32TestArray` is confusing. Just style suggestions. Cheers. – GoZoner Jul 20 '16 at 15:59

3 Answers3

19

Use

UInt32TestArray.map { Double($0) }

to get an array of Double.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
0

I don't think you can use a cast for these types of things. Usually you use a map function for converting an array of one type to another. An answer to a similar question can be found here. Conversion of entire array from int to double in order to do some aritmetic operations

Community
  • 1
  • 1
Amloelxer
  • 752
  • 2
  • 8
  • 20
-1

Try this

var Int32TestArray = UInt32TestArray.map{UInt32(abs($0))}
Feldur
  • 1,121
  • 9
  • 23