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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Sub;
use std::ops::SubAssign;
use std::ops::Mul;
use std::ops::Neg;
use std::fmt;
use std::str::FromStr;
use std::num::ParseFloatError;

mod four_vec;
pub use four_vec::Sinv;
pub use four_vec::beta;
pub use four_vec::gamma;
pub use four_vec::FourVec;

pub use four_vec::ThreeMat;
pub use four_vec::ThreeVec;
pub use four_vec::{radians_between, degrees_between};

pub use four_vec::consts;
pub use four_vec::Serializable;

/// Four Matrix
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct FourMat {
    /// Four rows, each a calcify::FourVec
    n0: FourVec,
    n1: FourVec,
    n2: FourVec,
    n3: FourVec,
}

impl FourMat {
    /// Returns a new FourMat from four FourVecs
    ///
    /// # Arguments
    ///
    /// * `n0` - calcify::FourVec
    /// * `n1` - calcify::FourVec
    /// * `n2` - calcify::FourVec
    /// * `n3` - calcify::FourVec
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// ```
    pub fn new(n0: FourVec, n1: FourVec, n2: FourVec, n3: FourVec) -> FourMat {
        FourMat {
            n0,
            n1,
            n2,
            n3,
        }
    }

    /// Returns a new FourMat identity matrix
    ///
    /// # Example
    /// ```
    /// use calcify::FourMat;
    /// let mat4 = FourMat::eye();
    ///
    /// assert_eq!(*mat4.n1().m1(),1.0);
    /// ```
    pub fn eye() -> FourMat {
        FourMat {
            n0: FourVec::new(1.0,0.0,0.0,0.0),
            n1: FourVec::new(0.0,1.0,0.0,0.0),
            n2: FourVec::new(0.0,0.0,1.0,0.0),
            n3: FourVec::new(0.0,0.0,0.0,1.0),
        }
    }

    /// Returns a new FourMat zero matrix
    ///
    /// # Example
    /// ```
    /// use calcify::FourMat;
    /// let mat4 = FourMat::zero();
    ///
    /// assert_eq!(*mat4.n1().m1(),0.0);
    /// ```
    pub fn zero() -> FourMat {
        FourMat {
            n0: FourVec::new(0.0,0.0,0.0,0.0),
            n1: FourVec::new(0.0,0.0,0.0,0.0),
            n2: FourVec::new(0.0,0.0,0.0,0.0),
            n3: FourVec::new(0.0,0.0,0.0,0.0),
        }
    }

    /// Returns a new FourMat metric tensor
    ///
    /// # Example
    /// ```
    /// use calcify::FourMat;
    /// let mat4 = FourMat::metric();
    ///
    /// assert_eq!(*mat4.n0().m0(),1.0);
    /// assert_eq!(*mat4.n1().m1(),-1.0);
    /// assert_eq!(*mat4.n2().m1(),0.0);
    /// ```
    pub fn metric() -> FourMat {
        FourMat {
            n0: FourVec::new(1.0,0.0,0.0,0.0),
            n1: FourVec::new(0.0,-1.0,0.0,0.0),
            n2: FourVec::new(0.0,0.0,-1.0,0.0),
            n3: FourVec::new(0.0,0.0,0.0,-1.0),
        }
    }

    /// Returns a new FourMat one matrix
    ///
    /// # Example
    /// ```
    /// use calcify::FourMat;
    /// let mat4 = FourMat::one();
    ///
    /// assert_eq!(*mat4.n1().m1(),1.0);
    /// ```
    pub fn one() -> FourMat {
        FourMat {
            n0: FourVec::new(1.0,1.0,1.0,1.0),
            n1: FourVec::new(1.0,1.0,1.0,1.0),
            n2: FourVec::new(1.0,1.0,1.0,1.0),
            n3: FourVec::new(1.0,1.0,1.0,1.0),
        }
    }

    /// Returns a reference to the first row of the matrix.
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// let row_zero: FourVec = *mat4.n0();
    /// let element_zero_zero: f64 = *mat4.n0().m0();
    /// assert_eq!(row_zero,FourVec::new(1.0,2.0,3.0,4.0));
    /// assert_eq!(element_zero_zero,1.0);
    /// ```
    pub fn n0(&self) -> &FourVec {
        &self.n0
    }


    /// Returns a reference to the second row of the matrix.
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// let row_one: FourVec = *mat4.n1();
    /// let element_one_one: f64 = *mat4.n1().m1();
    /// assert_eq!(row_one,FourVec::new(5.0,6.0,7.0,8.0));
    /// assert_eq!(element_one_one,6.0);
    /// ```
    pub fn n1(&self) -> &FourVec {
        &self.n1
    }

    /// Returns a reference to the third row of the matrix.
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// let row_two: FourVec = *mat4.n2();
    /// let element_two_two: f64 = *mat4.n2().m2();
    /// assert_eq!(row_two,FourVec::new(9.0,10.0,11.0,12.0));
    /// assert_eq!(element_two_two,11.0);
    /// ```
    pub fn n2(&self) -> &FourVec {
        &self.n2
    }

    /// Returns a reference to the forth row of the matrix.
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// let row_three: FourVec = *mat4.n3();
    /// let element_three_three: f64 = *mat4.n3().m3();
    /// assert_eq!(row_three,FourVec::new(13.0,14.0,15.0,16.0));
    /// assert_eq!(element_three_three,16.0);
    /// ```
    pub fn n3(&self) -> &FourVec {
        &self.n3
    }

    /// Returns a new memory FourVec of the first column of the matrix.
    ///
    /// # Example
    /// ```
    /// use calcify::FourVec;
    /// use calcify::FourMat;
    /// let mat4 = FourMat::new(
    ///               FourVec::new(1.0,2.0,3.0,4.0),
    ///               FourVec::new(5.0,6.0,7.0,8.0),
    ///               FourVec::new(9.0,10.0,11.0,12.0),
    ///               FourVec::new(13.0,14.0,15.0,16.0)
    ///            );
    /// let col_one: FourVec = mat4.c0();
    /// let element_one_one: f64 = *mat4.c0().m0();
    /// assert_eq!(col_one,FourVec::new(1.0,5.0,9.0,13.0));
    /// assert_eq!(element_one_one,1.0);
    /// ```
    pub fn c0(&self) -> FourVec {
        FourVec::new(*self.n0.m0(),*self.n1.m0(),*self.n2.m0(),*self.n3.m0())
    }

    pub fn c1(&self) -> FourVec {
        FourVec::new(*self.n0.m1(),*self.n1.m1(),*self.n2.m1(),*self.n3.m1())
    }

    pub fn c2(&self) -> FourVec {
        FourVec::new(*self.n0.m2(),*self.n1.m2(),*self.n2.m2(),*self.n3.m2())
    }

    pub fn c3(&self) -> FourVec {
        FourVec::new(*self.n0.m3(),*self.n1.m3(),*self.n2.m3(),*self.n3.m3())
    }
}

impl fmt::Display for FourMat {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{},\n{},\n{},\n{}]", self.n0(), self.n1(), self.n2(), self.n3())
    }
}

impl Serializable for FourMat {
    fn to_json(&self) -> String {
        format!("{{\"n0\":{},\"n1\":{},\"n2\":{},\"n3\":{}}}",
            self.n0().to_json(),
            self.n1().to_json(),
            self.n2().to_json(),
            self.n3().to_json()
        )
    }
}

impl FromStr for FourMat {
    type Err = ParseFloatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut n0: FourVec = FourVec::new(std::f64::NAN,std::f64::NAN,std::f64::NAN,std::f64::NAN);
        let mut n1: FourVec = FourVec::new(std::f64::NAN,std::f64::NAN,std::f64::NAN,std::f64::NAN);
        let mut n2: FourVec = FourVec::new(std::f64::NAN,std::f64::NAN,std::f64::NAN,std::f64::NAN);
        let mut n3: FourVec = FourVec::new(std::f64::NAN,std::f64::NAN,std::f64::NAN,std::f64::NAN);
        for dim in s.replace("}}","|}").replace("},","}|").replace(":{",":!{").trim_matches(|p| p == '{' || p == '}' ).split_terminator('|') {
            let n_v: Vec<&str> = dim.split(":!").collect();
            match n_v[0] {
                "\"n0\"" => n0 = FourVec::from_str(n_v[1])?,
                "\"n1\"" => n1 = FourVec::from_str(n_v[1])?,
                "\"n2\"" => n2 = FourVec::from_str(n_v[1])?,
                "\"n3\"" => n3 = FourVec::from_str(n_v[1])?,
                x => panic!("Unexpected invalid token {:?}", x),
            }
        }
        Ok(FourMat{n0,n1,n2,n3})
    }
}


impl Add for FourMat {
    type Output = FourMat;

    fn add(self, other: FourMat) -> FourMat {
        FourMat {
            n0: self.n0 + *other.n0(),
            n1: self.n1 + *other.n1(),
            n2: self.n2 + *other.n2(),
            n3: self.n3 + *other.n3(),
        }
    }
}

impl AddAssign for FourMat {
    fn add_assign(&mut self, other: FourMat) {
        self.n0 +=*other.n0();
        self.n1 +=*other.n1();
        self.n2 +=*other.n2();
        self.n3 +=*other.n3();
    }
}

impl Sub for FourMat {
    type Output = FourMat;

    fn sub(self, other: FourMat) -> FourMat {
        FourMat {
            n0: self.n0 -*other.n0(),
            n1: self.n1 -*other.n1(),
            n2: self.n2 -*other.n2(),
            n3: self.n3 -*other.n3(),
        }
    }
}

impl SubAssign for FourMat {
    fn sub_assign(&mut self, other: FourMat) {
        self.n0 -=*other.n0();
        self.n1 -=*other.n1();
        self.n2 -=*other.n2();
        self.n3 -=*other.n3();
    }
}

impl Mul<f64> for FourMat {
    type Output = FourMat;

    fn mul(self, coef: f64) -> FourMat {
        FourMat {
            n0: self.n0 *coef,
            n1: self.n1 *coef,
            n2: self.n2 *coef,
            n3: self.n3 *coef,
        }
    }
}

impl Mul<FourMat> for f64 {
    type Output = FourMat;

    fn mul(self, vec: FourMat) -> FourMat {
        FourMat {
            n0: *vec.n0() * self,
            n1: *vec.n1() * self,
            n2: *vec.n2() * self,
            n3: *vec.n3() * self,
        }
    }
}

impl Mul<FourMat> for FourMat {
    type Output = FourMat;
    /// Matrix multiplication
    ///
    /// # Example
    ///
    /// ```
    /// use calcify::FourMat;
    /// use calcify::FourVec;
    ///
    /// let mat4 = FourMat::new(FourVec::new(1.0,2.0,3.0,4.0),
    ///                             FourVec::new(5.0,6.0,7.0,8.0),
    ///                             FourVec::new(9.0,10.0,11.0,12.0),
    ///                             FourVec::new(13.0,14.0,15.0,16.0));
    ///
    /// assert_eq!(
    ///     mat4*mat4,
    ///     FourMat::new(FourVec::new(90.0,100.0,110.0,120.0),
    ///                 FourVec::new(202.0,228.0,254.0,280.0),
    ///                 FourVec::new(314.0,356.0,398.0,440.0),
    ///                 FourVec::new(426.0,484.0,542.0,600.0)));
    /// ```
    fn mul(self, other: FourMat) -> FourMat {
        let c0 = other.c0();
        let c1 = other.c1();
        let c2 = other.c2();
        let c3 = other.c3();
        FourMat {
            n0: FourVec::new(self.n0*c0, self.n0*c1, self.n0*c2, self.n0*c3),
            n1: FourVec::new(self.n1*c0, self.n1*c1, self.n1*c2, self.n1*c3),
            n2: FourVec::new(self.n2*c0, self.n2*c1, self.n2*c2, self.n2*c3),
            n3: FourVec::new(self.n3*c0, self.n3*c1, self.n3*c2, self.n3*c3),
        }
    }
}

impl Mul<FourVec> for FourMat {
    type Output = FourVec;
    /// Matrix multiplication with vector
    ///
    /// # Note
    ///
    /// Only works in one direction FourMat*FourVec, implying FourVec as a column vector.
    ///
    /// # Example
    ///
    /// ```
    /// use calcify::FourMat;
    /// use calcify::FourVec;
    ///
    /// let mat4 = FourMat::new(FourVec::new(1.0,2.0,3.0,4.0),
    ///                             FourVec::new(1.0,2.0,3.0,4.0),
    ///                             FourVec::new(1.0,2.0,3.0,4.0),
    ///                             FourVec::new(1.0,2.0,3.0,4.0));
    ///
    /// assert_eq!(
    ///     mat4*FourVec::new(2.0,2.0,2.0,2.0),
    ///     FourVec::new(20.0,20.0,20.0,20.0)
    /// );
    /// ```
    fn mul(self, other: FourVec) -> FourVec {
        FourVec::new(self.n0*other,self.n1*other,self.n2*other,self.n3*other)
    }
}

impl Neg for FourMat {
    type Output = FourMat;

    fn neg(self) -> FourMat {
        FourMat {
            n0: -self.n0,
            n1: -self.n1,
            n2: -self.n2,
            n3: -self.n3,
        }
    }
}

/// Returns a FourVec, inside a Result, boosted into a frame of arbitrary velocity **v**.
///
/// Each componant of **v** must be less than calcify::C_LIGHT.
/// Uses a FourMat Lorentz Transformation tensor.
/// If **v** = [0,0,0], then the boost tensor will be an identity by definition.
///
/// # Arguments
///
/// * `initial` - calcify::FourVec
/// * `v` - calcify::ThreeVec
///
/// # Example
/// ```
/// use calcify::boost;
/// use calcify::FourVec;
/// use calcify::ThreeVec;
///
/// let vv = ThreeVec::random(100.0);
/// let vec4 = FourVec::new(10.0,1.0,1.0,1.0);
/// let bVec = boost(vec4,vv);
///
/// assert_eq!(boost(vec4,ThreeVec::new(0.0,0.0,0.0)).unwrap(),vec4);
///
/// ```
pub fn boost(initial: FourVec, v: ThreeVec) -> Result<FourVec,&'static str> {
    let bx = beta(*v.x0())?;
    let by = beta(*v.x1())?;
    let bz = beta(*v.x2())?;
    let bb = bx*bx + by*by + bz*bz;
    let g = gamma(beta((v*v).sqrt())?);
    let mut ll = FourMat::eye();
    if bb > 0.0 {
        ll = FourMat::new(FourVec::new(g,-g*bx,-g*by,-g*bz),
                          FourVec::new(-g*bx,(g - 1.0)*(bx*bx)/bb + 1.0,(g - 1.0)*(bx*by)/bb,(g - 1.0)*(bx*bz)/bb),
                          FourVec::new(-g*by,(g - 1.0)*(bx*by)/bb,(g - 1.0)*(by*by)/bb + 1.0,(g - 1.0)*(by*bz)/bb),
                          FourVec::new(-g*bz,(g - 1.0)*(bx*bz)/bb,(g - 1.0)*(by*bz)/bb,(g - 1.0)*(bz*bz)/bb + 1.0));
    }

    Ok(ll*initial)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_parse() {
        let xx = FourMat::new(FourVec::new(1.0,1.0,1.0,1.0),
                                    FourVec::new(1.0,1.0,1.0,1.0),
                                    FourVec::new(1.0,1.0,1.0,1.0),
                                    FourVec::new(1.0,1.0,1.0,1.0));
        let pp = xx.to_json();
        assert_eq!(FourMat::from_str(&pp).unwrap(),xx);
    }
}