1use std::f64::consts;
2use std::fmt;
3use std::fmt::{Debug, Formatter};
4use serde::{Serialize, Deserialize};
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub enum Constant {
8 C1,
9 C2,
10 C3,
11 C10,
12 C100,
13 C1000,
14 CNeg1,
15 Pi,
16 E,
17 Epsilon,
18}
19impl Constant {
20 pub fn value(&self) -> f64 {
21 match self {
22 Constant::C1 => 1_f64,
23 Constant::C2 => 2_f64,
24 Constant::C3 => 3_f64,
25 Constant::C10 => 10_f64,
26 Constant::C100 => 100_f64,
27 Constant::C1000 => 1000_f64,
28 Constant::CNeg1 => -1_f64,
29 Constant::Pi => consts::PI,
30 Constant::E => consts::E,
31 Constant::Epsilon => f64::EPSILON,
32 }
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37pub enum Argument {
38 Arg(u32)
39}
40
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub enum Modifier {
43 Sqr,
44 Pow3,
45 Sqrt,
46 Log2,
47 Log10,
48 Sin,
49 Cos,
50 Tanh,
51 Sigmoid,
52}
53impl Modifier {
54 pub fn compute(&self, x: f64) -> f64 {
55 match self {
56 Modifier::Sqr => x.powi(2),
57 Modifier::Pow3 => x.powi(3),
58 Modifier::Sqrt => x.sqrt(),
59 Modifier::Log2 => x.log2(),
60 Modifier::Log10 => x.log10(),
61 Modifier::Sin => x.sin(),
62 Modifier::Cos => x.cos(),
63 Modifier::Tanh => x.tanh(),
64 Modifier::Sigmoid => {
65 1_f64 / (1_f64 + consts::E.powf(-x))
66 }
67 }
68 }
69}
70
71#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72pub enum Operator {
73 Plus,
74 Minus,
75 Multiply,
76 Divide,
77 Pow,
78 Root,
79 Log,
80}
81impl Operator {
82 pub fn compute(&self, x: f64, y: f64) -> f64 {
83 match self {
84 Operator::Plus => x + y,
85 Operator::Minus => x - y,
86 Operator::Multiply => x * y,
87 Operator::Divide => x / y,
88 Operator::Pow => x.powf(y),
89 Operator::Root => x.powf(1_f64/y),
90 Operator::Log => x.log(y),
91 }
92 }
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96pub enum PrimitiveOperation {
97 Constant(Constant),
98 Argument(Argument),
99 Modifier(Modifier),
100 Operator(Operator),
101}
102
103impl fmt::Display for PrimitiveOperation {
104 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
105 match self {
106 PrimitiveOperation::Constant(c) => write!(f, "{:?}", c),
107 PrimitiveOperation::Argument(a) => write!(f, "{:?}", a),
108 PrimitiveOperation::Modifier(m) => write!(f, "{:?}", m),
109 PrimitiveOperation::Operator(o) => write!(f, "{:?}", o),
110 }
111 }
112}