feo_math/multivariate/
with_var.rs1use core::fmt;
2use std::ops::{Add, Div, Mul, Neg, Sub};
3#[derive(Copy, Clone)]
6pub struct WithVar<T, C = char>{
7 pub val: T,
8 pub var: C,
9}
10
11impl<T> fmt::Debug for WithVar<T>
12where T: fmt::Debug + Copy {
13 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14 write!(f, "{:?}{}", self.val, self.var)
15 }
16}
17
18impl<T, C> Add for WithVar<T, C>
19where T: Add<T, Output = T> {
20 type Output = Self;
21
22 fn add(self, rhs: Self) -> Self::Output {
23 WithVar{
24 val: self.val + rhs.val,
25 var: self.var,
26 }
27 }
28}
29
30impl<T, C> Sub for WithVar<T, C>
31where T: Sub<T, Output = T> {
32 type Output = Self;
33
34 fn sub(self, rhs: Self) -> Self::Output {
35 WithVar{
36 val: self.val - rhs.val,
37 var: self.var,
38 }
39 }
40}
41impl<T, C> Mul for WithVar<T, C>
42where T: Mul<T, Output = T> {
43 type Output = Self;
44
45 fn mul(self, rhs: Self) -> Self::Output {
46 WithVar{
47 val: self.val * rhs.val,
48 var: self.var,
49 }
50 }
51}
52
53impl<T, C> Div for WithVar<T, C>
54where T: Div<T, Output = T> {
55 type Output = Self;
56
57 fn div(self, rhs: Self) -> Self::Output {
58 WithVar{
59 val: self.val / rhs.val,
60 var: self.var,
61 }
62 }
63}
64impl<T, C> Neg for WithVar<T, C>
65where T: Neg<Output = T> {
66 type Output = Self;
67
68 fn neg(self) -> Self::Output {
69 WithVar{
70 val: -self.val,
71 var: self.var,
72 }
73 }
74}
75
76impl<T, C> Mul<T> for WithVar<T, C>
80where T: Mul<T, Output = T> {
81 type Output = Self;
82
83 fn mul(self, rhs: T) -> Self::Output {
84 WithVar{
85 val: self.val * rhs,
86 var: self.var,
87 }
88 }
89}
90
91impl<T, C> Div<T> for WithVar<T, C>
92where T: Div<T, Output = T> {
93 type Output = Self;
94
95 fn div(self, rhs: T) -> Self::Output {
96 WithVar{
97 val: self.val / rhs,
98 var: self.var,
99 }
100 }
101}