zmatrix 0.4.2

Compile-time dimensional analysis & physics-aware linear algebra for aerospace and robotics
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
use std::any::Any;
use std::ops::{Add, Div, Mul, Neg, Sub};
use crate::physics::basic::{Coef, Force, ForceType, PhysicalQuantity, Distance, Energy, Mass, Acceleration, Momentum};
use crate::physics::basic::time_delta_to_secs_f64;

impl Default for Force {
    fn default() -> Self {
        Self::from_newton(0.0)
    }
}

impl PhysicalQuantity for Force {
    fn as_any(&self) -> &dyn Any {
        self
}

    fn is_zero(&self) -> bool {
        self.v == 0.0
    }

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

    fn set_value(&mut self, value: f64) {
        self.v = value;
    }
    fn unit_display_name(&self) -> String {
        "Force (N)".to_string()
    }
}

impl Force {
    pub fn from_newton(newton: f64) -> Self {
        Self {
            default_type: ForceType::Newton,
            v: newton,
        }
    }

    pub fn from_mill_newton(mill_newton: f64) -> Self {
        Self {
            default_type: ForceType::MillNewton,
            v: mill_newton,
        }
    }

    pub fn from_micro_newton(micro_newton: f64) -> Self {
        Self {
            default_type: ForceType::MicroNewton,
            v: micro_newton,
        }
    }

    pub fn from_nano_newton(nano_newton: f64) -> Self {
        Self {
            default_type: ForceType::NanoNewton,
            v: nano_newton,
        }
    }

    pub fn from_kilo_newton(kilo_newton: f64) -> Self {
        Self {
            default_type: ForceType::KiloNewton,
            v: kilo_newton,
        }
    }

    pub fn from_mega_newton(mega_newton: f64) -> Self {
        Self {
            default_type: ForceType::MegaNewton,
            v: mega_newton,
        }
    }

    pub fn as_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v,
            ForceType::MillNewton => self.v * 1e-3,
            ForceType::MicroNewton => self.v * 1e-6,
            ForceType::NanoNewton => self.v * 1e-9,
            ForceType::KiloNewton => self.v * 1e3,
            ForceType::MegaNewton => self.v * 1e6,
        }
    }

    pub fn as_mill_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v * 1e3,
            ForceType::MillNewton => self.v,
            ForceType::MicroNewton => self.v * 1e-3,
            ForceType::NanoNewton => self.v * 1e-6,
            ForceType::KiloNewton => self.v * 1e6,
            ForceType::MegaNewton => self.v * 1e9,
        }
    }

    pub fn as_micro_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v * 1e6,
            ForceType::MillNewton => self.v * 1e3,
            ForceType::MicroNewton => self.v,
            ForceType::NanoNewton => self.v * 1e-3,
            ForceType::KiloNewton => self.v * 1e9,
            ForceType::MegaNewton => self.v * 1e12,
        }
    }

    pub fn as_nano_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v * 1e9,
            ForceType::MillNewton => self.v * 1e6,
            ForceType::MicroNewton => self.v * 1e3,
            ForceType::NanoNewton => self.v,
            ForceType::KiloNewton => self.v * 1e12,
            ForceType::MegaNewton => self.v * 1e15,
        }
    }

    pub fn as_kilo_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v * 1e-3,
            ForceType::MillNewton => self.v * 1e-6,
            ForceType::MicroNewton => self.v * 1e-9,
            ForceType::NanoNewton => self.v * 1e-12,
            ForceType::KiloNewton => self.v,
            ForceType::MegaNewton => self.v * 1e3,
        }
    }

    pub fn as_mega_newton(&self) -> f64 {
        match self.default_type {
            ForceType::Newton => self.v * 1e-6,
            ForceType::MillNewton => self.v * 1e-9,
            ForceType::MicroNewton => self.v * 1e-12,
            ForceType::NanoNewton => self.v * 1e-15,
            ForceType::KiloNewton => self.v * 1e-3,
            ForceType::MegaNewton => self.v,
        }
    }
}

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

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

// Force + 裸 f64:按**当前存储单位**直接相加,不做单位换算 —— 有意设计。
// 裸 f64 不带单位,库无从判断它代表哪种单位;擅自换算等于替调用方做假设。
// 需要单位安全时改用两个物理量运算,如 `x + Force::from_newton(1.0)`。
// 详见 `physics::basic` 模块文档「物理量与裸 f64 的运算」。
impl Add<f64> for Force {
    type Output = Self;
    fn add(self, rhs: f64) -> Self::Output {
        let v = self.v + rhs;
        Force {
            v,
            default_type: self.default_type,
        }
    }
}

impl Sub for Force {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        let v = self.as_newton() - rhs.as_newton();
        Self::from_newton(v)
    }
}

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

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

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

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

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

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

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

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

impl Neg for Force {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let v = -self.as_newton();
        Self::from_newton(v)
    }
}

// 力与距离的乘积(得到能量)
impl Mul<Distance> for Force {
    type Output = Energy; // 能量,单位:焦耳
    fn mul(self, rhs: Distance) -> Self::Output {
        let energy_value = self.as_newton() * rhs.as_m();
        Energy::from_joule(energy_value)
    }
}

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

// 距离与力的乘积(得到能量,满足交换律)
impl Mul<Force> for Distance {
    type Output = Energy; // 能量,单位:焦耳
    fn mul(self, rhs: Force) -> Self::Output {
        let energy_value = self.as_m() * rhs.as_newton();
        Energy::from_joule(energy_value)
    }
}

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

// 质量与加速度的乘积(得到力)
impl Mul<Acceleration> for Mass {
    type Output = Force; // 力,单位:牛顿
    fn mul(self, rhs: Acceleration) -> Self::Output {
        let force_value = self.as_kg() * rhs.as_m_per_s2();
        Force::from_newton(force_value)
    }
}

// 加速度与质量的乘积(得到力,满足交换律)
impl Mul<Mass> for Acceleration {
    type Output = Force; // 力,单位:牛顿
    fn mul(self, rhs: Mass) -> Self::Output {
        let force_value = self.as_m_per_s2() * rhs.as_kg();
        Force::from_newton(force_value)
    }
}

// 力与时间的乘积(得到动量):N·s = kg·m/s
impl Mul<chrono::TimeDelta> for Force {
    type Output = Momentum;
    fn mul(self, rhs: chrono::TimeDelta) -> Self::Output {
        let v = self.as_newton() * time_delta_to_secs_f64(&rhs);
        Momentum::from_kg_m_s(v)
    }
}

// 引用版本:Force * TimeDelta -> Momentum
impl<'a> Mul<chrono::TimeDelta> for &'a Force {
    type Output = Momentum;
    fn mul(self, rhs: chrono::TimeDelta) -> Self::Output {
        Momentum::from_kg_m_s(self.as_newton() * time_delta_to_secs_f64(&rhs))
    }
}
impl<'a> Mul<&'a chrono::TimeDelta> for Force {
    type Output = Momentum;
    fn mul(self, rhs: &'a chrono::TimeDelta) -> Self::Output {
        Momentum::from_kg_m_s(self.as_newton() * time_delta_to_secs_f64(rhs))
    }
}
impl<'a, 'b> Mul<&'b chrono::TimeDelta> for &'a Force {
    type Output = Momentum;
    fn mul(self, rhs: &'b chrono::TimeDelta) -> Self::Output {
        Momentum::from_kg_m_s(self.as_newton() * time_delta_to_secs_f64(rhs))
    }
}

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

    // 力 * 时间 = 动量(冲量定理):F·Δt = Δp
    #[test]
    fn test_mul_timedelta() {
        let f = Force::from_newton(3.0);
        let p = f * chrono::TimeDelta::seconds(2);
        assert_relative_eq!(p.as_kg_m_s(), 6.0);

        // 负时间:冲量与力反向
        let p_neg = f * chrono::TimeDelta::seconds(-2);
        assert_relative_eq!(p_neg.as_kg_m_s(), -6.0);

        // 零时间:无冲量
        let p_zero = f * chrono::TimeDelta::zero();
        assert_relative_eq!(p_zero.as_kg_m_s(), 0.0);

        // 单位无关性:1 kN 作用 1 ms 与 1000 N 作用 1 ms 等价
        let a = Force::from_kilo_newton(1.0) * chrono::TimeDelta::milliseconds(1);
        let b = Force::from_newton(1000.0) * chrono::TimeDelta::milliseconds(1);
        assert_relative_eq!(a.as_kg_m_s(), b.as_kg_m_s());

        // 引用变体
        let p_ref = &f * chrono::TimeDelta::seconds(2);
        assert_relative_eq!(p_ref.as_kg_m_s(), 6.0);
        let td = chrono::TimeDelta::seconds(2);
        let p_ref2 = f * &td;
        assert_relative_eq!(p_ref2.as_kg_m_s(), 6.0);
        let p_ref3 = &f * &td;
        assert_relative_eq!(p_ref3.as_kg_m_s(), 6.0);
    }
    use approx::assert_relative_eq;
    use crate::physics::basic::{Distance, Energy, Mass, Acceleration};

    #[test]
    fn test_force() {
        let f1 = Force::from_newton(10.0);
        let f2 = Force::from_mill_newton(10000.0);
        let f3 = Force::from_micro_newton(10000000.0);
        let f4 = Force::from_nano_newton(10000000000.0);
        let f5 = Force::from_kilo_newton(0.01);
        let f6 = Force::from_mega_newton(0.00001);

        assert_relative_eq!(f1.as_newton(), 10.0);
        assert_relative_eq!(f2.as_newton(), 10.0);
        assert_relative_eq!(f3.as_newton(), 10.0);
        assert_relative_eq!(f4.as_newton(), 10.0);
        assert_relative_eq!(f5.as_newton(), 10.0);
        assert_relative_eq!(f6.as_newton(), 10.0);
    }

    #[test]
    fn test_force_as() {
        let f = Force::from_newton(1.0);
        
        assert_relative_eq!(f.as_mill_newton(), 1000.0);
        assert_relative_eq!(f.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f.as_kilo_newton(), 0.001);
        assert_relative_eq!(f.as_mega_newton(), 0.000001);
    }

    #[test]
    fn test_force_add() {
        let f1 = Force::from_newton(5.0);
        let f2 = Force::from_mill_newton(3000.0); // 3 N
        let result = f1 + f2;
        
        assert_relative_eq!(result.as_newton(), 8.0);
    }

    #[test]
    fn test_force_sub() {
        let f1 = Force::from_newton(10.0);
        let f2 = Force::from_mill_newton(2000.0); // 2 N
        let result = f1 - f2;
        
        assert_relative_eq!(result.as_newton(), 8.0);
    }

    #[test]
    fn test_force_mul() {
        let f = Force::from_newton(5.0);
        let result = f * 3.0;
        
        assert_relative_eq!(result.as_newton(), 15.0);
    }

    #[test]
    fn test_force_div() {
        let f = Force::from_newton(15.0);
        let result = f / 3.0;
        
        assert_relative_eq!(result.as_newton(), 5.0);
    }

    #[test]
    fn test_force_mul_distance() {
        let force = Force::from_newton(10.0); // 10 N
        let distance = Distance::from_m(5.0);  // 5 m
        let energy: Energy = force * distance; // 50 J
        
        assert_relative_eq!(energy.as_joule(), 50.0);
    }

    #[test]
    fn test_distance_mul_force() {
        let distance = Distance::from_m(5.0);  // 5 m
        let force = Force::from_newton(10.0); // 10 N
        let energy: Energy = distance * force; // 50 J
        
        assert_relative_eq!(energy.as_joule(), 50.0);
    }

    #[test]
    fn test_mass_mul_acceleration() {
        let mass = Mass::from_kg(2.0);                    // 2 kg
        let acceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
        let force: Force = mass * acceleration;           // 10 N
        
        assert_relative_eq!(force.as_newton(), 10.0);
    }

    #[test]
    fn test_acceleration_mul_mass() {
        let acceleration = Acceleration::from_m_per_s2(5.0); // 5 m/s²
        let mass = Mass::from_kg(2.0);                    // 2 kg
        let force: Force = acceleration * mass;           // 10 N
        
        assert_relative_eq!(force.as_newton(), 10.0);
    }

    #[test]
    fn test_default_unit_value() {
        let f = Force::from_newton(5.0);
        assert_relative_eq!(f.default_unit_value(), 5.0);
    }

    #[test]
    fn test_is_zero() {
        let f1 = Force::from_newton(0.0);
        let f2 = Force::from_newton(1.0);
        
        assert!(f1.is_zero());
        assert!(!f2.is_zero());
    }

    #[test]
    fn test_set_value() {
        let mut f = Force::from_newton(5.0);
        f.set_value(10.0);
        assert_relative_eq!(f.as_newton(), 10.0);
    }

    #[test]
    fn test_force_operations_with_different_types() {
        // 测试不同力类型之间的运算
        let f1 = Force::from_newton(1000.0);
        let f2 = Force::from_kilo_newton(1.0);
        let f3 = Force::from_mega_newton(0.001);
        
        // 加法
        let sum = f1 + f2 + f3;
        assert_relative_eq!(sum.as_newton(), 3000.0);
        
        // 减法
        let diff = f1 - f2;
        assert_relative_eq!(diff.as_newton(), 0.0);
        
        // 乘法
        let product = f1 * 2.0;
        assert_relative_eq!(product.as_newton(), 2000.0);
        
        // 除法
        let quotient = f1 / 2.0;
        assert_relative_eq!(quotient.as_newton(), 500.0);
    }

    #[test]
    fn test_force_all_unit_conversions() {
        let f = Force::from_newton(1.0);
        
        // 测试所有单位的转换
        assert_relative_eq!(f.as_newton(), 1.0);
        assert_relative_eq!(f.as_mill_newton(), 1000.0);
        assert_relative_eq!(f.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f.as_kilo_newton(), 0.001);
        assert_relative_eq!(f.as_mega_newton(), 0.000001);
        
        // 从不同单位创建并测试转换
        let f_mill = Force::from_mill_newton(1000.0);
        assert_relative_eq!(f_mill.as_newton(), 1.0);
        
        let f_micro = Force::from_micro_newton(1000000.0);
        assert_relative_eq!(f_micro.as_newton(), 1.0);
        
        let f_nano = Force::from_nano_newton(1000000000.0);
        assert_relative_eq!(f_nano.as_newton(), 1.0);
        
        let f_kilo = Force::from_kilo_newton(0.001);
        assert_relative_eq!(f_kilo.as_newton(), 1.0);
        
        let f_mega = Force::from_mega_newton(0.000001);
        assert_relative_eq!(f_mega.as_newton(), 1.0);
    }

    #[test]
    fn test_force_edge_cases() {
        // 测试边界情况
        let zero_force = Force::from_newton(0.0);
        assert!(zero_force.is_zero());
        
        let negative_force = Force::from_newton(-100.0);
        assert!(!negative_force.is_zero());
        assert_relative_eq!(negative_force.as_newton(), -100.0);
        
        // 测试大数值
        let large_force = Force::from_mega_newton(1000.0);
        assert_relative_eq!(large_force.as_newton(), 1e9);
        
        // 测试小数值
        let small_force = Force::from_nano_newton(1.0);
        assert_relative_eq!(small_force.as_newton(), 1e-9);
    }

    #[test]
    fn test_force_physical_quantity_trait() {
        let mut f = Force::from_newton(10.0);
        
        // 测试 default_unit_value
        assert_relative_eq!(f.default_unit_value(), 10.0);
        
        // 测试 set_value
        f.set_value(20.0);
        assert_relative_eq!(f.as_newton(), 20.0);
        
        // 测试 is_zero
        assert!(!f.is_zero());
        f.set_value(0.0);
        assert!(f.is_zero());
        
        // 测试 as_any
        let any_ref = f.as_any();
        assert!(any_ref.is::<Force>());
    }

    #[test]
    fn test_force_coef_operations() {
        let f = Force::from_newton(10.0);
        let coef = Coef::new(2.0);
        
        // 测试与系数的乘法
        let result = f * coef;
        assert_relative_eq!(result.as_newton(), 20.0);
        
        // 测试与系数的除法
        let result = f / coef;
        assert_relative_eq!(result.as_newton(), 5.0);
    }

    #[test]
    fn test_force_f64_operations() {
        let f = Force::from_newton(10.0);
        
        // 测试 f64 与力的乘法
        let result = 3.0 * f;
        assert_relative_eq!(result.as_newton(), 30.0);
        
        // 测试 f64 与力的除法
        let result = 30.0 / f;
        assert_relative_eq!(result.as_newton(), 3.0);
    }

    #[test]
    fn test_force_arithmetic_with_f64() {
        let f = Force::from_newton(10.0);
        
        // 测试与 f64 的加法
        let result = f + 5.0;
        assert_relative_eq!(result.as_newton(), 15.0);
        
        // 测试与 f64 的减法
        let result = f - 3.0;
        assert_relative_eq!(result.as_newton(), 7.0);
    }

    #[test]
    fn test_force_ref_ops() {
        let f1 = Force::from_newton(2.0);
        let f2 = Force::from_kilo_newton(0.001); // 1 N
        let s = &f1 + &f2;
        assert_relative_eq!(s.as_newton(), 3.0);

        let d = &f1 - &f2;
        assert_relative_eq!(d.as_newton(), 1.0);

        let e1 = &f1 * &Distance::from_m(3.0);
        assert_relative_eq!(e1.as_joule(), 6.0);

        let e2 = &Distance::from_m(3.0) * &f1;
        assert_relative_eq!(e2.as_joule(), 6.0);

        // 混合引用:加/减
        let s2 = f1 + &f2;
        assert_relative_eq!(s2.as_newton(), 3.0);
        let s3 = &f1 + f2;
        assert_relative_eq!(s3.as_newton(), 3.0);
        let f2b = Force::from_kilo_newton(0.001);
        let d2 = f1 - &f2b;
        assert_relative_eq!(d2.as_newton(), 1.0);
        let d3 = &f1 - f2b;
        assert_relative_eq!(d3.as_newton(), 1.0);

        // 混合引用:与 Distance 双向
        let e3 = f1 * &Distance::from_m(3.0);
        assert_relative_eq!(e3.as_joule(), 6.0);
        let e4 = &f1 * Distance::from_m(3.0);
        assert_relative_eq!(e4.as_joule(), 6.0);
        let e5 = Distance::from_m(3.0) * &f1;
        assert_relative_eq!(e5.as_joule(), 6.0);
        let e6 = &Distance::from_m(3.0) * f1;
        assert_relative_eq!(e6.as_joule(), 6.0);
    }

    #[test]
    fn test_force_comprehensive_as_methods() {
        // 测试所有单位到所有单位的转换
        // 从 Newton 开始
        let f_newton = Force::from_newton(1.0);
        assert_relative_eq!(f_newton.as_newton(), 1.0);
        assert_relative_eq!(f_newton.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_newton.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_newton.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_newton.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_newton.as_mega_newton(), 0.000001);

        // 从 MillNewton 开始
        let f_mill = Force::from_mill_newton(1000.0);
        assert_relative_eq!(f_mill.as_newton(), 1.0);
        assert_relative_eq!(f_mill.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_mill.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_mill.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_mill.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_mill.as_mega_newton(), 0.000001);

        // 从 MicroNewton 开始
        let f_micro = Force::from_micro_newton(1000000.0);
        assert_relative_eq!(f_micro.as_newton(), 1.0);
        assert_relative_eq!(f_micro.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_micro.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_micro.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_micro.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_micro.as_mega_newton(), 0.000001);

        // 从 NanoNewton 开始
        let f_nano = Force::from_nano_newton(1000000000.0);
        assert_relative_eq!(f_nano.as_newton(), 1.0);
        assert_relative_eq!(f_nano.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_nano.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_nano.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_nano.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_nano.as_mega_newton(), 0.000001);

        // 从 KiloNewton 开始
        let f_kilo = Force::from_kilo_newton(0.001);
        assert_relative_eq!(f_kilo.as_newton(), 1.0);
        assert_relative_eq!(f_kilo.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_kilo.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_kilo.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_kilo.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_kilo.as_mega_newton(), 0.000001);

        // 从 MegaNewton 开始
        let f_mega = Force::from_mega_newton(0.000001);
        assert_relative_eq!(f_mega.as_newton(), 1.0);
        assert_relative_eq!(f_mega.as_mill_newton(), 1000.0);
        assert_relative_eq!(f_mega.as_micro_newton(), 1000000.0);
        assert_relative_eq!(f_mega.as_nano_newton(), 1000000000.0);
        assert_relative_eq!(f_mega.as_kilo_newton(), 0.001);
        assert_relative_eq!(f_mega.as_mega_newton(), 0.000001);

        // 测试负值
        let f_negative = Force::from_newton(-2.0);
        assert_relative_eq!(f_negative.as_newton(), -2.0);
        assert_relative_eq!(f_negative.as_mill_newton(), -2000.0);
        assert_relative_eq!(f_negative.as_micro_newton(), -2000000.0);
        assert_relative_eq!(f_negative.as_nano_newton(), -2000000000.0);
        assert_relative_eq!(f_negative.as_kilo_newton(), -0.002);
        assert_relative_eq!(f_negative.as_mega_newton(), -0.000002);

        // 测试零值
        let f_zero = Force::from_newton(0.0);
        assert_relative_eq!(f_zero.as_newton(), 0.0);
        assert_relative_eq!(f_zero.as_mill_newton(), 0.0);
        assert_relative_eq!(f_zero.as_micro_newton(), 0.0);
        assert_relative_eq!(f_zero.as_nano_newton(), 0.0);
        assert_relative_eq!(f_zero.as_kilo_newton(), 0.0);
        assert_relative_eq!(f_zero.as_mega_newton(), 0.0);
    }

    #[test]
    fn test_force_neg() {
        // 测试正值的负号
        let f1 = Force::from_newton(5.0);
        let neg_f1 = -f1;
        assert_relative_eq!(neg_f1.as_newton(), -5.0);

        // 测试负值的负号
        let f2 = Force::from_kilo_newton(-10.0);
        let neg_f2 = -f2;
        assert_relative_eq!(neg_f2.as_kilo_newton(), 10.0);

        // 测试零值
        let f3 = Force::from_newton(0.0);
        let neg_f3 = -f3;
        assert_relative_eq!(neg_f3.as_newton(), 0.0);

        // 测试不同单位的负号操作
        let f4 = Force::from_mega_newton(1.0);
        let neg_f4 = -f4;
        assert_relative_eq!(neg_f4.as_newton(), -1_000_000.0);

        // 测试大数值
        let f5 = Force::from_micro_newton(1000000.0);
        let neg_f5 = -f5;
        assert_relative_eq!(neg_f5.as_newton(), -1.0);
    }
}