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
pub use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign};
#[cfg(feature = "decimal")]
use decimal::d128;
use num::Num;
use num_complex::Complex;
pub trait Operator: Copy {
fn operator_token() -> Self;
}
pub trait TwoSidedInverse<O: Operator>: Sized + Inverse<O> {
fn two_sided_inverse(&self) -> Self;
#[inline]
fn two_sided_inverse_mut(&mut self) {
*self = self.two_sided_inverse()
}
}
#[deprecated(note = "This trait will be replaced by the `TwoSidedInverse` trait to avoid some confusions.")]
pub trait Inverse<O: Operator>: Sized {
#[inline]
#[deprecated(note = "This method is likely **not** what you are looking for. Use `.two_sided_inverse` if you really want the inverse from abstract algebra. \
If you are calling this method on a matrix from the nalgebra crate, you are probably searching for `.try_inverse` instead.")]
fn inverse(&self) -> Self;
#[inline]
#[deprecated(note = "This method is likely **not** what you are looking for. Use `.two_sided_inverse_mut` if you really want the inverse from abstract algebra. \
If you are calling this method on a matrix from the nalgebra crate, you are probably searching for `.try_inverse_mut` instead.")]
fn inverse_mut(&mut self) {
*self = self.inverse()
}
}
impl<T: Inverse<O>, O: Operator> TwoSidedInverse<O> for T {
#[inline]
fn two_sided_inverse(&self) -> Self {
self.inverse()
}
#[inline]
fn two_sided_inverse_mut(&mut self) {
self.inverse_mut()
}
}
#[derive(Clone, Copy)]
pub struct Additive;
#[derive(Clone, Copy)]
pub struct Multiplicative;
#[derive(Clone, Copy)]
pub struct AbstractOperator;
impl Operator for Additive {
#[inline]
fn operator_token() -> Self {
Additive
}
}
impl Operator for Multiplicative {
#[inline]
fn operator_token() -> Self {
Multiplicative
}
}
impl Operator for AbstractOperator {
#[inline]
fn operator_token() -> Self {
AbstractOperator
}
}
macro_rules! impl_additive_inverse(
($($T:ty),* $(,)*) => {$(
impl Inverse<Additive> for $T {
fn inverse(&self) -> Self {
-*self
}
}
)*}
);
impl_additive_inverse!(i8, i16, i32, i64, isize, f32, f64);
#[cfg(feature = "decimal")]
impl_additive_inverse!(d128);
impl<N: TwoSidedInverse<Additive>> Inverse<Additive> for Complex<N> {
#[inline]
fn inverse(&self) -> Complex<N> {
Complex {
re: self.re.two_sided_inverse(),
im: self.im.two_sided_inverse(),
}
}
}
impl Inverse<Multiplicative> for f32 {
#[inline]
fn inverse(&self) -> f32 {
1.0 / self
}
}
impl Inverse<Multiplicative> for f64 {
#[inline]
fn inverse(&self) -> f64 {
1.0 / self
}
}
#[cfg(feature = "decimal")]
impl Inverse<Multiplicative> for d128 {
#[inline]
fn inverse(&self) -> d128 {
d128!(1.0) / self
}
}
impl<N: Num + Clone + ClosedNeg> Inverse<Multiplicative> for Complex<N> {
#[inline]
fn inverse(&self) -> Self {
self.inv()
}
}
pub trait ClosedAdd<Right = Self>: Sized + Add<Right, Output = Self> + AddAssign<Right> {}
pub trait ClosedSub<Right = Self>: Sized + Sub<Right, Output = Self> + SubAssign<Right> {}
pub trait ClosedMul<Right = Self>: Sized + Mul<Right, Output = Self> + MulAssign<Right> {}
pub trait ClosedDiv<Right = Self>: Sized + Div<Right, Output = Self> + DivAssign<Right> {}
pub trait ClosedNeg: Sized + Neg<Output = Self> {}
impl<T, Right> ClosedAdd<Right> for T
where
T: Add<Right, Output = T> + AddAssign<Right>,
{
}
impl<T, Right> ClosedSub<Right> for T
where
T: Sub<Right, Output = T> + SubAssign<Right>,
{
}
impl<T, Right> ClosedMul<Right> for T
where
T: Mul<Right, Output = T> + MulAssign<Right>,
{
}
impl<T, Right> ClosedDiv<Right> for T
where
T: Div<Right, Output = T> + DivAssign<Right>,
{
}
impl<T> ClosedNeg for T
where
T: Neg<Output = T>,
{
}