6

I'm using the dgeev algorithm from the LAPACK implementation in the Accelerate framework to calculate eigenvectors and eigenvalues of a matrix. Sadly the LAPACK functions are not described in the Apple Documentation with a mere link to http://netlib.org/lapack/faq.html included.

If you look it up, you will find that the first two arguments in dgeev are characters signifying whether to calculate eigenvectors or not. In Swift, it is asking for UnsafeMutablePointer<Int8>. When I simply use "N", I get an error. The dgeev function and the error are described in the following screenshotenter image description here

What should I do to solve this?

Youssef Moawad
  • 2,846
  • 5
  • 28
  • 50

3 Answers3

4

The "problem" is that the first two parameters are declared as char * and not as const char *, even if the strings are not modified by the function:

int dgeev_(char *__jobvl, char *__jobvr, ...);

is mapped to Swift as

func dgeev_(__jobvl: UnsafeMutablePointer<Int8>, __jobvr: UnsafeMutablePointer<Int8>, ...) -> Int32;

A possible workaround is

let result = "N".withCString { 
    dgeev_(UnsafeMutablePointer($0), UnsafeMutablePointer($0), &N, ...)
}

Inside the block, $0 is a pointer to a NUL-terminated array of char with the UTF-8 representation of the string.


Remark: dgeev_() does not modify the strings pointed to by the first two arguments, so it "should be" declared as

int dgeev_(const char *__jobvl, const char *__jobvr, ...);

which would be mapped to Swift as

func dgeev_(__jobvl: UnsafePointer<Int8>, __jobvr: UnsafePointer<Int8>, ...) -> Int32;

and in that case you could simply call it as

let result = dgeev_("N", "N", &N, ...)

because Swift strings are converted to UnsafePointer<Int8>) automatically, as explained in String value to UnsafePointer<UInt8> function parameter behavior.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
3

It is ugly, but you can use:

let unsafePointerOfN = ("N" as NSString).UTF8String
var unsafeMutablePointerOfN: UnsafeMutablePointer<Int8> = UnsafeMutablePointer(unsafePointerOfN)

and use unsafeMutablePointerOfN as a parameter instead of "N".

Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
  • There is at least a theoretical problem with this solution: The string returned by `.UTF8String` is only guaranteed to be valid as long as the `NSString` object exists, and that could be destroyed immediately because there is no reference to it after the first line. Most probably this will not happen here because the string here is a literal constant. – Martin R Jan 10 '15 at 13:49
0

With Swift 4.2 and 5 you can use this similar approach

let str = "string"
let unsafePointer = UnsafeMutablePointer<Int8>(mutating: (str as NSString).utf8String)

You can get the result from unsafePointer.

abdullahselek
  • 7,893
  • 3
  • 50
  • 40