thales 0.4.2

A comprehensive Computer Algebra System (CAS) library for symbolic mathematics, equation solving, calculus, and linear algebra
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
//! # Thales - Computer Algebra System
//!
//! A comprehensive Computer Algebra System (CAS) library for symbolic mathematics,
//! equation solving, calculus, and numerical methods. Named after
//! [Thales of Miletus](https://en.wikipedia.org/wiki/Thales_of_Miletus),
//! the first mathematician in the Greek tradition.
//!
//! ## Overview
//!
//! Thales provides:
//! - **Symbolic equation solving** - Linear, quadratic, polynomial, transcendental, and systems
//! - **Calculus** - Differentiation, integration, limits, Taylor series, first and second-order ODEs
//! - **Numerical methods** - Newton-Raphson, bisection, Brent's method, Levenberg-Marquardt
//! - **Coordinate systems** - 2D/3D transformations, complex numbers, De Moivre's theorem
//! - **Units & dimensions** - Dimensional analysis and unit conversion
//! - **Step-by-step solutions** - Resolution paths for educational applications
//! - **iOS/macOS support** - FFI bindings via swift-bridge
//!
//! ## Key Features
//!
//! - Zero-cost abstractions with compile-time guarantees
//! - Memory-safe implementation (no unsafe code except FFI boundary)
//! - 1152 tests including property-based tests with proptest
//! - Optimized for mobile targets (iOS)
//! - Clear separation between symbolic and numerical methods
//!
//! # Quick Start
//!
//! ## Example 1: Coordinate Transformations
//!
//! Convert between Cartesian and polar coordinate systems:
//!
//! ```rust
//! use thales::{Cartesian2D, Polar};
//!
//! // 2D Cartesian to Polar
//! let cartesian = Cartesian2D::new(3.0, 4.0);
//! let polar = cartesian.to_polar();
//! assert!((polar.r - 5.0).abs() < 1e-10);
//! assert!((polar.theta - 0.927295218).abs() < 1e-6);
//!
//! // Polar to Cartesian round-trip
//! let back = polar.to_cartesian();
//! assert!((back.x - 3.0).abs() < 1e-10);
//! assert!((back.y - 4.0).abs() < 1e-10);
//! ```
//!
//! ## Example 2: 3D Coordinate Transformations
//!
//! Convert between Cartesian and spherical coordinates:
//!
//! ```rust
//! use thales::{Cartesian3D, Spherical};
//!
//! // Cartesian to Spherical
//! let cart3d = Cartesian3D::new(1.0, 1.0, 1.0);
//! let spherical = cart3d.to_spherical();
//! assert!((spherical.r - 1.732050808).abs() < 1e-6);
//!
//! // Spherical to Cartesian round-trip
//! let back = spherical.to_cartesian();
//! assert!((back.x - 1.0).abs() < 1e-10);
//! assert!((back.y - 1.0).abs() < 1e-10);
//! assert!((back.z - 1.0).abs() < 1e-10);
//! ```
//!
//! ## Example 3: Complex Number Operations
//!
//! Work with complex numbers and polar form:
//!
//! ```rust
//! use thales::ComplexOps;
//! use num_complex::Complex64;
//!
//! // De Moivre's theorem: (r∠θ)^n = r^n∠(nθ)
//! let z = Complex64::new(1.0, 1.0);
//! let result = ComplexOps::de_moivre(z, 2.0);
//!
//! // Complex conjugate (using num_complex methods)
//! let conj = z.conj();
//! assert_eq!(conj.re, z.re);
//! assert_eq!(conj.im, -z.im);
//!
//! // Modulus (magnitude) of complex number
//! let modulus = z.norm();
//! assert!((modulus - 1.4142135623730951).abs() < 1e-10);
//! ```
//!
//! ## Example 4: Expression and Variable Basics
//!
//! Build mathematical expressions using the AST:
//!
//! ```rust
//! use thales::{Expression, Variable, BinaryOp};
//!
//! // Create expression: 2*x + 5
//! let x = Variable::new("x");
//! let two_x = Expression::Binary(
//!     BinaryOp::Mul,
//!     Box::new(Expression::Integer(2)),
//!     Box::new(Expression::Variable(x.clone()))
//! );
//! let expr = Expression::Binary(
//!     BinaryOp::Add,
//!     Box::new(two_x),
//!     Box::new(Expression::Integer(5))
//! );
//!
//! // Check variable containment
//! assert!(expr.contains_variable("x"));
//! assert!(!expr.contains_variable("y"));
//! ```
//!
//! ## User Guides
//!
//! For detailed tutorials and workflows, see the [`guides`] module:
//!
//! - [`guides::solving_equations`] - Linear, quadratic, polynomial, and systems
//! - [`guides::calculus_operations`] - Derivatives, integrals, limits, ODEs
//! - [`guides::series_expansions`] - Taylor, Maclaurin, Laurent, asymptotic
//! - [`guides::coordinate_systems`] - 2D/3D transforms, complex numbers
//! - [`guides::numerical_methods`] - Root-finding when symbolic fails
//! - [`guides::working_with_units`] - Dimensional analysis
//! - [`guides::error_handling`] - Working with [`ThalesError`]
//!
//! ## Feature Summary
//!
//! | Category | Features |
//! |----------|----------|
//! | **Parsing** | Expression parser, equation parser, LaTeX input/output |
//! | **Solving** | Linear, quadratic, polynomial, transcendental, multi-equation systems |
//! | **Calculus** | Differentiation, integration (by parts, substitution), limits, L'Hôpital |
//! | **ODEs** | First-order (separable, linear), second-order (constant coefficient) |
//! | **Series** | Taylor, Maclaurin, Laurent, asymptotic expansions, Big-O |
//! | **Special** | Gamma, beta, error functions with derivation steps |
//! | **Numerical** | Newton-Raphson, bisection, Brent's, secant, Levenberg-Marquardt |
//! | **Transforms** | Cartesian ↔ Polar ↔ Spherical ↔ Cylindrical, complex operations |
//! | **Units** | SI base/derived units, dimensional analysis, conversions |
//! | **FFI** | Swift bindings via swift-bridge, iOS device + simulator |
//!
//! # Architecture Overview
//!
//! The library follows a modular design with clear separation of concerns:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────┐
//! │                    Public API Layer                     │
//! │  (parse_equation, SmartSolver, Cartesian2D, etc.)      │
//! └─────────────────────────────────────────────────────────┘
//!//!           ┌────────────────┼────────────────┐
//!           ▼                ▼                ▼
//!    ┌──────────┐     ┌──────────┐    ┌──────────┐
//!    │  Parser  │     │  Solver  │    │Transform │
//!    │ (mathlex)│     │ (symbolic)│    │(nalgebra)│
//!    └──────────┘     └──────────┘    └──────────┘
//!           │                │                │
//!           └────────────────┼────────────────┘
//!//!                     ┌──────────┐
//!                     │   AST    │
//!                     │(equation,│
//!                     │  expr,   │
//!                     │variable) │
//!                     └──────────┘
//!//!           ┌────────────────┼────────────────┐
//!           ▼                ▼                ▼
//!    ┌──────────┐     ┌──────────┐    ┌──────────┐
//!    │Numerical │     │Resolution│    │Dimensions│
//!    │ (argmin) │     │   Path   │    │  (units) │
//!    └──────────┘     └──────────┘    └──────────┘
//!//!//!                     ┌──────────┐
//!                     │   FFI    │
//!                     │  (Swift) │
//!                     └──────────┘
//! ```
//!
//! ## Module Responsibilities
//!
//! - [`ast`]: Core data structures for mathematical expressions, equations, variables,
//!   operators, and functions. All other modules build upon these types.
//!
//! - [`parser`]: String → AST conversion using the mathlex parsing library.
//!   Handles operator precedence, function calls, and complex number literals.
//!
//! - [`solver`]: Symbolic equation solving using algebraic manipulation. Includes
//!   specialized solvers for linear, quadratic, polynomial, and transcendental equations.
//!   The [`SmartSolver`] automatically dispatches to the appropriate solver.
//!
//! - [`numerical`]: Numerical root-finding methods for equations that cannot be solved
//!   symbolically. Integrates with symbolic differentiation from the AST module.
//!
//! - [`resolution_path`]: Records the step-by-step solution process for educational
//!   applications. Each transformation is recorded with its operation type.
//!
//! - [`dimensions`]: Dimensional analysis and unit conversion. Ensures physical
//!   equations maintain dimensional consistency.
//!
//! - [`transforms`]: Coordinate system conversions (Cartesian, Polar, Spherical,
//!   Cylindrical) and complex number operations. Built on nalgebra for linear algebra.
//!
//! - `ffi`: Foreign function interface for Swift via swift-bridge. Provides
//!   C-compatible bindings for iOS/macOS integration. Enabled with the `ffi` feature flag.
//!
//! # Safety Guarantees
//!
//! This library adheres to strict memory safety principles:
//!
//! - **No unsafe code in core logic**: All mathematical operations, parsing, solving,
//!   and transformations use only safe Rust.
//!
//! - **FFI boundary isolation**: The only `unsafe` code appears in the `ffi` module
//!   for C interoperability, which is:
//!   - Isolated behind the `ffi` feature flag
//!   - Managed by the swift-bridge library
//!   - Validated at the FFI boundary with explicit error handling
//!
//! - **Ownership guarantees**: The Rust type system prevents:
//!   - Use-after-free bugs
//!   - Data races in concurrent access
//!   - Null pointer dereferences
//!   - Buffer overflows
//!
//! - **Integer overflow protection**: All arithmetic operations use checked or saturating
//!   semantics where appropriate.
//!
//! - **Thread safety**: All public types are `Send + Sync` where semantically appropriate,
//!   with compile-time verification.
//!
//! # Performance Characteristics
//!
//! ## Time Complexity Guarantees
//!
//! | Operation | Complexity | Notes |
//! |-----------|-----------|-------|
//! | Parse expression | O(n) | Linear in input string length |
//! | Variable lookup | O(1) | HashMap-based symbol table |
//! | Coordinate transform | O(1) | Fixed number of trig operations |
//! | Linear solve | O(1) | Constant number of operations |
//! | Quadratic solve | O(1) | Discriminant calculation + sqrt |
//! | Polynomial solve (degree d) | O(d²) | Companion matrix method |
//! | Numerical solve (Newton) | O(k) | k iterations to convergence |
//!
//! ## Space Complexity
//!
//! - **AST storage**: O(n) where n is the number of expression nodes
//! - **Resolution path**: O(k) where k is the number of solution steps
//! - **Parser stack**: O(d) where d is maximum nesting depth
//!
//! ## Optimization Features
//!
//! Build configuration in release mode enables:
//!
//! - **Link-Time Optimization (LTO)**: Cross-module inlining and dead code elimination
//! - **Single codegen unit**: Maximum optimization at cost of compile time
//! - **opt-level=3**: Aggressive compiler optimizations
//! - **Zero-cost abstractions**: Generic functions specialized at compile time
//! - **SIMD auto-vectorization**: Compiler-generated vectorized code where applicable
//!
//! ```toml
//! [profile.release]
//! opt-level = 3
//! lto = true
//! codegen-units = 1
//! ```
//!
//! Benchmark your performance-critical paths with:
//!
//! ```bash
//! cargo bench
//! ```
//!
//! # Platform Support
//!
//! ## Tier 1: Fully Supported
//!
//! - **iOS Devices** (`aarch64-apple-ios`): Native ARM64 execution on iPhone/iPad
//! - **iOS Simulator on ARM** (`aarch64-apple-ios-sim`): M1/M2/M3 Mac simulator
//! - **iOS Simulator on Intel** (`x86_64-apple-ios`): Intel Mac simulator
//!
//! Build for all iOS targets:
//!
//! ```bash
//! # Add targets (one-time setup)
//! rustup target add aarch64-apple-ios
//! rustup target add aarch64-apple-ios-sim
//! rustup target add x86_64-apple-ios
//!
//! # Build for device
//! cargo build --release --target aarch64-apple-ios
//!
//! # Build for simulator (ARM)
//! cargo build --release --target aarch64-apple-ios-sim
//!
//! # Build universal library with lipo
//! lipo -create \
//!   target/aarch64-apple-ios-sim/release/libthales.a \
//!   target/x86_64-apple-ios/release/libthales.a \
//!   -output libthales_universal.a
//! ```
//!
//! ## Tier 2: Standard Rust Targets
//!
//! The library uses only stable Rust features and should compile on any tier 1 Rust platform:
//! - Linux (x86_64, aarch64)
//! - macOS (x86_64, aarch64)
//! - Windows (x86_64)
//!
//! ## FFI Integration
//!
//! Enable Swift bindings with the `ffi` feature:
//!
//! ```bash
//! cargo build --release --features ffi --target aarch64-apple-ios
//! ```
//!
//! This generates Swift bridge code and C headers for Xcode integration.
//!
//! ## Version History
//!
//! **Current: v0.3.3** - CI/CD Infrastructure & Release Fixes
//!
//! - Release workflow: `--allow-dirty` flag for cargo publish
//! - Swift XCFramework workflow: permissions for release uploads
//!
//! **v0.3.2** - CI/CD Infrastructure & Code Quality
//!
//! - GitHub Actions CI workflow with build status badge
//! - Automated release workflow for crates.io publishing
//! - Swift XCFramework build workflow for iOS/macOS distribution
//! - Swift Package Index configuration for DocC documentation hosting
//! - Compiler warning fixes and cargo fmt formatting cleanup
//!
//! **v0.3.1** - Documentation Fix
//!
//! - Corrected "LaTeX rendering" terminology to "LaTeX generation/output"
//!
//! **v0.3.0** - Advanced Calculus & API Stabilization
//!
//! - Second-order ODEs with characteristic equation method
//! - Nonlinear system solver (Newton-Raphson for systems)
//! - Taylor, Maclaurin, Laurent series expansions
//! - Asymptotic expansions with Big-O notation
//! - Special functions (gamma, beta, erf, erfc)
//! - Small angle approximations with error bounds
//! - Unified [`ThalesError`] type
//! - 1152 tests including property-based tests
//!
//! See [CHANGELOG.md](https://github.com/ChrisGVE/thales/blob/main/CHANGELOG.md)
//! for complete version history.
//!
//! ## Module Reference
//!
//! | Module | Description |
//! |--------|-------------|
//! | [`ast`] | Abstract syntax tree types for expressions and equations |
//! | [`parser`] | String → AST conversion with mathlex |
//! | [`solver`] | Symbolic equation solving |
//! | [`equation_system`] | Multi-equation system solver |
//! | [`numerical`] | Numerical root-finding methods |
//! | [`integration`] | Symbolic integration |
//! | [`limits`] | Limit evaluation with L'Hôpital's rule |
//! | [`ode`] | First and second-order ODE solving |
//! | [`series`] | Taylor, Laurent, asymptotic series |
//! | [`special`] | Gamma, beta, error functions |
//! | [`approximations`] | Small angle and scaled approximations |
//! | [`transforms`] | Coordinate system conversions |
//! | [`dimensions`] | Units and dimensional analysis |
//! | [`pattern`] | Rule-based expression rewriting |
//! | [`latex`] | LaTeX parsing and generation |
//! | [`resolution_path`] | Step-by-step solution tracking |
//! | [`guides`] | User tutorials and workflows |
//!
//! ## Running Tests
//!
//! ```bash
//! cargo test              # All tests
//! cargo test --doc        # Documentation examples only
//! cargo test --release    # Optimized build
//! ```

#![warn(missing_docs)]
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]

// Public module exports
pub mod approximations;
pub mod ast;
pub mod dimensions;
pub mod equation_system;
pub mod fourier;
pub mod inequality;
pub mod integration;
pub mod latex;
pub mod limits;
pub mod mathlex_bridge;
pub mod matrix;
pub mod numerical;
pub mod ode;
pub mod ode_higher;
pub mod optimization;
pub mod parser;
pub mod partial_fractions;
pub mod pattern;
pub mod precision;
pub mod resolution_path;
pub mod runge_kutta;
pub mod series;
pub mod simplification_rules;
pub mod solver;
pub mod special;
pub mod transforms;
pub mod trigonometric;

// User guides for common workflows
pub mod guides;

// LAPACK-accelerated linear algebra (conditionally compiled)
#[cfg(feature = "lapack")]
pub mod lapack;

// FFI module (conditionally compiled for FFI builds)
#[cfg(feature = "ffi")]
pub mod ffi;

// Re-export commonly used types at crate root for convenience
pub use approximations::{
    apply_small_angle_approx, compute_approximation_error, generate_approximation_step,
    is_approximation_valid, optimize_pythagorean, select_exp_scaling, ApproxResult, ApproxType,
    ScaledExpForm,
};
pub use ast::{BinaryOp, Equation, Expression, Function, UnaryOp, Variable};
pub use dimensions::{Dimension, Quantity, Unit, UnitRegistry};
pub use equation_system::{
    broyden_system,
    fixed_point_system,
    newton_raphson_system,
    residual_norm,
    solve_linear_system_lu,
    validate_jacobian,
    BroydenSolver,
    Constraint,
    ConvergenceBehavior,
    ConvergenceDiagnostics,
    DependencyGraph,
    EquationSystem,
    EquationType,
    FixedPointSolver,
    IntegralInfo,
    MultiEquationSolution,
    MultiEquationSolver,
    NamedEquation,
    NewtonRaphsonSolver,
    // Nonlinear system solver
    NonlinearSystem,
    NonlinearSystemConfig,
    NonlinearSystemSolver,
    NonlinearSystemSolverError,
    NonlinearSystemSolverResult,
    ODEInfo,
    SmartNonlinearSystemSolver,
    SolutionStrategy,
    SolutionValue,
    SolveMethod,
    SolveStep,
    SolverConfig,
    StepResult,
    SystemContext,
    SystemError,
    SystemOperation,
    SystemResolutionPath,
    SystemStep,
};
pub use fourier::{fourier_series, FourierSeries, FourierSeriesError, FourierSeriesResult};
pub use inequality::{
    solve_inequality, solve_system, Bound, Inequality, InequalityError, IntervalSolution,
};
pub use integration::{
    definite_integral, definite_integral_with_fallback, definite_integral_with_steps,
    improper_integral_to_infinity, integrate, integrate_by_parts, integrate_by_parts_with_steps,
    integrate_by_substitution, integrate_with_substitution, numerical_integrate,
    tabular_integration, IntegrationError,
};
pub use latex::{parse_latex, parse_latex_equation};
pub use matrix::{BracketStyle, MatrixError, MatrixExpr};
pub use numerical::{
    BisectionMethod, BrentsMethod, NewtonRaphson, NumericalConfig, NumericalError, NumericalResult,
    NumericalSolution, SmartNumericalSolver,
};
pub use ode::{
    solve_characteristic_equation, solve_ivp, solve_linear, solve_second_order_homogeneous,
    solve_second_order_ivp, solve_separable, CharacteristicRoots, FirstOrderODE, ODEError,
    ODESolution, RootType, SecondOrderODE, SecondOrderSolution,
};
pub use optimization::{
    analyze_expression, find_multiplicative_chains, optimize_computation_order, to_manual_steps,
    track_precision, ComputationStep, ManualStep, MultiplicativeChain, OperationConfig,
    OperationType, PrecisionReport, StepOperand,
};
pub use parser::{parse_equation, parse_equation_system, parse_expression};
pub use partial_fractions::{
    decompose, is_polynomial, is_rational_function, DecomposeError, PartialFractionResult,
    PartialFractionTerm,
};
pub use precision::{EvalContext, EvalError, PrecisionMode, RoundingMode, Value};
pub use resolution_path::{
    Operation, OperationCounts, PathStatistics, ResolutionPath, ResolutionPathBuilder,
    ResolutionStep, Verbosity,
};
pub use series::{
    arctan_series,
    asymptotic,
    binomial_series,
    // Series arithmetic (composition and reversion)
    compose_series,
    compute_nth_derivative,
    cos_series,
    evaluate_at,
    exp_series,
    factorial,
    factorial_expr,
    find_singularities,
    laurent,
    limit_via_asymptotic,
    ln_1_plus_x_series,
    maclaurin,
    pole_order,
    residue,
    reversion,
    sin_series,
    taylor,
    // Asymptotic expansions
    AsymptoticDirection,
    AsymptoticSeries,
    AsymptoticTerm,
    BigO,
    // Laurent series support
    LaurentSeries,
    RemainderTerm,
    Series,
    SeriesError,
    SeriesResult,
    SeriesTerm,
    Singularity,
    SingularityType,
};
pub use solver::{LinearSystem, SmartSolver, Solution, Solver, SystemSolution, SystemSolver};
pub use special::{beta, erf, erfc, gamma, SpecialFunctionError, SpecialFunctionResult};
pub use transforms::{
    Cartesian2D, Cartesian3D, ComplexOps, Cylindrical, Polar, Spherical, Transform2D,
};
pub use trigonometric::{
    all_trig_rules, double_angle_rules, inverse_rules, parity_rules, product_to_sum_rules,
    pythagorean_rules, quotient_rules, simplify_double_angle, simplify_pythagorean,
    simplify_quotient, simplify_trig, simplify_trig_step, simplify_trig_with_steps,
    special_value_rules,
};

/// Library version information.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Library name.
pub const NAME: &str = env!("CARGO_PKG_NAME");

/// Get library version string.
pub fn version() -> &'static str {
    VERSION
}

/// Check if library is compiled with FFI support.
pub fn has_ffi_support() -> bool {
    cfg!(feature = "ffi")
}

/// Unified error type for the thales library.
///
/// This enum provides a single error type that encompasses all possible errors
/// that can occur within the library. It wraps error types from individual modules,
/// allowing for consistent error handling across the entire library.
///
/// # Design
///
/// The `#[non_exhaustive]` attribute allows future versions to add new error variants
/// without breaking existing code. Users should always include a wildcard match arm
/// when matching on this type.
///
/// # Examples
///
/// ```rust
/// use thales::{ThalesError, parse_expression};
///
/// match parse_expression("2 + x") {
///     Ok(expr) => println!("Parsed: {:?}", expr),
///     Err(errors) => {
///         // parse_expression returns Vec<ParseError>, not ThalesError
///         println!("Parse errors: {:?}", errors);
///     }
/// }
/// ```
///
/// Future usage with unified error handling:
///
/// ```rust,ignore
/// use thales::ThalesError;
///
/// fn process() -> Result<(), ThalesError> {
///     // Future API will return ThalesError
///     Ok(())
/// }
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub enum ThalesError {
    /// Error from the parser module.
    Parse(parser::ParseError),
    /// Error from the solver module.
    Solver(solver::SolverError),
    /// Error from the series module.
    Series(series::SeriesError),
    /// Error from the matrix module.
    Matrix(matrix::MatrixError),
    /// Error from the integration module.
    Integration(integration::IntegrationError),
    /// Error from the numerical module.
    Numerical(numerical::NumericalError),
    /// Error from the limits module.
    Limits(limits::LimitError),
    /// Error from the ODE module.
    ODE(ode::ODEError),
    /// Error from the special functions module.
    SpecialFunction(special::SpecialFunctionError),
    /// Error from the inequality module.
    Inequality(inequality::InequalityError),
    /// Error from the precision module.
    Evaluation(precision::EvalError),
    /// Error from the partial fractions module.
    PartialFractions(partial_fractions::DecomposeError),
    /// Error from the LaTeX parser module.
    LaTeXParse(latex::LaTeXParseError),
    /// Error from the equation system module.
    System(equation_system::SystemError),
    /// Error from the nonlinear system solver.
    NonlinearSystem(equation_system::NonlinearSystemSolverError),
}

impl std::fmt::Display for ThalesError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ThalesError::Parse(e) => write!(f, "Parse error: {}", e),
            ThalesError::Solver(e) => write!(f, "Solver error: {:?}", e),
            ThalesError::Series(e) => write!(f, "Series error: {}", e),
            ThalesError::Matrix(e) => write!(f, "Matrix error: {}", e),
            ThalesError::Integration(e) => write!(f, "Integration error: {}", e),
            ThalesError::Numerical(e) => write!(f, "Numerical error: {:?}", e),
            ThalesError::Limits(e) => write!(f, "Limits error: {}", e),
            ThalesError::ODE(e) => write!(f, "ODE error: {}", e),
            ThalesError::SpecialFunction(e) => write!(f, "Special function error: {}", e),
            ThalesError::Inequality(e) => write!(f, "Inequality error: {}", e),
            ThalesError::Evaluation(e) => write!(f, "Evaluation error: {}", e),
            ThalesError::PartialFractions(e) => write!(f, "Partial fractions error: {}", e),
            ThalesError::LaTeXParse(e) => write!(f, "LaTeX parse error: {}", e),
            ThalesError::System(e) => write!(f, "System error: {:?}", e),
            ThalesError::NonlinearSystem(e) => write!(f, "Nonlinear system error: {:?}", e),
        }
    }
}

impl std::error::Error for ThalesError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ThalesError::Parse(e) => Some(e),
            ThalesError::Series(e) => Some(e),
            ThalesError::Matrix(e) => Some(e),
            ThalesError::Integration(e) => Some(e),
            ThalesError::Limits(e) => Some(e),
            ThalesError::ODE(e) => Some(e),
            ThalesError::SpecialFunction(e) => Some(e),
            ThalesError::Inequality(e) => Some(e),
            ThalesError::Evaluation(e) => Some(e),
            ThalesError::PartialFractions(e) => Some(e),
            ThalesError::LaTeXParse(e) => Some(e),
            // These error types don't implement std::error::Error
            ThalesError::Solver(_) => None,
            ThalesError::Numerical(_) => None,
            ThalesError::System(_) => None,
            ThalesError::NonlinearSystem(_) => None,
        }
    }
}

// Implement From conversions for each error type
impl From<parser::ParseError> for ThalesError {
    fn from(e: parser::ParseError) -> Self {
        ThalesError::Parse(e)
    }
}

impl From<solver::SolverError> for ThalesError {
    fn from(e: solver::SolverError) -> Self {
        ThalesError::Solver(e)
    }
}

impl From<series::SeriesError> for ThalesError {
    fn from(e: series::SeriesError) -> Self {
        ThalesError::Series(e)
    }
}

impl From<matrix::MatrixError> for ThalesError {
    fn from(e: matrix::MatrixError) -> Self {
        ThalesError::Matrix(e)
    }
}

impl From<integration::IntegrationError> for ThalesError {
    fn from(e: integration::IntegrationError) -> Self {
        ThalesError::Integration(e)
    }
}

impl From<numerical::NumericalError> for ThalesError {
    fn from(e: numerical::NumericalError) -> Self {
        ThalesError::Numerical(e)
    }
}

impl From<limits::LimitError> for ThalesError {
    fn from(e: limits::LimitError) -> Self {
        ThalesError::Limits(e)
    }
}

impl From<ode::ODEError> for ThalesError {
    fn from(e: ode::ODEError) -> Self {
        ThalesError::ODE(e)
    }
}

impl From<special::SpecialFunctionError> for ThalesError {
    fn from(e: special::SpecialFunctionError) -> Self {
        ThalesError::SpecialFunction(e)
    }
}

impl From<inequality::InequalityError> for ThalesError {
    fn from(e: inequality::InequalityError) -> Self {
        ThalesError::Inequality(e)
    }
}

impl From<precision::EvalError> for ThalesError {
    fn from(e: precision::EvalError) -> Self {
        ThalesError::Evaluation(e)
    }
}

impl From<partial_fractions::DecomposeError> for ThalesError {
    fn from(e: partial_fractions::DecomposeError) -> Self {
        ThalesError::PartialFractions(e)
    }
}

impl From<latex::LaTeXParseError> for ThalesError {
    fn from(e: latex::LaTeXParseError) -> Self {
        ThalesError::LaTeXParse(e)
    }
}

impl From<equation_system::SystemError> for ThalesError {
    fn from(e: equation_system::SystemError) -> Self {
        ThalesError::System(e)
    }
}

impl From<equation_system::NonlinearSystemSolverError> for ThalesError {
    fn from(e: equation_system::NonlinearSystemSolverError) -> Self {
        ThalesError::NonlinearSystem(e)
    }
}

// TODO: Add prelude module with commonly used imports
// TODO: Add error types module with unified error handling
// TODO: Add traits module with common trait definitions
// TODO: Add macro module for expression DSL
// TODO: Add serde support for serialization
// TODO: Add wasm support for web usage
// TODO: Add Python bindings via PyO3
// TODO: Add comprehensive integration tests
// TODO: Add performance benchmarks
// TODO: Add documentation examples that compile and run

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thales_error_from_parse_error() {
        let parse_err = parser::ParseError::UnexpectedCharacter { pos: 0, found: 'x' };
        let thales_err: ThalesError = parse_err.clone().into();

        match thales_err {
            ThalesError::Parse(e) => assert_eq!(e, parse_err),
            _ => panic!("Expected ThalesError::Parse"),
        }
    }

    #[test]
    fn test_thales_error_from_solver_error() {
        let solver_err = solver::SolverError::NoSolution;
        let thales_err: ThalesError = solver_err.clone().into();

        match thales_err {
            ThalesError::Solver(e) => assert_eq!(e, solver_err),
            _ => panic!("Expected ThalesError::Solver"),
        }
    }

    #[test]
    fn test_thales_error_from_numerical_error() {
        let num_err = numerical::NumericalError::NoConvergence;
        let thales_err: ThalesError = num_err.clone().into();

        match thales_err {
            ThalesError::Numerical(e) => assert_eq!(e, num_err),
            _ => panic!("Expected ThalesError::Numerical"),
        }
    }

    #[test]
    fn test_thales_error_display() {
        let solver_err = solver::SolverError::NoSolution;
        let thales_err: ThalesError = solver_err.into();
        let display_str = format!("{}", thales_err);

        assert!(display_str.contains("Solver error"));
        assert!(display_str.contains("NoSolution"));
    }

    #[test]
    fn test_thales_error_source() {
        use std::error::Error;

        let parse_err = parser::ParseError::UnexpectedCharacter { pos: 5, found: '!' };
        let thales_err: ThalesError = parse_err.into();

        assert!(thales_err.source().is_some());
    }

    #[test]
    fn test_thales_error_from_integration_error() {
        let int_err = integration::IntegrationError::DivisionByZero;
        let thales_err: ThalesError = int_err.clone().into();

        match thales_err {
            ThalesError::Integration(e) => assert_eq!(e, int_err),
            _ => panic!("Expected ThalesError::Integration"),
        }
    }

    #[test]
    fn test_thales_error_from_matrix_error() {
        let matrix_err = matrix::MatrixError::EmptyMatrix;
        let thales_err: ThalesError = matrix_err.clone().into();

        match thales_err {
            ThalesError::Matrix(e) => assert_eq!(e, matrix_err),
            _ => panic!("Expected ThalesError::Matrix"),
        }
    }
}