Skip to main content

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 -e .

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mathcode-0.1.0.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mathcode-0.1.0-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

Details for the file mathcode-0.1.0.tar.gz.

File metadata

  • Download URL: mathcode-0.1.0.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

Hashes for mathcode-0.1.0.tar.gz
Algorithm Hash digest
SHA256 36dc56b862e512ea0c6e84245167edbdada968b5b6c9ef71f9c37dcae0b27bcb
MD5 acca98b979eafb7f3e7730b4160143c8
BLAKE2b-256 51dfe5c7b121183ed85b4764ff710351fb3aa5300c1d4a07dbf20f97d84897d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathcode-0.1.0.tar.gz:

Publisher: publish.yml on igoeldc/mathcode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mathcode-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mathcode-0.1.0-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

Hashes for mathcode-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35fdd38e7b38b1bcd407e50e0a4ae23fc9f9081cbc15e3fe0bd722952fd1d051
MD5 36c4c74c77b5be36ce968c0233557636
BLAKE2b-256 3d6edde1109f9f7e11aa7209aba7f27550fedf6f8bc49d877656adf260f30688

See more details on using hashes here.

Provenance

The following attestation bundles were made for mathcode-0.1.0-py3-none-any.whl:

Publisher: publish.yml on igoeldc/mathcode

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page