Skip to content

Direct comparison of a fundamental linear algebra equation R = A B + c across C23, C++23, Rust, and Python.

License

Notifications You must be signed in to change notification settings

MarcosBorgesPhD/linear_algebra_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linear Algebra Example

This project provides a direct, head-to-head comparison of how to implement the fundamental linear algebra equation $\mathbf{R} = \mathbf{A} \times \mathbf{B} + c$ across four major languages:

  • 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 Problem Statement

The core operation being compared is a matrix-matrix multiplication followed by a scalar addition:

$\mathbf{R} = \mathbf{A} \times \mathbf{B} + c$

Input Data

Matrix / Scalar Description Dimensions
A Left Multiplier $2 \times 2$
B Right Multiplier $2 \times 4$
R Result $2 \times 4$
c Scalar Constant -

$$ \mathbf{A} = \begin{pmatrix} 1.0 & 2.0 \ 3.0 & 4.0 \end{pmatrix}, \quad \mathbf{B} = \begin{pmatrix} 5.0 & 6.0 & 7.0 & 8.0 \ 9.0 & 10.0 & 11.0 & 12.0 \end{pmatrix}, \quad c = 5.0 $$

The expected result is:

$$ \mathbf{R} = \begin{pmatrix} 28.00 & 31.00 & 34.00 & 37.00 \ 56.00 & 63.00 & 70.00 & 77.00 \end{pmatrix} $$

Compilation and Execution

C23

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
./c23

C++23

The 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
./cpp23

C++23 with Eigen

For 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_eigen

Python

The 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.py

Rust

Rust provides two approaches in this comparison:

  1. Rust

    Simple, high-performance solution using only Rust arrays and manual nested loops.

  2. Rust with nalgebra

    Uses the industry-standard nalgebra library for clean, type-safe operator overloading.

# Run the manual implementation
cd rust && cargo run

# Run the nalgebra implementation
cd rust_nalgebra && cargo run

Author

This 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!

Connect with me on LinkedIn or YouTube.

About

Direct comparison of a fundamental linear algebra equation R = A B + c across C23, C++23, Rust, and Python.

Topics

Resources

License

Stars

Watchers

Forks