Environment

  • Operating System: Win 10
  • Android Studio: Version 4.2
  • SDK: API 26: Android 8.0(Oreo)
  • Eigen: 3.4.0

Introduction

  • Eigen is versatile.
    • It supports all matrix sizes, from small fixed-size matrices to arbitrarily large dense matrices, and even sparse matrices.
    • It supports all standard numeric types, including std::complex, integers, and is easily extensible to custom numeric types.
    • It supports various matrix decompositions and geometry features.
    • Its ecosystem of unsupported modules provides many specialized features such as non-linear optimization, matrix functions, a polynomial solver, FFT, and much more.
  • Eigen is fast.
    • Expression templates allow intelligently removing temporaries and enable lazy evaluation, when that is appropriate.
    • Explicit vectorization is performed for SSE 2/3/4, AVX, AVX2, FMA, AVX512, ARM NEON (32-bit and 64-bit), PowerPC AltiVec/VSX (32-bit and 64-bit), ZVector (s390x/zEC13) SIMD instruction sets, and since 3.4 MIPS MSA with graceful fallback to non-vectorized code.
    • Fixed-size matrices are fully optimized: dynamic memory allocation is avoided, and the loops are unrolled when that makes sense.
    • For large matrices, special attention is paid to cache-friendliness.
  • Eigen is reliable.
    • Algorithms are carefully selected for reliability. Reliability trade-offs are clearly documented and extremely safe decompositions are available.
    • Eigen is thoroughly tested through its own test suite (over 500 executables), the standard BLAS test suite, and parts of the LAPACK test suite.
  • Eigen is elegant.
    • The API is extremely clean and expressive while feeling natural to C++ programmers, thanks to expression templates.
    • Implementing an algorithm on top of Eigen feels like just copying pseudocode.
  • Eigen has good compiler support as we run our test suite against many compilers to guarantee reliability and work around any compiler bugs. Eigen up to version 3.4 is standard C03 and maintains reasonable compilation times. Versions following 3.4 will be C14.

Step1

下载Eigen

  1. 首先到Eigen官网下载Eigen3的压缩包

1

  1. 解压到本地

Step2

创建安卓项目

  1. 打开Android Studio创建项目

2

  1. 选择创建Native C++项目

3

  1. 填写相应字段后点击finish创建项目

4

Step3

引入Eigen3

  1. 打开第一步解压到本地的文件夹

我这里下载的是3.4.0版本,所以文件夹名称为eigen-3.4.0

5

  1. 找到其中的Eigen文件夹

6

  1. Eigen文件夹中的所有内容(包括文件夹)复制到项目中src/main/cpp路径下

7

  1. 修改src/main/cpp路径下的CMakeLists.txt文件 - 添加如下内容
1
2
3
4
5
####### Eigen3 #######

INCLUDE_DIRECTORIES(src/main/cpp/Eigen)

####### ###### #######
  1. 同步编译 - FileSync Project with Gradle Files

8

至此就引入完成了,可以尝试写一个调用Eigen库中函数的方法进行测试,如Step4所示

Step4

  1. 修改src/main/cpp路径下的native-lib文件

修改stringFromJNI函数或新声明函数都可以

不需要真的实现某个功能,只需要调用库中函数即可,编译成功说明引入成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <jni.h>
#include <string>
#include "Eigen/Dense"

using namespace std;
using namespace Eigen;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_androidstudioeigen3_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_androidstudioeigen3_MainActivity_eigen3FromJNI(
JNIEnv* env,
jobject /* this */) {
string temp = "Eigen3";
MatrixXd m = MatrixXd::Random(3,3);
m = (m+MatrixXd::Constant(3,3,1.2)) * 50;
return env->NewStringUTF(temp.c_str());
}
  1. 修改MainActivity.java的代码进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.androidstudioeigen3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.androidstudioeigen3.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}

private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

// Example of a call to a native method
TextView tv = binding.sampleText;
tv.setText(stringFromJNI());

tv.setText(eigen3FromJNI());

}

/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native String eigen3FromJNI();
}
  1. 运行结果如下

修改前屏幕应显示Hello from C++

修改后则显示Eigen3

9

导入成功!