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
use super::consts::ALLOWED;
use {ArError, Result};
use std::collections::HashSet;
use std::fmt;
use std::hash::{Hash, Hasher};
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct KeyVec(Vec<Index>);
impl KeyVec {
pub fn new(v: Vec<Index>) -> KeyVec {
let mut v = v.clone();
v.sort();
KeyVec(v)
}
}
impl Hash for KeyVec {
fn hash<H: Hasher>(&self, state: &mut H) {
let KeyVec(ref elems) = *self;
for elem in elems.iter() {
elem.hash(state);
}
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
pub enum Sign {
Pos,
Neg,
}
#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
pub enum Index {
Zero,
One,
Two,
Three,
}
#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
pub enum Component {
Point,
Vector(Index),
Bivector(Index, Index),
Trivector(Index, Index, Index),
Quadrivector(Index, Index, Index, Index),
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
pub struct Alpha {
index: Component,
sign: Sign,
}
#[derive(Debug, PartialOrd, PartialEq, Clone)]
pub enum Xi {
Real(f64),
Symbolic(String),
}
#[derive(Debug, PartialOrd, PartialEq, Clone)]
pub struct Pair {
xi: Xi,
alpha: Alpha,
}
impl fmt::Display for Index {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Index::Zero => write!(f, "0"),
Index::One => write!(f, "1"),
Index::Two => write!(f, "2"),
Index::Three => write!(f, "3"),
}
}
}
impl fmt::Display for Component {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Component::Point => write!(f, "p"),
Component::Vector(ref i) => write!(f, "{}", i),
Component::Bivector(ref i, ref j) => write!(f, "{}{}", i, j),
Component::Trivector(ref i, ref j, ref k) => write!(f, "{}{}{}", i, j, k),
Component::Quadrivector(ref i, ref j, ref k, ref l) => write!(f, "{}{}{}{}", i, j, k, l),
}
}
}
impl fmt::Display for Alpha {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.sign {
Sign::Pos => write!(f, "α{}", self.index),
Sign::Neg => write!(f, "-α{}", self.index),
}
}
}
impl fmt::Display for Xi {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Xi::Real(ref n) => write!(f, "ξ({})", n),
Xi::Symbolic(ref s) => write!(f, "ξ{}", s),
}
}
}
impl fmt::Display for Pair {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.alpha, self.xi)
}
}
impl Sign {
pub fn combine_with(&self, j: &Sign) -> Sign {
if self == j { Sign::Pos } else { Sign::Neg }
}
}
impl Index {
pub fn try_from_str(s: &str) -> Result<Index> {
match s {
"0" => Ok(Index::Zero),
"1" => Ok(Index::One),
"2" => Ok(Index::Two),
"3" => Ok(Index::Three),
&_ => Err(ArError::InvalidIndex(String::from(s))),
}
}
}
impl Component {
pub fn new(ix: &str, allowed: &HashSet<Component>) -> Result<Component> {
let index = Component::unsafe_new(ix)?;
if !allowed.contains(&index) {
return Err(ArError::ComponentNotAllowed(String::from(ix)));
}
Ok(index)
}
pub fn unsafe_new(ix: &str) -> Result<Component> {
if ix == "p" {
return Ok(Component::Point);
}
let v: Vec<&str> = ix.split("")
.filter(|&c| c != "")
.collect();
match v.len() {
1 => {
let i = Index::try_from_str(v[0])?;
Ok(Component::Vector(i))
}
2 => {
let i1 = Index::try_from_str(v[0])?;
let i2 = Index::try_from_str(v[1])?;
Ok(Component::Bivector(i1, i2))
}
3 => {
let i1 = Index::try_from_str(v[0])?;
let i2 = Index::try_from_str(v[1])?;
let i3 = Index::try_from_str(v[2])?;
Ok(Component::Trivector(i1, i2, i3))
}
4 => {
let i1 = Index::try_from_str(v[0])?;
let i2 = Index::try_from_str(v[1])?;
let i3 = Index::try_from_str(v[2])?;
let i4 = Index::try_from_str(v[3])?;
Ok(Component::Quadrivector(i1, i2, i3, i4))
}
_ => return Err(ArError::InvalidComponentOrder(String::from(ix))),
}
}
pub fn as_vec(&self) -> Vec<Index> {
match *self {
Component::Vector(i) => vec![i],
Component::Bivector(i, j) => vec![i, j],
Component::Trivector(i, j, k) => vec![i, j, k],
Component::Quadrivector(i, j, k, l) => vec![i, j, k, l],
Component::Point => vec![],
}
}
}
impl Alpha {
pub fn new(ix: &str) -> Result<Alpha> {
let sign = match ix.starts_with("-") {
true => Sign::Neg,
false => Sign::Pos,
};
let ix = ix.trim_matches('-');
let index = Component::new(ix, &ALLOWED.indices())?;
Ok(Alpha { index, sign })
}
pub fn new_override(ix: &str, sign: Sign, allowed: &HashSet<Component>) -> Result<Alpha> {
let index = Component::new(ix, allowed)?;
Ok(Alpha { index, sign })
}
pub fn from_index(index: &Component, sign: &Sign) -> Alpha {
Alpha {
index: index.clone(),
sign: sign.clone(),
}
}
pub fn is_point(&self) -> bool {
self.index == Component::Point
}
pub fn index(&self) -> &Component {
&self.index
}
pub fn sign(&self) -> &Sign {
&self.sign
}
pub fn as_vec(&self) -> Vec<Index> {
self.index.as_vec()
}
}
impl Pair {
pub fn new(xi: Xi, alpha: Alpha) -> Pair {
Pair { xi, alpha }
}
pub fn sym(ix: &str) -> Result<Pair> {
let alpha = Alpha::new(ix)?;
let ix = ix.trim_matches('-');
let xi = Xi::Symbolic(String::from(ix));
Ok(Pair { xi, alpha })
}
pub fn xi(&self) -> &Xi {
&self.xi
}
pub fn alpha(&self) -> &Alpha {
&self.alpha
}
}