ginger-rs 0.1.2

Parallel Bairstow Root-finding Method in Rust
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// #![no_std]
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
use num_traits::{Num, Signed, Zero};

/// The `Vector2` struct represents a 2-dimensional vector with elements of type `T`.
///
/// Properties:
///
/// * `x_`: The `x_` property represents the first element of the `Vector2` object. It is of type `T`,
///         which means it can be any type that is specified when creating an instance of `Vector2`.
/// * `y_`: The `y_` property is the second element of the `Vector2` object. It represents the
///         y-coordinate of a 2D vector.
///
/// # Examples:
///
/// ```
/// use ginger::vector2::Vector2;
///
/// assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4});
/// ```
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug, Default)]
pub struct Vector2<T> {
    /// The first element of the vector2 object
    pub x_: T,
    /// The second element of the vector2 object
    pub y_: T,
}

impl<T> Vector2<T> {
    /// Creates a new [`Vector2<T>`].
    ///
    /// The `new` function creates a new `Vector2` instance with the given `x` and `y` values.
    ///
    /// Arguments:
    ///
    /// * `x_`: The parameter `x_` represents the x-coordinate of the vector. It is of type `T`, which means
    ///         it can be any type that implements the necessary operations for vector calculations (e.g., addition,
    ///         subtraction, multiplication).
    /// * `y_`: The `y_` parameter represents the y-coordinate of the vector.
    ///
    /// Returns:
    ///
    /// The `new` function returns a new instance of the `Vector2<T>` struct.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4});
    /// ```
    #[inline]
    pub const fn new(x_: T, y_: T) -> Self {
        Vector2 { x_, y_ }
    }
}

impl<T: Clone + Num> Vector2<T> {
    /// The `dot` function calculates the dot product of two vectors.
    ///
    /// Arguments:
    ///
    /// * `other`: The `other` parameter is a reference to another `Vector2` object that we want to
    ///            calculate the dot product with.
    ///
    /// Returns:
    ///
    /// The dot product of two vectors is being returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3, 4);
    /// let other = &Vector2::new(5, 6);
    /// assert_eq!(vector2.dot(other), 15 + 24);
    /// assert_eq!(vector2.dot(&vector2), 9 + 16);
    /// ```
    #[inline]
    pub fn dot(&self, other: &Self) -> T {
        self.x_.clone() * other.x_.clone() + self.y_.clone() * other.y_.clone()
    }

    /// The `cross` function calculates the cross product of two vectors.
    ///
    /// Arguments:
    ///
    /// * `other`: The `other` parameter is a reference to another `Vector2` object that we want to
    ///            calculate the cross product with.
    ///
    /// Returns:
    ///
    /// The cross product of two vectors is being returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3, 4);
    /// let other = &Vector2::new(5, 6);
    /// assert_eq!(vector2.cross(other), 18 - 20);
    /// assert_eq!(vector2.cross(&vector2), 0);
    /// ```
    #[inline]
    pub fn cross(&self, other: &Self) -> T {
        self.x_.clone() * other.y_.clone() - self.y_.clone() * other.x_.clone()
    }

    /// Returns the norm sqr of this [`Vector2<T>`].
    ///
    /// The `norm_sqr` function calculates the squared norm of a `Vector2` object.
    ///
    /// Returns:
    ///
    /// The `norm_sqr` function returns the squared norm of the `Vector2<T>`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3, 4);
    /// assert_eq!(vector2.norm_sqr(), 9 + 16);
    /// ```
    #[inline]
    pub fn norm_sqr(&self) -> T {
        self.dot(self)
    }

    /// The `scale` function multiplies the x and y components of a `Vector2` object by a given scalar
    /// value.
    ///
    /// Arguments:
    ///
    /// * `alpha`: The parameter `alpha` represents the scaling factor that will be applied to the vector.
    ///
    /// Returns:
    ///
    /// The `scale` method returns a new `Vector2` object.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3.0, 4.0);
    /// assert_eq!(vector2.scale(10.0), Vector2::new(30.0, 40.0));
    /// assert_eq!(vector2.scale(0.5), Vector2::new(1.5, 2.0));
    /// ```
    #[inline]
    pub fn scale(&self, alpha: T) -> Self {
        Self::new(self.x_.clone() * alpha.clone(), self.y_.clone() * alpha)
    }

    /// The `unscale` function divides the x and y components of a `Vector2` by a given value.
    ///
    /// Arguments:
    ///
    /// * `alpha`: The `alpha` parameter is a value of type `T` that is used to divide the `x_` and `y_`
    ///            values of the `Vector2` object.
    ///
    /// Returns:
    ///
    /// The `unscale` method returns a new `Vector2` object.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(30, 40);
    /// assert_eq!(vector2.unscale(10), Vector2::new(3, 4));
    /// ```
    #[inline]
    pub fn unscale(&self, alpha: T) -> Self {
        Self::new(self.x_.clone() / alpha.clone(), self.y_.clone() / alpha)
    }
}

impl<T: Clone + Signed> Vector2<T> {
    /// The `l1_norm` function calculates the Manhattan distance from the origin for a 2D vector.
    ///
    /// [Manhattan distance]: https://en.wikipedia.org/wiki/Taxicab_geometry
    ///
    /// Returns:
    ///
    /// The function `l1_norm` returns the L1 norm of a `Vector2` object, which is the sum of the absolute
    /// values of its `x_` and `y_` components.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3, -4);
    /// assert_eq!(vector2.l1_norm(), 7);
    /// ```
    #[inline]
    pub fn l1_norm(&self) -> T {
        self.x_.abs() + self.y_.abs()
    }
}

impl<T: Clone + PartialOrd> Vector2<T> {
    /// The `norm_inf` function returns the maximum absolute value of the two elements in a `Vector2`
    /// object.
    ///
    /// Returns:
    ///
    /// The `norm_inf` function returns the maximum value between `self.x_` and `self.y_`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ginger::vector2::Vector2;
    ///
    /// let vector2 = &Vector2::new(3, -4);
    /// assert_eq!(vector2.norm_inf(), 3);
    /// ```
    #[inline]
    pub fn norm_inf(&self) -> T {
        if self.x_ > self.y_ {
            self.x_.clone()
        } else {
            self.y_.clone()
        }
    }
}

macro_rules! forward_xf_xf_binop {
    (impl $imp:ident, $method:ident) => {
        impl<'a, 'b, T: Clone + Num> $imp<&'b Vector2<T>> for &'a Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: &Vector2<T>) -> Self::Output {
                self.clone().$method(other.clone())
            }
        }
    };
}

macro_rules! forward_xf_val_binop {
    (impl $imp:ident, $method:ident) => {
        impl<'a, T: Clone + Num> $imp<Vector2<T>> for &'a Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: Vector2<T>) -> Self::Output {
                self.clone().$method(other)
            }
        }
    };
}

macro_rules! forward_val_xf_binop {
    (impl $imp:ident, $method:ident) => {
        impl<'a, T: Clone + Num> $imp<&'a Vector2<T>> for Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: &Vector2<T>) -> Self::Output {
                self.$method(other.clone())
            }
        }
    };
}

macro_rules! forward_all_binop {
    (impl $imp:ident, $method:ident) => {
        forward_xf_xf_binop!(impl $imp, $method);
        forward_xf_val_binop!(impl $imp, $method);
        forward_val_xf_binop!(impl $imp, $method);
    };
}

// arithmetic
forward_all_binop!(impl Add, add);

// (a, b) + (c, d) == (a + c), (b + d)
impl<T: Clone + Num> Add<Vector2<T>> for Vector2<T> {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self::Output {
        Self::Output::new(self.x_ + other.x_, self.y_ + other.y_)
    }
}

forward_all_binop!(impl Sub, sub);

// (a, b) - (c, d) == (a - c), (b - d)
impl<T: Clone + Num> Sub<Vector2<T>> for Vector2<T> {
    type Output = Self;

    #[inline]
    fn sub(self, other: Self) -> Self::Output {
        Self::Output::new(self.x_ - other.x_, self.y_ - other.y_)
    }
}

// Op Assign

mod opassign {
    use core::ops::{AddAssign, DivAssign, MulAssign, SubAssign};

    use num_traits::NumAssign;

    use crate::Vector2;

    impl<T: Clone + NumAssign> AddAssign for Vector2<T> {
        fn add_assign(&mut self, other: Self) {
            self.x_ += other.x_;
            self.y_ += other.y_;
        }
    }

    impl<T: Clone + NumAssign> SubAssign for Vector2<T> {
        fn sub_assign(&mut self, other: Self) {
            self.x_ -= other.x_;
            self.y_ -= other.y_;
        }
    }

    impl<T: Clone + NumAssign> MulAssign<T> for Vector2<T> {
        fn mul_assign(&mut self, other: T) {
            self.x_ *= other.clone();
            self.y_ *= other;
        }
    }

    impl<T: Clone + NumAssign> DivAssign<T> for Vector2<T> {
        fn div_assign(&mut self, other: T) {
            self.x_ /= other.clone();
            self.y_ /= other;
        }
    }

    macro_rules! forward_op_assign1 {
        (impl $imp:ident, $method:ident) => {
            impl<'a, T: Clone + NumAssign> $imp<&'a Vector2<T>> for Vector2<T> {
                #[inline]
                fn $method(&mut self, other: &Self) {
                    self.$method(other.clone())
                }
            }
        };
    }

    macro_rules! forward_op_assign2 {
        (impl $imp:ident, $method:ident) => {
            impl<'a, T: Clone + NumAssign> $imp<&'a T> for Vector2<T> {
                #[inline]
                fn $method(&mut self, other: &T) {
                    self.$method(other.clone())
                }
            }
        };
    }

    forward_op_assign1!(impl AddAssign, add_assign);
    forward_op_assign1!(impl SubAssign, sub_assign);
    forward_op_assign2!(impl MulAssign, mul_assign);
    forward_op_assign2!(impl DivAssign, div_assign);
}

impl<T: Clone + Num + Neg<Output = T>> Neg for Vector2<T> {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self::Output {
        Self::Output::new(-self.x_, -self.y_)
    }
}

impl<T: Clone + Num + Neg<Output = T>> Neg for &Vector2<T> {
    type Output = Vector2<T>;

    #[inline]
    fn neg(self) -> Self::Output {
        -self.clone()
    }
}

macro_rules! scalar_arithmetic {
    (@forward $imp:ident::$method:ident for $($scalar:ident),*) => (
        impl<'a, T: Clone + Num> $imp<&'a T> for Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: &T) -> Self::Output {
                self.$method(other.clone())
            }
        }
        impl<'a, T: Clone + Num> $imp<T> for &'a Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: T) -> Self::Output {
                self.clone().$method(other)
            }
        }
        impl<'a, 'b, T: Clone + Num> $imp<&'a T> for &'b Vector2<T> {
            type Output = Vector2<T>;

            #[inline]
            fn $method(self, other: &T) -> Self::Output {
                self.clone().$method(other.clone())
            }
        }
        $(
            impl<'a> $imp<&'a Vector2<$scalar>> for $scalar {
                type Output = Vector2<$scalar>;

                #[inline]
                fn $method(self, other: &Vector2<$scalar>) -> Vector2<$scalar> {
                    self.$method(other.clone())
                }
            }
            impl<'a> $imp<Vector2<$scalar>> for &'a $scalar {
                type Output = Vector2<$scalar>;

                #[inline]
                fn $method(self, other: Vector2<$scalar>) -> Vector2<$scalar> {
                    self.clone().$method(other)
                }
            }
            impl<'a, 'b> $imp<&'a Vector2<$scalar>> for &'b $scalar {
                type Output = Vector2<$scalar>;

                #[inline]
                fn $method(self, other: &Vector2<$scalar>) -> Vector2<$scalar> {
                    self.clone().$method(other.clone())
                }
            }
        )*
    );
    ($($scalar:ident),*) => (
        scalar_arithmetic!(@forward Mul::mul for $($scalar),*);
        // scalar_arithmetic!(@forward Div::div for $($scalar),*);
        // scalar_arithmetic!(@forward Rem::rem for $($scalar),*);

        $(
            impl Mul<Vector2<$scalar>> for $scalar {
                type Output = Vector2<$scalar>;

                #[inline]
                fn mul(self, other: Vector2<$scalar>) -> Self::Output {
                    Self::Output::new(self * other.x_, self * other.y_)
                }
            }

        )*
    );
}

impl<T: Clone + Num> Mul<T> for Vector2<T> {
    type Output = Vector2<T>;

    #[inline]
    fn mul(self, other: T) -> Self::Output {
        Self::Output::new(self.x_ * other.clone(), self.y_ * other)
    }
}

impl<T: Clone + Num> Div<T> for Vector2<T> {
    type Output = Self;

    #[inline]
    fn div(self, other: T) -> Self::Output {
        Self::Output::new(self.x_ / other.clone(), self.y_ / other)
    }
}

impl<T: Clone + Num> Rem<T> for Vector2<T> {
    type Output = Vector2<T>;

    #[inline]
    fn rem(self, other: T) -> Self::Output {
        Self::Output::new(self.x_ % other.clone(), self.y_ % other)
    }
}

scalar_arithmetic!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64);

// constants
impl<T: Clone + Num> Zero for Vector2<T> {
    #[inline]
    fn zero() -> Self {
        Self::new(Zero::zero(), Zero::zero())
    }

    #[inline]
    fn is_zero(&self) -> bool {
        self.x_.is_zero() && self.y_.is_zero()
    }

    #[inline]
    fn set_zero(&mut self) {
        self.x_.set_zero();
        self.y_.set_zero();
    }
}

// #[cfg(test)]
// fn hash<T: hash::Hash>(x: &T) -> u64 {
//     use std::collections::hash_map::RandomState;
//     use std::hash::{BuildHasher, Hasher};
//     let mut hasher = <RandomState as BuildHasher>::Hasher::new();
//     x.hash(&mut hasher);
//     hasher.finish()
// }

#[cfg(test)]
mod test {
    #![allow(non_upper_case_globals)]

    // use super::{hash, Vector2};
    use super::Vector2;
    use core::f64;
    use num_traits::Zero;

    pub const _0_0v: Vector2<f64> = Vector2 { x_: 0.0, y_: 0.0 };
    pub const _1_0v: Vector2<f64> = Vector2 { x_: 1.0, y_: 0.0 };
    pub const _1_1v: Vector2<f64> = Vector2 { x_: 1.0, y_: 1.0 };
    pub const _0_1v: Vector2<f64> = Vector2 { x_: 0.0, y_: 1.0 };
    pub const _neg1_1v: Vector2<f64> = Vector2 { x_: -1.0, y_: 1.0 };
    pub const _05_05v: Vector2<f64> = Vector2 { x_: 0.5, y_: 0.5 };
    pub const all_consts: [Vector2<f64>; 5] = [_0_0v, _1_0v, _1_1v, _neg1_1v, _05_05v];
    pub const _4_2v: Vector2<f64> = Vector2 { x_: 4.0, y_: 2.0 };

    #[test]
    fn test_consts() {
        // check our constants are what Vector2::new creates
        fn test(c: Vector2<f64>, r: f64, i: f64) {
            assert_eq!(c, Vector2::new(r, i));
        }
        test(_0_0v, 0.0, 0.0);
        test(_1_0v, 1.0, 0.0);
        test(_1_1v, 1.0, 1.0);
        test(_neg1_1v, -1.0, 1.0);
        test(_05_05v, 0.5, 0.5);
        assert_eq!(_0_0v, Zero::zero());
    }

    #[test]
    fn test_scale_unscale() {
        assert_eq!(_05_05v.scale(2.0), _1_1v);
        assert_eq!(_1_1v.unscale(2.0), _05_05v);
        for &c in all_consts.iter() {
            assert_eq!(c.scale(2.0).unscale(2.0), c);
        }
    }

    // #[test]
    // fn test_hash() {
    //     let a = Vector2::new(0i32, 0i32);
    //     let b = Vector2::new(1i32, 0i32);
    //     let c = Vector2::new(0i32, 1i32);
    //     assert!(hash(&a) != hash(&b));
    //     assert!(hash(&b) != hash(&c));
    //     assert!(hash(&c) != hash(&a));
    // }

    #[test]
    fn test_new() {
        let v = Vector2::new(1, 2);
        assert_eq!(v.x_, 1);
        assert_eq!(v.y_, 2);
    }

    #[test]
    fn test_dot() {
        let v1 = Vector2::new(3, 4);
        let v2 = Vector2::new(5, 6);
        assert_eq!(v1.dot(&v2), 3 * 5 + 4 * 6);
        assert_eq!(v1.dot(&v1), 3 * 3 + 4 * 4);
    }

    #[test]
    fn test_cross() {
        let v1 = Vector2::new(3, 4);
        let v2 = Vector2::new(5, 6);
        assert_eq!(v1.cross(&v2), 3 * 6 - 4 * 5);
        assert_eq!(v1.cross(&v1), 0);
    }

    #[test]
    fn test_norm_sqr() {
        let v = Vector2::new(3, 4);
        assert_eq!(v.norm_sqr(), 9 + 16);
    }

    #[test]
    fn test_scale() {
        let v = Vector2::new(3.0, 4.0);
        assert_eq!(v.scale(2.0), Vector2::new(6.0, 8.0));
        assert_eq!(v.scale(0.5), Vector2::new(1.5, 2.0));
    }

    #[test]
    fn test_unscale() {
        let v = Vector2::new(30, 40);
        assert_eq!(v.unscale(10), Vector2::new(3, 4));
    }

    #[test]
    fn test_l1_norm() {
        let v = Vector2::new(3, -4);
        assert_eq!(v.l1_norm(), 7);
    }

    #[test]
    fn test_norm_inf() {
        let v1 = Vector2::new(3, -4);
        assert_eq!(v1.norm_inf(), 3);

        let v2 = Vector2::new(5, 2);
        assert_eq!(v2.norm_inf(), 5);
    }

    #[test]
    fn test_add() {
        let v1 = Vector2::new(1, 2);
        let v2 = Vector2::new(3, 4);
        assert_eq!(v1 + v2, Vector2::new(4, 6));

        let v3 = v1 + v2;
        assert_eq!(v3, Vector2::new(4, 6));

        let v4 = v1 + &v2;
        assert_eq!(v4, Vector2::new(4, 6));
    }

    #[test]
    fn test_sub() {
        let v1 = Vector2::new(5, 6);
        let v2 = Vector2::new(3, 4);
        assert_eq!(v1 - v2, Vector2::new(2, 2));

        let v3 = v1 - v2;
        assert_eq!(v3, Vector2::new(2, 2));
    }

    #[test]
    fn test_neg() {
        let v = Vector2::new(1, -2);
        assert_eq!(-v, Vector2::new(-1, 2));
        assert_eq!(-&v, Vector2::new(-1, 2));
    }

    #[test]
    fn test_scalar_mul() {
        let v = Vector2::new(2, 3);
        assert_eq!(v * 4, Vector2::new(8, 12));
        assert_eq!(&v * 4, Vector2::new(8, 12));
        assert_eq!(4 * v, Vector2::new(8, 12));
        assert_eq!(4 * &v, Vector2::new(8, 12));
    }

    #[test]
    fn test_scalar_div() {
        let v = Vector2::new(10, 20);
        assert_eq!(v / 5, Vector2::new(2, 4));
    }

    #[test]
    fn test_scalar_rem() {
        let v = Vector2::new(10, 21);
        assert_eq!(v % 3, Vector2::new(1, 0));
    }

    #[test]
    fn test_zero() {
        let zero = Vector2::<i32>::zero();
        assert_eq!(zero, Vector2::new(0, 0));
        assert!(zero.is_zero());

        let mut v = Vector2::new(1, 2);
        assert!(!v.is_zero());
        v.set_zero();
        assert!(v.is_zero());
    }

    #[test]
    fn test_add_assign() {
        let mut v1 = Vector2::new(1, 2);
        let v2 = Vector2::new(3, 4);
        v1 += v2;
        assert_eq!(v1, Vector2::new(4, 6));

        let mut v3 = Vector2::new(1, 2);
        v3 += &v2;
        assert_eq!(v3, Vector2::new(4, 6));
    }

    #[test]
    fn test_sub_assign() {
        let mut v1 = Vector2::new(5, 6);
        let v2 = Vector2::new(3, 4);
        v1 -= v2;
        assert_eq!(v1, Vector2::new(2, 2));

        let mut v3 = Vector2::new(5, 6);
        v3 -= &v2;
        assert_eq!(v3, Vector2::new(2, 2));
    }

    #[test]
    fn test_mul_assign() {
        let mut v = Vector2::new(1, 2);
        v *= 3;
        assert_eq!(v, Vector2::new(3, 6));

        let mut v2 = Vector2::new(1, 2);
        let scalar = 3;
        v2 *= &scalar;
        assert_eq!(v2, Vector2::new(3, 6));
    }

    #[test]
    fn test_div_assign() {
        let mut v = Vector2::new(6, 9);
        v /= 3;
        assert_eq!(v, Vector2::new(2, 3));

        let mut v2 = Vector2::new(6, 9);
        let scalar = 3;
        v2 /= &scalar;
        assert_eq!(v2, Vector2::new(2, 3));
    }

    #[test]
    fn test_float_operations() {
        let v = Vector2::new(1.5, 2.5);
        assert_eq!(v.scale(2.0), Vector2::new(3.0, 5.0));
        assert_eq!(v.unscale(0.5), Vector2::new(3.0, 5.0));
        assert_eq!(v.dot(&v), 1.5 * 1.5 + 2.5 * 2.5);
    }

    #[test]
    fn test_clone_and_eq() {
        let v1 = Vector2::new(1, 2);
        let v2 = v1;
        assert_eq!(v1, v2);

        let v3 = Vector2::new(2, 1);
        assert_ne!(v1, v3);
    }

    #[test]
    fn test_debug() {
        let v = Vector2::new(1, 2);
        assert_eq!(format!("{:?}", v), "Vector2 { x_: 1, y_: 2 }");
    }
}