Eigen C++ Template Library with C++Builder and VCL
This post describes how to using the Eigen C++ Template Library with C++ Builder and VCL.
What is the Eigen C++ Template Library?
Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. A Gentle Introduction to Linear Algebra is here. Eigen doesn’t have any dependencies other than the C++ standard library. Eigen consists only of header files, so there is nothing to compile before you can use it. There is no binary library to link to, and no configured header file. Eigen is a pure template library defined in the headers.
What is Linear algebra? Linear algebra is the study of lines and planes, vector spaces and mappings that are required for linear transforms. It is a relatively young field of study, having initially been formalized in the 1800s in order to find unknowns in systems of linear equations.
A linear equation is just a series of terms and mathematical operations where some terms are unknown; for example: y = 4 * x + 1
Equations like this are linear in that they describe a line on a two-dimensional graph. The line comes from plugging in different values into the unknown x to find out what the equation or model does to the value of y.
Eigen’s source code: In order to use Eigen, you just need to download and extract Eigen’s source code from here. The header files in the Eigen subdirectory are the only files required to compile programs using Eigen. The header files are the same for all platforms. It is not necessary to use CMake or install anything!
How to use the Eigen Library with C++ Builder?
- Download and extract Eigen’s source code from here.
- Add your Eigen Source Folder to your C++ Builder Include and Library Paths. In my case, my Eigen Source Folder is: C:/Users/amannarino/Documents/Eigen3/Eigen
- In C++ Builder, use Tools | Options | Language | C++ | Paths and Directories
4. In my sample C++ Builder VCL application, in my MainUnit.cpp file, I added the Eigen header files I wanted to use to test, for example, the member functions for the MatrixXd and IOFormat types, to try the Eigen Getting Started example from here: https://eigen.tuxfamily.org/dox/GettingStarted.html
5. This is the first Eigen sample program we will try to build and run using C++ Builder 12:
1
2
3
4
5
6
7
8
9
10
11
12
|
#include
#include
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = –1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
|
Let’s create the above sample application in C++ Builder 12:
1. File | New | VCL Application – C++ Builder
2. File | Save Project As, create a new folder, Eigen1. Project File name = Eigen1.cbproj
3. Rename and Save Unit1.cpp to uMain.cpp
4. Since, there is no library to link to, the only thing that you need to keep in mind when compiling the application is that the compiler must be able to find the Eigen header files. (The directory in which you placed Eigen’s source code must be in the include path). In C++ Builder 12, use Project | Options, C++ Shared Options, Include path, and add the directory where you placed Eigen’s source code. In my case it’s folder C:UsersamannarinoDocumentsEigen3
5. And also include the Eigen files to the C++ Builder Project Library Path (Project | Options, C++ Shared Options, Library path)
6. Now that the Include path has the location of the Eigen header files, we can include the Eigen/Dense, in the uMain.cpp like this:
1
|
#include
|
and we can use the Eigen Matrix class (Dense matrix and array manipulation) described here: https://eigen.tuxfamily.org/dox/group__DenseMatrixManipulation__chapter.html
and use the Eigen IOFormat Class described here: https://eigen.tuxfamily.org/dox/structEigen_1_1IOFormat.html
by adding this code:
1
2
|
using Eigen::MatrixXd;
using Eigen::IOFormat;
|
7. To verify the C++ Builder application can find the needed Eigen file, BUILD the application.
If you get the error: [bcc32c Fatal Error] uMain.cpp(6): ‘Eigen/Dense’ file not found
then you have the incorrect Library path for the Eigen source code.
8. The C++ Builder 12 source code for this application adapted from Eigen documentation for our VCL application for our first Eigen sample program is this code in our C++ Builder uMain.cpp file:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include
#pragma hdrstop
//Include Path: C:/Users/amannarino/Documents/Eigen3
#include
using Eigen::MatrixXd;
using Eigen::IOFormat;
#include “MainUnit.h”
//—————————————————————————
#pragma package(smart_init)
#pragma resource “*.dfm”
TForm1 *Form1;
// create string stream from Eigen Matrix
static std::string ConvertToString(const Eigen::MatrixXd& mat){
std::stringstream ss;
IOFormat CleanFmt(Eigen::FullPrecision, 0, “, “, “n”, “[“, “]”);
ss << mat.format(CleanFmt);
return ss.str();
}
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// App adapted from Eigen documentation for VCL app
// display Eigen version number
EigenVersionLabel->Caption = “Using Eigen version: “
+ IntToStr(EIGEN_WORLD_VERSION)
+ “.”
+ IntToStr(EIGEN_MAJOR_VERSION)
+ “.”
+ IntToStr(EIGEN_MINOR_VERSION);
// define a 3×3 matrix of doubles
MatrixXd m(3,3);
// Fill matrix with random numbers between -1 and +1
m.setRandom(3,3);
// Change three matrix items using + – and *
m(0,2) = m(0,0) + m(0,1);
m(1,2) = m(1,0) – m(1,1);
m(2,2) = m(2,0) * m(2,1);
// output matrix using strings
MatrixOutputMemo->Lines->Clear();
MatrixOutputMemo->Lines->Text = ConvertToString(m).c_str();
}
|
9.Build and Run the application. Click on the Using Eigen/Dense button, and you will see the Matrix Output, like this:
Congratulations! You now know how to using the Eigen C++ Template Library with C++ Builder and VCL.
If you are interetsed in learning more on what you can do with the Eigen C++ Template Library with C++ Builder and VCL, it’s worth taking the time to read this long tutorial on The Matrix class (Dense matrix and array manipulation).
You can try using the Eigen C++ Template Library with C++ Builder and VCL by downloading and installing the free 30 day trial of the current C++ Builder 12 from here!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition