C++ scientific computing library with Python bindings (pybind11)
Project description
NumCore
NumCore is a lightweight scientific computing library written in modern C++ with Python bindings powered by pybind11.
It implements core numerical methods from scratch, including custom vector/matrix classes, linear algebra routines, ODE solvers, and optimization algorithms. The package is designed as both a usable numerical library and a learning-focused implementation of fundamental scientific computing algorithms.
Install package:
numcore-scicomp
Python import name:numcore
Features
Matrix and Vector Core
- Custom
VectorandMatrixclasses - Operator overloading for natural arithmetic syntax
- Shape and bounds validation
- NumPy interoperability through
.numpy() - Python bindings for direct use from Python
Linear Algebra
- Matrix-vector multiplication
- Linear system solving
- Determinant computation
- Matrix inverse
- Least-squares solver
- LU decomposition with partial pivoting
ODE Solvers
- Forward Euler method
- Classical fourth-order Runge-Kutta method, RK4
- Adaptive Dormand-Prince RK45 solver
Optimization
- Gradient Descent
- Adam optimizer
- Newton's method with numerical Hessian
- BFGS with Armijo line search
Installation
Install from PyPI
pip install numcore-scicomp
Then use it in Python as:
import numcore as nc
Install from source
git clone https://github.com/ChaitanyaK07/NumCore---Scientific-Computing-Library.git
cd NumCore---Scientific-Computing-Library
python -m pip install .
For development:
python -m pip install -e .
Quick Start
import numcore as nc
v = nc.Vector([3.0, 4.0])
print(v.norm()) # 5.0
Python Examples
Vector operations
import numcore as nc
x = nc.Vector([1.0, 2.0, 3.0])
print(x.norm()) # 3.741657...
print(x.dot(x)) # 14.0
print((2.0 * x)[1]) # 4.0
Matrix operations
import numcore as nc
A = nc.Matrix(2, 2, [1.0, 2.0,
3.0, 4.0])
B = A @ A
print(B[(0, 0)]) # 7.0
print(B[(1, 1)]) # 22.0
print(A.T().numpy()) # convert to NumPy array
Solve a linear system
import numcore as nc
A = nc.Matrix(3, 3, [
2.0, 1.0, -1.0,
-3.0, -1.0, 2.0,
-2.0, 1.0, 2.0,
])
b = nc.Vector([8.0, -11.0, -3.0])
x = nc.linalg.solve(A, b)
print(x.numpy()) # approximately [2.0, 3.0, -1.0]
ODE solving with RK4
import numcore as nc
def harmonic_oscillator(t, y):
return nc.Vector([y[1], -y[0]])
sol = nc.ode.rk4(
harmonic_oscillator,
0.0,
6.28,
nc.Vector([1.0, 0.0]),
0.01,
)
t = sol.t_array()
y = sol.y_array()
print(t.shape)
print(y.shape)
Optimization with BFGS
import numcore as nc
def rosenbrock(v):
x = v[0]
y = v[1]
return (1.0 - x) ** 2 + 100.0 * (y - x * x) ** 2
def rosenbrock_grad(v):
x = v[0]
y = v[1]
return nc.Vector([
-2.0 * (1.0 - x) - 400.0 * x * (y - x * x),
200.0 * (y - x * x),
])
res = nc.optim.bfgs(
rosenbrock,
rosenbrock_grad,
nc.Vector([0.0, 0.0]),
)
print(res.x[0], res.x[1]) # approximately 1.0, 1.0
print(res.converged)
print(res.iterations)
C++ Usage
NumCore can also be used directly as a C++ header-based library.
Example:
#include <iostream>
#include "NumCore/vector.hpp"
#include "NumCore/matrix.hpp"
#include "NumCore/linalg.hpp"
int main() {
Vector v({3.0, 4.0});
std::cout << v.norm() << std::endl;
Matrix A(2, 2, {2.0, 1.0,
5.0, 7.0});
Vector b({11.0, 13.0});
Vector x = linalg::solve(A, b);
std::cout << x[0] << " " << x[1] << std::endl;
}
Compile with:
g++ main.cpp -I include -std=c++17 -o main
Building from Source
Python package build
python -m pip install .
Manual CMake build
If CMake cannot find pybind11, pass the pybind11 CMake directory explicitly.
Windows PowerShell
$pybind11_DIR = python -m pybind11 --cmakedir
cmake -S . -B build -Dpybind11_DIR="$pybind11_DIR"
cmake --build build --config Release
Run C++ tests:
.\build\Release\tests_cpp.exe
Linux/macOS
pybind11_DIR=$(python -m pybind11 --cmakedir)
cmake -S . -B build -Dpybind11_DIR="$pybind11_DIR" -DCMAKE_BUILD_TYPE=Release
cmake --build build
Run C++ tests:
./build/tests_cpp
Running Tests
Python tests
python -m pytest tests/tests_python.py -q
C++ tests
Windows:
.\build\Release\tests_cpp.exe
Linux/macOS:
./build/tests_cpp
Project Structure
NumCore/
├── include/
│ └── NumCore/
│ ├── NumCore.hpp
│ ├── vector.hpp
│ ├── matrix.hpp
│ ├── linalg.hpp
│ ├── ode.hpp
│ └── optim.hpp
├── python/
│ ├── bindings.cpp
│ └── numcore/
│ └── __init__.py
├── tests/
│ ├── tests_cpp.cpp
│ └── tests_python.py
├── CMakeLists.txt
├── pyproject.toml
└── README.md
Algorithms Implemented
| Category | Algorithm | Notes |
|---|---|---|
| Matrix/Vector | Core arithmetic | Operator overloading and validation |
| Linear Algebra | Matrix-vector multiplication | C++ implementation exposed to Python |
| Linear Algebra | Linear solve | LU-based solve with pivoting |
| Linear Algebra | Determinant | Uses elimination/LU-style logic |
| Linear Algebra | Matrix inverse | Solves against identity basis |
| Linear Algebra | Least squares | Normal-equation based implementation |
| ODE | Euler | Fixed-step first-order method |
| ODE | RK4 | Fixed-step fourth-order Runge-Kutta |
| ODE | RK45 | Adaptive Dormand-Prince method |
| Optimization | Gradient Descent | User-supplied objective and gradient |
| Optimization | Adam | Adaptive first/second moment optimizer |
| Optimization | Newton | Numerical Hessian with regularization |
| Optimization | BFGS | Quasi-Newton inverse-Hessian update |
Notes
NumCore is implemented from scratch for clarity and educational value. It is not intended to replace mature high-performance numerical libraries such as NumPy, SciPy, Eigen, BLAS, or LAPACK for large-scale production workloads.
For small examples, learning, experimentation, and understanding how numerical algorithms are built internally, NumCore provides a compact C++/Python implementation.
Roadmap
- More decomposition methods, including QR and Cholesky
- Sparse matrix support
- Conjugate Gradient and iterative solvers
- More robust benchmarking suite
- Prebuilt wheels for more Python versions and operating systems
- Expanded documentation and examples
License
Add a license file before public production use. Recommended options for open-source numerical libraries include MIT, BSD-3-Clause, or Apache-2.0.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file numcore_scicomp-0.1.1.tar.gz.
File metadata
- Download URL: numcore_scicomp-0.1.1.tar.gz
- Upload date:
- Size: 431.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d5145dba4f92eab21486823312aebc5a6567728c6da3badc44ff506f8b73cb0
|
|
| MD5 |
39c0928648a767074bc18996f230e47c
|
|
| BLAKE2b-256 |
e492d80bbbd480283f1839322244fbd3d737785f76be568be450daeedac95c4e
|
File details
Details for the file numcore_scicomp-0.1.1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: numcore_scicomp-0.1.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 169.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84eb84db6e84a116ca1f820548a3936e6e7a5f2d3647713857a505bfe53fbccc
|
|
| MD5 |
489fefc28fcbf7bd9f8808f73d119378
|
|
| BLAKE2b-256 |
5f6a96271cea02b24fdd4f1a94e238fb21267c76eed74f8682c438ae57875e54
|