rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
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
//! # Numerical Elementary Operations
//!
//! This module provides numerical evaluation and implementations of elementary
//! mathematical functions. It serves as the bridge between symbolic expressions
//! and their numerical values.
//!
//! ## Core Functionalities
//!
//! - **Expression Evaluation**: Recursively computes the numerical value of any
//!   symbolic `Expr` given a set of variable assignments.
//! - **Domain Validation**: Rigorously checks for mathematical domain violations
//!   (e.g., division by zero, logarithm of non-positive numbers).
//! - **Numerical Stability**: Handles edge cases like infinite values and NaN
//!   to ensure robust calculations.
//! - **Pure Numerical Functions**: High-performance wrappers around standard
//!   library functions for direct use on `f64`.
//!
//! ## Mathematical Formulas
//!
//! The evaluation follows standard mathematical definitions:
//!
//! - **Addition**: $f(x, y) = x + y$
//! - **Multiplication**: $f(x, y) = x \cdot y$
//! - **Power**: $f(x, y) = x^y$
//! - **Exponential**: $f(x) = e^x$
//! - **Logarithm**: $f(x) = \ln(x)$
//! - **Trigonometry**: $\sin(x), \cos(x), \tan(x)$, etc.

use std::collections::HashMap;

use num_traits::ToPrimitive;

use crate::symbolic::core::Expr;

/// Evaluates a symbolic expression to a numerical `f64` value.
///
/// This function recursively traverses the expression tree and computes the numerical value.
/// It comprehensively supports arithmetic, trigonometric, hyperbolic, and special constants.
///
/// # Arguments
///
/// * `expr` - The symbolic expression to evaluate.
/// * `vars` - A map containing numerical values for the variables in the expression.
///
/// # Returns
///
/// * `Ok(f64)` - The computed numerical result.
///
/// # Errors
///
/// Returns an error if:
/// - A variable in the expression is not provided in the `vars` map.
/// - A domain violation occurs (e.g., division by zero, log of non-positive number).
/// - An unsupported expression variant is encountered.
///
/// # Examples
///
/// ```rust
/// use std::collections::HashMap;
///
/// use rssn::numerical::elementary::eval_expr;
/// use rssn::symbolic::core::Expr;
///
/// let x = Expr::new_variable("x");
///
/// let expr = Expr::new_add(x, Expr::new_constant(2.0));
///
/// let mut vars = HashMap::new();
///
/// vars.insert("x".to_string(), 3.0);
///
/// let result = eval_expr(&expr, &vars).unwrap();
///
/// assert_eq!(result, 5.0);
/// ```
pub fn eval_expr<S: ::std::hash::BuildHasher>(
    expr: &Expr,
    vars: &HashMap<String, f64, S>,
) -> Result<f64, String> {
    match expr {
        | Expr::Dag(node) => {
            let converted_expr = node
                .to_expr()
                .map_err(|e| format!("Invalid DAG node: {e}"))?;

            eval_expr(&converted_expr, vars)
        },
        | Expr::Constant(c) => Ok(*c),
        | Expr::BigInt(i) => {
            i.to_f64()
                .ok_or_else(|| "BigInt overflow during evaluation".to_string())
        },
        | Expr::Rational(r) => {
            r.to_f64()
                .ok_or_else(|| "Rational overflow during evaluation".to_string())
        },
        | Expr::Variable(v) => {
            vars.get(v)
                .copied()
                .ok_or_else(|| format!("Unknown variable: '{v}'"))
        },

        // Arithmetic
        | Expr::Add(a, b) => Ok(eval_expr(a, vars)? + eval_expr(b, vars)?),
        | Expr::AddList(list) => {
            let mut sum = 0.0;

            for e in list {
                sum += eval_expr(e, vars)?;
            }

            Ok(sum)
        },
        | Expr::Sub(a, b) => Ok(eval_expr(a, vars)? - eval_expr(b, vars)?),
        | Expr::Mul(a, b) => Ok(eval_expr(a, vars)? * eval_expr(b, vars)?),
        | Expr::MulList(list) => {
            let mut prod = 1.0;

            for e in list {
                prod *= eval_expr(e, vars)?;
            }

            Ok(prod)
        },
        | Expr::Div(a, b) => {
            let den = eval_expr(b, vars)?;

            if den == 0.0 {
                return Err("Division by zero".to_string());
            }

            Ok(eval_expr(a, vars)? / den)
        },
        | Expr::Neg(a) => Ok(-eval_expr(a, vars)?),
        | Expr::Power(b, e) => {
            let base = eval_expr(b, vars)?;

            let exp = eval_expr(e, vars)?;

            if base == 0.0 && exp < 0.0 {
                return Err("Undefined operation: 0^negative power".to_string());
            }

            if base < 0.0 && exp.fract() != 0.0 {
                return Err("Complex result: negative base raised to non-integer power".to_string());
            }

            Ok(base.powf(exp))
        },
        | Expr::Abs(a) => Ok(eval_expr(a, vars)?.abs()),
        | Expr::Sqrt(a) => {
            let val = eval_expr(a, vars)?;

            if val < 0.0 {
                return Err("Square root of negative number".to_string());
            }

            Ok(val.sqrt())
        },

        // Trigonometric
        | Expr::Sin(a) => Ok(eval_expr(a, vars)?.sin()),
        | Expr::Cos(a) => Ok(eval_expr(a, vars)?.cos()),
        | Expr::Tan(a) => Ok(eval_expr(a, vars)?.tan()),
        | Expr::Sec(a) => Ok(1.0 / eval_expr(a, vars)?.cos()),
        | Expr::Csc(a) => Ok(1.0 / eval_expr(a, vars)?.sin()),
        | Expr::Cot(a) => Ok(1.0 / eval_expr(a, vars)?.tan()),

        // Inverse Trigonometric
        | Expr::ArcSin(a) => {
            let val = eval_expr(a, vars)?;

            if !(-1.0..=1.0).contains(&val) {
                return Err("Inverse sine argument out of domain [-1, 1]".to_string());
            }

            Ok(val.asin())
        },
        | Expr::ArcCos(a) => {
            let val = eval_expr(a, vars)?;

            if !(-1.0..=1.0).contains(&val) {
                return Err("Inverse cosine argument out of domain [-1, 1]".to_string());
            }

            Ok(val.acos())
        },
        | Expr::ArcTan(a) => Ok(eval_expr(a, vars)?.atan()),
        | Expr::Atan2(y, x) => Ok(eval_expr(y, vars)?.atan2(eval_expr(x, vars)?)),
        | Expr::ArcSec(a) => {
            let val = eval_expr(a, vars)?;

            if val.abs() < 1.0 {
                return Err(
                    "Inverse secant argument out of domain (-inf, -1] U [1, inf)".to_string(),
                );
            }

            Ok((1.0 / val).acos())
        },
        | Expr::ArcCsc(a) => {
            let val = eval_expr(a, vars)?;

            if val.abs() < 1.0 {
                return Err(
                    "Inverse cosecant argument out of domain (-inf, -1] U [1, inf)".to_string(),
                );
            }

            Ok((1.0 / val).asin())
        },
        | Expr::ArcCot(a) => Ok((1.0 / eval_expr(a, vars)?).atan()),

        // Hyperbolic
        | Expr::Sinh(a) => Ok(eval_expr(a, vars)?.sinh()),
        | Expr::Cosh(a) => Ok(eval_expr(a, vars)?.cosh()),
        | Expr::Tanh(a) => Ok(eval_expr(a, vars)?.tanh()),
        | Expr::Sech(a) => Ok(1.0 / eval_expr(a, vars)?.cosh()),
        | Expr::Csch(a) => Ok(1.0 / eval_expr(a, vars)?.sinh()),
        | Expr::Coth(a) => Ok(1.0 / eval_expr(a, vars)?.tanh()),

        // Inverse Hyperbolic
        | Expr::ArcSinh(a) => Ok(eval_expr(a, vars)?.asinh()),
        | Expr::ArcCosh(a) => {
            let val = eval_expr(a, vars)?;

            if val < 1.0 {
                return Err("Inverse hyperbolic cosine argument < 1".to_string());
            }

            Ok(val.acosh())
        },
        | Expr::ArcTanh(a) => {
            let val = eval_expr(a, vars)?;

            if val <= -1.0 || val >= 1.0 {
                return Err("Inverse hyperbolic tangent argument out of domain (-1, 1)".to_string());
            }

            Ok(val.atanh())
        },

        // Exponential and Logarithmic
        | Expr::Exp(a) => Ok(eval_expr(a, vars)?.exp()),
        | Expr::Log(a) => {
            let val = eval_expr(a, vars)?;

            if val <= 0.0 {
                return Err("Logarithm of non-positive number".to_string());
            }

            Ok(val.ln())
        },
        | Expr::LogBase(b, a) => {
            let base = eval_expr(b, vars)?;

            let val = eval_expr(a, vars)?;

            if base <= 0.0 || (base - 1.0).abs() < f64::EPSILON {
                return Err("Invalid logarithm base".to_string());
            }

            if val <= 0.0 {
                return Err("Logarithm of non-positive number".to_string());
            }

            Ok(val.log(base))
        },

        // Constants
        | Expr::Pi => Ok(std::f64::consts::PI),
        | Expr::E => Ok(std::f64::consts::E),
        | Expr::Infinity => Ok(f64::INFINITY),
        | Expr::NegativeInfinity => Ok(f64::NEG_INFINITY),

        // Rounding
        | Expr::Floor(a) => Ok(eval_expr(a, vars)?.floor()),

        // Comparison/Selection
        | Expr::Max(a, b) => Ok(eval_expr(a, vars)?.max(eval_expr(b, vars)?)),

        // Fallback or Unimplemented
        | _ => Err(format!("Numerical evaluation of {expr:?} is not supported")),
    }
}

/// Evaluates an expression with a single variable `x`.
///
/// # Errors
///
/// Returns an error if the expression evaluation fails.
pub fn eval_expr_single(
    expr: &Expr,
    x_name: &str,
    x_val: f64,
) -> Result<f64, String> {
    let mut vars = HashMap::new();

    vars.insert(x_name.to_string(), x_val);

    eval_expr(expr, &vars)
}

/// # Pure Numerical Elementary Functions
///
/// Direct `f64` implementations of elementary functions for high-performance use.
pub mod pure {

    /// Sine function.
    #[must_use]
    pub fn sin(x: f64) -> f64 {
        x.sin()
    }

    /// Cosine function.
    #[must_use]
    pub fn cos(x: f64) -> f64 {
        x.cos()
    }

    /// Tangent function.
    #[must_use]
    pub fn tan(x: f64) -> f64 {
        x.tan()
    }

    /// Inverse sine.
    #[must_use]
    pub fn asin(x: f64) -> f64 {
        x.asin()
    }

    /// Inverse cosine.
    #[must_use]
    pub fn acos(x: f64) -> f64 {
        x.acos()
    }

    /// Inverse tangent.
    #[must_use]
    pub fn atan(x: f64) -> f64 {
        x.atan()
    }

    /// Two-argument inverse tangent.
    #[must_use]
    pub fn atan2(
        y: f64,
        x: f64,
    ) -> f64 {
        y.atan2(x)
    }

    /// Hyperbolic sine.
    #[must_use]
    pub fn sinh(x: f64) -> f64 {
        x.sinh()
    }

    /// Hyperbolic cosine.
    #[must_use]
    pub fn cosh(x: f64) -> f64 {
        x.cosh()
    }

    /// Hyperbolic tangent.
    #[must_use]
    pub fn tanh(x: f64) -> f64 {
        x.tanh()
    }

    /// Inverse hyperbolic sine.
    #[must_use]
    pub fn asinh(x: f64) -> f64 {
        x.asinh()
    }

    /// Inverse hyperbolic cosine.
    #[must_use]
    pub fn acosh(x: f64) -> f64 {
        x.acosh()
    }

    /// Inverse hyperbolic tangent.
    #[must_use]
    pub fn atanh(x: f64) -> f64 {
        x.atanh()
    }

    /// Absolute value.
    #[must_use]
    pub const fn abs(x: f64) -> f64 {
        x.abs()
    }

    /// Square root.
    #[must_use]
    pub fn sqrt(x: f64) -> f64 {
        x.sqrt()
    }

    /// Natural logarithm.
    #[must_use]
    pub fn ln(x: f64) -> f64 {
        x.ln()
    }

    /// Logarithm with base.
    #[must_use]
    pub fn log(
        x: f64,
        base: f64,
    ) -> f64 {
        x.log(base)
    }

    /// Exponential.
    #[must_use]
    pub fn exp(x: f64) -> f64 {
        x.exp()
    }

    /// Power.
    #[must_use]
    pub fn pow(
        base: f64,
        exp: f64,
    ) -> f64 {
        base.powf(exp)
    }

    /// Floor rounding.
    #[must_use]
    pub const fn floor(x: f64) -> f64 {
        x.floor()
    }

    /// Ceil rounding.
    #[must_use]
    pub const fn ceil(x: f64) -> f64 {
        x.ceil()
    }

    /// Round to nearest integer.
    #[must_use]
    pub const fn round(x: f64) -> f64 {
        x.round()
    }

    /// Signum function.
    #[must_use]
    pub const fn signum(x: f64) -> f64 {
        x.signum()
    }
}