The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings Garamon
Summary Activity SCM

SCM Repository

authorbreuils <stephane.breuils@u-pem.frcd>
Tue, 30 Apr 2019 21:54:10 +0000 (23:54 +0200)
committerbreuils <stephane.breuils@u-pem.frcd>
Tue, 30 Apr 2019 21:54:10 +0000 (23:54 +0200)
data/CMakeLists.txt
data/PythonBindings.cpp [new file with mode: 0644]
data/sample/src/main.cpp
data/setup.py [new file with mode: 0644]
src/ProductToString.cpp
src/ProductToString.hpp
src/main.cpp

index 431553b..8a49e99 100644 (file)
@@ -13,6 +13,12 @@ else()
     include_directories("/usr/include/eigen3") # manually specify the include location\r
 endif()\r
 \r
+option(BUILD_PYTHON "Build Python library" OFF)\r
+if (BUILD_PYTHON)\r
+    find_package(pybind11 REQUIRED)\r
+endif()\r
+\r
+\r
 # call the CMakeLists.txt to make the documentation (Doxygen)\r
 # > 'make html' to generate the documentation\r
 add_subdirectory(doc)\r
@@ -48,6 +54,15 @@ else()
        add_library(cmake_project_name_original_case SHARED ${source_files} ${header_files})\r
 endif()\r
 \r
+if (BUILD_PYTHON)\r
+    pybind11_add_module(cmake_project_name_original_case_py \r
+        src/cmake_project_name_original_case/PythonBindings.cpp\r
+    )\r
+    target_link_libraries(cmake_project_name_original_case_py PRIVATE \r
+        cmake_project_name_original_case)\r
+endif()\r
+\r
+\r
 # include directory path\r
 include_directories(src)\r
 include_directories(${EIGEN3_INCLUDE_DIR})\r
diff --git a/data/PythonBindings.cpp b/data/PythonBindings.cpp
new file mode 100644 (file)
index 0000000..a4afaa0
--- /dev/null
@@ -0,0 +1,125 @@
+// Copyright (c) 2018 by University Paris-Est Marne-la-Vallee
+// Copyright (c) 2018 by Norwegian University of Science and Technology
+// PythonBindings.cpp
+// This file is part of the Garamon for project_namespace.
+// Authors: Stephane Breuils, Vincent Nozick, and Lars Tingelstad
+// Contact: vincent.nozick@u-pem.fr
+//
+// Licence MIT
+// A a copy of the MIT License is given along with this program
+
+/// \file PythonBindings.cpp
+/// \author Lars Tingelstad
+/// \brief Python bindings using pybind11.
+
+#include "project_namespace/Mvec.hpp"
+
+#include <pybind11/operators.h>
+#include <pybind11/pybind11.h>
+#include <pybind11/iostream.h>
+
+namespace py = pybind11;
+
+
+/*!
+ * @namespace project_namespace
+ */
+namespace project_namespace {
+
+PYBIND11_MODULE(project_namespace_py, m) {
+
+  project_basis_vector_index
+  
+  m.def("metric", [](){return metric;});
+
+  m.attr("scalar") = 0;
+  project_static_multivector_one_component_python
+
+  // Class definition
+  auto mvec = py::class_<Mvec<double>>(m, "Mvec");
+  // Constructors
+  mvec.def(py::init<>());
+  // Get/Set
+  mvec.def("__setitem__",
+           [](Mvec<double>& mv, int idx, double value) { mv[idx] = value; });
+  mvec.def("__getitem__",
+           [](Mvec<double>& mv, int idx) { return mv[idx]; });
+  // Operators
+  mvec.def(py::self + py::self)
+      .def(py::self + float())
+      .def(float() + py::self)
+      .def(py::self += py::self)
+      .def(py::self - py::self)
+      .def(py::self - float())
+      .def(float() - py::self)
+      .def(py::self -= py::self)
+      .def(py::self * py::self)
+      .def(py::self * float())
+      .def(float() * py::self)
+      .def(py::self *= py::self)
+      .def(py::self / py::self)
+      .def(py::self / float())
+      .def(float() / py::self)
+      .def(py::self /= py::self)
+      .def(py::self < py::self)
+      .def(py::self < float())
+      .def(float() < py::self)
+      .def(py::self > py::self)
+      .def(py::self > float())
+      .def(float() > py::self)
+      .def("__invert__",
+           [](const Mvec<double>& a) { return ~a; })
+      .def("__eq__",
+           [](Mvec<double>& a, const Mvec<double>& b) { return a == b; })
+      .def("__neq__",
+           [](Mvec<double>& a, const Mvec<double>& b) { return a != b; })
+      .def("__or__",
+           [](const Mvec<double>& a, const Mvec<double>& b) { return a ^ b; })
+      .def("__or__",
+           [](const Mvec<double>& a, double b) { return a ^ b; })
+      .def("__ror__",
+           [](const Mvec<double>& a, double b) { return a ^ b; })
+      .def("__ior__",
+           [](Mvec<double>& a, const Mvec<double>& b) { a ^= b; return a; })
+      .def("__xor__",
+           [](const Mvec<double>& a, const Mvec<double>& b) { return a ^ b; })
+      .def("__xor__",
+           [](const Mvec<double>& a, double b) { return a ^ b; })
+      .def("__rxor__",
+           [](const Mvec<double>& a, double b) { return a ^ b; })
+      .def("__ixor__",
+           [](Mvec<double>& a, const Mvec<double>& b) { a ^= b; return a; });
+
+  // Print
+  mvec.def("__repr__",
+           [](const Mvec<double>& mv) {
+             std::stringstream ss;
+             ss << mv;
+             return ss.str();
+           })
+      .def("norm", &Mvec<double>::norm)
+      .def("quadratic_norm", &Mvec<double>::quadraticNorm)
+      .def("reverse", &Mvec<double>::reverse)
+      .def("display", &Mvec<double>::display,
+        py::call_guard<py::scoped_ostream_redirect,
+                       py::scoped_estream_redirect>());
+
+project_singular_metric_comment_begin
+  mvec.def("outer_primal_dual", &Mvec<double>::outerPrimalDual);
+  mvec.def("outer_dual_primal", &Mvec<double>::outerDualPrimal);
+  mvec.def("outer_dual_dual", &Mvec<double>::outerDualDual);
+  mvec.def("dual", &Mvec<double>::dual);
+project_singular_metric_comment_end
+
+  mvec.def("scalar_product", &Mvec<double>::scalarProduct);
+  mvec.def("hestenes_product", &Mvec<double>::hestenesProduct);
+  mvec.def("inv", &Mvec<double>::inv);
+
+  mvec.def("grades", &Mvec<double>::grades);
+  mvec.def("grade", [](const Mvec<double>& a){return a.grade();});
+  mvec.def("grade", [](const Mvec<double>& a, const int i){return a.grade(i);});
+  mvec.def("clear", &Mvec<double>::clear);
+
+}
+
+}  // namespace project_namespace
index f13e501..fffb687 100644 (file)
@@ -25,8 +25,8 @@ int main(){
     std::cout << "outer product     : " << (mv1 ^ mv2) << std::endl;\r
     std::cout << "inner product     : " << (mv1 | mv2) << std::endl;\r
     std::cout << "geometric product : " << (mv1 * mv2) << std::endl;\r
-    std::cout << "left contraction : " << (mv1 < mv2) << std::endl;\r
-    std::cout << "right contraction: " << (mv1 > mv2) << std::endl;\r
+    std::cout << "left contraction  : " << (mv1 < mv2) << std::endl;\r
+    std::cout << "right contraction : " << (mv1 > mv2) << std::endl;\r
     std::cout << std::endl;\r
 \r
     // some tools\r
diff --git a/data/setup.py b/data/setup.py
new file mode 100644 (file)
index 0000000..2a8e470
--- /dev/null
@@ -0,0 +1,86 @@
+import os
+import re
+import sys
+import platform
+import subprocess
+
+from setuptools import setup, find_packages, Extension
+from setuptools.command.build_ext import build_ext
+from distutils.version import LooseVersion
+
+
+class CMakeExtension(Extension):
+    def __init__(self, name, sourcedir=''):
+        Extension.__init__(self, name, sources=[])
+        self.sourcedir = os.path.abspath(sourcedir)
+
+
+class CMakeBuild(build_ext):
+    def run(self):
+        try:
+            out = subprocess.check_output(['cmake', '--version'])
+        except OSError:
+            raise RuntimeError(
+                "CMake must be installed to build the following extensions: " +
+                ", ".join(e.name for e in self.extensions))
+
+        if platform.system() == "Windows":
+            cmake_version = LooseVersion(
+                re.search(r'version\s*([\d.]+)', out.decode()).group(1))
+            if cmake_version < '3.1.0':
+                raise RuntimeError("CMake >= 3.1.0 is required on Windows")
+
+        for ext in self.extensions:
+            self.build_extension(ext)
+
+    def build_extension(self, ext):
+        extdir = os.path.abspath(
+            os.path.dirname(self.get_ext_fullpath(ext.name)))
+        cmake_args = [
+            '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
+            '-DPYTHON_EXECUTABLE=' + sys.executable,
+            '-DBUILD_PYTHON=ON'
+        ]
+
+        cfg = 'Debug' if self.debug else 'Release'
+        build_args = ['--config', cfg]
+
+        if platform.system() == "Windows":
+            cmake_args += [
+                '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(),
+                                                                extdir)
+            ]
+            # if sys.maxsize > 2**32:
+            #     cmake_args += ['-A', 'x64']
+            # build_args += ['--', '/m']
+        elif platform.system() == "Darwin":
+            cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
+            cmake_args += ['-DCMAKE_CXX_COMPILER=clang++']
+        else:
+            cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
+            cmake_args += ['-DCMAKE_CXX_COMPILER=g++']
+            build_args += ['--', '-j8']
+
+        env = os.environ.copy()
+        env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(
+            env.get('CXXFLAGS', ''), self.distribution.get_version())
+        if not os.path.exists(self.build_temp):
+            os.makedirs(self.build_temp)
+        subprocess.check_call(
+            ['cmake', ext.sourcedir] + cmake_args,
+            cwd=self.build_temp,
+            env=env)
+        subprocess.check_call(
+            ['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
+
+
+setup(
+    name='project_namespace_py',
+    version='0.0.0',
+    author='garamon',
+    description='Python bindings for project_namespace_py',
+    long_description='',
+    packages=find_packages(exclude=['tests']),
+    ext_modules=[CMakeExtension('project_namespace_py')],
+    cmdclass=dict(build_ext=CMakeBuild),
+    zip_safe=False, )
index bc4715b..ca53871 100644 (file)
@@ -283,8 +283,11 @@ std::string loadAllDualCoefficientsArray(const std::vector<int> & sizeDualCoeffi
 
 
 
-
-
+// prototype of the methods namespace::e12() in Python
+std::string staticOneComponentMultivectorPrototypePython(){
+    std::string res = "m.def(\"eproject_name_blade\", &eproject_name_blade<double>);\n";
+    return res;
+}
 
 
 
index 857d1d1..16c5b09 100644 (file)
@@ -50,6 +50,8 @@ std::string loadAllDualCoefficientsArray(const std::vector<int> & sizeDualCoeffi
 
 std::string dualPermutationToString(const std::vector<double>& basisChangesComponents);
 
+std::string staticOneComponentMultivectorPrototypePython();
+
 std::string staticOneComponentMultivectorPrototype();
 
 std::string oneComponentMultivectorPrototype();
index 162a8c9..347d8b0 100644 (file)
@@ -336,6 +336,7 @@ int main(int argc, char** argv){
     substitute(data,"cmake_project_name_sample", metaData.namespaceName + "_sample");
     substitute(data,"cmake_project_name_upper_case", upperCaseNamespace);
     substitute(data,"cmake_project_name_original_case", metaData.namespaceName);
+    substitute(data,"cmake_project_name_original_case_py", metaData.namespaceName + "_py");
     writeFile(data, sampleDirectory + "/CMakeLists.txt");
 
 
@@ -354,6 +355,31 @@ int main(int argc, char** argv){
     substitute(data,"project_second_vector_basis", metaData.basisVectorName[1]);
     writeFile(data, srcSampleDirectory + "/main.cpp");
 
+    // PythonBindings.cpp
+    data = readFile(templateDataDirectory + "PythonBindings.cpp");
+    substitute(data,"project_namespace", metaData.namespaceName);
+    substitute(data,"project_namespace_py", metaData.namespaceName + "_py");
+    substitute(data,"project_static_multivector_one_component_python", multivectorComponentBuilder(metaData,staticOneComponentMultivectorPrototypePython())); // i.e. Mvec a = 2 * cga::e12()
+    if(metaData.fullRankMetric == true){
+        // keep the dual and other functions
+        substitute(data,"project_singular_metric_comment_begin", "");
+        substitute(data,"project_singular_metric_comment_end", "");
+    }else{
+        // comment the dual and other functions
+        substitute(data,"project_singular_metric_comment_begin", singularMetricCommentBegin());
+        substitute(data,"project_singular_metric_comment_end", singularMetricCommentEnd());       
+    }
+    substitute(data,"project_basis_vector_index", multivectorComponentBuilder(metaData,
+    "m.attr(\"Eproject_name_blade\") = project_xor_index_blade;\n"
+    ));
+    writeFile(data, srcDirectory + "/PythonBindings.cpp");
+
+    // setup.py
+    data = readFile(templateDataDirectory + "setup.py");
+    substitute(data,"project_namespace_py", metaData.namespaceName + "_py");
+    writeFile(data, projectDirectory + "/setup.py");
+
+
 
     return EXIT_SUCCESS;
 }