Skip to content

Commit 58dc3a6

Browse files
committed
feat: Add unified quantizer, ML integration, hidden dimensions
Iteration 1 Implementation: - quantizer.py: PythagoreanQuantizer with mode selection - ml.py: ConstraintEnforcedLayer, HiddenDimensionNetwork - hidden_dims.py: Hidden dimension encoding (k = ⌈log₂(1/ε)⌉) - financial.py: Exact arithmetic, portfolio optimization - manifold.py: Pure Python fallback implementation - tests/: Comprehensive test suite - examples/: ML training, NumPy integration examples API matches constraint-theory-core (Rust) for consistency.
1 parent 17c9189 commit 58dc3a6

12 files changed

Lines changed: 6193 additions & 60 deletions

‎constraint_theory/__init__.py‎

Lines changed: 140 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,155 @@
22
Constraint Theory - Python Bindings
33
44
Deterministic geometric snapping with O(log n) KD-tree lookup.
5+
6+
This package provides Python bindings for the Constraint Theory library,
7+
enabling exact constraint satisfaction in Python applications.
8+
9+
Key Features:
10+
- PythagoreanManifold: Discrete manifold with exact Pythagorean states
11+
- PythagoreanQuantizer: Unified quantization (TERNARY, POLAR, TURBO, HYBRID)
12+
- ConstraintEnforcedLayer: ML integration for PyTorch/TensorFlow
13+
- Hidden Dimension Encoding: k = ⌈log₂(1/ε)⌉ for exact constraints
14+
- Financial Applications: Exact arithmetic, portfolio optimization, risk constraints
15+
16+
Quick Start:
17+
>>> from constraint_theory import PythagoreanManifold
18+
>>> manifold = PythagoreanManifold(density=200)
19+
>>> x, y, noise = manifold.snap(0.577, 0.816)
20+
>>> print(f"Snapped: ({x:.4f}, {y:.4f}), noise: {noise:.6f}")
21+
Snapped: (0.6000, 0.8000), noise: 0.0236
22+
23+
For ML Integration:
24+
>>> from constraint_theory import ConstraintEnforcedLayer, QuantizationMode
25+
>>> layer = ConstraintEnforcedLayer(
26+
... input_dim=128,
27+
... output_dim=64,
28+
... constraints=['unit_norm']
29+
... )
30+
31+
For Financial Applications:
32+
>>> from constraint_theory import ExactMoney, PortfolioOptimizer, RiskConstraints
33+
>>> price = ExactMoney.from_float(100.50)
34+
>>> print(price.to_string()) # Exact representation, no floating-point drift
535
"""
636

37+
__version__ = "0.3.0"
38+
39+
# Core manifold module
40+
from .manifold import (
41+
PythagoreanManifold,
42+
snap,
43+
generate_triples,
44+
generate_pythagorean_lattice,
45+
)
46+
47+
# Unified quantizer module
48+
from .quantizer import (
49+
QuantizationMode,
50+
QuantizationResult,
51+
PythagoreanQuantizer,
52+
auto_select_mode,
53+
snap_to_pythagorean,
54+
quantize,
55+
)
56+
57+
# ML integration module
58+
from .ml import (
59+
ConstraintConfig,
60+
ConstraintEnforcedLayer,
61+
HiddenDimensionNetwork,
62+
GradientSnapper,
63+
)
64+
65+
# Hidden dimension encoding module
66+
from .hidden_dims import (
67+
HiddenDimConfig,
68+
compute_hidden_dim_count,
69+
lift_to_hidden,
70+
project_visible,
71+
snap_in_lifted_space,
72+
generate_nd_lattice,
73+
encode_with_hidden_dimensions,
74+
cross_plane_finetune,
75+
get_orthogonal_planes,
76+
project_to_plane,
77+
reconstruct_from_plane,
78+
constraint_error,
79+
holographic_accuracy,
80+
)
81+
82+
# Financial applications module
83+
from .financial import (
84+
RoundingMode,
85+
ExactMoney,
86+
RiskConstraints,
87+
TradingSignal,
88+
MultiPlaneOptimizer,
89+
PortfolioOptimizer,
90+
PriceLevelSnapper,
91+
calculate_var,
92+
calculate_sharpe_ratio,
93+
)
94+
95+
# Try to import Rust backend for performance
796
try:
897
from .constraint_theory_python import (
9-
PythagoreanManifold,
10-
snap,
11-
generate_triples,
12-
__version__,
98+
PythagoreanManifold as _RustManifold,
99+
snap as _rust_snap,
100+
generate_triples as _rust_generate_triples,
13101
)
102+
HAS_RUST_BACKEND = True
14103
except ImportError:
15-
# Fallback for development
16-
__version__ = "0.1.0"
17-
18-
class PythagoreanManifold:
19-
def __init__(self, density: int = 200):
20-
raise ImportError(
21-
"constraint-theory is not properly installed. "
22-
"Please install with: pip install constraint-theory"
23-
)
24-
25-
def snap(x: float, y: float, density: int = 200):
26-
raise ImportError(
27-
"constraint-theory is not properly installed. "
28-
"Please install with: pip install constraint-theory"
29-
)
30-
31-
def generate_triples(max_c: int):
32-
raise ImportError(
33-
"constraint-theory is not properly installed. "
34-
"Please install with: pip install constraint-theory"
35-
)
104+
HAS_RUST_BACKEND = False
36105

37106
__all__ = [
107+
# Version
108+
"__version__",
109+
"HAS_RUST_BACKEND",
110+
111+
# Core manifold
38112
"PythagoreanManifold",
39113
"snap",
40114
"generate_triples",
41-
"__version__",
115+
"generate_pythagorean_lattice",
116+
117+
# Quantization
118+
"QuantizationMode",
119+
"QuantizationResult",
120+
"PythagoreanQuantizer",
121+
"auto_select_mode",
122+
"snap_to_pythagorean",
123+
"quantize",
124+
125+
# ML Integration
126+
"ConstraintConfig",
127+
"ConstraintEnforcedLayer",
128+
"HiddenDimensionNetwork",
129+
"GradientSnapper",
130+
131+
# Hidden Dimensions
132+
"HiddenDimConfig",
133+
"compute_hidden_dim_count",
134+
"lift_to_hidden",
135+
"project_visible",
136+
"snap_in_lifted_space",
137+
"generate_nd_lattice",
138+
"encode_with_hidden_dimensions",
139+
"cross_plane_finetune",
140+
"get_orthogonal_planes",
141+
"project_to_plane",
142+
"reconstruct_from_plane",
143+
"constraint_error",
144+
"holographic_accuracy",
145+
146+
# Financial Applications
147+
"RoundingMode",
148+
"ExactMoney",
149+
"RiskConstraints",
150+
"TradingSignal",
151+
"MultiPlaneOptimizer",
152+
"PortfolioOptimizer",
153+
"PriceLevelSnapper",
154+
"calculate_var",
155+
"calculate_sharpe_ratio",
42156
]

0 commit comments

Comments
 (0)