1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! **symplex** — a fast, correct symbolic mathematics library for Rust.
//!
//! Expressions are exact (`Ratio<BigInt>` arithmetic, hash-consed in a
//! [`Context`](prelude::Context) arena) and the library can differentiate,
//! integrate (indefinite, definite, improper, numeric), sum, take limits and
//! series, solve equations, systems, ODEs and recurrences, simplify with a
//! public rewrite-rule engine, work with sets and boolean logic, do exact
//! linear algebra, view expressions as polynomials with symbolic
//! coefficients ([`Poly`](prelude::Poly)), put rational functions into
//! normal form (`ratsimp`), solve linear programs exactly with dual values
//! and Farkas certificates, compute Hermite and Smith normal forms of
//! integer matrices, run numerical root finding and minimisation, apply
//! Laplace/Fourier/Mellin/Z transforms, and generate optimized Rust or C99
//! code. See the [README](https://github.com/cgorski/symplex) and
//! [The Symplex Book](https://cgorski.github.io/symplex/) for a guided tour,
//! and `CHANGELOG.md` for the 0.1 → 0.2 breaking changes and the 0.2 → 0.3
//! behaviour changes.
//!
//! Symplex is designed around seven principles:
//!
//! 1. **Construction is cheap, evaluation is explicit.** Constructors only
//! canonicalize (flatten, sort, combine). No expansion, no function
//! evaluation, no identity application. Call `.eval()`, `.expand()`, or
//! `.simplify()` when *you* choose.
//!
//! 2. **Never silently wrong.** Operations return `Result::Err`, an
//! unevaluated node, or `None` instead of a guess: `∫₋₁¹ dx/x²` is
//! `Err(Divergent)`, `solve(x − x)` is `Err(InfiniteSolutions)`, `re(z)` stays
//! `re(z)` until `z` is known to be real. Structural substitution by default.
//!
//! 3. **One representation per concept.** One assumption system. One polynomial
//! type. One number type. One solve function.
//!
//! 4. **Thread-safe from day one.** Expression handles are `Send + Sync`.
//!
//! 5. **No recursive tree walks.** All traversals use explicit stacks.
//!
//! 6. **The compiler is the API contract.** `pub` = stable. `pub(crate)` = internal.
//! `Ex`, `BoolEx` and `SetEx` are distinct types.
//!
//! 7. **Extensible without inheritance.** Custom functions via registered rules
//! ([`Rule`](prelude::Rule), [`RuleSet`](prelude::RuleSet)).
//!
//! # API model
//!
//! * Operations for which "unevaluated" is a valid answer return
//! [`Ex`](prelude::Ex) and have a `try_` twin returning `Result`
//! (`integrate` / `try_integrate`, `integrate_definite` /
//! `try_integrate_definite`, `summation` / `try_summation`, …).
//! * Numeric boundaries (`eval_f64`, `compile`, `to_rust_fn`, `to_c_fn`,
//! `integrate_numeric`) and structural preconditions (`Matrix::inv`,
//! `cholesky`) return `Result`.
//! * Queries (`is_positive`, `equals`, `SetEx::contains`,
//! `Matrix::is_symmetric`) return `Option<bool>`: yes, no, or unknown.
//!
//! # Quick Start
//!
//! ```
//! use symplex::prelude::*;
//! use symplex::syms;
//!
//! let ctx = Context::new();
//! syms!(ctx; x, y);
//! let expr = &x * &x + &x * 2 + 1;
//! assert_eq!(format!("{expr}"), "x^2 + 2*x + 1");
//!
//! // Differentiate, integrate over an infinite range, solve, compile.
//! assert_eq!(format!("{}", expr.diff(&x)), "2*x + 2");
//! let gauss = (-x.powi(2)).exp().integrate_definite(&x, &ctx.neg_infinity(), &ctx.infinity());
//! assert_eq!(format!("{gauss}"), "sqrt(pi)");
//! let roots = (&x.powi(2) - 4).solve(&x).unwrap();
//! assert_eq!(roots.len(), 2);
//! let f = expr.compile(&["x"]).unwrap();
//! assert_eq!(f(&[2.0]), 9.0);
//! ```
//!
//! # Module map
//!
//! The [`prelude`] re-exports everything most programs need. Domain modules
//! are re-exported at the crate root: [`ntheory`], [`diophantine`],
//! [`combinatorics`], [`mod@matrix`], [`matrix_decomp`], [`normalforms`],
//! [`linprog`], [`optimize`], [`vector`], [`quaternion`], [`control`],
//! [`robotics`], [`dynamics`], [`poly_ex`], [`multipoly`], [`polysys`],
//! [`groebner`], [`factor_zassenhaus`], [`definite`], [`summation`],
//! [`formal_series`], [`finite_diff`], [`fourier_transform`], [`mellin`],
//! [`z_transform`], [`ode`], [`rsolve`], [`sets`], [`logic`], [`parse`],
//! [`tree`], [`codegen`], [`lambdify`], [`units`], [`assumptions`],
//! [`numeric`], [`errors`], [`config`].
//!
//! New in 0.3: [`poly_ex`] (the [`Poly`](prelude::Poly) view of an
//! expression), [`linprog`] (exact simplex), [`normalforms`] (Hermite /
//! Smith normal forms, integer kernels) and [`optimize`] (Brent,
//! Nelder–Mead, differential evolution, least-squares fitting).
// ── Self-referencing extern crate so proc-macro-generated paths
// (`::symplex::__macro_support::…`) resolve inside the crate itself. ──
extern crate self as symplex;
// ── Directory modules (internal organisation) ──────────────────────────
pub
/// Foundation layer: expression nodes, arena, tree traversal, canonicalization, and core types.
pub
pub
pub
pub
pub
pub
pub
/// Compile-time dimensional analysis for physical quantities.
// ── Public re-exports (backwards-compatible crate-root paths) ──────────
// The exact-arithmetic crates whose types appear in the public API
// (`Ratio<BigInt>` from `as_rational`, `linprog::Q`, `poly_fit_exact`,
// `Matrix::from_ratio`, …). Re-exported so a downstream crate can name and
// manipulate those values without adding — and version-matching — the crates
// itself: `symplex::num_rational::Ratio`, `symplex::num_bigint::BigInt`,
// `symplex::num_traits::{Zero, One, Signed}`, `symplex::num_integer::Integer`,
// `symplex::num_complex::Complex64` (from `eval_complex64`, `nroots`).
pub use num_bigint;
pub use num_complex;
pub use num_integer;
pub use num_rational;
pub use num_traits;
// base
/// Assumption system for symbolic variables.
pub use assumptions;
/// Library-wide configuration knobs.
pub use config;
/// Error types used throughout the library.
pub use errors;
/// A value or `±∞` — the extended line; `Interval<Extended<T>>` is an unbounded interval.
pub use Extended;
/// Intervals (`[a, b]`, `(a, b)`, …) and possibly-unbounded closed bounds with named endpoints — the types behind every pair of bounds in the API.
pub use ;
// base
/// Exact `f64` ↔ rational conversions (dyadic exact, and best bounded-denominator approximations).
pub use numeric;
// poly
/// Univariate factorization over ℤ via Berlekamp–Zassenhaus.
pub use factor_zassenhaus;
/// Gröbner basis computation via Buchberger's algorithm with FGLM order conversion.
pub use groebner;
/// Sparse multivariate polynomials over ℚ.
pub use multipoly;
/// Polynomial system solving via Gröbner bases.
pub use polysys;
// calculus
/// Definite and improper integration.
pub use definite;
/// Finite difference methods: weights, application, and differentiation.
pub use finite_diff;
/// Formal power series representations and algorithms.
pub use formal_series;
/// Symbolic Fourier transform.
pub use fourier_transform;
/// Mellin transform.
pub use mellin;
/// Ordinary differential equation solver.
pub use ode;
/// Symbolic summation and products.
pub use summation;
/// Z-transform for discrete-time signal analysis.
pub use z_transform;
// transforms
/// Boolean-logic simplification, normal forms, satisfiability.
pub use logic;
/// Recurrence-relation solver.
pub use rsolve;
/// Set algebra on intervals, finite sets, unions.
pub use sets;
// output
/// Lean 4 / Mathlib rendering (`Ex::to_lean`, `LeanOpts`).
pub use lean;
/// Presentation MathML rendering.
pub use mathml;
/// Runtime expression parser — convert strings to symbolic expressions.
pub use parse;
/// Serializable expression tree for interchange (JSON, etc.).
pub use tree;
// plotting
/// Data export utilities: CSV, TSV, JSON, Markdown, HTML, LaTeX table output.
pub use data_export;
// domains
/// Exact, machine-checkable non-negativity certificates: Handelman (boxes),
/// half-lines, parametric polyhedra and sums of squares, with Lean export.
pub use certificates;
/// Combinatorics: Stirling numbers, multinomial coefficients, partition counting.
pub use combinatorics;
/// Control systems: state-space models, transfer functions, stability analysis.
pub use control;
/// Named results of matrix decompositions (`Qr`, `Lu`, `HermiteNormalForm`, …) shared by `Matrix`, `ZMatrix` and `QMatrix`.
pub use decompositions;
/// Diophantine equations.
pub use diophantine;
/// Discrete transforms on exact sequences (convolution, NTT, Walsh–Hadamard, Möbius).
pub use discrete;
/// Lagrangian dynamics: equations of motion, mass matrix, Coriolis, gravity.
pub use dynamics;
/// Exact linear programming over ℚ (two-phase simplex, duals, Farkas certificates).
pub use linprog;
/// Symbolic matrix type and operations.
pub use matrix;
/// Additional matrix decompositions (QR, Gram–Schmidt) and structure tests.
pub use matrix_decomp;
/// Integer matrix normal forms: Hermite, Smith, unimodular transforms, integer kernels.
pub use normalforms;
/// Number theory: primality, factorization, divisors, modular arithmetic.
pub use ntheory;
/// Numerical optimisation and root bracketing (Brent, Nelder–Mead, polynomial fitting).
pub use optimize;
/// Exact convex polyhedra in ℚⁿ from half-spaces: vertices, volume,
/// containment, cutting.
pub use polytope;
/// Symbolic quaternion algebra for attitude representation.
pub use quaternion;
/// Robotics kinematics: DH parameters, forward kinematics, rotations.
pub use robotics;
/// Symbolic probability and statistics: random variables, exact moments, probabilities, densities.
pub use stats;
/// Vector calculus: gradient, divergence, curl, laplacian.
pub use vector;
// api
/// Expression context — arena, symbol table, configuration.
pub use context;
/// Symbolic equation type (`lhs = rhs`).
pub use eq;
/// The core expression handle and types.
pub use expr;
/// Complex-analysis methods on `Ex` (`re`, `im`, `conjugate`, `arg`, `polar`, …).
pub use expr_complex;
/// Definite / improper / numeric integration methods on `Ex`.
pub use expr_integrate_ext as integrate_api;
/// Operator overloads and scalar-conversion traits (`ToEx`, `Scalar`).
pub use expr_ops;
/// Polynomial-algebra methods on `Ex` (resultant, discriminant, division, numeric roots, …).
pub use expr_poly_ext as poly_api;
/// Public rewrite-rule engine: `Rule`, `RuleSet`, `Bindings`, `RewriteOpts`, `Step`.
pub use expr_rules_ext as rules;
/// Summation, products, series and formal-power-series methods on `Ex`.
pub use expr_series_ext as series_api;
/// Set-algebra and boolean-logic helpers (`reduce_inequalities`).
pub use expr_sets_ext as sets_api;
/// Solver entry points beyond `Ex::solve`: `linsolve`, `LinearSolution`, `GeneralSolution`, Newton systems.
pub use expr_solve_ext as solvers;
/// Integral transforms (Fourier, Mellin, Laplace helpers) and directional limits on `Ex`.
pub use expr_transforms_ext as transforms_api;
/// A non-locking, read-only view of an expression node for use in `replace()`.
pub use expr_view;
/// Convenience macros for building expressions.
pub use macros;
/// Public sparse polynomial view (`Poly`) over explicit generators.
pub use poly_ex;
// output
/// Code-generation options and compiled numeric functions.
pub use codegen;
/// Compiled numeric closures (`CompiledFn`, `CompiledFnVec`).
pub use lambdify;
// ── Proc macro re-exports ──────────────────────────────────────────────
pub use ;
// ── Macro support (hidden internals used by generated code) ────────────
// bitflags types don't auto-derive Default; provide it here so
// Assumptions::default() works.
/// The symplex prelude — one import to get started.
///
/// ```
/// use symplex::prelude::*;
/// ```