aboutsummaryrefslogtreecommitdiff
path: root/demos/mix_eigen_and_c
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2012-11-02 10:59:05 +0000
committerXiaotao Duan <xiaotao@google.com>2012-11-07 14:17:48 -0800
commitc981c48f5bc9aefeffc0bcb0cc3934c2fae179dd (patch)
tree54d1c7d66098154c1d7c5bd414394ef4cf255810 /demos/mix_eigen_and_c
parent63f67d748682b46d58be31235a0a2d64d81b998c (diff)
downloadeigen-c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd.tar.gz
Added a README.android and a MODULE_LICENSE_MPL2 file. Added empty Android.mk and CleanSpec.mk to optimize Android build. Non MPL2 license code is disabled in ./Eigen/src/Core/util/NonMPL2.h. Trying to include such files will lead to an error. Change-Id: I0e148b7c3e83999bcc4dfaa5809d33bfac2aac32
Diffstat (limited to 'demos/mix_eigen_and_c')
-rw-r--r--demos/mix_eigen_and_c/README9
-rw-r--r--demos/mix_eigen_and_c/binary_library.cpp185
-rw-r--r--demos/mix_eigen_and_c/binary_library.h71
-rw-r--r--demos/mix_eigen_and_c/example.c65
4 files changed, 330 insertions, 0 deletions
diff --git a/demos/mix_eigen_and_c/README b/demos/mix_eigen_and_c/README
new file mode 100644
index 000000000..21dba8679
--- /dev/null
+++ b/demos/mix_eigen_and_c/README
@@ -0,0 +1,9 @@
+This is an example of how one can wrap some of Eigen into a C library.
+
+To try this with GCC, do:
+
+ g++ -c binary_library.cpp -O2 -msse2 -I ../..
+ gcc example.c binary_library.o -o example -lstdc++
+ ./example
+
+TODO: add CMakeLists, add more explanations here \ No newline at end of file
diff --git a/demos/mix_eigen_and_c/binary_library.cpp b/demos/mix_eigen_and_c/binary_library.cpp
new file mode 100644
index 000000000..15a2d03e9
--- /dev/null
+++ b/demos/mix_eigen_and_c/binary_library.cpp
@@ -0,0 +1,185 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+// This C++ file compiles to binary code that can be linked to by your C program,
+// thanks to the extern "C" syntax used in the declarations in binary_library.h.
+
+#include "binary_library.h"
+
+#include <Eigen/Core>
+
+using namespace Eigen;
+
+/************************* pointer conversion methods **********************************************/
+
+////// class MatrixXd //////
+
+inline MatrixXd& c_to_eigen(C_MatrixXd* ptr)
+{
+ return *reinterpret_cast<MatrixXd*>(ptr);
+}
+
+inline const MatrixXd& c_to_eigen(const C_MatrixXd* ptr)
+{
+ return *reinterpret_cast<const MatrixXd*>(ptr);
+}
+
+inline C_MatrixXd* eigen_to_c(MatrixXd& ref)
+{
+ return reinterpret_cast<C_MatrixXd*>(&ref);
+}
+
+inline const C_MatrixXd* eigen_to_c(const MatrixXd& ref)
+{
+ return reinterpret_cast<const C_MatrixXd*>(&ref);
+}
+
+////// class Map<MatrixXd> //////
+
+inline Map<MatrixXd>& c_to_eigen(C_Map_MatrixXd* ptr)
+{
+ return *reinterpret_cast<Map<MatrixXd>*>(ptr);
+}
+
+inline const Map<MatrixXd>& c_to_eigen(const C_Map_MatrixXd* ptr)
+{
+ return *reinterpret_cast<const Map<MatrixXd>*>(ptr);
+}
+
+inline C_Map_MatrixXd* eigen_to_c(Map<MatrixXd>& ref)
+{
+ return reinterpret_cast<C_Map_MatrixXd*>(&ref);
+}
+
+inline const C_Map_MatrixXd* eigen_to_c(const Map<MatrixXd>& ref)
+{
+ return reinterpret_cast<const C_Map_MatrixXd*>(&ref);
+}
+
+
+/************************* implementation of classes **********************************************/
+
+
+////// class MatrixXd //////
+
+
+C_MatrixXd* MatrixXd_new(int rows, int cols)
+{
+ return eigen_to_c(*new MatrixXd(rows,cols));
+}
+
+void MatrixXd_delete(C_MatrixXd *m)
+{
+ delete &c_to_eigen(m);
+}
+
+double* MatrixXd_data(C_MatrixXd *m)
+{
+ return c_to_eigen(m).data();
+}
+
+void MatrixXd_set_zero(C_MatrixXd *m)
+{
+ c_to_eigen(m).setZero();
+}
+
+void MatrixXd_resize(C_MatrixXd *m, int rows, int cols)
+{
+ c_to_eigen(m).resize(rows,cols);
+}
+
+void MatrixXd_copy(C_MatrixXd *dst, const C_MatrixXd *src)
+{
+ c_to_eigen(dst) = c_to_eigen(src);
+}
+
+void MatrixXd_copy_map(C_MatrixXd *dst, const C_Map_MatrixXd *src)
+{
+ c_to_eigen(dst) = c_to_eigen(src);
+}
+
+void MatrixXd_set_coeff(C_MatrixXd *m, int i, int j, double coeff)
+{
+ c_to_eigen(m)(i,j) = coeff;
+}
+
+double MatrixXd_get_coeff(const C_MatrixXd *m, int i, int j)
+{
+ return c_to_eigen(m)(i,j);
+}
+
+void MatrixXd_print(const C_MatrixXd *m)
+{
+ std::cout << c_to_eigen(m) << std::endl;
+}
+
+void MatrixXd_multiply(const C_MatrixXd *m1, const C_MatrixXd *m2, C_MatrixXd *result)
+{
+ c_to_eigen(result) = c_to_eigen(m1) * c_to_eigen(m2);
+}
+
+void MatrixXd_add(const C_MatrixXd *m1, const C_MatrixXd *m2, C_MatrixXd *result)
+{
+ c_to_eigen(result) = c_to_eigen(m1) + c_to_eigen(m2);
+}
+
+
+
+////// class Map_MatrixXd //////
+
+
+C_Map_MatrixXd* Map_MatrixXd_new(double *array, int rows, int cols)
+{
+ return eigen_to_c(*new Map<MatrixXd>(array,rows,cols));
+}
+
+void Map_MatrixXd_delete(C_Map_MatrixXd *m)
+{
+ delete &c_to_eigen(m);
+}
+
+void Map_MatrixXd_set_zero(C_Map_MatrixXd *m)
+{
+ c_to_eigen(m).setZero();
+}
+
+void Map_MatrixXd_copy(C_Map_MatrixXd *dst, const C_Map_MatrixXd *src)
+{
+ c_to_eigen(dst) = c_to_eigen(src);
+}
+
+void Map_MatrixXd_copy_matrix(C_Map_MatrixXd *dst, const C_MatrixXd *src)
+{
+ c_to_eigen(dst) = c_to_eigen(src);
+}
+
+void Map_MatrixXd_set_coeff(C_Map_MatrixXd *m, int i, int j, double coeff)
+{
+ c_to_eigen(m)(i,j) = coeff;
+}
+
+double Map_MatrixXd_get_coeff(const C_Map_MatrixXd *m, int i, int j)
+{
+ return c_to_eigen(m)(i,j);
+}
+
+void Map_MatrixXd_print(const C_Map_MatrixXd *m)
+{
+ std::cout << c_to_eigen(m) << std::endl;
+}
+
+void Map_MatrixXd_multiply(const C_Map_MatrixXd *m1, const C_Map_MatrixXd *m2, C_Map_MatrixXd *result)
+{
+ c_to_eigen(result) = c_to_eigen(m1) * c_to_eigen(m2);
+}
+
+void Map_MatrixXd_add(const C_Map_MatrixXd *m1, const C_Map_MatrixXd *m2, C_Map_MatrixXd *result)
+{
+ c_to_eigen(result) = c_to_eigen(m1) + c_to_eigen(m2);
+}
diff --git a/demos/mix_eigen_and_c/binary_library.h b/demos/mix_eigen_and_c/binary_library.h
new file mode 100644
index 000000000..0b983ad3a
--- /dev/null
+++ b/demos/mix_eigen_and_c/binary_library.h
@@ -0,0 +1,71 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+// This is a pure C header, no C++ here.
+// The functions declared here will be implemented in C++ but
+// we don't have to know, because thanks to the extern "C" syntax,
+// they will be compiled to C object code.
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+ // just dummy empty structs to give different pointer types,
+ // instead of using void* which would be type unsafe
+ struct C_MatrixXd {};
+ struct C_Map_MatrixXd {};
+
+ // the C_MatrixXd class, wraps some of the functionality
+ // of Eigen::MatrixXd.
+ struct C_MatrixXd* MatrixXd_new(int rows, int cols);
+ void MatrixXd_delete (struct C_MatrixXd *m);
+ double* MatrixXd_data (struct C_MatrixXd *m);
+ void MatrixXd_set_zero (struct C_MatrixXd *m);
+ void MatrixXd_resize (struct C_MatrixXd *m, int rows, int cols);
+ void MatrixXd_copy (struct C_MatrixXd *dst,
+ const struct C_MatrixXd *src);
+ void MatrixXd_copy_map (struct C_MatrixXd *dst,
+ const struct C_Map_MatrixXd *src);
+ void MatrixXd_set_coeff (struct C_MatrixXd *m,
+ int i, int j, double coeff);
+ double MatrixXd_get_coeff (const struct C_MatrixXd *m,
+ int i, int j);
+ void MatrixXd_print (const struct C_MatrixXd *m);
+ void MatrixXd_add (const struct C_MatrixXd *m1,
+ const struct C_MatrixXd *m2,
+ struct C_MatrixXd *result);
+ void MatrixXd_multiply (const struct C_MatrixXd *m1,
+ const struct C_MatrixXd *m2,
+ struct C_MatrixXd *result);
+
+ // the C_Map_MatrixXd class, wraps some of the functionality
+ // of Eigen::Map<MatrixXd>
+ struct C_Map_MatrixXd* Map_MatrixXd_new(double *array, int rows, int cols);
+ void Map_MatrixXd_delete (struct C_Map_MatrixXd *m);
+ void Map_MatrixXd_set_zero (struct C_Map_MatrixXd *m);
+ void Map_MatrixXd_copy (struct C_Map_MatrixXd *dst,
+ const struct C_Map_MatrixXd *src);
+ void Map_MatrixXd_copy_matrix(struct C_Map_MatrixXd *dst,
+ const struct C_MatrixXd *src);
+ void Map_MatrixXd_set_coeff (struct C_Map_MatrixXd *m,
+ int i, int j, double coeff);
+ double Map_MatrixXd_get_coeff (const struct C_Map_MatrixXd *m,
+ int i, int j);
+ void Map_MatrixXd_print (const struct C_Map_MatrixXd *m);
+ void Map_MatrixXd_add (const struct C_Map_MatrixXd *m1,
+ const struct C_Map_MatrixXd *m2,
+ struct C_Map_MatrixXd *result);
+ void Map_MatrixXd_multiply (const struct C_Map_MatrixXd *m1,
+ const struct C_Map_MatrixXd *m2,
+ struct C_Map_MatrixXd *result);
+
+#ifdef __cplusplus
+} // end extern "C"
+#endif \ No newline at end of file
diff --git a/demos/mix_eigen_and_c/example.c b/demos/mix_eigen_and_c/example.c
new file mode 100644
index 000000000..508eb5467
--- /dev/null
+++ b/demos/mix_eigen_and_c/example.c
@@ -0,0 +1,65 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include "binary_library.h"
+#include "stdio.h"
+
+void demo_MatrixXd()
+{
+ struct C_MatrixXd *matrix1, *matrix2, *result;
+ printf("*** demo_MatrixXd ***\n");
+
+ matrix1 = MatrixXd_new(3, 3);
+ MatrixXd_set_zero(matrix1);
+ MatrixXd_set_coeff(matrix1, 0, 1, 2.5);
+ MatrixXd_set_coeff(matrix1, 1, 0, 1.4);
+ printf("Here is matrix1:\n");
+ MatrixXd_print(matrix1);
+
+ matrix2 = MatrixXd_new(3, 3);
+ MatrixXd_multiply(matrix1, matrix1, matrix2);
+ printf("Here is matrix1*matrix1:\n");
+ MatrixXd_print(matrix2);
+
+ MatrixXd_delete(matrix1);
+ MatrixXd_delete(matrix2);
+}
+
+// this helper function takes a plain C array and prints it in one line
+void print_array(double *array, int n)
+{
+ struct C_Map_MatrixXd *m = Map_MatrixXd_new(array, 1, n);
+ Map_MatrixXd_print(m);
+ Map_MatrixXd_delete(m);
+}
+
+void demo_Map_MatrixXd()
+{
+ struct C_Map_MatrixXd *map;
+ double array[5];
+ int i;
+ printf("*** demo_Map_MatrixXd ***\n");
+
+ for(i = 0; i < 5; ++i) array[i] = i;
+ printf("Initially, the array is:\n");
+ print_array(array, 5);
+
+ map = Map_MatrixXd_new(array, 5, 1);
+ Map_MatrixXd_add(map, map, map);
+ Map_MatrixXd_delete(map);
+
+ printf("Now the array is:\n");
+ print_array(array, 5);
+}
+
+int main()
+{
+ demo_MatrixXd();
+ demo_Map_MatrixXd();
+}