Skip to main content

matfun

DOI

Matrix functions exp, log, sqrt and pow of 3×3 matrices with first and second derivatives, for Newton solvers in finite element codes with many integration points:

  • smf: symmetric matrices, derivatives with respect to the matrix or its Voigt components;
  • gmf: general (non-symmetric) real matrices, the full derivatives ∂Fab/∂Acd, principal branch, including complex eigenvalues (rotations) and defective matrices (simple shear).

The derivatives are exact also for repeated eigenvalues. Implementations: C/C++ (SIMD, OpenMP), CUDA, Python (NumPy and JAX), Java and Julia, all tested against the same reference cases.

The methods (closed-form eigendecomposition, Daleckii–Krein formulas with stable divided differences; for general matrices the Newton–Hermite form over the eigenvalues with confluent divided differences) are described in theory.md.

Installation

Python

pip install matfun                  # NumPy interface, C kernel
pip install "matfun[jax]"           # + JAX (CPU)
pip install "matfun[cuda13]"        # + JAX with CUDA 13 and the CUDA kernel (or "matfun[cuda12]")

C / C++ / CUDA with CMake (FetchContent):

include(FetchContent)
FetchContent_Declare(matfun GIT_REPOSITORY https://github.com/BodeTobias/matfun.git GIT_TAG main)
FetchContent_MakeAvailable(matfun)                        # CUDA library too: set(MATFUN_BUILD_CUDA ON) before
target_link_libraries(myapp PRIVATE matfun::matfun)       # CUDA: matfun::matfun_cuda

or installed system-wide:

git clone https://github.com/BodeTobias/matfun.git && cd matfun
cmake -B build && cmake --build build -j && cmake --install build --prefix ~/.local
gcc myapp.c -I ~/.local/include -L ~/.local/lib -lmatfun -Wl,-rpath,$HOME/.local/lib

Java (Maven):

<dependency>
  <groupId>io.github.bodetobias</groupId>
  <artifactId>matfun</artifactId>
  <version>0.1.1</version>
</dependency>

Julia:

using Pkg; Pkg.add("MatFun")

Usage

smf takes one or many symmetric 3×3 matrices, either as matrices or as Voigt vectors v = (A11, A22, A33, A12, A13, A23), and returns the results in the same form; gmf takes general 3×3 matrices:

input F J H
matrix A (3×3) 3×3 3×3×3×3, ∂Fab/∂Acd 3×3×3×3×3×3, ∂²Fab/∂Acd∂Aef
Voigt vector v (6) 6 6×6, ∂Fi/∂vk 6×6×6, ∂²Fi/∂vp∂vq
import matfun
F, J, H = matfun.smf("log", A, order=2)      # A: (..., 3, 3) or (..., 6); "exp", "log", "sqrt", "pow" (alpha=...)
F, J, H = matfun.gmf("log", A, order=2)      # A: (..., 3, 3), general real matrices

import jax; jax.config.update("jax_enable_x64", True)
import matfun.jax as mf
F, J = mf.smf("pow", A, alpha=0.3, order=1)   # C kernel on the CPU, CUDA kernel on NVIDIA GPUs
K = jax.jacfwd(lambda a: mf.smf("log", a))(A) # jit, vmap, jacfwd, jacrev, grad, hessian (up to 2nd order)
F = mf.gmf("exp", X)                          # general matrices, the same features
#include "matfun/matfun.h"
smf_eval_matrix(SMF_LOG, 0.0, n, A, F, J, H);  /* n matrices; smf_eval: Voigt vectors; J, H may be NULL */
gmf_eval(SMF_EXP, 0.0, n, A, F, J, H);         /* general matrices; CUDA: smf_cuda_eval*, gmf_cuda_eval */
import io.github.bodetobias.matfun.SymMatFun;
import io.github.bodetobias.matfun.GenMatFun;
SymMatFun.evalMatrix(SymMatFun.Kind.LOG, 0.0, A, F, J, H);   // eval: Voigt vectors; J, H may be null
GenMatFun.eval(SymMatFun.Kind.EXP, 0.0, A, F, J, H);         // general matrices
using MatFun
F, J, H = smf(:log, A; order = 2)           # A: 3×3, 3×3×n, Voigt vector or 6×n; in place: smf!(F, J, H, :log, A)
F, J, H = gmf(:exp, A; order = 2)           # general matrices, A: 3×3 or 3×3×n; in place: gmf!

Conventions:

  • The indices mean the same in all languages: J[a,b,c,d] = ∂Fab/∂Acd, as in the table. Only the storage follows each language's array convention, and in all of them every matrix and its results are contiguous: C, Java and Python are row-major and 0-based with the matrix index first (one matrix after the other, e.g. J[n,a,b,c,d] in NumPy); Julia is column-major and 1-based with the matrix index last, e.g. J[a,b,c,d,n].
  • smf: the matrix derivatives are those of A ↦ f((A + Aᵀ)/2) and have the symmetries Jabcd = Jbacd = Jabdc = Jcdab, likewise for H. The derivative with respect to the Voigt component v₄ = A12 changes A12 and A21 together.
  • smf: matrices must be symmetric up to round-off, |Aij − Aji| ≤ 10⁻⁸·max|A|. Otherwise C returns −3, and NumPy, Java and Julia raise an error; in JAX and in the CUDA API the results of that matrix are NaN.
  • smf: log needs a positive definite A. sqrt and pow need a positive semidefinite A; eigenvalues below zero from round-off are set to 0.
  • gmf: the full derivatives (no symmetrization), H symmetric in (cd) ↔ (ef). exp is defined for every matrix. A matrix has many logarithms, square roots and powers; gmf returns the principal one (eigenvalues of log A with imaginary part in (−π, π), of √A and Aα with argument α·arg λ, arg λ ∈ (−π, π)), which is real for real A. It exists for A without real eigenvalues ≤ 0 (complex eigenvalues with negative real part are fine); otherwise the results of that matrix are NaN.

Building from source and tests

cmake -B build -DMATFUN_BUILD_TESTS=ON [-DMATFUN_BUILD_CUDA=ON] && cmake --build build -j && ctest --test-dir build
pip install ".[test]" && pytest python/tests
cd java && mvn test
julia --project=julia/MatFun -e 'using Pkg; Pkg.test()'

Development

Parts of the code, tests and documentation were developed with AI assistance (Claude, Anthropic). All content was reviewed and tested by the author, who maintains the project.

Citation and license

Please cite matfun as doi:10.5281/zenodo.23981300 (all versions); details in CITATION.cff (GitHub: "Cite this repository"). License: MIT (LICENSE); third_party/xla_ffi is Apache 2.0.

Metadata

Release files for matfun 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matfun 0.1.3
File Size Uploaded Provenance Actions
matfun-0.1.3.tar.gz 1.3 MB With provenance Details for matfun-0.1.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for matfun 0.1.3
File Actions
matfun-0.1.3-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for matfun-0.1.3-py3-none-win_amd64.whl
matfun-0.1.3-py3-none-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for matfun-0.1.3-py3-none-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
matfun-0.1.3-py3-none-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64, Linux glibc 2.24+ ARM64 Details for matfun-0.1.3-py3-none-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
matfun-0.1.3-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for matfun-0.1.3-py3-none-macosx_11_0_arm64.whl
matfun-0.1.3-py3-none-macosx_10_13_x86_64.whl Python 3 none macOS 10.13+ x86-64 Details for matfun-0.1.3-py3-none-macosx_10_13_x86_64.whl

Total release size: 3.7 MB

Release files / matfun-0.1.3.tar.gz

Download URL matfun-0.1.3.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
3eda7bf1983acfae8242116750fb8a4c6d2a9716126765252cace175c8d28e12
BLAKE2b-256 checksum
How to use checksums
2c93bd6cc5e365891a775b45a976e0f1a4029149bc145bb0e650785e7cc3b003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / matfun-0.1.3-py3-none-win_amd64.whl

Download URL matfun-0.1.3-py3-none-win_amd64.whl
Size 337.0 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
ab2f3abf68c307aa9ad68409925d1182f481f202544bca06dd02beb755866d2a
BLAKE2b-256 checksum
How to use checksums
179a7b896aaed03ad0a6d716e835e373aa59036042ce17b51c7dd07d3fe79011
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / matfun-0.1.3-py3-none-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL matfun-0.1.3-py3-none-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.0 MB
Tags Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
accab60cf12eb10470af48bb509015d1a1ad0385d89e8dfd994fae3b40214002
BLAKE2b-256 checksum
How to use checksums
a1242f8fd398c6aaa3b326b6684dccf0d8877b98ecfd8d8aa292cffc6b50f5ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / matfun-0.1.3-py3-none-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl

Download URL matfun-0.1.3-py3-none-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Size 477.7 kB
Tags Linux glibc 2.24+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
cb8c391e037f30dd3e8f793f80a3f69e95dfdb547d935cdbbfec2727c162b3b5
BLAKE2b-256 checksum
How to use checksums
347bbfc312a8b640c432de090dfb480a28cb1e3545876cba4a479b4474c5acff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / matfun-0.1.3-py3-none-macosx_11_0_arm64.whl

Download URL matfun-0.1.3-py3-none-macosx_11_0_arm64.whl
Size 283.5 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5e63405327510d462c8e5a4de4d46d4e3fd301d1e4d2148cee43a7c506018146
BLAKE2b-256 checksum
How to use checksums
074bdd57fd92d41ffce60e85052f85f82a1b6cbfdc77ed59b5599653565dbd94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / matfun-0.1.3-py3-none-macosx_10_13_x86_64.whl

Download URL matfun-0.1.3-py3-none-macosx_10_13_x86_64.whl
Size 362.5 kB
Tags Python 3 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
2e15d3ea8b541356355cd52ddb7ca0b9e2dfee37ff280921ef52fb2844e6a49f
BLAKE2b-256 checksum
How to use checksums
acc4dd9d25703c68efaebba99b42a274528827139d5a2d6b5a44cf0205087a75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for matfun 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for matfun-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page