Python interface

This library provides an interface to pybind11 such that an interface to NumPy arrays is automatically provided when including a function with any of the cppmat classes:

cppmat class Rank of NumPy-array
cppmat::array n
cppmat::matrix 2
cppmat::vector 1
cppmat::tiny::array 2
cppmat::tiny::matrix 2
cppmat::tiny::vector 1
cppmat::cartesian::tensor4 4
cppmat::cartesian::tensor2 2
cppmat::cartesian::tensor2s 2
cppmat::cartesian::tensor2d 2
cppmat::cartesian::vector 1
cppmat::tiny::cartesian::tensor4 4
cppmat::tiny::cartesian::tensor2 2
cppmat::tiny::cartesian::tensor2s 2
cppmat::tiny::cartesian::tensor2d 2
cppmat::tiny::cartesian::vector 1


On the Python side all the matrices (cppmat::matrix, cppmat::symmetric::matrix, and cppmat::diagonal::matrix) and 2nd-order tensors (cppmat::cartesian::tensor2, cppmat::cartesian::tensor2s, and cppmat::cartesian::tensor2d) are all square matrices (rank 2 NumPy arrays). This means that when a function that has cppmat::symmetric::matrix or cppmat::cartesian::tensor2s as argument, the upper-diagonal part is read; while when it has an argument cppmat::diagonal::matrix or cppmat::cartesian::tensor2d only the diagonal is considered.

This requires extra attention as information might be lost. To optimize for speed and flexibility no checks are performed in the release libraries derived from cppmat!

You can ask cppmat to check for this, by omitting the -DNDEBUG compiler flag (this enables several assertions, so it may cost you some efficiency).

(The same holds for the classes under cppmat::tiny::.)

To use this feature one has to:

#include <cppmat/pybind11.h>



Building is demonstrated based on the ‘tensorlib’ example.



cmake_minimum_required(VERSION 2.8.12)

# set optimization level
# - aggressive optimization
#   compiler: ... -O3
# - switch off assertions (more risky but faster)
#   compiler: ... -DNDEBUG

# set C++ standard
# - compiler: ... -std=c++14

# find cppmat
# - compiler: ... -I${CPPMAT_INCLUDE_DIRS}
pkg_check_modules(CPPMAT REQUIRED cppmat)

# find pybind11
find_package(pybind11 REQUIRED)
pybind11_add_module(tensorlib tensorlib.cpp)

Build using

cd /path/to/tempdir
cmake /path/to/tensorlib

For this to work, pybind11 must be ‘installed’ on the system.


Alternatively you can include pybind11 as a sub-folder (for example using git submodule add In that case, replace find_package(pybind11 REQUIRED) by add_subdirectory(pybind11) in CMakeLists.txt.


To link to external libraries, include at the end of your CMakeLists.txt

target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_LIBS})


from setuptools import setup, Extension

import sys
import setuptools
import pybind11
import cppmat

__version__ = '0.0.1'

ext_modules = [
      pybind11.get_include(True ),
      cppmat  .get_include(False),
      cppmat  .get_include(True )

  name               = 'tensorlib',
  description        = 'Tensorlib',
  long_description   = 'This is an example module, it no real use!',
  keywords           = 'Example, C++, C++11, Python bindings, pybind11',
  version            = __version__,
  license            = 'MIT',
  author             = 'Tom de Geus',
  author_email       = '',
  url                = '',
  ext_modules        = ext_modules,
  extra_compile_args = ["-DNDEBUG"], # switch off assertions
  install_requires   = ['pybind11>=2.1.0','cppmat>=0.2.1'],
  cmdclass           = {'build_ext': cppmat.BuildExt},
  zip_safe           = False,

As shown in this example building using the can be simplified using some routines in the cppmat python module. These routines have been taken from pybind, most notably from Sylvain Corlay and Dean Moldovan. They are merely attached to this module to simplify your life.

Build using

python3 build
python3 install


Replace the executable with your favorite Python version, e.g. with python.

CMake &

CMake can be called from the to take advantage of both. In that case the would be simply

import setuptools, cppmat

  name             = 'tensorlib',
  version          = '0.0.1',
  author           = 'Tom de Geus',
  author_email     = '',
  description      = 'Description',
  long_description = '',
  ext_modules      = [cppmat.CMakeExtension('tensorlib')],
  cmdclass         = dict(build_ext=cppmat.CMakeBuild),
  zip_safe         = False,