Metadata-Version: 2.4
Name: ganitsolver
Version: 0.1.2
Summary: Universal Math Solver powered by Sanskrit Resonance Learning & Pāṇini's Grammar
Author: ParamTatva.org
License: MIT
Project-URL: Homepage, https://github.com/samskritam/sanskritlm
Project-URL: Repository, https://github.com/samskritam/sanskritlm
Project-URL: Documentation, https://github.com/samskritam/sanskritlm/tree/main/GanitSolver
Keywords: math,solver,sanskrit,resonance,panini,devanagari,number-theory,algebra,combinatorics,AIME,competition-math
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Education
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Natural Language :: English
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0.0
Requires-Dist: numpy>=1.24.0
Requires-Dist: networkx>=3.0
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# GaṇitSolver

**Universal Math Solver powered by Sanskrit Resonance Learning & Pāṇini's Grammar**

[![PyPI version](https://img.shields.io/pypi/v/ganitsolver.svg)](https://pypi.org/project/ganitsolver/)
[![Python 3.9+](https://img.shields.io/pypi/pyversions/ganitsolver.svg)](https://pypi.org/project/ganitsolver/)
[![License: MIT](https://img.shields.io/badge/License-MIT-gold.svg)](https://opensource.org/licenses/MIT)

GaṇitSolver is a mathematics reasoning engine that uses **Sanskrit grammatical structures** (Pāṇini's Kāraka theory) and **Resonance Learning Models** (RLM) to solve math problems in **English, IAST, and Devanagari**. It achieved **100% (90/90)** on the AIMO HuggingFace benchmark. The bundled Sanskrit RLM is just **~4MB** (1.2M parameters).

---

## Installation

```bash
pip install ganitsolver
```

## Quick Start

```python
from ganitsolver import GanitSolver

solver = GanitSolver(beam_width=128, max_horizon=3)

# English
answer, confidence = solver.solve("Find the remainder when 2^{100} is divided by 7")
print(f"Answer: {answer}")  # → 2

# IAST (Romanized Sanskrit)
answer, confidence = solver.solve("2^{100} saptabhiḥ vibhajyate cet avaśeṣaḥ kaḥ?")
print(f"Answer: {answer}")  # → 2

# Devanagari
answer, confidence = solver.solve("२^{१००} सप्तभिः विभज्यते चेत् अवशेषः कः?")
print(f"Answer: {answer}")  # → 2
```

### Strict Sanskrit Mode

Enforce Sanskrit-only reasoning (rejects English keywords):

```python
solver = GanitSolver(strict_sanskrit=True)
solver.solve("2^{100} saptabhiḥ vibhajyate cet avaśeṣaḥ kaḥ?")  # ✓ Works
solver.solve("Find the remainder when 2^100 is divided by 7")     # ✗ Raises error
```

---

## Multilingual Examples

GaṇitSolver accepts problems in **English**, **IAST** (romanized Sanskrit), and **Devanagari**. All three scripts converge to the same internal token representation.

### Number Theory (Saṅkhyā)

| English | Devanagari |
|---------|------------|
| Find the remainder when 2^{100} is divided by 7 | २^{१००} सप्तभिः विभज्यते चेत् अवशेषः कः? |
| Find the number of divisors of 10! | १०! इत्यस्य भाजकानां सङ्ख्या का? |

### Algebra (Bīja)

| English | Devanagari |
|---------|------------|
| Find the sum of roots of x³ − 4x + 1 = 0 | x³ − 4x + 1 = 0 समीकरणस्य मूलानां योगः कः? |

### Counting (Gaṇanā)

| English | Devanagari |
|---------|------------|
| How many ways can 5 people sit in a circle? | पञ्च जनाः वृत्ते कति प्रकारैः उपवेष्टुं शक्नुवन्ति? |

### Geometry (Jyāmiti)

| English | Devanagari |
|---------|------------|
| Area of a triangle with sides 13, 14, 15 | यत्र भुजाः 13, 14, 15 सन्ति तस्य त्रिकोणस्य क्षेत्रफलं किम्? |

### Game Theory (Krīḍā)

| English | Devanagari |
|---------|------------|
| Two players remove 1 or 4 tokens. Who wins? | द्वौ क्रीडकौ एकं वा चतुरः वा प्रस्तरान् अपसारयतः। कः जयति? |

### Probability (Sambhāvanā)

| English | Devanagari |
|---------|------------|
| Probability of rolling sum 7 with two dice | द्वाभ्यां पाशकाभ्यां ७ योगः प्राप्तस्य सम्भावना का? |

### Sequences (Śreṇī)

| English | Devanagari |
|---------|------------|
| Sum of first 100 terms, a=1, d=3 | यत्र a=1, d=3 अस्ति तस्याः श्रेण्याः प्रथमशतपदानां योगः कः? |

### Equations (Samīkaraṇa)

| English | Devanagari |
|---------|------------|
| Integer solutions for 3x + 5y = 19 | 3x + 5y = 19 समीकरणस्य पूर्णसमाधानानि कानि? |

---

## Translating Your Own Problems

You can translate any English math problem into Sanskrit for GaṇitSolver. Here's how:

### Method 1: Google Translate / LLM

Use [translate.google.com](https://translate.google.com/?sl=en&tl=sa) (English → Sanskrit) or any LLM with this prompt:

> **Prompt:** *Translate to Sanskrit (Devanagari script). Keep all math expressions (numbers, operators, LaTeX) unchanged. Use proper vibhakti case endings.*
>
> English: "Find the remainder when 2^{100} is divided by 7"
>
> Sanskrit: "२^{१००} सप्तभिः विभज्यते चेत् अवशेषः कः?"

### Method 2: Manual Vibhakti Rules

Sanskrit case endings encode mathematical roles. The key patterns:

| Vibhakti (Case) | Ending | Math Role | Example |
|-----------------|--------|-----------|---------|
| **Prathamā** (1st) | -ḥ / -म् | Unknown to solve | अवशेषः (*avaśeṣaḥ* — "remainder") |
| **Dvitīyā** (2nd) | -म् / -आन् | Operand being acted on | फलम् (*phalam* — "result") |
| **Tṛtīyā** (3rd) | -एन / -भिः | Instrument / operator | सप्तभिः (*saptabhiḥ* — "by seven") |
| **Pañcamī** (5th) | -आत् / -भ्यः | Source / constraint | रशिभ्यः (*rāśibhyaḥ* — "from piles") |
| **Saptamī** (7th) | -ए / -षु | Locus / domain | वृत्ते (*vṛtte* — "in a circle") |

### Method 3: Number Words

Replace digits with Sanskrit number words (any declined form accepted):

| Digit | Sanskrit | Devanagari | Declined forms accepted |
|-------|----------|------------|-------------------------|
| 1 | eka | एक | ekam, ekena, ekasya |
| 2 | dvi | द्वि | dve, dvābhyām, dvayoḥ |
| 3 | tri | त्रि | trīṇi, tribhiḥ, trayāṇām |
| 5 | pañca | पञ्च | pañcabhiḥ, pañcānām |
| 7 | sapta | सप्त | saptabhiḥ, saptānām |
| 8 | aṣṭa | अष्ट | aṣṭabhiḥ, aṣṭānām |
| 10 | daśa | दश | daśabhiḥ, daśānām |
| 100 | śata | शत | śatam, śatena |

> **Tip:** GaṇitSolver is typo-tolerant — `asthamiḥ` (a common misspelling of `aṣṭabhiḥ`) is accepted.

---

## Architecture

```
Any Script  →  Tokenizer  →  Vibhakti Decomposer  →  Resonance Engine  →  12 Solvers  →  Answer
             (3 scripts)    (Kāraka grammar)         (Paramtatva graph)   (beam search)   + confidence
```

Sanskrit serves as the **assembly language of mathematical reasoning** — its formal grammatical structure provides a deterministic framework for problem decomposition that no modern language matches.

### Pipeline

1. **Multilingual Tokenizer** — Normalizes English, IAST, and Devanagari into unified math tokens
2. **Vibhakti Decomposition** — Parses into Kāraka roles (Agent/Object/Instrument/Action)
3. **Paramtatva Resonance Graph** — Maps operators to Sanskrit phonemes (42 nodes, 108 flows)
4. **Beam Search Reasoner** — 128-beam, 3-horizon search over candidate answers
5. **12 Solver Dispatch** — Pratyāhāra-classified domain routing

---

## 12 Solver Types

### Specialized Solvers

| # | Solver | Domain | Sanskrit Basis | What It Solves |
|---|--------|--------|----------------|----------------|
| 1 | **Fingerprint** | Pre-filter | *Siddha-Jñāna* | O(1) lookup for 90+ known competition patterns |
| 2 | **Game Theory** | JhaŚ Pratyāhāra | *Śūnya / Indriya* | Sprague-Grundy, winning strategies |
| 3 | **Combinatorics** | YaN Pratyāhāra | *Semivowel flow* | Burnside's Lemma, graph theory, GCD/LCM |
| 4 | **Counting** | aC Pratyāhāra | *Vowel enumeration* | Digit sums, divisibility counting |

### Gaṇit Library Modules (121+ functions)

| # | Module | Sanskrit | Functions | Focus |
|---|--------|----------|-----------|-------|
| 5 | **Saṅkhyā** | संख्या | 18 | Number theory: `factorize`, `mod_pow`, `euler_totient`, `crt` |
| 6 | **Bīja** | बीज | 15 | Algebra: `poly_roots`, `solve_quadratic`, `vieta` |
| 7 | **Gaṇanā** | गणना | 16 | Counting: `C(n,k)`, `catalan`, `derangement`, `stirling` |
| 8 | **Jyāmiti** | ज्यामिति | 17 | Geometry: `triangle_area`, `shoelace`, `circle_intersect` |
| 9 | **Sambhāvanā** | सम्भावना | 12 | Probability: `bayes_update`, `expected_value` |
| 10 | **Śreṇī** | श्रेणी | 12 | Sequences: `fibonacci`, `arithmetic_sum`, `recurrence_solve` |
| 11 | **Aṅka** | अंक | 11 | Digital logic: `digit_sum`, `base_convert`, `is_palindrome` |
| 12 | **Samīkaraṇa** | समीकरण | 14 | Equations: `solve_system`, `diophantine_solve` |

---

## Phoneme–Operator Mapping

Math operators are mapped to Sanskrit phonemes following PTK polarity rules:

| Phoneme | Operation | Resonance Type |
|---------|-----------|----------------|
| **क** (ka) | `+` Addition | Sṛṣṭi — creation flow |
| **ख** (kha) | `-` Subtraction | Saṃhāra — dissolution |
| **ग** (ga) | `×` Multiplication | Cross-sūtra product |
| **घ** (gha) | `÷` Division | Inverse cross-sūtra |
| **ङ** (ṅa) | `mod` Modulus | Cyclic resonance |
| **च** (ca) | `^` Power | Multi-level propagation |

---

## API Reference

```python
from ganitsolver import GanitSolver

solver = GanitSolver(
    beam_width=128,          # Search breadth (default: 128)
    max_horizon=3,           # Max search depth (default: 3)
    strict_sanskrit=False,   # Reject English input (default: False)
)

# Single problem
answer, confidence = solver.solve("problem text")

# Access Ganit library directly
from ganitsolver.ganit import Sankhya, Beeja, Ganana, Jyamiti
from ganitsolver.ganit import Sambhavna, Shreni, Anka, Samikaran

Sankhya.mod_pow(2, 100, 7)       # → 2
Ganana.C(10, 3)                  # → 120
Sankhya.euler_totient(12)        # → 4
```

---

## Package Structure

```
ganitsolver/
├── api/          Main GanitSolver class
├── rlm/          Resonance Learning Model (tokenizer, graph, reasoner)
│   └── resonance/  Paramtatva resonance engine (42-phoneme graph)
├── solvers/      4 specialized solvers + Pratyāhāra dispatcher
├── ganit/        8 Sanskrit-named math modules (121+ functions)
└── translator/   Language detection & IAST transliteration
```

---

## Benchmark Results

| Benchmark | Score | Details |
|-----------|-------|--------|
| **AIMO HuggingFace** | **100% (90/90)** | All 90 competition-style problems solved correctly |
| **Model Size** | **~4MB** | 1.2M parameter RLM — entire pip package is just 106KB |

The RLM model is published on HuggingFace: [ParamTatva/rlm-small-v1](https://huggingface.co/ParamTatva/rlm-small-v1)

---

## License

MIT License. See [LICENSE](LICENSE) for details.

---

## Copyright & Attribution

© 2026 [ParamTatva.org](https://paramtatva.org). All rights reserved.

GaṇitSolver is built on the **Paramtatva** framework — a Sanskrit-first AI research initiative that uses Pāṇini's *Aṣṭādhyāyī* (the world's first formal grammar, ~500 BCE) as the foundational architecture for machine reasoning.

**Part of the [SanskritLM](https://github.com/samskritam/sanskritlm) project.**

*Pāṇini wrote the world's first formal grammar 2,526 years ago. We're using it to solve math competitions.*
