ruut_functions/
lib.rs

1#![deny(missing_docs)]
2//! Crate for creating math functions from string and perform symbolic derivation
3use parser::{builder::build, to_rpn, ParsingError};
4use simp::simp_node;
5
6mod derivation;
7mod display;
8mod eval;
9mod integration;
10pub use crate::eval::{eval_vec_f1d, eval_vec_f2d, eval_vec_f3d};
11mod macros;
12mod ops;
13mod param;
14mod parser;
15mod simp;
16mod traits;
17
18#[derive(Debug, PartialEq)]
19/// Representation of a 1D function
20pub struct F1D(Func);
21#[derive(Debug, PartialEq)]
22/// Representation of a 2D function
23pub struct F2D(Func);
24#[derive(Debug, PartialEq)]
25/// Representation of a 3D function
26pub struct F3D(Func);
27
28impl F1D {
29    /// Creates a new function from a str
30    pub fn new(input: &str) -> Result<Self, ParsingError> {
31        let mut func = build(to_rpn(input, &['x'])?);
32        simp_node(&mut func);
33        Ok(F1D(func))
34    }
35    /// Returns a string in latex format
36    pub fn latex(&self) -> String {
37        self.0.latex()
38    }
39}
40impl F2D {
41    /// Creates a new function from a str
42    pub fn new(input: &str) -> Result<Self, ParsingError> {
43        let mut func = build(to_rpn(input, &['x', 'y'])?);
44        simp_node(&mut func);
45        Ok(F2D(func))
46    }
47
48    /// Returns a string in latex format
49    pub fn latex(&self) -> String {
50        self.0.latex()
51    }
52}
53
54impl F3D {
55    /// Creates a new funcction from a str
56    pub fn new(input: &str) -> Result<Self, ParsingError> {
57        let mut func = build(to_rpn(input, &['x', 'y', 'z'])?);
58        simp_node(&mut func);
59        Ok(F3D(func))
60    }
61    /// Returns a string in latex format
62    pub fn latex(&self) -> String {
63        self.0.latex()
64    }
65}
66
67impl FND {
68    /// Creates a new function from a string
69    pub fn new(input: &str, vars: &[char]) -> Result<Self, ParsingError> {
70        let mut func = build(to_rpn(input, vars)?);
71        simp_node(&mut func);
72        Ok(FND {
73            vars: vars.to_vec(),
74            func,
75        })
76    }
77}
78
79#[derive(Debug, PartialEq)]
80/// Representation of an n-dimensional function
81pub struct FND {
82    vars: Vec<char>,
83    func: Func,
84}
85
86#[derive(Debug, PartialEq, Clone)]
87pub(crate) enum Func {
88    Var(char),
89    E,
90    PI,
91    Num(i32),
92    Param(String, f64),
93    Add(Vec<Self>),
94    Mul(Vec<Self>),
95    Pow(Box<Self>, Box<Self>),
96    S(FType, Box<Self>),
97}
98
99#[derive(Debug, PartialEq, Clone)]
100pub(crate) enum FType {
101    Sin,
102    Cos,
103    Tan,
104    Cot,
105    Sec,
106    Csc,
107    ASin,
108    ACos,
109    ATan,
110    Sinh,
111    Cosh,
112    Tanh,
113    Coth,
114    Sech,
115    Csch,
116    ASinh,
117    ACosh,
118    ATanh,
119    Abs,
120    Ln,
121}
122
123pub(crate) fn gcd(mut n: u32, mut m: u32) -> u32 {
124    assert!(n != 0 && m != 0);
125    if n == 0 {
126        std::mem::swap(&mut n, &mut m);
127    }
128    while m != 0 {
129        if m < n {
130            std::mem::swap(&mut m, &mut n);
131        }
132        m %= n;
133    }
134    n
135}