scilib 1.0.0

A scientific library for the Rust programming language.
Documentation
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//!
//! # Cartesian coordinates
//! 
//! Cartesian coordinates store the distance of the point compared to the origin for each axis.
//! 
//! Support conversion to and from Spherical and Cylindrical coordinates.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

use std::f64::consts::{         // Using std lib constants
    PI                          // Pi
};

use std::ops::{                 // Implementing basic operations
    Add,                        // Addition
    AddAssign,                  // Assigning addition
    Sub,                        // Subtraction
    SubAssign,                  // Assigning addition
    Mul,                        // Multiplication
    MulAssign,                  // Assigning multiplication
    Div,                        // Division
    DivAssign,                  // Assigning division
    Neg                         // Negation
};

use std::fmt::{                 // Formatter display
    Display,                    // The display itself
    Result as DRes              // The associated result
};

use super::{                    // Using parts from the crate
    cylindrical::Cylindrical,   // Cylindrical coordinates
    spherical::Spherical        // Spherical coordinates
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// # Cartesian coordinates
/// 
/// Defined for 3D space.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Cartesian {
    /// x axis
    pub x: f64,
    /// y axis
    pub y: f64,
    /// z axis
    pub z: f64
}

/// # Display for Cartesian
/// 
/// Simply shows each value associated to an axis.
impl Display for Cartesian {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> DRes {
        write!(f, "x={} :: y={} :: z={}", self.x, self.y, self.z)?;
        Ok(())
    }
}

/// Implementing required methods
impl Cartesian {

    /// # Creates a new entity
    /// 
    /// Returns the same value as `Self::default()`, all elements are equal to zero.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// let m = Cartesian { x: 0.0, y: 0.0, z: 0.0 };
    /// let n = Cartesian::origin();
    /// let d = Cartesian::default();
    /// 
    /// assert_eq!(m, n);
    /// assert_eq!(n, d);
    /// ```
    pub const fn origin() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0
        }
    }

    /// # From each point
    /// 
    /// Creates a Cartesian struct from three given points in space.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// let m = Cartesian { x: 3.2, y: -2.0, z: 0.5e-3 };
    /// let f = Cartesian::from(3.2, -2, 0.5e-3);
    /// 
    /// assert_eq!(m, f);
    /// ```
    pub fn from<T, U, V>(x: T, y: U, z: V) -> Self
    where T: Into<f64>, U: Into<f64>, V: Into<f64> {
        Self {
            x: x.into(),
            y: y.into(),
            z: z.into()
        }
    }

    /// # From another coordinate system
    /// 
    /// Creates an Cartesian struct from another coordinate system. Calls the `Into<Cartesian>` method,
    /// which is verified in its implementation.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// # use scilib::coordinate::spherical::Spherical;
    /// # use scilib::coordinate::cylindrical::Cylindrical;
    /// let s: Spherical = Spherical::from(2.4, 65, 15);
    /// let c: Cylindrical = Cylindrical::from_degree(1.2, 32, -3);
    /// let res1: Cartesian = Cartesian::from_coord(s);
    /// let res2: Cartesian = Cartesian::from_coord(c);
    /// 
    /// assert_eq!(res1, s.into());
    /// assert_eq!(res2, c.into());
    /// ```
    pub fn from_coord<T>(c: T) -> Self
    where T: Into<Self> {
        c.into()
    }

    /// # Computes the vector norm
    /// 
    /// We follow the convention of the l2 norm in this implementation.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// let v = Cartesian::from(1, -2.5, 5.2);
    /// let n = v.norm();
    /// 
    /// assert!((n - 5.85576638878294).abs() < 1.0e-14);
    /// ```
    pub fn norm(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
    }

    /// # Distance squared between two points
    /// 
    /// Computes the square of the distance between two points. As the distance is the L2 norm,
    /// it requires taking the square root of the sum of the squared difference of each term.
    /// Computing the distance squared is therefore more efficient, as it simply skips computing
    /// the square root followed by the the squaring operation.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// let f = Cartesian::from(1.0, 2, 0.5);
    /// let p = Cartesian::from(2, -1, 3.5);
    /// assert!((f.distance_square(p) - 19.0).abs() < 1.0e-15);
    /// ```
    pub fn distance_square(self, rhs: Self) -> f64 {
        (rhs.x - self.x).powi(2) + (rhs.y - self.y).powi(2) + (rhs.z - self.z).powi(2)
    }

    /// # Distance between two points
    /// 
    /// Computes the distance between two points in space.
    /// 
    /// ```
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// let f = Cartesian::from(1.0, 2, 0.5);
    /// let p = Cartesian::from(2, -1, 3.5);
    /// assert!((f.distance(p) - 4.358898943540674).abs() < 1.0e-15);
    /// ```
    pub fn distance(self, rhs: Self) -> f64 {
        self.distance_square(rhs).sqrt()
    }

    /// # Coordinate rotation
    /// 
    /// Computes the resulting coordinates after an arbitrary rotation in 3D. The rotation
    /// arguments are the yaw, pitch and roll in radians.
    /// 
    /// - Yaw is the rotation around the `z` axis
    /// - Pitch is the rotation around the `y` axis
    /// - Roll is the rotation around the `x` axis
    /// 
    /// ```
    /// # use std::f64::consts::FRAC_PI_2;
    /// # use scilib::coordinate::cartesian::Cartesian;
    /// 
    /// // Setting a point in y=0, and rotating it 90° trigonometry-wise
    /// let f = Cartesian::from(0, 1, 0);
    /// let res = f.rotate(FRAC_PI_2, 0.0, 0.0);
    /// 
    /// assert_eq!(res.x, -1.0);
    /// assert!((res.y - 0.0).abs() < f64::EPSILON);
    /// assert_eq!(res.z, 0.0);
    /// ```
    pub fn rotate(&self, yaw: f64, pitch: f64, roll: f64) -> Self {

        let (y_sin, y_cos): (f64, f64) = yaw.sin_cos();
        let (p_sin, p_cos): (f64, f64) = pitch.sin_cos();
        let (r_sin, r_cos): (f64, f64) = roll.sin_cos();

        // First row of the matrix
        let a11: f64 = y_cos * p_cos;
        let a12: f64 = y_cos * p_sin * r_sin - y_sin * r_cos;
        let a13: f64 = y_cos * p_sin * r_cos + y_sin * r_sin;

        // Second row
        let a21: f64 = y_sin * p_cos;
        let a22: f64 = y_sin * p_sin * r_sin + y_cos * r_cos;
        let a23: f64 = y_sin * p_sin * r_cos - y_cos * r_sin;

        // Third row
        let a31: f64 = -p_sin;
        let a32: f64 = p_cos * r_sin;
        let a33: f64 = p_cos * r_cos;

        // Following matrix multiplication
        Self {
            x: a11 * self.x + a12 * self.y + a13 * self.z,
            y: a21 * self.x + a22 * self.y + a23 * self.z,
            z: a31 * self.x + a32 * self.y + a33 * self.z
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// # Conversion to spherical coordinates
/// 
/// ```
/// # use std::f64::consts::SQRT_2;
/// # use scilib::coordinate::spherical::Spherical;
/// # use scilib::coordinate::cartesian::Cartesian;
/// 
/// let s = Cartesian::from(1, 1, 0);
/// let conv: Spherical = s.into();
/// let expected = Spherical::from_degree(SQRT_2, 45, 90);
/// 
/// assert_eq!(conv, expected);
/// 
/// let s = Cartesian::from(1, 0, 1);
/// let conv: Spherical = s.into();
/// let expected = Spherical::from_degree(SQRT_2, 0, 45);
/// 
/// assert_eq!(conv.r, expected.r);
/// assert_eq!(conv.theta, expected.theta);
/// assert!((conv.phi - expected.phi).abs() < 1.0e-15);
/// ```
impl Into<Spherical> for Cartesian {
    fn into(self) -> Spherical {
        let rho: f64 = self.norm();
        let mut nt: f64 = (self.y / self.x).atan();

        // If we were in the wrong quadrants, the atan range doesn't work
        if self.x.is_sign_negative() {
            nt += PI;
        }

        Spherical {
            r: rho,
            theta: nt,
            phi: (self.z / rho).acos()
        }
    }
}

/// # Conversion to cylindrical coordinates
/// 
/// ```
/// # use std::f64::consts::SQRT_2;
/// # use scilib::coordinate::cylindrical::Cylindrical;
/// # use scilib::coordinate::cartesian::Cartesian;
/// 
/// let s = Cartesian::from(1, 1, 1.2);
/// let conv: Cylindrical = s.into();
/// let expected = Cylindrical::from_degree(SQRT_2, 45, 1.2);
/// 
/// assert_eq!(conv, expected);
/// 
/// let s = Cartesian::from(1, 0, 1);
/// let conv: Cylindrical = s.into();
/// let expected = Cylindrical::from_degree(1.0, 0, 1.0);
/// 
/// assert_eq!(conv, expected);
/// ```
impl Into<Cylindrical> for Cartesian {
    fn into(self) -> Cylindrical {
        let rho: f64 = (self.x.powi(2) + self.y.powi(2)).sqrt();
        let mut nt: f64 = (self.y / self.x).atan();

        // If we were in the wrong quadrants, the atan range doesn't work
        if self.x.is_sign_negative() {
            nt += PI;
        }

        Cylindrical {
            r: rho,
            theta: nt,
            z: self.z
        }
    }
}

/// # Addition
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// let res = c1 + c2;
/// let expected = Cartesian::from(0.0, 3.5, 3.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl<T: Into<Self>> Add<T> for Cartesian {
    type Output = Self;
    fn add(self, rhs: T) -> Self::Output {
        let rhs: Self = rhs.into();
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z
        }
    }
}

/// # Addition with reference
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// let res = c1 + &c2;
/// let expected = Cartesian::from(0.0, 3.5, 3.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl Add<&Self> for Cartesian {
    type Output = Self;
    fn add(self, rhs: &Self) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z
        }
    }
}

/// # Assigning addition
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// c1 += c2;
/// let expected = Cartesian::from(0.0, 3.5, 3.0);
/// 
/// assert_eq!(c1, expected);
/// ```
impl<T: Into<Self>> AddAssign<T> for Cartesian {
    fn add_assign(&mut self, rhs: T) {
        let rhs: Self = rhs.into();
        self.x += rhs.x;
        self.y += rhs.y;
        self.z += rhs.z;
    }
}

/// # Assigning addition with reference
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// c1 += &c2;
/// let expected = Cartesian::from(0.0, 3.5, 3.0);
/// 
/// assert_eq!(c1, expected);
/// ```
impl AddAssign<&Self> for Cartesian {
    fn add_assign(&mut self, rhs: &Self) {
        self.x += rhs.x;
        self.y += rhs.y;
        self.z += rhs.z;
    }
}

/// # Subtraction
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// let res = c1 - c2;
/// let expected = Cartesian::from(2, 1.5, 3.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl<T: Into<Self>> Sub<T> for Cartesian {
    type Output = Self;
    fn sub(self, rhs: T) -> Self::Output {
        let rhs: Self = rhs.into();
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z
        }
    }
}

/// # Subtraction with reference
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// let res = c1 - &c2;
/// let expected = Cartesian::from(2, 1.5, 3.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl Sub<&Self> for Cartesian {
    type Output = Self;
    fn sub(self, rhs: &Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z
        }
    }
}

/// # Assigning subtraction
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// c1 -= c2;
/// let expected = Cartesian::from(2, 1.5, 3.0);
/// 
/// assert_eq!(c1, expected);
impl<T: Into<Self>> SubAssign<T> for Cartesian {
    fn sub_assign(&mut self, rhs: T) {
        let rhs: Self = rhs.into();
        self.x -= rhs.x;
        self.y -= rhs.y;
        self.z -= rhs.z;
    }
}

/// # Assigning subtraction with reference
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(1.0, 2.5, 3.0);
/// let c2 = Cartesian::from(-1.0, 1, 0.0);
/// c1 -= &c2;
/// let expected = Cartesian::from(2, 1.5, 3.0);
/// 
/// assert_eq!(c1, expected);
impl SubAssign<&Self> for Cartesian {
    fn sub_assign(&mut self, rhs: &Self) {
        self.x -= rhs.x;
        self.y -= rhs.y;
        self.z -= rhs.z;
    }
}

/// # Scalar multiplication
/// 
/// Multiplies each field by the scalar.
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(-1.0, 2.5, 3.0);
/// let res = c1 * 2;
/// let expected = Cartesian::from(-2, 5, 6.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl<T: Into<f64>> Mul<T> for Cartesian {
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        let f: f64 = rhs.into();
        Self {
            x: self.x * f,
            y: self.y * f,
            z: self.z * f
        }
    }
}

/// # Assigning scalar multiplication
/// 
/// Multiplies each field by the scalar in place.
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(-1.0, 2.5, 3.0);
/// c1 *= 2;
/// let expected = Cartesian::from(-2, 5, 6.0);
/// 
/// assert_eq!(c1, expected);
/// ```
impl<T: Into<f64>> MulAssign<T> for Cartesian {
    fn mul_assign(&mut self, rhs: T) {
        let f: f64 = rhs.into();
        self.x *= f;
        self.y *= f;
        self.z *= f;
    }
}

/// # Scalar division
/// 
/// Divises each field by the scalar.
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(-2, 5, 6.0);
/// let res = c1 / 2;
/// let expected = Cartesian::from(-1.0, 2.5, 3.0);
/// 
/// assert_eq!(res, expected);
/// ```
impl<T: Into<f64>> Div<T> for Cartesian {
    type Output = Self;
    fn div(self, rhs: T) -> Self::Output {
        let f: f64 = 1.0 / rhs.into();
        Self {
            x: self.x * f,
            y: self.y * f,
            z: self.z * f,
        }
    }
}

/// # Assigning scalar division
/// 
/// Divises each field by the scalar in place.
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let mut c1 = Cartesian::from(-2, 5, 6.0);
/// c1 /= 2;
/// let expected = Cartesian::from(-1.0, 2.5, 3.0);
/// 
/// assert_eq!(c1, expected);
/// ```
impl<T: Into<f64>> DivAssign<T> for Cartesian {
    fn div_assign(&mut self, rhs: T) {
        let f: f64 = 1.0 / rhs.into();
        self.x *= f;
        self.y *= f;
        self.z *= f;
    }
}

/// # Negation
/// 
/// Changes the values to their opposites.
/// 
/// ```
/// # use scilib::coordinate::cartesian::Cartesian;
/// let c1 = Cartesian::from(-2, 5, 6.0);
/// let c2 = -c1;
/// let expected = Cartesian::from(2, -5.0, -6);
/// 
/// assert_eq!(c2, expected);
/// ```
impl Neg for Cartesian {
    type Output = Self;
    fn neg(self) -> Self::Output {
        self * -1
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////