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
use std::ops::{Add, Div, Mul, Neg, Sub};
use std::time::Duration;
use super::*;

const LIGHT_SPEED: f64 = 299792458.0;
const LIGHT_YEAR_TO_METER: f64 = 9460730472580800.0;

impl Distance {
    pub fn from_m(v: f64) -> Distance {
        Distance {
            default_type: DistanceType::M,
            v,
        }
    }

    pub fn from_km(v: f64) -> Distance {
        Distance {
            default_type: DistanceType::KM,
            v,
        }
    }

    pub fn from_light_year(v: f64) -> Distance {
        Distance {
            default_type: DistanceType::LightYear,
            v,
        }
    }

    pub fn as_m(&self) -> f64 {
        match self.default_type {
            DistanceType::M => self.v,
            DistanceType::KM => self.v * 1000.0,
            DistanceType::LightYear => self.v * LIGHT_YEAR_TO_METER,
        }
    }

    pub fn as_km(&self) -> f64 {
        match self.default_type {
            DistanceType::M => self.v / 1000.0,
            DistanceType::KM => self.v,
            DistanceType::LightYear => self.v * LIGHT_YEAR_TO_METER / 1000.0,
        }
    }

    pub fn as_light_year(&self) -> f64 {
        match self.default_type {
            DistanceType::M => self.v / LIGHT_YEAR_TO_METER,
            DistanceType::KM => self.v / LIGHT_YEAR_TO_METER * 1000.0,
            DistanceType::LightYear => self.v,
        }
    }
}

impl Div<Duration> for Distance {
    type Output = Velocity;

    fn div(self, rhs: Duration) -> Self::Output {
        let v = self.as_m() / rhs.as_secs_f64();
        Velocity::from_m_per_sec(v)
    }
}

impl Div<chrono::TimeDelta> for Distance {
    type Output = Velocity;

    fn div(self, rhs: chrono::TimeDelta) -> Self::Output {
        let v = self.as_m() / time_delta_to_secs_f64(&rhs);
        Velocity::from_m_per_sec(v)
    }
}
impl Add for Distance {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let v = self.as_m() + rhs.as_m();
        Self::from_m(v)
    }
}

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

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

impl Sub for Distance {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        let v = self.as_m() - rhs.as_m();
        Self::from_m(v)
    }
}

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

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

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

impl Mul<Area> for Distance {
    type Output = Volume;
    fn mul(self, rhs: Area) -> Self::Output {
        let v = self.as_m() * rhs.as_m2();
        Volume::from_m3(v)
    }
}

impl Mul for Distance {
    type Output = Area;
    fn mul(self, rhs: Self) -> Self::Output {
        let v = self.as_m() * rhs.as_m();
        Area::from_m2(v)
    }
}

// 引用-引用 与 混合引用:Distance * Distance -> Area
impl<'a, 'b> Mul<&'b Distance> for &'a Distance {
    type Output = Area;
    fn mul(self, rhs: &'b Distance) -> Self::Output { Area::from_m2(self.as_m() * rhs.as_m()) }
}
impl<'a> Mul<&'a Distance> for Distance {
    type Output = Area;
    fn mul(self, rhs: &'a Distance) -> Self::Output { Area::from_m2(self.as_m() * rhs.as_m()) }
}
impl<'a> Mul<Distance> for &'a Distance {
    type Output = Area;
    fn mul(self, rhs: Distance) -> Self::Output { Area::from_m2(self.as_m() * rhs.as_m()) }
}
impl Div<f64> for Distance {
    type Output = Self;
    fn div(self, rhs: f64) -> Self::Output {
        let v = self.as_m() / rhs;
        Self::from_m(v)
    }
}

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

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

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

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

impl Neg for Distance {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let v = -self.as_m();
        Self::from_m(v)
    }
}

impl Mul<Momentum> for Distance {
    type Output = AngularMomentum;

    fn mul(self, rhs: Momentum) -> Self::Output {
        let v = self.default_unit_value() * rhs.default_unit_value();
        AngularMomentum::from_kg_m2_per_second(v)
    }
}

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

// 引用-引用 与 混合引用:Distance / Distance -> Coef
impl<'a, 'b> Div<&'b Distance> for &'a Distance {
    type Output = Coef;
    fn div(self, rhs: &'b Distance) -> Self::Output { Coef::new(self.as_m() / rhs.as_m()) }
}
impl<'a> Div<&'a Distance> for Distance {
    type Output = Coef;
    fn div(self, rhs: &'a Distance) -> Self::Output { Coef::new(self.as_m() / rhs.as_m()) }
}
impl<'a> Div<Distance> for &'a Distance {
    type Output = Coef;
    fn div(self, rhs: Distance) -> Self::Output { Coef::new(self.as_m() / rhs.as_m()) }
}

// 距离 ÷ 速度 = 有符号时间
impl Div<Velocity> for Distance {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: Velocity) -> Self::Output {
        let time_value = self.as_m() / rhs.as_m_per_sec();
        time_delta_from_secs_f64(time_value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    #[test]
    fn test_distance() {
        let d1 = Distance::from_m(1000.0);
        assert_eq!(d1.v, 1000.0);
        assert_eq!(d1.default_type, DistanceType::M);
        let d2 = Distance::from_km(1000.0);
        assert_eq!(d2.v, 1000.0);
        assert_eq!(d2.default_type, DistanceType::KM);
    }

    #[test]
    fn test_distance_as() {
        let d1 = Distance::from_km(1000.0);
        assert_eq!(d1.as_km(), 1000.0);
        assert_eq!(d1.as_m(), 1000.0 * 1000.0);
        assert_relative_eq!(d1.as_light_year(), 1.0570008340246155 * 1e-10);

        let d2 = Distance::from_m(1000.0);
        assert_eq!(d2.as_m(), 1000.0);
        assert_eq!(d2.as_km(), 1.0);
        assert_relative_eq!(d2.as_light_year(), 1.0570008340246155 * 1e-13);

        let d3 = Distance::from_light_year(1.0);
        assert_eq!(d3.as_m(), 9_460_730_472_580_800.0);
        assert_eq!(d3.as_km(), 9_460_730_472_580_800.0 / 1000.0);
    }

    #[test]
    fn test_to_velocity() {
        let d1 = Distance::from_m(1000.0);
        let duration = Duration::from_secs(5);
        let v = d1 / duration;
        assert_eq!(v.as_m_per_sec(), 200.0);

        let d1 = Distance::from_km(1200.0);
        let duration = Duration::from_secs(60 * 60 * 24);
        let v = d1 / duration;
        assert_eq!(v.as_km_per_h(), 50.0);
    }

    #[test]
    fn test_to_velocity_with_timedelta() {
        let d1 = Distance::from_m(1000.0);
        let td = time_delta_from_secs_f64(5.0);
        let v = d1 / td;
        assert_eq!(v.as_m_per_sec(), 200.0);

        // 负时间:距离为正,时间负 → 速度负
        let neg_td = time_delta_from_secs_f64(-5.0);
        let v_neg = d1 / neg_td;
        assert_eq!(v_neg.as_m_per_sec(), -200.0);

        // 距离为负
        let d_neg = Distance::from_m(-100.0);
        let td2 = time_delta_from_secs_f64(2.0);
        let v2 = d_neg / td2;
        assert_eq!(v2.as_m_per_sec(), -50.0);
    }

    #[test]
    fn test_change() {
        let d1 = Distance::from_m(2.0);
        let d2 = Distance::from_m(4.0);
        let a = d1 * d2;
        assert_eq!(a.default_type, AreaType::M2);
        assert_eq!(a.as_m2(), 8.0);

        let d1 = Distance::from_m(20.0);
        let m1 = Momentum::from_kg_m_s(10.0);
        let a1 = d1 * m1;
        assert_eq!(a1.default_type, AngularMomentumType::KgM2perSecond);
        assert_eq!(a1.as_kg_m2_per_second(), 200.0);

        let d1 = Distance::from_m(20.0);
        let a1 = Area::from_m2(10.0);
        let d3 = d1 * a1;
        assert_eq!(d3.as_m3(), 200.0);
    }

    #[test]
    fn test_to_add() {
        {
            let d1 = Distance::from_m(1000.0);
            let d2: Distance = Distance::from_m(1000.0);
            let d3 = d1 + d2;
            assert_eq!(d3.as_m(), 2000.0);

            let d1 = Distance::from_km(2000.0);
            let d2: Distance = Distance::from_m(1000.0);
            let d3 = d1 + d2;
            assert_eq!(d3.as_km(), 2001.0);

            let d1 = Distance::from_m(1000.0);
            let d2: Distance = Distance::from_km(3000.0);
            let d3 = d1 + d2;
            assert_eq!(d3.as_km(), 3001.0);
            let d1 = Distance::from_km(300.0);
            let d2 = Distance::from_km(100.0);
            let d3 = d1 + d2;
            assert_eq!(d3.as_m(), 400000.0);

            let d1 = Distance::from_light_year(1.8);
            let d2: Distance = Distance::from_light_year(1.1);
            let d3 = d1 + d2;
            assert_eq!(d3.as_light_year(), 2.9);
        }
        {
            let d1 = Distance::from_m(1000.0);
            let d2 = d1 + 1000.0;
            assert_eq!(d2.as_m(), 2000.0);

            let d1 = Distance::from_km(1000.0);
            let d2 = d1 + 1000.0;
            assert_eq!(d2.as_km(), 2000.0);

            let d1 = Distance::from_light_year(1.8);
            let d2 = d1 + 1.9;
            assert_eq!(d2.as_light_year(), 3.7);
        }
    }

    #[test]
    fn test_to_sub() {
        {
            let d1 = Distance::from_m(1000.0);
            let d2: Distance = Distance::from_m(1000.0);
            let d3 = d1 - d2;
            assert_eq!(d3.as_m(), 0.0);

            let d1 = Distance::from_km(1000.0);
            let d2: Distance = Distance::from_m(1000.0);
            let d3 = d1 - d2;
            assert_eq!(d3.as_m(), 999000.0);

            let d1 = Distance::from_km(1000.0);
            let d2: Distance = Distance::from_km(1000.0);
            let d3 = d1 - d2;
            assert_eq!(d3.as_m(), 0.0);
        }
        {
            let d1 = Distance::from_m(1000.0);
            let d2 = d1 - 1000.0;
            assert_eq!(d2.as_m(), 0.0);

            let d1 = Distance::from_km(2000.0);
            let d2 = d1 - 1000.0;
            assert_eq!(d2.as_km(), 1000.0);

            let d1 = Distance::from_light_year(1.8);
            let d2 = d1 - 0.9;
            assert_eq!(d2.as_light_year(), 0.9);
        }
    }

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

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

        let d1 = Distance::from_m(1000.0);
        let d2 = Distance::from_m(200.0);
        let c = d1 / d2;
        assert_eq!(c.get_value(), 5.0);
    }

    #[test]
    fn test_f64_mul_distance() {
        // f64 * Distance 测试
        let d = Distance::from_m(2.0);
        let result = 3.0 * d;
        assert_relative_eq!(result.as_m(), 6.0);

        let d = Distance::from_km(1.0);
        let result = 2.0 * d;
        assert_relative_eq!(result.as_km(), 2.0);

        let d = Distance::from_light_year(0.5);
        let result = 2.0 * d;
        assert_relative_eq!(result.as_light_year(), 1.0);
    }

    #[test]
    fn test_f64_div_distance() {
        // f64 / Distance 测试
        let d = Distance::from_m(2.0);
        let result = 6.0 / d;
        assert_relative_eq!(result.as_m(), 3.0);

        let d = Distance::from_km(1.0);
        let result = 2.0 / d;
        assert_relative_eq!(result.as_km(), 2.0);

        let d = Distance::from_light_year(0.5);
        let result = 1.0 / d;
        assert_relative_eq!(result.as_light_year(), 2.0);
    }

    #[test]
    fn test_distance_div_velocity() {
        let distance = Distance::from_m(100.0); // 100 m
        let velocity = Velocity::from_m_per_sec(20.0); // 20 m/s
        let time = distance / velocity; // 5 s
        
        assert_relative_eq!(time_delta_to_secs_f64(&time), 5.0);

        // 距离和速度方向相反 → 负数时间
        let dist_neg = Distance::from_m(-100.0);
        let time_neg = dist_neg / velocity;
        assert_relative_eq!(time_delta_to_secs_f64(&time_neg), -5.0);
    }

    #[test]
    fn test_distance_ref_ops() {
        let d1 = Distance::from_m(2.0);
        let d2 = Distance::from_km(1.0); // 1000 m
        let s = &d1 + &d2;
        assert_relative_eq!(s.as_m(), 1002.0);

        let sub = &d2 - &d1;
        assert_relative_eq!(sub.as_m(), 998.0);

        let area = &d1 * &d2;
        assert_relative_eq!(area.as_m2(), 2000.0);

        let coef = &d2 / &d1;
        assert_relative_eq!(coef.get_value(), 500.0);

        // 混合引用:加/减/乘/除
        let s2 = d1 + &d2;
        assert_relative_eq!(s2.as_m(), 1002.0);
        let s3 = &d1 + d2;
        assert_relative_eq!(s3.as_m(), 1002.0);

        let d2b = Distance::from_km(1.0);
        let sub2 = d2b - &d1;
        assert_relative_eq!(sub2.as_m(), 998.0);
        let sub3 = &d2b - d1;
        assert_relative_eq!(sub3.as_m(), 998.0);

        let d1b = Distance::from_m(2.0);
        let a2 = d1b * &Distance::from_km(1.0);
        assert_relative_eq!(a2.as_m2(), 2000.0);
        let a3 = &d1b * Distance::from_km(1.0);
        assert_relative_eq!(a3.as_m2(), 2000.0);

        let c2 = d2b / &Distance::from_m(2.0);
        assert_relative_eq!(c2.get_value(), 500.0);
        let c3 = &d2b / Distance::from_m(2.0);
        assert_relative_eq!(c3.get_value(), 500.0);
    }

    #[test]
    fn test_distance_neg() {
        // 测试正值的负号
        let d1 = Distance::from_m(5.0);
        let neg_d1 = -d1;
        assert_relative_eq!(neg_d1.as_m(), -5.0);

        // 测试负值的负号
        let d2 = Distance::from_km(-10.0);
        let neg_d2 = -d2;
        assert_relative_eq!(neg_d2.as_km(), 10.0);

        // 测试零值
        let d3 = Distance::from_m(0.0);
        let neg_d3 = -d3;
        assert_relative_eq!(neg_d3.as_m(), 0.0);

        // 测试不同单位的负号操作
        let d4 = Distance::from_km(1.0);
        let neg_d4 = -d4;
        assert_relative_eq!(neg_d4.as_m(), -1000.0);

        // 测试大数值
        let d5 = Distance::from_light_year(1.0);
        let neg_d5 = -d5;
        assert_relative_eq!(neg_d5.as_light_year(), -1.0);
    }

    #[test]
    fn test_matrix_product_with_physics() {
        use crate::dense::Matrix;

        // 距离行向量 × Coef矩阵 = 线性组合后的距离行向量
        // Distance * Coef = Distance,由物理量 Mul trait 推导
        let pos: Matrix<1, 2, Distance> = Matrix::new([
            [Distance::from_m(10.0), Distance::from_m(20.0)]
        ]);
        let coef: Matrix<2, 2, Coef> = Matrix::new([
            [Coef::new(1.0), Coef::new(2.0)],
            [Coef::new(3.0), Coef::new(4.0)],
        ]);

        // [10, 20] × [[1,2],[3,4]] = [1*10+3*20, 2*10+4*20] = [70, 100]
        let result: Matrix<1, 2, Distance> = pos.product(&coef).unwrap();
        assert_relative_eq!(result.get(0, 0).unwrap().as_m(), 70.0);
        assert_relative_eq!(result.get(1, 0).unwrap().as_m(), 100.0);
    }

    #[test]
    fn test_matrix_product_f_equals_ma() {
        use crate::dense::Matrix;

        // F = m × a
        // Mass × Acceleration = Force
        let m: Matrix<1, 2, Mass> = Matrix::new([[Mass::from_kg(2.0), Mass::from_kg(3.0)]]);
        let a: Matrix<2, 2, Acceleration> = Matrix::new([
            [Acceleration::from_m_per_s2(5.0), Acceleration::from_m_per_s2(0.0)],
            [Acceleration::from_m_per_s2(0.0), Acceleration::from_m_per_s2(4.0)],
        ]);
        // [2,3] × [[5,0],[0,4]] = [2*5 + 3*0, 2*0 + 3*4] = [10, 12]
        let f: Matrix<1, 2, Force> = m.product(&a).unwrap();
        assert_relative_eq!(f.get(0, 0).unwrap().as_newton(), 10.0);
        assert_relative_eq!(f.get(1, 0).unwrap().as_newton(), 12.0);
    }

    #[test]
    fn test_matrix_product_work_equals_force_times_distance() {
        use crate::dense::Matrix;

        // W = F × d
        // Force × Distance = Energy
        let f: Matrix<1, 3, Force> = Matrix::new([
            [Force::from_newton(10.0), Force::from_newton(20.0), Force::from_newton(30.0)]
        ]);
        let d: Matrix<3, 1, Distance> = Matrix::new([
            [Distance::from_m(2.0)],
            [Distance::from_m(0.5)],
            [Distance::from_m(1.0)],
        ]);
        // [10, 20, 30] × [[2],[0.5],[1]] = [10*2 + 20*0.5 + 30*1] = [60]
        let w: Matrix<1, 1, Energy> = f.product(&d).unwrap();
        assert_relative_eq!(w.get(0, 0).unwrap().as_joule(), 60.0);
    }
}