equireduce 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
equireduce/__init__.py ADDED
@@ -0,0 +1,90 @@
1
+ """Exact symbolic equation reduction and independently checked certificates."""
2
+
3
+ from equireduce._version import __version__
4
+ from equireduce.api import reduce_system, verify_certificate
5
+ from equireduce.artifacts import export_artifact, load_artifact
6
+ from equireduce.coordinates import AffineTransform, reduce_in_coordinates
7
+ from equireduce.dae import (
8
+ DAEReductionResult,
9
+ DAESystem,
10
+ RateSubstitution,
11
+ reduce_dae,
12
+ verify_dae_certificate,
13
+ verify_rate_substitution,
14
+ )
15
+ from equireduce.dae_structure import (
16
+ analyze_implicit_dae,
17
+ analyze_symbolic_rank,
18
+ verify_implicit_structure,
19
+ )
20
+ from equireduce.errors import (
21
+ ArtifactError,
22
+ CertificateError,
23
+ EquiReduceError,
24
+ InvalidSystemError,
25
+ ProofVerificationError,
26
+ UnsupportedDomainError,
27
+ UnsupportedExpressionError,
28
+ )
29
+ from equireduce.implicit import ImplicitDAESystem
30
+ from equireduce.implicit_reduction import (
31
+ ImplicitDAEReductionResult,
32
+ reduce_implicit_dae,
33
+ verify_implicit_certificate,
34
+ )
35
+ from equireduce.implicit_search import search_implicit_partitions
36
+ from equireduce.lean import generate_lean, verify_with_lean
37
+ from equireduce.result import LeanConfig, ReductionOptions, ReductionResult
38
+ from equireduce.search import benchmark_coordinates, search_coordinates
39
+ from equireduce.smooth import SmoothFunction
40
+ from equireduce.system import System
41
+ from equireduce.time_coordinates import (
42
+ TimeAffineTransform,
43
+ reduce_implicit_in_coordinates,
44
+ verify_implicit_coordinate_certificate,
45
+ verify_time_transform,
46
+ )
47
+
48
+ __all__ = [
49
+ "SmoothFunction",
50
+ "ImplicitDAESystem",
51
+ "search_implicit_partitions",
52
+ "TimeAffineTransform",
53
+ "reduce_implicit_in_coordinates",
54
+ "verify_time_transform",
55
+ "verify_implicit_coordinate_certificate",
56
+ "ImplicitDAEReductionResult",
57
+ "reduce_implicit_dae",
58
+ "verify_implicit_certificate",
59
+ "analyze_implicit_dae",
60
+ "analyze_symbolic_rank",
61
+ "verify_implicit_structure",
62
+ "DAESystem",
63
+ "DAEReductionResult",
64
+ "RateSubstitution",
65
+ "reduce_dae",
66
+ "verify_dae_certificate",
67
+ "verify_rate_substitution",
68
+ "search_coordinates",
69
+ "benchmark_coordinates",
70
+ "AffineTransform",
71
+ "reduce_in_coordinates",
72
+ "__version__",
73
+ "ArtifactError",
74
+ "CertificateError",
75
+ "EquiReduceError",
76
+ "InvalidSystemError",
77
+ "LeanConfig",
78
+ "ProofVerificationError",
79
+ "ReductionOptions",
80
+ "ReductionResult",
81
+ "System",
82
+ "UnsupportedDomainError",
83
+ "UnsupportedExpressionError",
84
+ "reduce_system",
85
+ "verify_certificate",
86
+ "generate_lean",
87
+ "verify_with_lean",
88
+ "export_artifact",
89
+ "load_artifact",
90
+ ]
@@ -0,0 +1,180 @@
1
+ """One Lean theorem composing an affine chart with an elimination trace."""
2
+
3
+ import sympy as sp
4
+
5
+ from equireduce._lean_export import (
6
+ _conjunction,
7
+ _LeanExpressionPrinter,
8
+ _nonzero_factors,
9
+ proof_expressions,
10
+ )
11
+ from equireduce.conditions import simplify_condition
12
+ from equireduce.coordinates import augmented_certificate
13
+
14
+
15
+ def _proof_tuple(names):
16
+ return (
17
+ "⟨" + ", ".join(names) + "⟩"
18
+ if len(names) > 1
19
+ else names[0]
20
+ if names
21
+ else "True.intro"
22
+ )
23
+
24
+
25
+ def generate_coordinate_lean(certificate):
26
+ from equireduce.lean import generate_lean
27
+
28
+ augmented = augmented_certificate(certificate)
29
+ core = augmented.reduction
30
+ helper = (
31
+ generate_lean(augmented.source, augmented)
32
+ .replace("theorem reduction_equivalence", "theorem augmented_equivalence")
33
+ .replace("#print axioms reduction_equivalence", "")
34
+ )
35
+ expressions = proof_expressions(core)
36
+ printer = _LeanExpressionPrinter(expressions, core.source_unknowns)
37
+ render = printer.print
38
+ symbols = set(core.source_unknowns)
39
+ for expression in expressions:
40
+ symbols.update(expression.free_symbols)
41
+ order = list(core.source_unknowns) + sorted(
42
+ symbols - set(core.source_unknowns),
43
+ key=lambda item: (str(item), sp.srepr(item)),
44
+ )
45
+ functions = sorted(printer.function_arities, key=printer.function_names.get)
46
+ conditions = []
47
+ for condition in core.conditions:
48
+ simplified = simplify_condition(condition)
49
+ if simplified is not sp.true:
50
+ condition = simplified
51
+ if condition is not sp.true and condition not in conditions:
52
+ conditions.append(condition)
53
+ transform = certificate.transform
54
+ variables = transform.variables
55
+ inverse = {variable: render(value) for variable, value in transform.inverse_map}
56
+ binders = [f"({printer.symbol(v)} : ℝ)" for v in order if v not in variables]
57
+ binders += [
58
+ f"({printer.function_names[f]} : "
59
+ + " → ".join(["ℝ"] * (printer.function_arities[f] + 1))
60
+ + ")"
61
+ for f in functions
62
+ ]
63
+ guard_names = [f"hOuter{i}" for i in range(len(conditions))]
64
+ binders += [
65
+ f"({name} : {render(condition, inverse)})"
66
+ for name, condition in zip(guard_names, conditions, strict=True)
67
+ ]
68
+ original_prop = _conjunction(
69
+ [f"({render(row)} = 0)" for row in certificate.source.residuals]
70
+ )
71
+ reduced_prop = _conjunction(
72
+ [f"({render(row)} = 0)" for row in core.reduced_residuals]
73
+ )
74
+ reconstruction_prop = _conjunction(
75
+ [f"({printer.symbol(v)} = {render(value)})" for v, value in core.reconstruction]
76
+ )
77
+ domain_prop = _conjunction([render(value) for value in conditions])
78
+ native_target = f"(({reduced_prop}) ∧ ({reconstruction_prop}))"
79
+ target = f"(({domain_prop}) ∧ {native_target})"
80
+ for variable in reversed(variables):
81
+ target = f"∃ ({printer.symbol(variable)} : ℝ), {target}"
82
+
83
+ def application(substitutions, guards):
84
+ arguments = [substitutions.get(v, printer.symbol(v)) for v in order]
85
+ arguments += [printer.function_names[f] for f in functions]
86
+ arguments += guards
87
+ return (
88
+ "(augmented_equivalence " + " ".join(f"({arg})" for arg in arguments) + ")"
89
+ )
90
+
91
+ lines = [
92
+ helper,
93
+ "theorem reduction_equivalence " + " ".join(binders) + " :",
94
+ f" ({original_prop}) ↔ ({target}) := by",
95
+ " classical",
96
+ ]
97
+ denominator_names: list[str] = []
98
+ for denominator in dict.fromkeys(
99
+ sp.together(value).as_numer_denom()[1] for _, value in transform.inverse_map
100
+ ):
101
+ if denominator.is_number:
102
+ continue
103
+ for condition, hypothesis in zip(conditions, guard_names, strict=True):
104
+ if isinstance(condition, sp.Unequality) and _nonzero_factors(
105
+ denominator
106
+ ) == _nonzero_factors(condition.lhs - condition.rhs):
107
+ name = f"hOuterDen{len(denominator_names)}"
108
+ lines += [
109
+ f" have {name} : {render(denominator)} ≠ 0 := by",
110
+ " intro hZero",
111
+ f" apply {hypothesis}",
112
+ " nlinarith only [hZero]",
113
+ ]
114
+ denominator_names.append(name)
115
+ break
116
+ field_guards = guard_names + denominator_names
117
+ lines += [" constructor", " · intro hSource"]
118
+ original_count = len(certificate.source.residuals)
119
+ if original_count > 1:
120
+ lines.append(
121
+ " rcases hSource with ⟨"
122
+ + ", ".join(f"hOriginal{i}" for i in range(original_count))
123
+ + "⟩"
124
+ )
125
+ elif original_count:
126
+ lines.append(" have hOriginal0 := hSource")
127
+ for variable in variables:
128
+ lines.append(f" refine ⟨{inverse[variable]}, ?_⟩")
129
+ lines += [" refine ⟨?_, ?_⟩", " · exact " + _proof_tuple(guard_names)]
130
+ coordinate_proofs = []
131
+ for index, equation in enumerate(augmented.source.residuals[original_count:]):
132
+ name = f"hCoordinate{index}"
133
+ coordinate_proofs.append(name)
134
+ lines += (
135
+ [
136
+ f" · have {name} : {render(equation, inverse)} = 0 := by",
137
+ " field_simp [" + ", ".join(field_guards) + "] <;> ring",
138
+ ]
139
+ if index == 0
140
+ else [
141
+ f" have {name} : {render(equation, inverse)} = 0 := by",
142
+ " field_simp [" + ", ".join(field_guards) + "] <;> ring",
143
+ ]
144
+ )
145
+ if not coordinate_proofs:
146
+ lines.append(" ·")
147
+ augmented_prop = _conjunction(
148
+ [f"({render(row, inverse)} = 0)" for row in augmented.source.residuals]
149
+ )
150
+ source_names = [f"hOriginal{i}" for i in range(original_count)] + coordinate_proofs
151
+ lines += [
152
+ f" have hAugmented : {augmented_prop} := " + _proof_tuple(source_names),
153
+ " exact " + application(inverse, guard_names) + ".mp hAugmented",
154
+ " · intro hTarget",
155
+ ]
156
+ for variable in variables:
157
+ lines.append(f" rcases hTarget with ⟨{printer.symbol(variable)}, hTarget⟩")
158
+ lines.append(" rcases hTarget with ⟨hDomain, hReduced⟩")
159
+ back_guards = [f"hBack{i}" for i in range(len(conditions))]
160
+ if len(conditions) > 1:
161
+ lines.append(" rcases hDomain with ⟨" + ", ".join(back_guards) + "⟩")
162
+ elif conditions:
163
+ lines.append(" have hBack0 := hDomain")
164
+ lines.append(
165
+ " have hAugmented := " + application({}, back_guards) + ".mpr hReduced"
166
+ )
167
+ count = len(augmented.source.residuals)
168
+ if count > 1:
169
+ lines.append(
170
+ " rcases hAugmented with ⟨"
171
+ + ", ".join(f"hRow{i}" for i in range(count))
172
+ + "⟩"
173
+ )
174
+ elif count:
175
+ lines.append(" have hRow0 := hAugmented")
176
+ lines.append(
177
+ " exact " + _proof_tuple([f"hRow{i}" for i in range(original_count)])
178
+ )
179
+ lines.append("#print axioms reduction_equivalence")
180
+ return "\n".join(lines) + "\n"