arael 0.3.0

Nonlinear optimization framework with compile-time symbolic differentiation
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
//! 2D and 3D vector types with standard math operations.

#![allow(non_camel_case_types)]

use std::ops;
use std::fmt;
use crate::utils::{deg2rad, rad2deg, left_side_scalar_multiplication};
use crate::utils::Float;

/// Trait for approximate floating-point equality comparisons.
pub trait Similar {
    /// Returns true if `self` and `other` are approximately equal within floating-point tolerance.
    fn similar(self, other: Self) -> bool;
}

/// 3D vector with x, y, z components.
///
/// Supports addition, subtraction, negation, scalar multiplication, dot product
/// (`*` operator), and cross product (`%` operator). Indexable by `usize` (0=x, 1=y, 2=z).
#[derive(Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct vect3<T : Float>
{
    pub x : T,
    pub y : T,
    pub z : T,
}

/// 3D vector with f32 components.
pub type vect3f = vect3<f32>;
/// 3D vector with f64 components.
pub type vect3d = vect3<f64>;

impl<T: Float> Default for vect3<T> {
    fn default() -> Self { vect3 { x: T::zero(), y: T::zero(), z: T::zero() } }
}

impl<T: Float> fmt::Debug for vect3<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:?}, {:?}, {:?}]", self.x, self.y, self.z)
    }
}

impl<T: Float> ops::Add<vect3<T>> for vect3<T>
{
    type Output = vect3<T>;
    fn add(self, _rhs: vect3<T>) -> vect3<T> {
        vect3::<T> {x: self.x + _rhs.x, y: self.y + _rhs.y, z: self.z + _rhs.z}
    }
}

impl<T: Float> ops::Sub<vect3<T>> for vect3<T>
{
    type Output = vect3<T>;
    fn sub(self, _rhs: vect3<T>) -> vect3<T> {
        vect3::<T> {x: self.x - _rhs.x, y: self.y - _rhs.y, z: self.z - _rhs.z}
    }
}

impl<T: Float> ops::Mul<vect3<T>> for vect3<T>
{
    type Output = T;
    fn mul(self, _rhs: vect3<T>) -> T {
        self.x * _rhs.x + self.y * _rhs.y + self.z * _rhs.z
    }
}

impl<T: Float> ops::Mul<T> for vect3<T>
{
    type Output = vect3<T>;
    fn mul(self, _rhs: T) -> vect3<T> {
        vect3::<T> {x: self.x * _rhs, y: self.y * _rhs, z: self.z * _rhs}
    }
}

left_side_scalar_multiplication!(vect3f,f32);
left_side_scalar_multiplication!(vect3d,f64);

impl<T: Float> ops::Neg for vect3<T>
{
    type Output = vect3<T>;
    fn neg(self) -> vect3<T> {
        vect3::<T> {x: -self.x, y: -self.y, z: -self.z}
    }
}

impl<T: Float> ops::Rem for vect3<T>
{
    type Output = vect3<T>;
    fn rem(self, _rhs: vect3<T>) -> vect3<T> {
        vect3::<T> {x: self.y * _rhs.z - self.z * _rhs.y, y: self.z * _rhs.x - self.x * _rhs.z, z: self.x * _rhs.y - self.y * _rhs.x}
    }
}

impl<T: Float> vect3<T> {
    /// Constructs a 3D vector from components.
    pub fn new(x: T, y: T, z: T) -> vect3<T> {
        vect3::<T> { x: x, y: y, z: z}
    }

    /// Returns the squared magnitude (dot product with itself).
    pub fn square(self) -> T {
        self * self
    }

    /// Returns the Euclidean norm (length).
    pub fn norm(self) -> T {
        self.square().sqrt()
    }

    /// Returns the unit (normalized) vector.
    pub fn unit(self) -> vect3<T> {
        self * self.norm().recip()
    }

    /// Returns a unit vector perpendicular to `self`.
    pub fn across(self) -> vect3<T> {
        if self.y.abs() < self.x.abs() {
            vect3::<T>::new(-self.z, T::zero(), self.x).unit()
        } else {
            vect3::<T>::new(T::zero(), self.z, -self.y).unit()
        }
    }

    /// Returns true if all components are finite (not NaN or infinity).
    pub fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
    }

    /// Converts all components from degrees to radians.
    pub fn deg2rad(self) -> vect3<T> {
        vect3::<T>::new(deg2rad(self.x), deg2rad(self.y), deg2rad(self.z))
    }

    /// Converts all components from radians to degrees.
    pub fn rad2deg(self) -> vect3<T> {
        vect3::<T>::new(rad2deg(self.x), rad2deg(self.y), rad2deg(self.z))
    }

    /// Constructs a `vect3<T>` by converting from a `vect3<K>` of a different float type.
    pub fn from<K: Float>(v: vect3<K>) -> vect3<T> {
        vect3::<T>::new(T::from(v.x).unwrap(), T::from(v.y).unwrap(), T::from(v.z).unwrap())
    }

    /// Converts this vector to a `vect3<K>` of a different float type.
    pub fn cast<K: Float>(self) -> vect3<K> {
        vect3::<K>::new(K::from(self.x).unwrap(), K::from(self.y).unwrap(), K::from(self.z).unwrap())
    }

    /// Logs an error if this vector is not approximately unit length.
    pub fn assert_unit_length(self) {
        if (self.square() - T::one()).abs() > T::from(1e5).unwrap() * T::epsilon() {
            error!("arael::vect: not unit length, norm = {:?}, error = {:?}", self.norm(), T::one() - self.norm());
        }
    }

    /// Builds a 3x3 rotation matrix from this vector interpreted as Euler angles
    /// (x=roll, y=pitch, z=yaw).
    pub fn rotation_matrix(self) -> crate::matrix::matrix3<T> {
        crate::matrix::matrix3::rotation_from_euler_angles(self)
    }

    /// Computes element-wise sin and cos. Returns `(sin_vec, cos_vec)`.
    pub fn sincos(self) -> (vect3<T>, vect3<T>) {
        let (sin_x, cos_x) = self.x.sin_cos();
        let (sin_y, cos_y) = self.y.sin_cos();
        let (sin_z, cos_z) = self.z.sin_cos();
        (vect3::<T>::new(sin_x, sin_y, sin_z), vect3::<T>::new(cos_x, cos_y, cos_z))
    }
}

impl<T: Float> Similar for vect3<T> {
    fn similar(self, other: vect3<T>) -> bool {
        (self - other).norm() < T::from(10).unwrap() * (self.norm() + other.norm() + T::epsilon()) * T::epsilon()
    }
}

impl<T: Float> ops::Index<usize> for vect3<T> {
    type Output = T;
    fn index(&self, index: usize) -> &T {
        match index {
            0 => &self.x,
            1 => &self.y,
            2 => &self.z,
            _ => panic!("arael::vect: index {} out of bounds", index)
        }
    }
}

impl<T: Float> ops::IndexMut<usize> for vect3<T> {
    fn index_mut(&mut self, index: usize) -> &mut T {
        match index {
            0 => &mut self.x,
            1 => &mut self.y,
            2 => &mut self.z,
            _ => panic!("arael::vect: index {} out of bounds", index)
        }
    }
}

/// 2D vector with x, y components.
///
/// Supports addition, subtraction, negation, scalar multiplication, and dot product
/// (`*` operator). Indexable by `usize` (0=x, 1=y).
#[derive(Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct vect2<T : Float>
{
    pub x : T,
    pub y : T,
}

/// 2D vector with f32 components.
pub type vect2f = vect2<f32>;
/// 2D vector with f64 components.
pub type vect2d = vect2<f64>;

impl<T: Float> Default for vect2<T> {
    fn default() -> Self { vect2 { x: T::zero(), y: T::zero() } }
}

impl<T: Float> fmt::Debug for vect2<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:?}, {:?}]", self.x, self.y)
    }
}

impl<T: Float> ops::Add<vect2<T>> for vect2<T>
{
    type Output = vect2<T>;
    fn add(self, _rhs: vect2<T>) -> vect2<T> {
        vect2::<T> {x: self.x + _rhs.x, y: self.y + _rhs.y}
    }
}

impl<T: Float> ops::Sub<vect2<T>> for vect2<T>
{
    type Output = vect2<T>;
    fn sub(self, _rhs: vect2<T>) -> vect2<T> {
        vect2::<T> {x: self.x - _rhs.x, y: self.y - _rhs.y}
    }
}

impl<T: Float> ops::Mul<vect2<T>> for vect2<T>
{
    type Output = T;
    fn mul(self, _rhs: vect2<T>) -> T {
        self.x * _rhs.x + self.y * _rhs.y
    }
}

impl<T: Float> ops::Mul<T> for vect2<T>
{
    type Output = vect2<T>;
    fn mul(self, _rhs: T) -> vect2<T> {
        vect2::<T> {x: self.x * _rhs, y: self.y * _rhs}
    }
}

left_side_scalar_multiplication!(vect2f,f32);
left_side_scalar_multiplication!(vect2d,f64);

impl<T: Float> ops::Neg for vect2<T>
{
    type Output = vect2<T>;
    fn neg(self) -> vect2<T> {
        vect2::<T> {x: -self.x, y: -self.y}
    }
}

impl<T: Float> vect2<T> {
    /// Constructs a 2D vector from components.
    pub fn new(x: T, y: T) -> vect2<T> {
        vect2::<T> { x: x, y: y}
    }

    /// Returns the squared magnitude (dot product with itself).
    pub fn square(self) -> T {
        self * self
    }

    /// Returns the Euclidean norm (length).
    pub fn norm(self) -> T {
        self.square().sqrt()
    }

    /// Returns the unit (normalized) vector.
    pub fn unit(self) -> vect2<T> {
        self * self.norm().recip()
    }

    /// Returns a unit vector perpendicular to `self` (90-degree counter-clockwise rotation).
    pub fn across(self) -> vect2<T> {
        vect2::<T>::new(-self.y, self.x)
    }

    /// Returns true if all components are finite (not NaN or infinity).
    pub fn is_finite(self) -> bool {
        self.x.is_finite() && self.y.is_finite()
    }

    /// Converts all components from degrees to radians.
    pub fn deg2rad(self) -> vect2<T> {
        vect2::<T>::new(deg2rad(self.x), deg2rad(self.y))
    }

    /// Converts all components from radians to degrees.
    pub fn rad2deg(self) -> vect2<T> {
        vect2::<T>::new(rad2deg(self.x), rad2deg(self.y))
    }

    /// Constructs a `vect2<T>` by converting from a `vect2<K>` of a different float type.
    pub fn from<K: Float>(v: vect2<K>) -> vect2<T> {
        vect2::<T>::new(T::from(v.x).unwrap(), T::from(v.y).unwrap())
    }

    /// Converts this vector to a `vect2<K>` of a different float type.
    pub fn cast<K: Float>(self) -> vect2<K> {
        vect2::<K>::new(K::from(self.x).unwrap(), K::from(self.y).unwrap())
    }

    /// Logs an error if this vector is not approximately unit length.
    pub fn assert_unit_length(self) {
        if (self.square() - T::one()).abs() > T::from(1e5).unwrap() * T::epsilon() {
            error!("arael::vect: not unit length, norm = {:?}, error = {:?}", self.norm(), T::one() - self.norm());
        }
    }

    /// Computes element-wise sin and cos. Returns `(sin_vec, cos_vec)`.
    pub fn sincos(self) -> (vect2<T>, vect2<T>) {
        let (sin_x, cos_x) = self.x.sin_cos();
        let (sin_y, cos_y) = self.y.sin_cos();
        (vect2::<T>::new(sin_x, sin_y), vect2::<T>::new(cos_x, cos_y))
    }
}

impl<T: Float> Similar for vect2<T> {
    fn similar(self, other: vect2<T>) -> bool {
        (self - other).norm() < T::from(10).unwrap() * (self.norm() + other.norm() + T::epsilon()) * T::epsilon()
    }
}

impl<T: Float> ops::Index<usize> for vect2<T> {
    type Output = T;
    fn index(&self, index: usize) -> &T {
        match index {
            0 => &self.x,
            1 => &self.y,
            _ => panic!("arael::vect: index {} out of bounds", index)
        }
    }
}

impl<T: Float> ops::IndexMut<usize> for vect2<T> {
    fn index_mut(&mut self, index: usize) -> &mut T {
        match index {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => panic!("arael::vect: index {} out of bounds", index)
        }
    }
}

// Re-export symbolic companion types from arael-sym
pub use arael_sym::vect3sym;
pub use arael_sym::vect2sym;

#[cfg(test)]
mod tests {
    use super::*;

    // compare two vectors taking numerical noise into account
    fn equal<O: Similar>(a: O, b: O) -> bool {
        a.similar(b)
    }

    #[test]
    fn test() {
        let v1 = vect3d::new(2.0, 3.0, 6.0);
        let v2 = vect3d::new(5.0, 3.0, 4.0);
        // see if our equal actually works
        assert!(!equal(v1, v2));
        // construction method
        assert!(equal(v1, vect3d {x: 2.0, y: 3.0, z: 6.0}));
        // individual components
        assert_eq!(v1.x, 2.0); assert_eq!(v1.y, 3.0); assert_eq!(v1.z, 6.0);
        assert_eq!(v1[0], 2.0); assert_eq!(v1[1], 3.0); assert_eq!(v1[2], 6.0);
        // square
        assert_eq!(v1.square(), 49.0);
        // norm
        assert_eq!(v1.norm(), 7.0);
        // neg
        assert!(equal(-v1, vect3d::new(-2.0, -3.0, -6.0)));
        // multiplication by scalar
        assert!(equal(2.0*v1, vect3d::new(4.0, 6.0, 12.0)));
        assert!(equal(v1*2.0, vect3d::new(4.0, 6.0, 12.0)));
        // across sanity
        assert_eq!(v1.across() * v1, 0.0);
        // unit
        assert!((v1.unit().norm() - 1.0).abs() < 10.0 * f64::EPSILON);
        assert!(equal(v1.unit(), vect3d::new(2.0 / 7.0, 3.0 / 7.0, 6.0 / 7.0)));
        // cast sanity
        assert!(v1.cast::<f32>().cast::<f64>().similar(v1));
        // adding
        assert!(equal(v1 + v2, vect3d::new(7.0, 6.0, 10.0)));
        // substracting
        assert!(equal(v1 - v2, vect3d::new(-3.0, 0.0, 2.0)));
        // dot product
        assert_eq!(v1 * v1, 49.0);
        assert_eq!(v1 * v2, 43.0);
        // cross product
        assert!(equal(v1 % v2, vect3d::new(-6.0, 22.0, -9.0)));
        assert!(equal(v1 % (5.0*v1), vect3d::new(0.0, 0.0, 0.0)));
    }

    #[test]
    fn test_vect2() {
        let v1 = vect2d::new(3.0, 4.0);
        let v2 = vect2d::new(5.0, 3.0);
        // see if our equal actually works
        assert!(!equal(v1, v2));
        // construction method
        assert!(equal(v1, vect2d {x: 3.0, y: 4.0}));
        // individual components
        assert_eq!(v1.x, 3.0); assert_eq!(v1.y, 4.0);
        assert_eq!(v1[0], 3.0); assert_eq!(v1[1], 4.0);
        // square
        assert_eq!(v1.square(), 25.0);
        // norm
        assert_eq!(v1.norm(), 5.0);
        // neg
        assert!(equal(-v1, vect2::new(-3.0, -4.0)));
        // multiplication by scalar
        assert!(equal(2.0*v1, vect2d::new(6.0, 8.0)));
        assert!(equal(v1*2.0, vect2d::new(6.0, 8.0)));
        // across sanity
        assert_eq!(v1.across() * v1, 0.0);
        // unit
        assert!((v1.unit().norm() - 1.0).abs() < 10.0 * f64::EPSILON);
        assert!(equal(v1.unit(), vect2d::new(3.0 / 5.0, 4.0 / 5.0)));
        // cast sanity
        assert!(v1.cast::<f32>().cast::<f64>().similar(v1));
        // adding
        assert!(equal(v1 + v2, vect2d::new(8.0, 7.0)));
        // substracting
        assert!(equal(v1 - v2, vect2d::new(-2.0, 1.0)));
        // dot product
        assert_eq!(v1 * v1, 25.0);
        assert_eq!(v1 * v2, 27.0);
    }

    #[test]
    fn test_vect3_unit_is_unit_length() {
        let v = vect3d::new(3.0, -4.0, 12.0);
        let u = v.unit();
        assert!((u.square() - 1.0).abs() < 1e-14);
    }

    #[test]
    fn test_vect3_cross_product_perpendicular() {
        let a = vect3d::new(1.0, 0.0, 0.0);
        let b = vect3d::new(0.0, 1.0, 0.0);
        let c = a % b;
        assert!(c.similar(vect3d::new(0.0, 0.0, 1.0)));
        // cross product is perpendicular to both
        assert!((c * a).abs() < 1e-14);
        assert!((c * b).abs() < 1e-14);
    }

    #[test]
    fn test_vect3_is_finite() {
        assert!(vect3d::new(1.0, 2.0, 3.0).is_finite());
        assert!(!vect3d::new(f64::INFINITY, 0.0, 0.0).is_finite());
        assert!(!vect3d::new(0.0, f64::NAN, 0.0).is_finite());
    }

    #[test]
    fn test_vect3_deg_rad_roundtrip() {
        let v = vect3d::new(30.0, 45.0, 90.0);
        assert!(v.deg2rad().rad2deg().similar(v));
    }
}