This project provides a direct, head-to-head comparison of how to implement the fundamental linear algebra equation
- C23
- C++23
- Rust
- Python
The goal is to showcase the differences in syntax, performance potential, and developer ergonomics when dealing with array and matrix operations in modern systems programming and scripting languages.
The core operation being compared is a matrix-matrix multiplication followed by a scalar addition:
| Matrix / Scalar | Description | Dimensions |
|---|---|---|
| A | Left Multiplier | |
| B | Right Multiplier | |
| R | Result | |
| c | Scalar Constant | - |
The expected result is:
The C version uses modern C23 with Variable Length Arrays (VLA) in function signatures for simple, non-heap-allocated matrices.
# Compile
gcc -std=c23 -Wall -Wextra -o c23 c23.c
# Run
./c23The C++ example uses C++23 features like multidimensional subscripting and explicit this parameter to achieve NumPy-like operator syntax A * B + c using std::array for fixed-size performance.
# Compile and run
g++ -std=c++23 -Wall -Wextra -o cpp23 cpp23.cpp
# Run
./cpp23For those interested in how the C++ code scales to professional, high-performance libraries, a placeholder is included for the Eigen library (a C++ header-only matrix template library).
# 1. Ensure Eigen is installed (e.g., via Homebrew, apt, or vcpkg)
# 2. Compile with the Eigen include path
g++ -std=c++23 -Wall -Wextra -I/path/to/eigen3 -o cpp23_eigen cpp23_eigen.cpp
# 3. Run
./cpp23_eigenThe Python implementation showcases the high-level syntax made possible by the NumPy library, which handles the complex matrix arithmetic and broadcasting.
# 1. Setup environment using uv
uv venv && source .venv/bin/activate
# 2. Install dependency
uv pip install numpy
# 3. Run script
python python.pyRust provides two approaches in this comparison:
-
Rust
Simple, high-performance solution using only Rust arrays and manual nested loops.
-
Rust with nalgebra
Uses the industry-standard
nalgebralibrary for clean, type-safe operator overloading.
# Run the manual implementation
cd rust && cargo run
# Run the nalgebra implementation
cd rust_nalgebra && cargo runThis project was created by Marcos Borges, PhD.
Feel free to share your thoughts, ask questions, or open a pull request if you find a more idiomatic or performant way to implement these examples in any of the listed languages!