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
//! # RSSN: Rust Symbolic, Scientific and Numerical Computing Library
//!
//! `rssn` is a high-performance, modern library for symbolic mathematics and scientific
//! computing in Rust. It leverages a Directed Acyclic Graph (DAG) based model for
//! efficient, canonical representation of mathematical expressions.
//!
//! ## Key Features
//!
//! - **Efficient DAG-based Expression Model**: Expressions are stored as a Directed Acyclic
//! Graph, ensuring that identical subexpressions are represented by a single node in memory.
//! This provides automatic canonicalization and significant performance gains.
//!
//! - **Advanced Symbolic Algebra**: A powerful Computer Algebra System (CAS) for manipulating
//! expressions. It goes beyond simple simplification by supporting:
//! - Polynomial algebra including **Gröbner basis** computation.
//! - Simplification and normalization of expressions with respect to polynomial side-relations.
//!
//! - **Symbolic Calculus**: Functions for differentiation, integration, limits, and series expansion.
//!
//! - **Numerical Methods**: A rich collection of algorithms for numerical integration, solving
//! differential equations (ODEs and PDEs), optimization, and more.
//!
//! - **Versatile Output**: Render expressions as pretty-printed text, LaTeX, or plots.
//!
//! ## Crate Structure
//!
//! The `rssn` crate is organized into the following main modules:
//!
//! - **`symbolic`**: The core of the CAS. It defines the `Expr` and `DagNode` representations and
//! provides all functionality for symbolic manipulation, including the `simplify_dag` engine
//! and advanced algebraic tools in `cas_foundations` and `grobner`.
//! - **`numerical`**: Contains implementations of various numerical algorithms.
//! - **`physics`**: Implements numerical methods specifically for physics simulations.
//! - **`input`**: Provides tools for parsing and formatting expressions.
//! - **`output`**: Provides tools for formatting and displaying expressions.
//! - **`jit`**: Just-in-time compilation of expressions and commands.
//! - **`compute`**: Provides tools for numerical and symbolic computation commands.
//! - **`plugins`**: Provides tools for loading and managing plugins.
//! - **`nightly`**: Provides tools with nightly features such as with AVX512.
//! - **`prelude`**: Re-exports the most common types and functions for convenient use.
//!
//! ## Example: Simplification with Relations
//!
//! `rssn` can simplify expressions within the context of an algebraic variety. For example,
//! simplifying `x^2` given the side-relation that `x^2 + y^2 - 1 = 0` (the unit circle).
//!
//! ```rust
//! use rssn::symbolic::cas_foundations::simplify_with_relations;
//! use rssn::symbolic::core::Expr;
//! use rssn::symbolic::grobner::MonomialOrder;
//!
//! // Define variables x and y
//! let x = Expr::new_variable("x");
//!
//! let y = Expr::new_variable("y");
//!
//! // Expression to simplify: 2*x^2
//! let two = Expr::new_bigint(2.into());
//!
//! let x_sq = Expr::new_pow(x.clone(), Expr::new_bigint(2.into()));
//!
//! let expr_to_simplify = Expr::new_mul(two, x_sq);
//!
//! // Define the side-relation: x^2 + y^2 - 1 = 0
//! let y_sq = Expr::new_pow(y.clone(), Expr::new_bigint(2.into()));
//!
//! let one = Expr::new_bigint(1.into());
//!
//! let relation = Expr::new_sub(Expr::new_add(x.clone(), y.clone()), one);
//!
//! // Simplify the expression with respect to the relation
//! let simplified_expr = simplify_with_relations(
//! &expr_to_simplify,
//! &[relation],
//! &["x", "y"],
//! MonomialOrder::Lexicographical,
//! );
//!
//! // The result will be 2 - 4y + 2y^2
//! // Note: The exact output format and canonical form may vary between versions.
//! println!("Original expression: {}", expr_to_simplify);
//!
//! println!("Simplified expression: {}", simplified_expr);
//! ```
//!
//! 
//! 
//! This library is in active development. The API may change, and community
//! contributions are welcome.
// =========================================================================
// RUST LINT CONFIGURATION: rssn (Scientific Computing Library) -- version 1
// =========================================================================
//
// -------------------------------------------------------------------------
// LEVEL 1: CRITICAL ERRORS (Deny)
// -------------------------------------------------------------------------
// #![deny(
// Rust Compiler Errors
// dead_code,
// unreachable_code,
// improper_ctypes_definitions,
// future_incompatible,
// nonstandard_style,
// rust_2018_idioms,
// clippy::perf,
// clippy::correctness,
// clippy::suspicious,
// clippy::unwrap_used,
// =========================================================================
// == LINT DISCUSSIONS AND SUPPRESSIONS (Performance vs. Safety) ===========
// =========================================================================
//
// clippy::expect_used:
// We allow `expect()` in specific controlled scenarios where an unrecoverable
// logic error (e.g., failed quantity parsing where input is guaranteed clean)
// is assumed to be a bug, not a runtime failure. For unit testing and quick
// prototyping, this is often preferred over verbose unwrap_or_else.
//
// ENGINEERING ASSUMPTION: Scientific computing environments often operate
// under the assumption that user-facing input validation is handled **up-stream** // (at the API or parsing layer). Internal calculations proceed based on the
// assumption that values (like Quantity components) are valid, non-exceptional
// numbers, allowing us to favor performance in hot spots over repetitive
// internal validity checks.
//
// clippy::indexing_slicing:
// We suppress this globally or per-file because our FVM and grid calculations
// rely on flattened `Vec<f64>` and index arithmetic (e.g., `idx + width`)
// for performance.
//
// DISCUSSION: We all know that in many numerical computing cases, performance
// and security must be balanced. Direct indexing is structurally safe
// in our validated internal loops, and using it allows the compiler to
// perform **Bounds Check Elision (BCE)**, which is critical for performance
// in hot loops. The long-term plan is to refactor these loops to use
// Ghost Cells or clear range iteration to make the safety provable to Clippy
// without suppression.
//
// clippy::arithmetic_side_effects:
// We suppress this because we overload arithmetic operators for custom types
// (`SupportedQuantity`) where the underlying operations (e.g., `uom`'s types)
// are **mathematically pure** (side-effect-free).
//
// PERFORMANCE CONCERN: The lint encourages explicit function calls over
// operator overloading for complex types. However, using idiomatic operator
// overloading (`*`, `/`, `+`, `-`) here keeps the code clean and allows the
// compiler to better inline and optimize these fundamental algebraic steps
// within a complex expression tree, which is vital for the performance and
// readability of scientific calculations. We assert that these operations
// are side-effect-free.
//
// clippy::missing_safety_doc:
// This lint is suppressed due to the project's current instability and the
// high volume of FFI-exported functions (30,000+ lines).
//
// REASON FOR SUPPRESSION: All FFI export functions have been correctly marked
// as `unsafe` (the structural fix). However, manually writing the required
// `/// # Safety` documentation for every single one of these functions is
// an insurmountable task for an early-stage project with limited resources.
//
// TECHNICAL DEBT: This is considered a **critical technical debt item** that
// must be addressed before the project hits its Beta milestone. A tool
// or a specialized automated script will be required to audit and enforce
// detailed safety contracts at that stage. For now, the focus remains on
// functional correctness.
// )]
// -------------------------------------------------------------------------
// LEVEL 2: STYLE WARNINGS (Warn)
// -------------------------------------------------------------------------
// #![warn(
// warnings,
// unsafe_code,
// clippy::all,
// clippy::pedantic,
// clippy::nursery,
// clippy::dbg_macro,
// clippy::todo,
// clippy::implicit_clone,
// clippy::unnecessary_safety_comment,
// clippy::same_item_push
// )]
// -------------------------------------------------------------------------
// LEVEL 3: ALLOW/IGNORABLE (Allow)
// -------------------------------------------------------------------------
// #![allow(
// missing_docs, // Temporary: To be addressed gradually
// clippy::indexing_slicing, // Performance-critical in numerical computations
// clippy::match_same_arms, // Allowed for code clarity in some cases
// clippy::comparison_chain, // Sometimes more readable than alternatives
// clippy::redundant_closure_for_method_calls, // For clarity in some cases
// clippy::if_not_else, // Sometimes the negative condition is clearer
// clippy::single_match_else, // Sometimes clearer than refactoring
// clippy::redundant_else, // Sometimes clearer to explicitly show all branches
// clippy::missing_safety_doc, // FFI functions that need safety docs to be completed
// clippy::single_call_fn, // Sometimes used for clarity or future expansion
// clippy::min_ident_chars, // Mathematical notation often uses short variable names
// clippy::missing_docs_in_private_items, // Private items temporarily missing docs
// clippy::missing_errors_doc, // Documentation to be completed
// clippy::missing_panics_doc, // Documentation to be completed
// clippy::undocumented_unsafe_blocks, // Safety comments to be added
// clippy::doc_markdown, // Technical terms sometimes need backticks
// unused_doc_comments, // To be addressed in documentation improvements
// clippy::float_arithmetic, // Required in numerical computing
// clippy::cast_possible_truncation, // Sometimes necessary in numerical computing
// clippy::cast_precision_loss, // Sometimes necessary in numerical computing
// clippy::cast_sign_loss, // Sometimes necessary in numerical computing
// clippy::suboptimal_flops, // Performance trade-offs in numerical methods
// clippy::manual_midpoint, // Manual implementation for numerical stability
// clippy::non_std_lazy_statics, // For performance in some cases
// clippy::unreadable_literal, // Sometimes mathematical constants need specific formatting
// clippy::manual_let_else, // Not yet stabilized feature
// clippy::manual_map, // Sometimes explicit control flow is clearer
// clippy::option_if_let_else, // Sometimes match is clearer
// clippy::empty_line_after_doc_comments, // Formatting preference for some cases
// clippy::many_single_char_names, // Mathematical notation often uses single letters
// clippy::module_name_repetitions, // Sometimes module names naturally repeat
// clippy::redundant_field_names, // Sometimes clearer to be explicit
// clippy::similar_names, // Sometimes mathematical variables are naturally similar
// clippy::redundant_pub_crate, // For API consistency
// clippy::too_many_lines, // Complex functions that need refactoring over time
// clippy::must_use_candidate, // To be addressed gradually
// clippy::shadow_unrelated, // Sometimes appropriate for local variables
// clippy::use_self, // For consistency in some cases
// clippy::str_to_string, // Sometimes needed for API compatibility
// clippy::uninlined_format_args, // Performance considerations in hot paths
// clippy::collapsible_if,
// clippy::single_match,
// clippy::needless_pass_by_value,
// clippy::needless_pass_by_ref_mut,
// clippy::used_underscore_binding,
// )]
//
// =========================================================================
// RUST LINT CONFIGURATION: rssn (Scientific Computing Library) -- version 2
// =========================================================================
// -------------------------------------------------------------------------
// LEVEL 1: CRITICAL ERRORS (Deny)
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// LEVEL 2: STYLE WARNINGS (Warn)
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// LEVEL 3: ALLOW/IGNORABLE (Allow)
// -------------------------------------------------------------------------
/// Computation engine and task management.
/// System and physical constants.
/// FFI APIs for the 'rssn' Library.
// #[instability::unstable(feature = "experimental")]
// Disabled because it only works on nightly rust
/// FFI blinding and security utilities.
/// Input parsing and handling.
/// Just-In-Time (JIT) compilation for expressions.
/// Features requiring nightly Rust.
/// Numerical computation and optimization.
/// Output formatting and handling.
/// Physics-related functionality.
/// Plugin system for extending functionality.
/// Prelude for common imports.
/// The symbolic computation engine.
/// Unfortunatily, faer and many math libraries are not compatible with kani so we have to wait.
// --- Useful Public Re-exports ---
use Arc;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
/// Checks if an `Arc` has exclusive ownership (strong count is 1).
///
/// This is useful for optimizations where you want to mutate the contained data
/// in-place, avoiding a clone. If this returns `true`, `Arc::get_mut` or
/// `Arc::try_unwrap` will succeed.
///
/// # Arguments
/// * `arc` - A reference to the `Arc` to check for exclusive ownership
///
/// # Returns
/// * `bool` - True if the Arc has exclusive ownership, false otherwise