# Eigen in Unity

Unity3d has very basic linear algebra support, for example, in Matrix4x4, there are simple multiplication functions with vectors and matrices, however, functions like inverse and eigen value are not included. Eigen is the perfect candidate of performing complicated linear algebra algorithms, for example, Singluar Value Decomposition, Sparse Matrix, Linear Eqaution Solver.

### Unity Native Programming

The key idea of integrating Eigen with Unity3d is using the native programming interface of .NET. Here is another example: Native Programming in Unity. Basically, the native programming interface of unity provides a way for .NET environment to call C functions defined in built dynamic libraries.

What we will do is:

• Build a dynamic library that contains the functions that use Eigen (C++)
• Initiates the call to the functions in Unity scripts (C#)

### A Minimum Example

Here is a minimum example showing how to get the trace of a matrix in Eigen.

#### Dynamic Library using Eigen

• Create a Visual Studio dynamic library project.
• Add the root path of Eigen package to `Additional Include Directories`
• Add a CPP file to the project
• Copy the following to the CPP file:
``````#define EXPORT_API __declspec(dllexport)
#include <Eigen/Dense>
using namespace Eigen;
extern "C" {
EXPORT_API float getTrace() {
MatrixXf m = MatrixXf::Random(4,4);
return m.trace();
}
}
``````
• Configure the building so that it is compatible with targeted platform.
• Build
• Copy to Unity3D project

#### Call the Function in Unity Script

• Create a script `EigenTest.cs`.
• Copy the following to the CSharp file:
``````using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class EigenTest : MonoBehaviour {
[DllImport("YOURDLLNAME")]
public static extern float getTrace();
void Start () {
Debug.Log("The trace value is: " + getTrace());
}
}
``````
• Attach the script to any GameObject.
• Deploy to the targeted platform.