A Python library containing reusable models and methods from coursework, including modules on stochastic processes, optimization, graph theory, and more to come.
Project description
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
Project details
Release history Release notifications | RSS feed
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 mathcode-0.1.2.tar.gz.
File metadata
- Download URL: mathcode-0.1.2.tar.gz
- Upload date:
- Size: 70.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
062078f4dd7d62a926e77b233b115ffad489f40e03fc16e90788451162b89463
|
|
| MD5 |
53e613552ad5b0aecdafd14ac6a9b8b2
|
|
| BLAKE2b-256 |
949085837e0a8b1a967147bfe412a4ccc161d96a553cc3c42a8c9c87dc73715f
|
Provenance
The following attestation bundles were made for mathcode-0.1.2.tar.gz:
Publisher:
publish.yml on igoeldc/mathcode
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mathcode-0.1.2.tar.gz -
Subject digest:
062078f4dd7d62a926e77b233b115ffad489f40e03fc16e90788451162b89463 - Sigstore transparency entry: 712930055
- Sigstore integration time:
-
Permalink:
igoeldc/mathcode@639bad3c90bbcac6f239c1b5b5a585a58cb5e2d5 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/igoeldc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@639bad3c90bbcac6f239c1b5b5a585a58cb5e2d5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mathcode-0.1.2-py3-none-any.whl.
File metadata
- Download URL: mathcode-0.1.2-py3-none-any.whl
- Upload date:
- Size: 61.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88e9eb5d16ab7ecace2d4b82746b50aea2f0ed94c06d79fdad3eb1f9f24a1bc6
|
|
| MD5 |
ef1d1d7605dd1705f2105b338af857a1
|
|
| BLAKE2b-256 |
e6e85520e5d1f7badc38782eec3004b15036ad01d131474c227aec548e319db4
|
Provenance
The following attestation bundles were made for mathcode-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on igoeldc/mathcode
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mathcode-0.1.2-py3-none-any.whl -
Subject digest:
88e9eb5d16ab7ecace2d4b82746b50aea2f0ed94c06d79fdad3eb1f9f24a1bc6 - Sigstore transparency entry: 712930141
- Sigstore integration time:
-
Permalink:
igoeldc/mathcode@639bad3c90bbcac6f239c1b5b5a585a58cb5e2d5 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/igoeldc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@639bad3c90bbcac6f239c1b5b5a585a58cb5e2d5 -
Trigger Event:
release
-
Statement type: