mathcode
A Python library containing reusable mathematical models and methods from coursework, including modules on stochastic processes, optimization, graph theory, abstract algebra, and more.
Features
Abstract Algebra (mathcode.abstract_algebra)
- Groups: Cyclic groups, symmetric groups, permutation groups
- Rings: Integer rings, polynomial rings, quotient rings
- Fields: Finite fields (Galois fields), field extensions
Graph Theory (mathcode.graph_theory)
- Data Structures: Graph, DirectedGraph, WeightedGraph, WeightedDirectedGraph
- Traversal: BFS, DFS (iterative and recursive), level-order traversal
- Properties: Connectivity, cycle detection, bipartiteness, bridges, articulation points, strongly connected components
- Shortest Paths: Dijkstra's algorithm, Bellman-Ford, A* search
- Spanning Trees: Kruskal's algorithm, Prim's algorithm, Union-Find
- Network Flow: Ford-Fulkerson, Edmonds-Karp, min-cost max-flow
- Centrality: Degree, closeness, betweenness (Brandes' algorithm), PageRank
- Advanced: Hamiltonian paths/cycles, graph coloring, planarity testing, graph isomorphism
Optimization (mathcode.optimization)
- Gradient Descent: Standard, momentum, Nesterov, AdaGrad, RMSProp, Adam
- Linear Programming: Simplex method, dual conversion, Dantzig-Wolfe decomposition, Benders decomposition
Reinforcement Learning (mathcode.reinforcement_learning)
- Environments: GridWorld, abstract Environment base class
- Dynamic Programming: Value iteration, policy iteration, policy evaluation, Q-value iteration
- Monte Carlo Methods: First-visit/every-visit prediction, exploring starts, epsilon-greedy control, off-policy with importance sampling
- Policy/Value Functions: Deterministic and stochastic policies, value functions V(s), action-value functions Q(s,a)
Stochastic Processes (mathcode.stochastics)
- Brownian Motion: Standard, geometric, drifted, Ornstein-Uhlenbeck
- Markov Chains: Discrete-time Markov chains, transition matrices, stationary distributions
- Poisson Processes: Event simulation, counting processes
- Stochastic Differential Equations: Euler-Maruyama, Milstein methods
- Stochastic Partial Differential Equations: Heat equation, wave equation, Allen-Cahn, Burgers
Installation
pip install mathcode
Requirements
- Python >= 3.8
- numpy
- matplotlib
- networkx
- scipy
Usage Examples
Abstract Algebra
from mathcode.abstract_algebra import CyclicGroup, FiniteField
# Create cyclic group Z_12
g = CyclicGroup(12)
print(g.order()) # 12
# Create finite field GF(8)
f = FiniteField(8)
# ... field operations
Graph Theory
from mathcode.graph_theory import Graph, dijkstra, bfs, pagerank
# Create a graph
g = Graph()
g.add_edge(0, 1, weight=4.0)
g.add_edge(0, 2, weight=1.0)
g.add_edge(1, 3, weight=1.0)
# Find shortest paths
distances, predecessors = dijkstra(g, start=0)
# Traverse the graph
visited = bfs(g, start=0)
# Compute PageRank
scores = pagerank(g)
Optimization
from mathcode.optimization import Adam, SimplexLP
# Optimize with Adam
optimizer = Adam(learning_rate=0.001)
# ... training loop with optimizer.update(params, gradients)
# Solve linear program
lp = SimplexLP(c=[1, 2], A=[[1, 1], [2, 1]], b=[4, 5])
solution, value = lp.solve()
Reinforcement Learning
from mathcode.reinforcement_learning import GridWorld, value_iter, mc_es
# Create a grid world environment
env = GridWorld(height=4, width=4, start=(0, 0), goal=(3, 3))
# Solve with Value Iteration (Dynamic Programming)
V, policy = value_iter(env, gamma=0.9)
print(f"Optimal action at start: {policy.get_action((0, 0))}")
# Solve with Monte Carlo Exploring Starts
Q, policy = mc_es(env, num_episodes=5000, gamma=0.9)
Stochastic Processes
from mathcode.stochastics import GBM, MarkovChain
import numpy as np
# Simulate Geometric Brownian Motion
gbm = GBM(T=1.0, dt=0.01, mu=0.05, sigma=0.2, X0=100)
paths = gbm.sample(n_paths=1000)
# Create Markov chain
P = np.array([[0.7, 0.3], [0.4, 0.6]])
mc = MarkovChain(P)
stationary = mc.stationary_distribution()
Development
Running Tests
pytest tests/ -v
Project Structure
mathcode/
├── abstract_algebra/ # Group theory, ring theory, field theory
├── graph_theory/ # Graph algorithms and data structures
├── optimization/ # Optimization algorithms
├── reinforcement_learning/ # RL algorithms (DP, Monte Carlo)
└── stochastics/ # Stochastic processes and SDEs
tests/ # Unit tests for all modules
Author
Ishaan Goel
Links
Release files for mathcode 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mathcode-0.1.2.tar.gz | 70.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mathcode-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 132.1 kB
Release files / mathcode-0.1.2.tar.gz
| Download URL | mathcode-0.1.2.tar.gz |
|---|---|
| Size | 70.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
062078f4dd7d62a926e77b233b115ffad489f40e03fc16e90788451162b89463
|
|
BLAKE2b-256 checksum How to use checksums |
949085837e0a8b1a967147bfe412a4ccc161d96a553cc3c42a8c9c87dc73715f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 21, 2025.
Transparency logRelease files / mathcode-0.1.2-py3-none-any.whl
| Download URL | mathcode-0.1.2-py3-none-any.whl |
|---|---|
| Size | 61.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88e9eb5d16ab7ecace2d4b82746b50aea2f0ed94c06d79fdad3eb1f9f24a1bc6
|
|
BLAKE2b-256 checksum How to use checksums |
e6e85520e5d1f7badc38782eec3004b15036ad01d131474c227aec548e319db4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 21, 2025.
Transparency log