Taketo Sano
September 19, 2021
330

# Using Eigen from Swift (via C)

## Taketo Sano

September 19, 2021

## Transcript

1. ### Using Eigen from Swift (via C) Using Eigen from Swift

(via C) さ の た け と @ t a k e t o 1 0 2 4 ͞ͷ͚ͨͱ !UBLFUP  J04%\$+BQBO
2. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  J04%\$.F  さ の た け と @ t a k e t o 1 0 2 4   
3. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  &JHFO  &JHFOJTBIJHIMFWFM\$ MJCSBSZPGUFNQMBUFIFBEFSTGPS MJOFBSBMHFCSB NBUSJYBOEWFDUPSPQFSBUJPOT  'BTU  -JHIUXFJHIU  &YUFOTJCMF 8FXBOUBXSBQQFSMJCSBSZGPS4XJGU
4. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  6TJOH&JHFOGSPN4XJGU  ↟3FRVJSFNFOU4DBMBSUZQFTNVTUCFHFOFSJD let a = EigenIntMatrix( ... ) let b = EigenDoubleMatrix( ... ) let c = EigenRationalMatrix( ... ) 🙅 let a = EigenMatrix<Int>( ... ) let b = EigenMatrix<Double>( ... ) let c = EigenMatrix<Rational>( ... ) 🙆
5. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  )PX  4XJGU \$ \$ EigenMatrix<Int> EigenMatrix<Double> Eigen::Matrix<int64_t> Eigen::Matrix<double> EigenMatrix<Rational> Eigen::Matrix<rat64>
6. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   4XJGU  protocol Scalar { static func + (a: Self, b: Self) -> Self static func - (a: Self, b: Self) -> Self static func * (a: Self, b: Self) -> Self … } 4XJGU extension Int: Scalar {} extension Double: Scalar {} struct Rational: Scalar { … } 4XJGU
7. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   4XJGU  typealias EigenMatrixPointer = UnsafeMutableRawPointer typealias EigenMatrixOperation = (EigenMatrixPointer, EigenMatrixPointer, EigenMatrixPointer) -> Void protocol EigenCompatibleScalar: Scalar { static var eigen_init: (Int, Int) -> EigenMatrixPointer { get } static var eigen_add: EigenMatrixOperation { get } static var eigen_minus: EigenMatrixOperation { get } static var eigen_mul: EigenMatrixOperation { get } … } 4XJGU
8. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   4XJGU  struct EigenMatrix<R: EigenCompatibleScalar> { private var ptr: EigenMatrixPointer init(size: MatrixSize) { let ptr = R.eigen_init(size.rows, size.cols) self.ptr = ptr } static func + (a: Self, b: Self) -> Self { let c = Self(size: a.size) R.eigen_add(a.ptr, b.ptr, c.ptr) return c } … } 4XJGU
9. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   4XJGU\$  extension Int: EigenCompatibleScalar { static var eigen_init = eigen_int_init static var eigen_add = eigen_int_add static var eigen_minus = eigen_int_minus static var eigen_mul = eigen_int_mul … } 4XJGU void *eigen_int_init(int rows, int cols); void eigen_int_add (void *a, void *b, void *c); void eigen_int_minus(void *a, void *b, void *c); void eigen_int_mul (void *a, void *b, void *c); \$ EigenMatrixPointer EigenMatrixOperation
10. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   4XJGU\$  extension Double: EigenCompatibleScalar { static var eigen_init = eigen_dbl_init static var eigen_add = eigen_dbl_add static var eigen_minus = eigen_dbl_minus static var eigen_mul = eigen_dbl_mul … } 4XJGU void *eigen_dbl_init(int rows, int cols); void eigen_dbl_add (void *a, void *b, void *c); void eigen_dbl_minus(void *a, void *b, void *c); void eigen_dbl_mul (void *a, void *b, void *c); \$
11. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   \$\$  #import <Eigen/Eigen> using IntMatrix = Eigen::Matrix<int64_t>; void *eigen_int_init(int_t rows, int_t cols) { IntMatrix *A = new IntMatrix(rows, cols); A->setZero(); return static_cast<void *>(A); }   void eigen_int_add(void *a, void *b, void *c) { IntMatrix *A = static_cast<IntMatrix *>(a); IntMatrix *B = static_cast<IntMatrix *>(b); IntMatrix *C = static_cast<IntMatrix *>(c); *C = *A + *B; } \$
12. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4   \$\$  #import <Eigen/Eigen> using DoubleMatrix = Eigen::Matrix<double>; void *eigen_dbl_init(int_t rows, int_t cols) { DoubleMatrix *A = new DoubleMatrix(rows, cols); A->setZero(); return static_cast<void *>(A); }   void eigen_dbl_add(void *a, void *b, void *c) { DoubleMatrix *A = static_cast<DoubleMatrix *>(a); DoubleMatrix *B = static_cast<DoubleMatrix *>(b); DoubleMatrix *C = static_cast<DoubleMatrix *>(c); *C = *A + *B; } \$
13. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  *UXPSLT  let a: EigenMatrix2x2<Int> = [1, 2, 3, 4] let b: EigenMatrix2x2<Int> = [5, 6, 7, 8] print(a + b) 4XJGU [6, 8; 10, 12] DPOTPMF IUUQTHJUIVCDPNUBLFUPTXNFJHFO
14. ### Using Eigen from Swift (via C) さ の た け

と @ t a k e t o 1 0 2 4  5IBOLZPV 