zmatrix 0.3.1

This is a matrix calculation lib
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
use std::f64::consts::PI;
use std::ops::{Add, Div, Mul, Neg, Sub};
use std::time::Duration;

use crate::physics::basic::{AngularVelocity, MagneticInduction, MagneticAngularVelocity};
use super::*;
impl AngularVelocity {
    pub fn from_rad_per_second(v: f64) -> Self {
        AngularVelocity {
            default_type: AngularVelocityType::RadperSecond,
            v,
        }
    }

    pub fn from_deg_per_second(v: f64) -> Self {
        AngularVelocity {
            default_type: AngularVelocityType::DegPerSecond,
            v,
        }
    }
    pub fn from_deg_per_hour(v: f64) -> Self {
        AngularVelocity {
            default_type: AngularVelocityType::DegperHour,
            v,
        }
    }

    pub fn from_rad_per_hour(v: f64) -> Self {
        AngularVelocity {
            default_type: AngularVelocityType::RadperHour,
            v,
        }
    }

    pub fn as_rad_per_second(&self) -> f64 {
        return match self.default_type {
            AngularVelocityType::RadperSecond => self.v,
            AngularVelocityType::DegPerSecond => { self.v * PI / 180.0 }
            AngularVelocityType::RadperHour => { self.v / 3600.0 }
            AngularVelocityType::DegperHour => { self.v * PI / 180.0 / 3600.0 }
        };
    }

    pub fn as_deg_per_second(&self) -> f64 {
        return match self.default_type {
            AngularVelocityType::RadperSecond => { self.v * 180.0 / PI }
            AngularVelocityType::DegPerSecond => { self.v }
            AngularVelocityType::RadperHour => { self.v * 180.0 / PI / 3600.0 }
            AngularVelocityType::DegperHour => { self.v / 3600.0 }
        };
    }

    pub fn as_rad_per_hour(&self) -> f64 {
        return match self.default_type {
            AngularVelocityType::RadperSecond => { self.v * 3600.0 }
            AngularVelocityType::DegPerSecond => { self.v * 3600.0 * PI / 180.0 }
            AngularVelocityType::RadperHour => { self.v }
            AngularVelocityType::DegperHour => { self.v * PI / 180.0 }
        };
    }

    pub fn as_deg_per_hour(&self) -> f64 {
        return match self.default_type {
            AngularVelocityType::RadperSecond => { self.v * 3600.0 * 180.0 / PI }
            AngularVelocityType::DegPerSecond => { self.v * 3600.0 }
            AngularVelocityType::RadperHour => { self.v * 180.0 / PI }
            AngularVelocityType::DegperHour => { self.v }
        };
    }
}


impl PhysicalQuantity for AngularVelocity {
    fn as_any(&self) -> &dyn Any {
        self
}
    fn is_zero(&self) -> bool {
        self.v == 0.0
    }

    fn default_unit_value(&self) -> f64 {
        self.as_rad_per_second()
    }

    fn set_value(&mut self, value: f64) {
        self.v = value;
    }
    fn unit_display_name(&self) -> String {
        "AngularVelocity (rad/s)".to_string()
    }
}

impl Mul<Duration> for AngularVelocity {
    type Output = Angular;
    fn mul(self, rhs: Duration) -> Self::Output {
        let v = self.as_rad_per_second() * rhs.as_secs_f64();
        Angular::from_rad(v)
    }
}

impl Mul<chrono::TimeDelta> for AngularVelocity {
    type Output = Angular;
    fn mul(self, rhs: chrono::TimeDelta) -> Self::Output {
        let v = self.as_rad_per_second() * time_delta_to_secs_f64(&rhs);
        Angular::from_rad(v)
    }
}


impl Div<Duration> for AngularVelocity {
    type Output = AngularAcceleration;

    fn div(self, rhs: Duration) -> Self::Output {
        let v = self.as_rad_per_second() / rhs.as_secs_f64();
        AngularAcceleration::from_rad_per_second2(v)
    }
}

impl Div<chrono::TimeDelta> for AngularVelocity {
    type Output = AngularAcceleration;

    fn div(self, rhs: chrono::TimeDelta) -> Self::Output {
        let v = self.as_rad_per_second() / time_delta_to_secs_f64(&rhs);
        AngularAcceleration::from_rad_per_second2(v)
    }
}

impl Add for AngularVelocity {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let v = self.as_rad_per_second() + rhs.as_rad_per_second();
        AngularVelocity::from_rad_per_second(v)
    }
}

// 引用-引用 与 混合引用:AngularVelocity 加法
impl<'a, 'b> Add<&'b AngularVelocity> for &'a AngularVelocity {
    type Output = AngularVelocity;
    fn add(self, rhs: &'b AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() + rhs.as_rad_per_second()) }
}
impl<'a> Add<&'a AngularVelocity> for AngularVelocity {
    type Output = AngularVelocity;
    fn add(self, rhs: &'a AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() + rhs.as_rad_per_second()) }
}
impl<'a> Add<AngularVelocity> for &'a AngularVelocity {
    type Output = AngularVelocity;
    fn add(self, rhs: AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() + rhs.as_rad_per_second()) }
}

impl Add<f64> for AngularVelocity {
    type Output = Self;
    fn add(self, rhs: f64) -> Self::Output {
        let v = self.v + rhs;
        AngularVelocity {
            v,
            default_type: self.default_type,
        }
    }
}

impl Sub for AngularVelocity {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        let v = self.as_rad_per_second() - rhs.as_rad_per_second();
        AngularVelocity::from_rad_per_second(v)
    }
}

// 引用-引用 与 混合引用:AngularVelocity 减法
impl<'a, 'b> Sub<&'b AngularVelocity> for &'a AngularVelocity {
    type Output = AngularVelocity;
    fn sub(self, rhs: &'b AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() - rhs.as_rad_per_second()) }
}
impl<'a> Sub<&'a AngularVelocity> for AngularVelocity {
    type Output = AngularVelocity;
    fn sub(self, rhs: &'a AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() - rhs.as_rad_per_second()) }
}
impl<'a> Sub<AngularVelocity> for &'a AngularVelocity {
    type Output = AngularVelocity;
    fn sub(self, rhs: AngularVelocity) -> Self::Output { AngularVelocity::from_rad_per_second(self.as_rad_per_second() - rhs.as_rad_per_second()) }
}

impl Sub<f64> for AngularVelocity {
    type Output = Self;
    fn sub(self, rhs: f64) -> Self::Output {
        let v = self.v - rhs;
        AngularVelocity {
            v,
            default_type: self.default_type,
        }
    }
}

impl Mul<Distance> for AngularVelocity {
    type Output = Velocity;
    fn mul(self, rhs: Distance) -> Self::Output {
        let v = self.as_rad_per_second() * rhs.as_m();
        Velocity::from_m_per_sec(v)
    }
}

// 引用版本:AngularVelocity * Distance -> Velocity
impl<'a, 'b> Mul<&'b Distance> for &'a AngularVelocity {
    type Output = Velocity;
    fn mul(self, rhs: &'b Distance) -> Self::Output { Velocity::from_m_per_sec(self.as_rad_per_second() * rhs.as_m()) }
}
impl<'a> Mul<&'a Distance> for AngularVelocity {
    type Output = Velocity;
    fn mul(self, rhs: &'a Distance) -> Self::Output { Velocity::from_m_per_sec(self.as_rad_per_second() * rhs.as_m()) }
}
impl<'a> Mul<Distance> for &'a AngularVelocity {
    type Output = Velocity;
    fn mul(self, rhs: Distance) -> Self::Output { Velocity::from_m_per_sec(self.as_rad_per_second() * rhs.as_m()) }
}

impl Mul<f64> for AngularVelocity {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self::Output {
        let v = self.as_rad_per_second() * rhs;
        Self::from_rad_per_second(v)
    }
}

impl Div<f64> for AngularVelocity {
    type Output = Self;
    fn div(self, rhs: f64) -> Self::Output {
        let v = self.as_rad_per_second() / rhs;
        Self::from_rad_per_second(v)
    }
}

impl Mul<AngularVelocity> for f64 {
    type Output = AngularVelocity;
    fn mul(self, rhs: AngularVelocity) -> Self::Output {
        rhs * self
    }
}

impl Div<AngularVelocity> for f64 {
    type Output = AngularVelocity;
    fn div(self, rhs: AngularVelocity) -> Self::Output {
        let v = self / rhs.v;
        AngularVelocity {
            default_type: rhs.default_type,
            v: v,
        }
    }
}

impl Mul<Coef> for AngularVelocity {
    type Output = Self;
    fn mul(self, rhs: Coef) -> Self::Output {
        let v = self.as_rad_per_second() * rhs.get_value();
        Self::from_rad_per_second(v)
    }
}

impl Div<Coef> for AngularVelocity {
    type Output = Self;
    fn div(self, rhs: Coef) -> Self::Output {
        let v = self.as_rad_per_second() / rhs.get_value();
        Self::from_rad_per_second(v)
    }
}

// 角速度 × 角动量 = 力矩(满足交换律)
impl Mul<AngularMomentum> for AngularVelocity {
    type Output = Torque;
    fn mul(self, rhs: AngularMomentum) -> Self::Output {
        let torque_value = self.as_rad_per_second() * rhs.as_kg_m2_per_second();
        Torque::from_nm(torque_value)
    }
}

// 角速度 × 磁感应强度 = 磁角速度(满足交换律)
impl Mul<MagneticInduction> for AngularVelocity {
    type Output = MagneticAngularVelocity;
    fn mul(self, rhs: MagneticInduction) -> Self::Output {
        let rad_per_second_value = self.as_rad_per_second();
        let tesla_value = rhs.as_tesla();
        let result_value = rad_per_second_value * tesla_value;
        MagneticAngularVelocity::from_tesla_rad_per_second(result_value)
    }
}

impl Div for AngularVelocity {
    type Output = Coef;
    fn div(self, rhs: Self) -> Self::Output {
        let v = self.as_rad_per_second() / rhs.as_rad_per_second();
        Coef::new(v)
    }
}

// 引用版本:AngularVelocity / AngularVelocity -> Coef
impl<'a, 'b> Div<&'b AngularVelocity> for &'a AngularVelocity {
    type Output = Coef;
    fn div(self, rhs: &'b AngularVelocity) -> Self::Output { Coef::new(self.as_rad_per_second() / rhs.as_rad_per_second()) }
}
impl<'a> Div<&'a AngularVelocity> for AngularVelocity {
    type Output = Coef;
    fn div(self, rhs: &'a AngularVelocity) -> Self::Output { Coef::new(self.as_rad_per_second() / rhs.as_rad_per_second()) }
}
impl<'a> Div<AngularVelocity> for &'a AngularVelocity {
    type Output = Coef;
    fn div(self, rhs: AngularVelocity) -> Self::Output { Coef::new(self.as_rad_per_second() / rhs.as_rad_per_second()) }
}

// 角速度 ÷ 角加速度 = 有符号时间
impl Div<AngularAcceleration> for AngularVelocity {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: AngularAcceleration) -> Self::Output {
        let time_value = self.as_rad_per_second() / rhs.as_rad_per_second2();
        time_delta_from_secs_f64(time_value)
    }
}

// 引用版本:AngularVelocity / AngularAcceleration -> TimeDelta
impl<'a, 'b> Div<&'b AngularAcceleration> for &'a AngularVelocity {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: &'b AngularAcceleration) -> Self::Output { time_delta_from_secs_f64(self.as_rad_per_second() / rhs.as_rad_per_second2()) }
}
impl<'a> Div<&'a AngularAcceleration> for AngularVelocity {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: &'a AngularAcceleration) -> Self::Output { time_delta_from_secs_f64(self.as_rad_per_second() / rhs.as_rad_per_second2()) }
}
impl<'a> Div<AngularAcceleration> for &'a AngularVelocity {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: AngularAcceleration) -> Self::Output { time_delta_from_secs_f64(self.as_rad_per_second() / rhs.as_rad_per_second2()) }
}

impl Neg for AngularVelocity {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let v = self.as_rad_per_second();
        Self::from_rad_per_second(-v)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    #[test]
    fn test_angular_velocity_from() {
        let v = AngularVelocity::from_deg_per_second(1000.0);
        assert_eq!(v.default_type, AngularVelocityType::DegPerSecond);
        assert_eq!(v.v, 1000.0);
        let v = AngularVelocity::from_rad_per_second(1000.0);
        assert_eq!(v.default_type, AngularVelocityType::RadperSecond);
        assert_eq!(v.v, 1000.0);
        let v = AngularVelocity::from_rad_per_hour(1000.0);
        assert_eq!(v.default_type, AngularVelocityType::RadperHour);
        assert_eq!(v.v, 1000.0);
        let v = AngularVelocity::from_deg_per_hour(1000.0);
        assert_eq!(v.default_type, AngularVelocityType::DegperHour);
        assert_eq!(v.v, 1000.0);
    }


    #[test]
    fn test_angular_velocity_convert() {
        {
            // from_rad_per_second
            let omg = AngularVelocity::from_rad_per_second(PI * 0.5);
            assert_eq!(omg.as_rad_per_second(), PI * 0.5);
            assert_relative_eq!(omg.as_deg_per_second(),90.0);
            assert_relative_eq!(omg.as_rad_per_hour(), PI * 0.5*3600.0);
            assert_relative_eq!(omg.as_deg_per_hour(), 90.0*3600.0);
        }
        {
            let omg = AngularVelocity::from_deg_per_second(30.0);
            assert_relative_eq!(omg.as_deg_per_second(), 30.0);
            assert_relative_eq!(omg.as_rad_per_second(), PI / 6.0);
            assert_relative_eq!(omg.as_rad_per_hour(),3600.0 * PI / 6.0);
            assert_relative_eq!(omg.as_deg_per_hour(), 30.0*3600.0);
        }
        {
            // 36 PI 是18圈
            let omg = AngularVelocity::from_rad_per_hour(36.0 * PI);
            assert_relative_eq!(omg.as_rad_per_hour(), 36.0*PI);
            assert_relative_eq!(omg.as_deg_per_hour(), 18.0*360.0);
            assert_relative_eq!(omg.as_rad_per_second(),0.01 * PI);
            assert_relative_eq!(omg.as_deg_per_second(),1.8);
        }
        {
            // 7200 °是20圈
            let omg = AngularVelocity::from_deg_per_hour(7200.0);
            assert_relative_eq!(omg.as_deg_per_hour(),7200.0);
            assert_relative_eq!(omg.as_rad_per_hour(),20.0 * 2.0 * PI);
            assert_relative_eq!(omg.as_deg_per_second(),2.0);
            assert_relative_eq!(omg.as_rad_per_second(),0.034906585041,epsilon = 1e-8);
        }
    }

    #[test]
    fn test_angular_velocity_change() {
        let omg1 = AngularVelocity::from_rad_per_second(10.0);
        let duration = Duration::from_secs_f64(2.0);
        let theta1 = omg1 * duration;
        assert_eq!(theta1.default_type, AngularType::Rad);
        assert_eq!(theta1.as_rad(), 20.0);

        let omg1 = AngularVelocity::from_rad_per_second(10.0);
        let duration = Duration::from_secs_f64(2.0);
        let alpha = omg1 / duration;
        assert_eq!(alpha.default_type, AngularAccelerationType::RadperSecond2);
        assert_relative_eq!(alpha.as_rad_per_second2(),5.0);
    }

    #[test]
    fn test_angular_velocity_change_timedelta() {
        let omg = AngularVelocity::from_rad_per_second(10.0);
        let td = time_delta_from_secs_f64(2.0);
        let theta = omg * td;
        assert_eq!(theta.as_rad(), 20.0);

        // 负时间 → 负角度
        let neg_td = time_delta_from_secs_f64(-2.0);
        let theta_neg = omg * neg_td;
        assert_eq!(theta_neg.as_rad(), -20.0);

        // 除法:AngularVelocity / TimeDelta → AngularAcceleration
        let alpha = omg / td;
        assert_relative_eq!(alpha.as_rad_per_second2(), 5.0);

        // 负时间 → 负角加速度
        let alpha_neg = omg / neg_td;
        assert_relative_eq!(alpha_neg.as_rad_per_second2(), -5.0);
    }

    #[test]
    fn test_angular_velocity_sub() {
        {
            let a1 = AngularVelocity::from_rad_per_second(1000.0);
            let a2 = AngularVelocity::from_rad_per_second(1000.0);
            let a3 = a1 - a2;
            assert_eq!(a3.as_rad_per_second(), 0.0);

            let a1 = AngularVelocity::from_rad_per_second(0.5 * PI);
            let a2 = AngularVelocity::from_deg_per_second(60.0);
            let a3 = a1 - a2;
            assert_relative_eq!(a3.as_deg_per_second(), 30.0,epsilon = 1e-8);
        }
        {
            let a1 = AngularVelocity::from_rad_per_second(1000.0);
            let a2 = a1 - 100.0;
            assert_eq!(a2.as_rad_per_second(), 900.0);

            let a1 = AngularVelocity::from_deg_per_second(1000.0);
            let a2 = a1 - 100.0;
            assert_eq!(a2.as_deg_per_second(), 900.0);

            let a1 = AngularVelocity::from_rad_per_hour(2.0);
            let a2 = a1 - 1.5;
            assert_relative_eq!(a2.as_rad_per_hour(), 0.5,epsilon = 1e-8);

            let a1 = AngularVelocity::from_deg_per_hour(2.0);
            let a2 = a1 - 1.5;
            assert_relative_eq!(a2.as_deg_per_hour(), 0.5,epsilon = 1e-8);
        }
    }

    #[test]
    fn test_angular_velocity_add() {
        {
            let a1 = AngularVelocity::from_rad_per_second(1000.0);
            let a2 = AngularVelocity::from_rad_per_second(1000.0);
            let a3 = a1 + a2;
            assert_eq!(a3.as_rad_per_second(), 2000.0);

            let a1 = AngularVelocity::from_deg_per_second(10.0);
            let a2 = AngularVelocity::from_deg_per_second(1000.0);
            let a3 = a1 + a2;
            assert_relative_eq!(a3.as_deg_per_second(),1010.0,epsilon = 1e-8);

            let a1 = AngularVelocity::from_rad_per_hour(1000.0);
            let a2 = AngularVelocity::from_rad_per_hour(10.0);
            let a3 = a1 + a2;
            assert_relative_eq!(a3.as_rad_per_hour(),1010.0,epsilon = 1e-8);

            let a1 = AngularVelocity::from_deg_per_hour(10.0);
            let a2 = AngularVelocity::from_deg_per_hour(1.0);
            let a3 = a1 + a2;
            assert_relative_eq!(a3.as_deg_per_hour(), 11.0,epsilon = 1e-8);

            let a1 = AngularVelocity::from_rad_per_second(0.5 * PI);
            let a2 = AngularVelocity::from_deg_per_second(30.0);
            let a3 = a1 + a2;
            assert_relative_eq!(a3.as_deg_per_second(), 120.0,epsilon = 1e-8);
        }
        {
            let a1 = AngularVelocity::from_rad_per_second(1000.0);
            let a2 = a1 + 1.0;
            assert_relative_eq!(a2.as_rad_per_second(), 1001.0);

            let a1 = AngularVelocity::from_deg_per_second(1000.0);
            let a2 = a1 + 1.0;
            assert_relative_eq!(a2.as_deg_per_second(),1001.0);

            let a1 = AngularVelocity::from_rad_per_hour(1.5);
            let a2 = a1 + 1.5;
            assert_relative_eq!(a2.as_rad_per_hour(), 3.0);

            let a1 = AngularVelocity::from_deg_per_hour(1.5);
            let a2 = a1 + 1.5;
            assert_relative_eq!(a2.as_deg_per_hour(), 3.0);
        }
    }

    #[test]
    fn test_to_mul() {
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = d1 * 2.0;
        assert_eq!(d2.as_rad_per_second(), 2000.0);
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = d1 * Coef::new(2.0);
        assert_eq!(d2.as_rad_per_second(), 2000.0);
    }

    #[test]
    fn test_to_div() {
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = d1 / 2.0;
        assert_eq!(d2.as_rad_per_second(), 500.0);
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = d1 / Coef::new(2.0);
        assert_eq!(d2.as_rad_per_second(), 500.0);

        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = AngularVelocity::from_rad_per_second(2000.0);
        let d3 = d1 / d2;
        assert_relative_eq!(d3.get_value(),0.5,epsilon = 1e-8);
    }

    #[test]
    fn test_convert_to_velocity() {
        let omg1 = AngularVelocity::from_rad_per_second(PI);
        let v1 = omg1 * Distance::from_m(2.0);
        assert_eq!(v1.default_type, VelocityType::MPerSecond);
        assert_relative_eq!(v1.as_m_per_sec(),6.283185307180,epsilon = 1e-8);
    }

    #[test]
    fn test_as_any() {
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        let d2 = d1.as_any();
        let d3 = d2.downcast_ref::<AngularVelocity>().unwrap();
    }

    #[test]
    fn test_is_zero() {
        let d1 = AngularVelocity::from_rad_per_second(0.0);
        assert!(d1.is_zero());
        let d1 = AngularVelocity::from_rad_per_second(1000.0);
        assert!(!d1.is_zero());
    }

    #[test]
    fn test_default_unit_value() {
        let d1 = AngularVelocity::from_deg_per_second(180.0);
        assert_eq!(d1.default_unit_value(), PI);

        let mut d1 = AngularVelocity::from_rad_per_second(1000.0);
        d1.set_value(100.0);
        assert_relative_eq!(d1.v,100.0);
    }

    #[test]
    fn test_negative() {
        let d1 = AngularVelocity::from_rad_per_second(-10.0);
        let d2 = -d1;
        assert_relative_eq!(d2.as_rad_per_second(),10.0)
    }

    #[test]
    fn test_f64_mul_angular_velocity() {
        // f64 * AngularVelocity 测试
        let a = AngularVelocity::from_rad_per_second(2.0);
        let result = 3.0 * a;
        assert_relative_eq!(result.as_rad_per_second(), 6.0);

        let a = AngularVelocity::from_deg_per_second(180.0);
        let result = 2.0 * a;
        assert_relative_eq!(result.as_deg_per_second(), 360.0);

        let a = AngularVelocity::from_rad_per_hour(PI);
        let result = 2.0 * a;
        assert_relative_eq!(result.as_rad_per_hour(), 2.0 * PI);
    }

    #[test]
    fn test_f64_div_angular_velocity() {
        // f64 / AngularVelocity 测试
        let a = AngularVelocity::from_rad_per_second(2.0);
        let result = 6.0 / a;
        assert_relative_eq!(result.as_rad_per_second(), 3.0);

        let a = AngularVelocity::from_deg_per_second(180.0);
        let result = 180.0 / a;
        assert_relative_eq!(result.as_deg_per_second(), 1.0);

        let a = AngularVelocity::from_rad_per_hour(PI);
        let result = 2.0 * PI / a;
        assert_relative_eq!(result.as_rad_per_hour(), 2.0);
    }

    #[test]
    fn test_angular_velocity_mul_angular_momentum() {
        // 角速度 × 角动量 = 力矩(满足交换律)
        let omega = AngularVelocity::from_rad_per_second(5.0);
        let l = AngularMomentum::from_kg_m2_per_second(10.0);
        let torque = omega * l;
        assert_relative_eq!(torque.as_nm(), 50.0);

        // 测试不同单位
        let omega = AngularVelocity::from_deg_per_second(180.0);
        let l = AngularMomentum::from_kg_km2_per_second(1.0);
        let torque = omega * l;
        assert_relative_eq!(torque.as_nm(), 1e6 * std::f64::consts::PI);
    }

    #[test]
    fn test_angular_velocity_div_angular_acceleration() {
        // 基本测试:弧度每秒 / 弧度每秒² = 秒
        {
            let omega = AngularVelocity::from_rad_per_second(10.0);
            let alpha = AngularAcceleration::from_rad_per_second2(2.0);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 5.0);
        }

        // 基本测试:度每秒 / 度每秒² = 秒
        {
            let omega = AngularVelocity::from_deg_per_second(180.0);
            let alpha = AngularAcceleration::from_deg_per_second2(90.0);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 混合单位测试:度每秒 / 弧度每秒²
        {
            let omega = AngularVelocity::from_deg_per_second(180.0); // π 弧度/秒
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 混合单位测试:弧度每秒 / 度每秒²
        {
            let omega = AngularVelocity::from_rad_per_second(PI);
            let alpha = AngularAcceleration::from_deg_per_second2(180.0); // π 弧度/秒²
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 测试弧度/小时单位
        {
            let omega = AngularVelocity::from_rad_per_hour(3600.0 * PI); // π 弧度/秒 = 3600π 弧度/小时
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 测试度/小时单位
        {
            let omega = AngularVelocity::from_deg_per_hour(180.0 * 3600.0); // 180度/秒 = 180*3600度/小时
            let alpha = AngularAcceleration::from_deg_per_second2(180.0);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 引用-引用测试
        {
            let omega = AngularVelocity::from_rad_per_second(10.0);
            let alpha = AngularAcceleration::from_rad_per_second2(2.0);
            let duration = &omega / &alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 5.0);
        }

        // 混合引用测试:值 / 引用
        {
            let omega = AngularVelocity::from_rad_per_second(10.0);
            let alpha = AngularAcceleration::from_rad_per_second2(2.0);
            let duration = omega / &alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 5.0);
        }

        // 混合引用测试:引用 / 值
        {
            let omega = AngularVelocity::from_rad_per_second(10.0);
            let alpha = AngularAcceleration::from_rad_per_second2(2.0);
            let duration = &omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 5.0);
        }

        // 测试不同单位的混合引用
        {
            let omega = AngularVelocity::from_deg_per_second(180.0);
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = &omega / &alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        {
            let omega = AngularVelocity::from_deg_per_second(180.0);
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = omega / &alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        {
            let omega = AngularVelocity::from_deg_per_second(180.0);
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = &omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 测试零角速度
        {
            let omega = AngularVelocity::from_rad_per_second(0.0);
            let alpha = AngularAcceleration::from_rad_per_second2(1.0);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 0.0);
        }

        // 测试大角速度
        {
            let omega = AngularVelocity::from_rad_per_second(100.0 * PI);
            let alpha = AngularAcceleration::from_rad_per_second2(PI);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 100.0);
        }

        // 测试小角加速度
        {
            let omega = AngularVelocity::from_rad_per_second(PI);
            let alpha = AngularAcceleration::from_rad_per_second2(0.1);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 10.0 * PI, epsilon = 1e-8);
        }

        // 负角速度 → 负时间
        {
            let omega = AngularVelocity::from_rad_per_second(-10.0);
            let alpha = AngularAcceleration::from_rad_per_second2(2.0);
            let duration = omega / alpha;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), -5.0);
        }
    }

    #[test]
    fn test_angular_velocity_mul_magnetic_induction() {
        // 角速度 × 磁感应强度 = 磁角速度
        let angular_velocity = AngularVelocity::from_rad_per_second(4.0);
        let magnetic_induction = MagneticInduction::from_tesla(2.5);
        let result = angular_velocity * magnetic_induction;
        assert_relative_eq!(result.as_tesla_rad_per_second(), 10.0);

        // 测试不同单位的角速度
        let angular_velocity = AngularVelocity::from_deg_per_second(180.0); // π rad/s
        let magnetic_induction = MagneticInduction::from_tesla(1.0);
        let result = angular_velocity * magnetic_induction;
        assert_relative_eq!(result.as_tesla_rad_per_second(), std::f64::consts::PI);

        // 测试不同单位的磁感应强度
        let angular_velocity = AngularVelocity::from_rad_per_second(3.0);
        let magnetic_induction = MagneticInduction::from_gauss(10000.0); // 1 T
        let result = angular_velocity * magnetic_induction;
        assert_relative_eq!(result.as_tesla_rad_per_second(), 3.0);

        // 测试交换律
        let angular_velocity = AngularVelocity::from_rad_per_second(2.0);
        let magnetic_induction = MagneticInduction::from_tesla(3.0);
        let result1 = angular_velocity * magnetic_induction;
        let result2 = magnetic_induction * angular_velocity;
        assert_relative_eq!(result1.as_tesla_rad_per_second(), result2.as_tesla_rad_per_second());
    }

    #[test]
    fn test_angular_velocity_ref_ops() {
        let w1 = AngularVelocity::from_rad_per_second(std::f64::consts::PI);
        let w2 = AngularVelocity::from_deg_per_second(180.0); // PI rad/s
        let s = &w1 + &w2;
        assert_relative_eq!(s.as_rad_per_second(), 2.0 * std::f64::consts::PI);

        let d = &w1 - &w2;
        assert_relative_eq!(d.as_rad_per_second(), 0.0);

        let v = &w1 * &Distance::from_m(2.0);
        assert_relative_eq!(v.as_m_per_sec(), 2.0 * std::f64::consts::PI);

        let coef = &w1 / &w2;
        assert_relative_eq!(coef.get_value(), 1.0);

        let t = &w1 / &AngularAcceleration::from_rad_per_second2(std::f64::consts::PI);
        assert_relative_eq!(time_delta_to_secs_f64(&t), 1.0);

        // 混合引用:加/减
        let s2 = w1 + &w2;
        assert_relative_eq!(s2.as_rad_per_second(), 2.0 * std::f64::consts::PI);
        let s3 = &w1 + w2;
        assert_relative_eq!(s3.as_rad_per_second(), 2.0 * std::f64::consts::PI);
        let w2b = AngularVelocity::from_deg_per_second(180.0);
        let d2 = w1 - &w2b;
        assert_relative_eq!(d2.as_rad_per_second(), 0.0);
        let d3 = &w1 - w2b;
        assert_relative_eq!(d3.as_rad_per_second(), 0.0);

        // 混合引用:* Distance
        let v2 = w1 * &Distance::from_m(2.0);
        assert_relative_eq!(v2.as_m_per_sec(), 2.0 * std::f64::consts::PI);
        let v3 = &w1 * Distance::from_m(2.0);
        assert_relative_eq!(v3.as_m_per_sec(), 2.0 * std::f64::consts::PI);

        // 混合引用:/ AngularVelocity -> Coef
        let c2 = w1 / &w1;
        assert_relative_eq!(c2.get_value(), 1.0);
        let c3 = &w1 / w1;
        assert_relative_eq!(c3.get_value(), 1.0);

        // 混合引用:/ AngularAcceleration -> Duration
        let ta = w1 / &AngularAcceleration::from_rad_per_second2(std::f64::consts::PI);
        assert_relative_eq!(time_delta_to_secs_f64(&ta), 1.0);
        let tb = &w1 / AngularAcceleration::from_rad_per_second2(std::f64::consts::PI);
        assert_relative_eq!(time_delta_to_secs_f64(&tb), 1.0);
    }
}