rag-plusplus-core 0.1.0

High-performance retrieval engine with SIMD-accelerated vector search and trajectory memory
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
//! Trajectory Coordinate System
//!
//! 4D and 5D positioning systems for episodes in a trajectory, inspired by IRCP/RCP
//! DLM coordinates and TPO (Topological Preference Optimization).
//!
//! # Coordinate Dimensions
//!
//! ## 4D Coordinates (IRCP/RCP)
//!
//! | Dimension | Type | Meaning |
//! |-----------|------|---------|
//! | depth | u32 | Distance from root in trajectory tree (0 = root) |
//! | sibling_order | u32 | Position among siblings at branching points |
//! | homogeneity | f32 | Semantic similarity to parent [0, 1] |
//! | temporal | f32 | Normalized timestamp within trajectory [0, 1] |
//!
//! ## 5D Coordinates (TPO Extension)
//!
//! | Dimension | Type | Meaning |
//! |-----------|------|---------|
//! | depth | u32 | Distance from root in trajectory tree (0 = root) |
//! | sibling_order | u32 | Position among siblings at branching points |
//! | homogeneity | f32 | Semantic similarity to parent [0, 1] |
//! | temporal | f32 | Normalized timestamp within trajectory [0, 1] |
//! | complexity | u32 | Message complexity (n_parts: text + images + code blocks) |
//!
//! # Usage
//!
//! ```
//! use rag_plusplus_core::trajectory::{TrajectoryCoordinate, TrajectoryCoordinate5D};
//!
//! // 4D coordinates (IRCP/RCP)
//! let coord_a = TrajectoryCoordinate::new(3, 0, 0.85, 0.5);
//! let coord_b = TrajectoryCoordinate::new(4, 1, 0.72, 0.6);
//! let distance = coord_a.distance(&coord_b);
//!
//! // 5D coordinates (TPO extension with complexity)
//! let coord_5d_a = TrajectoryCoordinate5D::new(3, 0, 0.85, 0.5, 1);
//! let coord_5d_b = TrajectoryCoordinate5D::new(4, 1, 0.72, 0.6, 3); // multimodal
//! let distance_5d = coord_5d_a.distance(&coord_5d_b);
//! ```

use crate::distance::cosine_similarity_fast;

// ============================================================================
// DLM Weight Configuration (from DLM package analysis)
// ============================================================================

/// DLM coordinate weight configuration.
///
/// These weights control how each dimension contributes to distance calculations.
/// Default values are taken from the DLM package configuration.
///
/// # DLM Default Weights
///
/// | Dimension | Weight | Rationale |
/// |-----------|--------|-----------|
/// | depth | 0.25 | Structural position in tree |
/// | sibling | 0.15 | Branch selection importance |
/// | homogeneity | 0.30 | Semantic continuity (highest weight) |
/// | temporal | 0.20 | Time-based proximity |
/// | complexity | 0.10 | Information density |
///
/// # Example
///
/// ```
/// use rag_plusplus_core::trajectory::DLMWeights;
///
/// // Use default weights
/// let default = DLMWeights::default();
///
/// // Custom weights emphasizing semantic similarity
/// let semantic = DLMWeights::semantic_focused();
///
/// // Fully custom weights
/// let custom = DLMWeights::new(0.2, 0.2, 0.2, 0.2, 0.2);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DLMWeights {
    /// Weight for depth dimension
    pub depth: f32,
    /// Weight for sibling_order dimension
    pub sibling: f32,
    /// Weight for homogeneity dimension
    pub homogeneity: f32,
    /// Weight for temporal dimension
    pub temporal: f32,
    /// Weight for complexity dimension
    pub complexity: f32,
}

impl DLMWeights {
    /// Create custom weights.
    ///
    /// Weights are automatically normalized to sum to 1.0.
    #[inline]
    pub fn new(depth: f32, sibling: f32, homogeneity: f32, temporal: f32, complexity: f32) -> Self {
        let total = depth + sibling + homogeneity + temporal + complexity;
        if total > 0.0 {
            Self {
                depth: depth / total,
                sibling: sibling / total,
                homogeneity: homogeneity / total,
                temporal: temporal / total,
                complexity: complexity / total,
            }
        } else {
            Self::default()
        }
    }

    /// Create weights from array [depth, sibling, homogeneity, temporal, complexity].
    #[inline]
    pub fn from_array(weights: [f32; 5]) -> Self {
        Self::new(weights[0], weights[1], weights[2], weights[3], weights[4])
    }

    /// Convert to array [depth, sibling, homogeneity, temporal, complexity].
    #[inline]
    pub fn to_array(&self) -> [f32; 5] {
        [self.depth, self.sibling, self.homogeneity, self.temporal, self.complexity]
    }

    /// Semantic-focused weights (emphasize homogeneity).
    ///
    /// Useful when semantic similarity is most important.
    #[inline]
    pub fn semantic_focused() -> Self {
        Self::new(0.15, 0.10, 0.50, 0.15, 0.10)
    }

    /// Structural-focused weights (emphasize depth and sibling).
    ///
    /// Useful when tree position is most important.
    #[inline]
    pub fn structural_focused() -> Self {
        Self::new(0.35, 0.25, 0.15, 0.15, 0.10)
    }

    /// Temporal-focused weights (emphasize temporal).
    ///
    /// Useful for time-based retrieval.
    #[inline]
    pub fn temporal_focused() -> Self {
        Self::new(0.15, 0.10, 0.20, 0.45, 0.10)
    }

    /// Equal weights for all dimensions.
    #[inline]
    pub fn equal() -> Self {
        Self::new(0.2, 0.2, 0.2, 0.2, 0.2)
    }
}

impl Default for DLMWeights {
    /// DLM default weights from configuration.
    fn default() -> Self {
        Self {
            depth: 0.25,
            sibling: 0.15,
            homogeneity: 0.30,
            temporal: 0.20,
            complexity: 0.10,
        }
    }
}

/// 4D coordinate for positioning an episode within a trajectory.
///
/// Captures both structural (depth, sibling) and semantic (homogeneity, temporal)
/// dimensions of an episode's position.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrajectoryCoordinate {
    /// Depth in the trajectory tree (0 = root)
    pub depth: u32,
    /// Position among siblings at this level (0-indexed)
    pub sibling_order: u32,
    /// Semantic similarity to parent episode [0, 1]
    /// Computed as cosine similarity of embeddings
    pub homogeneity: f32,
    /// Normalized temporal position within trajectory [0, 1]
    /// 0 = start of trajectory, 1 = end
    pub temporal: f32,
}

impl TrajectoryCoordinate {
    /// Create a new trajectory coordinate.
    ///
    /// # Arguments
    ///
    /// * `depth` - Distance from root (0 = root)
    /// * `sibling_order` - Position among siblings (0-indexed)
    /// * `homogeneity` - Semantic similarity to parent [0, 1]
    /// * `temporal` - Normalized timestamp [0, 1]
    ///
    /// # Panics
    ///
    /// Panics if homogeneity or temporal are outside [0, 1].
    #[inline]
    pub fn new(depth: u32, sibling_order: u32, homogeneity: f32, temporal: f32) -> Self {
        debug_assert!(
            (0.0..=1.0).contains(&homogeneity),
            "homogeneity must be in [0, 1], got {}",
            homogeneity
        );
        debug_assert!(
            (0.0..=1.0).contains(&temporal),
            "temporal must be in [0, 1], got {}",
            temporal
        );

        Self {
            depth,
            sibling_order,
            homogeneity,
            temporal,
        }
    }

    /// Create a root coordinate (depth=0, sibling=0, homogeneity=1, temporal=0).
    #[inline]
    pub fn root() -> Self {
        Self {
            depth: 0,
            sibling_order: 0,
            homogeneity: 1.0,
            temporal: 0.0,
        }
    }

    /// Compute Euclidean distance between two coordinates.
    ///
    /// Uses normalized dimensions so each contributes equally.
    #[inline]
    pub fn distance(&self, other: &Self) -> f32 {
        let d_depth = (self.depth as f32 - other.depth as f32).abs();
        let d_sibling = (self.sibling_order as f32 - other.sibling_order as f32).abs();
        let d_homo = self.homogeneity - other.homogeneity;
        let d_time = self.temporal - other.temporal;

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2)).sqrt()
    }

    /// Compute weighted distance with custom dimension weights.
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    /// * `weights` - (depth_weight, sibling_weight, homogeneity_weight, temporal_weight)
    #[inline]
    pub fn weighted_distance(&self, other: &Self, weights: (f32, f32, f32, f32)) -> f32 {
        let d_depth = (self.depth as f32 - other.depth as f32).abs() * weights.0;
        let d_sibling = (self.sibling_order as f32 - other.sibling_order as f32).abs() * weights.1;
        let d_homo = (self.homogeneity - other.homogeneity).abs() * weights.2;
        let d_time = (self.temporal - other.temporal).abs() * weights.3;

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2)).sqrt()
    }

    /// Compute Manhattan distance between two coordinates.
    #[inline]
    pub fn manhattan_distance(&self, other: &Self) -> f32 {
        let d_depth = (self.depth as i64 - other.depth as i64).unsigned_abs() as f32;
        let d_sibling = (self.sibling_order as i64 - other.sibling_order as i64).unsigned_abs() as f32;
        let d_homo = (self.homogeneity - other.homogeneity).abs();
        let d_time = (self.temporal - other.temporal).abs();

        d_depth + d_sibling + d_homo + d_time
    }

    /// Compute coordinate from episode data.
    ///
    /// # Arguments
    ///
    /// * `depth` - Tree depth
    /// * `sibling_order` - Position among siblings
    /// * `embedding` - Episode embedding
    /// * `parent_embedding` - Parent episode embedding (for homogeneity)
    /// * `timestamp` - Episode timestamp
    /// * `trajectory_start` - First timestamp in trajectory
    /// * `trajectory_end` - Last timestamp in trajectory
    pub fn from_episode(
        depth: u32,
        sibling_order: u32,
        embedding: &[f32],
        parent_embedding: Option<&[f32]>,
        timestamp: i64,
        trajectory_start: i64,
        trajectory_end: i64,
    ) -> Self {
        // Compute homogeneity as cosine similarity to parent
        let homogeneity = parent_embedding
            .map(|parent| cosine_similarity_fast(embedding, parent).clamp(0.0, 1.0))
            .unwrap_or(1.0); // Root has homogeneity 1.0

        // Compute normalized temporal position
        let temporal = if trajectory_end > trajectory_start {
            let duration = (trajectory_end - trajectory_start) as f64;
            let elapsed = (timestamp - trajectory_start) as f64;
            (elapsed / duration).clamp(0.0, 1.0) as f32
        } else {
            0.0 // Single episode trajectory
        };

        Self {
            depth,
            sibling_order,
            homogeneity,
            temporal,
        }
    }

    /// Normalize depth relative to a maximum depth.
    ///
    /// Useful for comparing coordinates across trajectories of different depths.
    #[inline]
    pub fn normalize_depth(&self, max_depth: u32) -> f32 {
        if max_depth == 0 {
            0.0
        } else {
            self.depth as f32 / max_depth as f32
        }
    }

    /// Normalize sibling order relative to total siblings.
    #[inline]
    pub fn normalize_sibling(&self, total_siblings: u32) -> f32 {
        if total_siblings <= 1 {
            0.0
        } else {
            self.sibling_order as f32 / (total_siblings - 1) as f32
        }
    }

    /// Create a fully normalized coordinate for comparison across trajectories.
    ///
    /// # Arguments
    ///
    /// * `max_depth` - Maximum depth in trajectory
    /// * `total_siblings` - Number of siblings at this level
    pub fn normalized(&self, max_depth: u32, total_siblings: u32) -> NormalizedCoordinate {
        NormalizedCoordinate {
            depth: self.normalize_depth(max_depth),
            sibling_order: self.normalize_sibling(total_siblings),
            homogeneity: self.homogeneity,
            temporal: self.temporal,
        }
    }
}

impl Default for TrajectoryCoordinate {
    fn default() -> Self {
        Self::root()
    }
}

/// Fully normalized coordinate where all dimensions are [0, 1].
///
/// Useful for comparing coordinates across trajectories of different sizes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NormalizedCoordinate {
    /// Normalized depth [0, 1]
    pub depth: f32,
    /// Normalized sibling order [0, 1]
    pub sibling_order: f32,
    /// Semantic homogeneity [0, 1]
    pub homogeneity: f32,
    /// Temporal position [0, 1]
    pub temporal: f32,
}

impl NormalizedCoordinate {
    /// Compute Euclidean distance between normalized coordinates.
    #[inline]
    pub fn distance(&self, other: &Self) -> f32 {
        let d_depth = self.depth - other.depth;
        let d_sibling = self.sibling_order - other.sibling_order;
        let d_homo = self.homogeneity - other.homogeneity;
        let d_time = self.temporal - other.temporal;

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2)).sqrt()
    }

    /// Convert to array for batch operations.
    #[inline]
    pub fn to_array(&self) -> [f32; 4] {
        [self.depth, self.sibling_order, self.homogeneity, self.temporal]
    }
}

// ============================================================================
// 5D Coordinates (TPO Extension)
// ============================================================================

/// 5D coordinate for positioning an episode within a trajectory.
///
/// Extends the 4D IRCP/RCP coordinate with a complexity dimension from TPO
/// (Topological Preference Optimization). The complexity dimension captures
/// message information density (number of content parts: text, images, code blocks).
///
/// # TPO Integration
///
/// In TPO, the N dimension (n_parts) affects:
/// - **Path quality scoring**: Complex messages contribute more information
/// - **Preference strength**: Choosing paths with complex content over simple
/// - **Salience weighting**: Higher complexity = higher information density
///
/// # Example
///
/// ```
/// use rag_plusplus_core::trajectory::TrajectoryCoordinate5D;
///
/// // Simple text-only message
/// let simple = TrajectoryCoordinate5D::new(1, 0, 0.9, 0.3, 1);
///
/// // Complex multimodal message (text + 2 images + code block)
/// let complex = TrajectoryCoordinate5D::new(2, 0, 0.8, 0.5, 4);
///
/// // Complexity affects distance
/// let distance = simple.distance(&complex);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrajectoryCoordinate5D {
    /// Depth in the trajectory tree (0 = root)
    pub depth: u32,
    /// Position among siblings at this level (0-indexed)
    pub sibling_order: u32,
    /// Semantic similarity to parent episode [0, 1]
    pub homogeneity: f32,
    /// Normalized temporal position within trajectory [0, 1]
    pub temporal: f32,
    /// Message complexity (n_parts): number of content components
    /// - 1 = simple text
    /// - 2+ = multimodal (text + images, code blocks, etc.)
    pub complexity: u32,
}

impl TrajectoryCoordinate5D {
    /// Create a new 5D trajectory coordinate.
    ///
    /// # Arguments
    ///
    /// * `depth` - Distance from root (0 = root)
    /// * `sibling_order` - Position among siblings (0-indexed)
    /// * `homogeneity` - Semantic similarity to parent [0, 1]
    /// * `temporal` - Normalized timestamp [0, 1]
    /// * `complexity` - Number of content parts (1 = simple text)
    #[inline]
    pub fn new(depth: u32, sibling_order: u32, homogeneity: f32, temporal: f32, complexity: u32) -> Self {
        debug_assert!(
            (0.0..=1.0).contains(&homogeneity),
            "homogeneity must be in [0, 1], got {}",
            homogeneity
        );
        debug_assert!(
            (0.0..=1.0).contains(&temporal),
            "temporal must be in [0, 1], got {}",
            temporal
        );
        debug_assert!(complexity >= 1, "complexity must be at least 1");

        Self {
            depth,
            sibling_order,
            homogeneity,
            temporal,
            complexity,
        }
    }

    /// Create from a 4D coordinate with default complexity (1).
    #[inline]
    pub fn from_4d(coord: &TrajectoryCoordinate) -> Self {
        Self {
            depth: coord.depth,
            sibling_order: coord.sibling_order,
            homogeneity: coord.homogeneity,
            temporal: coord.temporal,
            complexity: 1,
        }
    }

    /// Create from a 4D coordinate with specified complexity.
    #[inline]
    pub fn from_4d_with_complexity(coord: &TrajectoryCoordinate, complexity: u32) -> Self {
        Self {
            depth: coord.depth,
            sibling_order: coord.sibling_order,
            homogeneity: coord.homogeneity,
            temporal: coord.temporal,
            complexity: complexity.max(1),
        }
    }

    /// Convert to 4D coordinate (discards complexity).
    #[inline]
    pub fn to_4d(&self) -> TrajectoryCoordinate {
        TrajectoryCoordinate {
            depth: self.depth,
            sibling_order: self.sibling_order,
            homogeneity: self.homogeneity,
            temporal: self.temporal,
        }
    }

    /// Create a root coordinate.
    #[inline]
    pub fn root() -> Self {
        Self {
            depth: 0,
            sibling_order: 0,
            homogeneity: 1.0,
            temporal: 0.0,
            complexity: 1,
        }
    }

    /// Compute Euclidean distance between two 5D coordinates.
    ///
    /// Uses log-scaled complexity to prevent domination by very complex messages.
    #[inline]
    pub fn distance(&self, other: &Self) -> f32 {
        let d_depth = (self.depth as f32 - other.depth as f32).abs();
        let d_sibling = (self.sibling_order as f32 - other.sibling_order as f32).abs();
        let d_homo = self.homogeneity - other.homogeneity;
        let d_time = self.temporal - other.temporal;
        // Log-scale complexity to prevent domination (ln(1)=0, ln(e)=1, ln(10)≈2.3)
        let d_complexity = ((self.complexity as f32).ln_1p() - (other.complexity as f32).ln_1p()).abs();

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2) + d_complexity.powi(2)).sqrt()
    }

    /// Compute weighted distance with custom dimension weights.
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    /// * `weights` - (depth, sibling, homogeneity, temporal, complexity)
    #[inline]
    pub fn weighted_distance(&self, other: &Self, weights: (f32, f32, f32, f32, f32)) -> f32 {
        let d_depth = (self.depth as f32 - other.depth as f32).abs() * weights.0;
        let d_sibling = (self.sibling_order as f32 - other.sibling_order as f32).abs() * weights.1;
        let d_homo = (self.homogeneity - other.homogeneity).abs() * weights.2;
        let d_time = (self.temporal - other.temporal).abs() * weights.3;
        let d_complexity = ((self.complexity as f32).ln_1p() - (other.complexity as f32).ln_1p()).abs() * weights.4;

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2) + d_complexity.powi(2)).sqrt()
    }

    /// Compute weighted Euclidean distance using DLMWeights configuration.
    ///
    /// This is the primary distance metric used by DLM for coordinate-based retrieval.
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    /// * `weights` - DLMWeights configuration
    ///
    /// # Example
    ///
    /// ```
    /// use rag_plusplus_core::trajectory::{TrajectoryCoordinate5D, DLMWeights};
    ///
    /// let a = TrajectoryCoordinate5D::new(2, 0, 0.9, 0.3, 1);
    /// let b = TrajectoryCoordinate5D::new(4, 1, 0.7, 0.6, 3);
    ///
    /// // Use default DLM weights
    /// let dist = a.dlm_distance(&b, &DLMWeights::default());
    ///
    /// // Use semantic-focused weights
    /// let semantic_dist = a.dlm_distance(&b, &DLMWeights::semantic_focused());
    /// ```
    #[inline]
    pub fn dlm_distance(&self, other: &Self, weights: &DLMWeights) -> f32 {
        self.weighted_distance(
            other,
            (weights.depth, weights.sibling, weights.homogeneity, weights.temporal, weights.complexity),
        )
    }

    /// Compute weighted Manhattan distance (L1 norm).
    ///
    /// DLM uses this for certain distance calculations where L1 is preferred
    /// over L2 (Euclidean).
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    /// * `weights` - DLMWeights configuration
    ///
    /// # Example
    ///
    /// ```
    /// use rag_plusplus_core::trajectory::{TrajectoryCoordinate5D, DLMWeights};
    ///
    /// let a = TrajectoryCoordinate5D::new(2, 0, 0.9, 0.3, 1);
    /// let b = TrajectoryCoordinate5D::new(4, 1, 0.7, 0.6, 3);
    ///
    /// let dist = a.weighted_manhattan(&b, &DLMWeights::default());
    /// ```
    #[inline]
    pub fn weighted_manhattan(&self, other: &Self, weights: &DLMWeights) -> f32 {
        let d_depth = (self.depth as f32 - other.depth as f32).abs() * weights.depth;
        let d_sibling = (self.sibling_order as f32 - other.sibling_order as f32).abs() * weights.sibling;
        let d_homo = (self.homogeneity - other.homogeneity).abs() * weights.homogeneity;
        let d_time = (self.temporal - other.temporal).abs() * weights.temporal;
        let d_complexity = ((self.complexity as f32).ln_1p() - (other.complexity as f32).ln_1p()).abs() * weights.complexity;

        d_depth + d_sibling + d_homo + d_time + d_complexity
    }

    /// Compute unweighted Manhattan distance (L1 norm).
    #[inline]
    pub fn manhattan_distance(&self, other: &Self) -> f32 {
        let d_depth = (self.depth as i64 - other.depth as i64).unsigned_abs() as f32;
        let d_sibling = (self.sibling_order as i64 - other.sibling_order as i64).unsigned_abs() as f32;
        let d_homo = (self.homogeneity - other.homogeneity).abs();
        let d_time = (self.temporal - other.temporal).abs();
        let d_complexity = ((self.complexity as f32).ln_1p() - (other.complexity as f32).ln_1p()).abs();

        d_depth + d_sibling + d_homo + d_time + d_complexity
    }

    /// Convert coordinate to a 5D vector for vector operations.
    ///
    /// Returns [depth, sibling_order, homogeneity, temporal, ln(1+complexity)].
    /// Uses log-scaled complexity for balanced vector representation.
    #[inline]
    pub fn as_vector(&self) -> [f32; 5] {
        [
            self.depth as f32,
            self.sibling_order as f32,
            self.homogeneity,
            self.temporal,
            (self.complexity as f32).ln_1p(),
        ]
    }

    /// Convert coordinate to a normalized 5D vector.
    ///
    /// All dimensions scaled to approximately [0, 1] range.
    ///
    /// # Arguments
    ///
    /// * `max_depth` - Maximum depth in trajectory
    /// * `max_siblings` - Maximum number of siblings
    /// * `max_complexity` - Maximum complexity value
    #[inline]
    pub fn as_normalized_vector(&self, max_depth: u32, max_siblings: u32, max_complexity: u32) -> [f32; 5] {
        let depth_norm = if max_depth > 0 { self.depth as f32 / max_depth as f32 } else { 0.0 };
        let sibling_norm = if max_siblings > 0 { self.sibling_order as f32 / max_siblings as f32 } else { 0.0 };
        let complexity_norm = if max_complexity > 1 {
            (self.complexity as f32).ln_1p() / (max_complexity as f32).ln_1p()
        } else {
            0.0
        };

        [depth_norm, sibling_norm, self.homogeneity, self.temporal, complexity_norm]
    }

    /// Compute cosine similarity between two coordinates as vectors.
    ///
    /// DLM supports this for comparing coordinate "directions" rather than magnitudes.
    /// Useful for finding coordinates with similar relative positions.
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    ///
    /// # Returns
    ///
    /// Cosine similarity in range [-1, 1], where 1 = identical direction.
    ///
    /// # Example
    ///
    /// ```
    /// use rag_plusplus_core::trajectory::TrajectoryCoordinate5D;
    ///
    /// let a = TrajectoryCoordinate5D::new(2, 0, 0.9, 0.3, 1);
    /// let b = TrajectoryCoordinate5D::new(4, 0, 0.9, 0.6, 2);  // Similar direction
    ///
    /// let sim = a.cosine_similarity(&b);
    /// assert!(sim > 0.9);  // High similarity
    /// ```
    #[inline]
    pub fn cosine_similarity(&self, other: &Self) -> f32 {
        let a = self.as_vector();
        let b = other.as_vector();

        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm_a > 0.0 && norm_b > 0.0 {
            dot / (norm_a * norm_b)
        } else {
            0.0
        }
    }

    /// Compute combined distance using both Euclidean and cosine components.
    ///
    /// This hybrid metric balances magnitude (Euclidean) and direction (cosine)
    /// for more nuanced coordinate comparison.
    ///
    /// # Arguments
    ///
    /// * `other` - Other coordinate
    /// * `weights` - DLMWeights for Euclidean component
    /// * `cosine_weight` - Weight for cosine component [0, 1]
    ///
    /// # Returns
    ///
    /// Combined distance where higher = less similar.
    #[inline]
    pub fn hybrid_distance(&self, other: &Self, weights: &DLMWeights, cosine_weight: f32) -> f32 {
        let euclidean = self.dlm_distance(other, weights);
        let cosine_dist = 1.0 - self.cosine_similarity(other); // Convert similarity to distance

        let cw = cosine_weight.clamp(0.0, 1.0);
        (1.0 - cw) * euclidean + cw * cosine_dist
    }

    /// Compute normalized complexity factor [0, 1].
    ///
    /// Uses log scaling: complexity 1 → 0.0, complexity 10 → ~0.7, complexity 100 → ~0.9
    #[inline]
    pub fn complexity_factor(&self) -> f32 {
        // ln(1+x)/ln(1+max_typical) where max_typical ≈ 10
        (self.complexity as f32).ln_1p() / 10.0_f32.ln_1p()
    }

    /// Check if this is a complex message (more than just text).
    #[inline]
    pub fn is_multimodal(&self) -> bool {
        self.complexity > 1
    }

    /// Normalize all dimensions to [0, 1] for cross-trajectory comparison.
    pub fn normalized(&self, max_depth: u32, total_siblings: u32, max_complexity: u32) -> NormalizedCoordinate5D {
        let depth = if max_depth == 0 {
            0.0
        } else {
            self.depth as f32 / max_depth as f32
        };

        let sibling_order = if total_siblings <= 1 {
            0.0
        } else {
            self.sibling_order as f32 / (total_siblings - 1) as f32
        };

        let complexity = if max_complexity <= 1 {
            0.0
        } else {
            (self.complexity as f32).ln_1p() / (max_complexity as f32).ln_1p()
        };

        NormalizedCoordinate5D {
            depth,
            sibling_order,
            homogeneity: self.homogeneity,
            temporal: self.temporal,
            complexity,
        }
    }
}

impl Default for TrajectoryCoordinate5D {
    fn default() -> Self {
        Self::root()
    }
}

impl From<TrajectoryCoordinate> for TrajectoryCoordinate5D {
    fn from(coord: TrajectoryCoordinate) -> Self {
        Self::from_4d(&coord)
    }
}

impl From<&TrajectoryCoordinate> for TrajectoryCoordinate5D {
    fn from(coord: &TrajectoryCoordinate) -> Self {
        Self::from_4d(coord)
    }
}

/// Fully normalized 5D coordinate where all dimensions are [0, 1].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NormalizedCoordinate5D {
    /// Normalized depth [0, 1]
    pub depth: f32,
    /// Normalized sibling order [0, 1]
    pub sibling_order: f32,
    /// Semantic homogeneity [0, 1]
    pub homogeneity: f32,
    /// Temporal position [0, 1]
    pub temporal: f32,
    /// Normalized complexity [0, 1] (log-scaled)
    pub complexity: f32,
}

impl NormalizedCoordinate5D {
    /// Compute Euclidean distance between normalized 5D coordinates.
    #[inline]
    pub fn distance(&self, other: &Self) -> f32 {
        let d_depth = self.depth - other.depth;
        let d_sibling = self.sibling_order - other.sibling_order;
        let d_homo = self.homogeneity - other.homogeneity;
        let d_time = self.temporal - other.temporal;
        let d_complexity = self.complexity - other.complexity;

        (d_depth.powi(2) + d_sibling.powi(2) + d_homo.powi(2) + d_time.powi(2) + d_complexity.powi(2)).sqrt()
    }

    /// Convert to array for batch operations.
    #[inline]
    pub fn to_array(&self) -> [f32; 5] {
        [self.depth, self.sibling_order, self.homogeneity, self.temporal, self.complexity]
    }
}

/// Compute coordinates for all episodes in a trajectory.
///
/// # Arguments
///
/// * `episodes` - List of (depth, sibling_order, embedding, timestamp)
/// * `parent_indices` - Parent index for each episode (None for root)
///
/// # Returns
///
/// Vector of TrajectoryCoordinate for each episode.
pub fn compute_trajectory_coordinates(
    episodes: &[(u32, u32, &[f32], i64)],
    parent_indices: &[Option<usize>],
) -> Vec<TrajectoryCoordinate> {
    if episodes.is_empty() {
        return Vec::new();
    }

    // Find trajectory time bounds
    let trajectory_start = episodes.iter().map(|(_, _, _, t)| *t).min().unwrap_or(0);
    let trajectory_end = episodes.iter().map(|(_, _, _, t)| *t).max().unwrap_or(0);

    let mut coordinates = Vec::with_capacity(episodes.len());

    for (i, (depth, sibling_order, embedding, timestamp)) in episodes.iter().enumerate() {
        let parent_embedding = parent_indices.get(i)
            .and_then(|p| *p)
            .and_then(|p| episodes.get(p))
            .map(|(_, _, e, _)| *e);

        let coord = TrajectoryCoordinate::from_episode(
            *depth,
            *sibling_order,
            embedding,
            parent_embedding,
            *timestamp,
            trajectory_start,
            trajectory_end,
        );

        coordinates.push(coord);
    }

    coordinates
}

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

    #[test]
    fn test_new_coordinate() {
        let coord = TrajectoryCoordinate::new(3, 1, 0.85, 0.5);
        assert_eq!(coord.depth, 3);
        assert_eq!(coord.sibling_order, 1);
        assert!((coord.homogeneity - 0.85).abs() < 1e-6);
        assert!((coord.temporal - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_root_coordinate() {
        let root = TrajectoryCoordinate::root();
        assert_eq!(root.depth, 0);
        assert_eq!(root.sibling_order, 0);
        assert!((root.homogeneity - 1.0).abs() < 1e-6);
        assert!((root.temporal - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_distance_identical() {
        let coord = TrajectoryCoordinate::new(5, 2, 0.7, 0.3);
        assert!((coord.distance(&coord) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_distance_different() {
        let a = TrajectoryCoordinate::new(0, 0, 1.0, 0.0);
        let b = TrajectoryCoordinate::new(1, 0, 1.0, 0.0);

        // Distance should be 1.0 (only depth differs by 1)
        assert!((a.distance(&b) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_distance_all_dimensions() {
        let a = TrajectoryCoordinate::new(0, 0, 0.0, 0.0);
        let b = TrajectoryCoordinate::new(1, 1, 1.0, 1.0);

        // sqrt(1 + 1 + 1 + 1) = 2.0
        assert!((a.distance(&b) - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_manhattan_distance() {
        let a = TrajectoryCoordinate::new(0, 0, 0.0, 0.0);
        let b = TrajectoryCoordinate::new(1, 1, 1.0, 1.0);

        // 1 + 1 + 1 + 1 = 4.0
        assert!((a.manhattan_distance(&b) - 4.0).abs() < 1e-6);
    }

    #[test]
    fn test_weighted_distance() {
        let a = TrajectoryCoordinate::new(0, 0, 0.5, 0.5);
        let b = TrajectoryCoordinate::new(2, 0, 0.5, 0.5);

        // Only depth differs by 2, weight of 0.5 → 1.0
        let weights = (0.5, 1.0, 1.0, 1.0);
        assert!((a.weighted_distance(&b, weights) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_depth() {
        let coord = TrajectoryCoordinate::new(5, 0, 0.5, 0.5);
        assert!((coord.normalize_depth(10) - 0.5).abs() < 1e-6);
        assert!((coord.normalize_depth(5) - 1.0).abs() < 1e-6);
        assert!((coord.normalize_depth(0) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_sibling() {
        let coord = TrajectoryCoordinate::new(0, 2, 0.5, 0.5);
        assert!((coord.normalize_sibling(5) - 0.5).abs() < 1e-6); // 2/4 = 0.5
        assert!((coord.normalize_sibling(3) - 1.0).abs() < 1e-6); // 2/2 = 1.0
        assert!((coord.normalize_sibling(1) - 0.0).abs() < 1e-6); // Only child
    }

    #[test]
    fn test_normalized_coordinate() {
        let coord = TrajectoryCoordinate::new(5, 2, 0.8, 0.6);
        let normalized = coord.normalized(10, 5);

        assert!((normalized.depth - 0.5).abs() < 1e-6);
        assert!((normalized.sibling_order - 0.5).abs() < 1e-6);
        assert!((normalized.homogeneity - 0.8).abs() < 1e-6);
        assert!((normalized.temporal - 0.6).abs() < 1e-6);
    }

    #[test]
    fn test_from_episode() {
        // Create test embeddings (unit vectors)
        let parent_embedding = vec![1.0, 0.0, 0.0];
        let child_embedding = vec![0.8, 0.6, 0.0]; // cos = 0.8

        let coord = TrajectoryCoordinate::from_episode(
            3,
            1,
            &child_embedding,
            Some(&parent_embedding),
            500,
            0,
            1000,
        );

        assert_eq!(coord.depth, 3);
        assert_eq!(coord.sibling_order, 1);
        assert!((coord.homogeneity - 0.8).abs() < 0.01);
        assert!((coord.temporal - 0.5).abs() < 1e-6);
    }

    // ========== 5D Coordinate Tests (TPO Extension) ==========

    #[test]
    fn test_5d_new_coordinate() {
        let coord = TrajectoryCoordinate5D::new(3, 1, 0.85, 0.5, 2);
        assert_eq!(coord.depth, 3);
        assert_eq!(coord.sibling_order, 1);
        assert!((coord.homogeneity - 0.85).abs() < 1e-6);
        assert!((coord.temporal - 0.5).abs() < 1e-6);
        assert_eq!(coord.complexity, 2);
    }

    #[test]
    fn test_5d_root_coordinate() {
        let root = TrajectoryCoordinate5D::root();
        assert_eq!(root.depth, 0);
        assert_eq!(root.sibling_order, 0);
        assert!((root.homogeneity - 1.0).abs() < 1e-6);
        assert!((root.temporal - 0.0).abs() < 1e-6);
        assert_eq!(root.complexity, 1);
    }

    #[test]
    fn test_5d_from_4d() {
        let coord_4d = TrajectoryCoordinate::new(2, 1, 0.7, 0.4);
        let coord_5d = TrajectoryCoordinate5D::from_4d(&coord_4d);

        assert_eq!(coord_5d.depth, 2);
        assert_eq!(coord_5d.sibling_order, 1);
        assert!((coord_5d.homogeneity - 0.7).abs() < 1e-6);
        assert!((coord_5d.temporal - 0.4).abs() < 1e-6);
        assert_eq!(coord_5d.complexity, 1); // Default
    }

    #[test]
    fn test_5d_from_4d_with_complexity() {
        let coord_4d = TrajectoryCoordinate::new(2, 1, 0.7, 0.4);
        let coord_5d = TrajectoryCoordinate5D::from_4d_with_complexity(&coord_4d, 5);

        assert_eq!(coord_5d.complexity, 5);
    }

    #[test]
    fn test_5d_to_4d() {
        let coord_5d = TrajectoryCoordinate5D::new(3, 2, 0.8, 0.6, 4);
        let coord_4d = coord_5d.to_4d();

        assert_eq!(coord_4d.depth, 3);
        assert_eq!(coord_4d.sibling_order, 2);
        assert!((coord_4d.homogeneity - 0.8).abs() < 1e-6);
        assert!((coord_4d.temporal - 0.6).abs() < 1e-6);
    }

    #[test]
    fn test_5d_distance_identical() {
        let coord = TrajectoryCoordinate5D::new(5, 2, 0.7, 0.3, 3);
        assert!((coord.distance(&coord) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_5d_distance_same_complexity() {
        // Same complexity should reduce to 4D distance
        let a = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(1, 0, 1.0, 0.0, 1);

        // Distance should be 1.0 (only depth differs by 1, complexity same)
        assert!((a.distance(&b) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_5d_distance_complexity_affects() {
        // Different complexity should increase distance
        let a = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 10);

        // Distance should be ln(11) - ln(2) ≈ 1.7
        let d = a.distance(&b);
        assert!(d > 1.0); // Complexity adds to distance
        assert!(d < 3.0); // But log scaling prevents domination
    }

    #[test]
    fn test_5d_complexity_factor() {
        let simple = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 1);
        let medium = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 5);
        let complex = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 10);

        // Complexity 1 → ~0.0 (ln(2)/ln(11))
        assert!(simple.complexity_factor() < 0.3);

        // Complexity 5 → ~0.5-0.6
        assert!(medium.complexity_factor() > 0.4);
        assert!(medium.complexity_factor() < 0.8);

        // Complexity 10 → ~0.9-1.0
        assert!(complex.complexity_factor() > 0.85);
    }

    #[test]
    fn test_5d_is_multimodal() {
        let simple = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 1);
        let multimodal = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 3);

        assert!(!simple.is_multimodal());
        assert!(multimodal.is_multimodal());
    }

    #[test]
    fn test_5d_from_trait() {
        let coord_4d = TrajectoryCoordinate::new(1, 0, 0.9, 0.2);
        let coord_5d: TrajectoryCoordinate5D = coord_4d.into();

        assert_eq!(coord_5d.depth, 1);
        assert_eq!(coord_5d.complexity, 1);
    }

    #[test]
    fn test_5d_normalized() {
        let coord = TrajectoryCoordinate5D::new(5, 2, 0.8, 0.6, 5);
        let normalized = coord.normalized(10, 5, 10);

        assert!((normalized.depth - 0.5).abs() < 1e-6);
        assert!((normalized.sibling_order - 0.5).abs() < 1e-6);
        assert!((normalized.homogeneity - 0.8).abs() < 1e-6);
        assert!((normalized.temporal - 0.6).abs() < 1e-6);
        // Complexity: ln(6)/ln(11) ≈ 0.75
        assert!(normalized.complexity > 0.7);
        assert!(normalized.complexity < 0.8);
    }

    #[test]
    fn test_normalized_5d_distance() {
        let a = NormalizedCoordinate5D {
            depth: 0.0,
            sibling_order: 0.0,
            homogeneity: 0.0,
            temporal: 0.0,
            complexity: 0.0,
        };
        let b = NormalizedCoordinate5D {
            depth: 1.0,
            sibling_order: 1.0,
            homogeneity: 1.0,
            temporal: 1.0,
            complexity: 1.0,
        };

        // sqrt(1 + 1 + 1 + 1 + 1) ≈ 2.236
        let d = a.distance(&b);
        assert!((d - 5.0_f32.sqrt()).abs() < 1e-5);
    }

    #[test]
    fn test_normalized_5d_to_array() {
        let coord = NormalizedCoordinate5D {
            depth: 0.1,
            sibling_order: 0.2,
            homogeneity: 0.3,
            temporal: 0.4,
            complexity: 0.5,
        };

        let arr = coord.to_array();
        assert_eq!(arr, [0.1, 0.2, 0.3, 0.4, 0.5]);
    }

    // ========== DLM Weight Configuration Tests ==========

    #[test]
    fn test_dlm_weights_default() {
        let weights = DLMWeights::default();

        // Default weights from DLM config
        assert!((weights.depth - 0.25).abs() < 1e-6);
        assert!((weights.sibling - 0.15).abs() < 1e-6);
        assert!((weights.homogeneity - 0.30).abs() < 1e-6);
        assert!((weights.temporal - 0.20).abs() < 1e-6);
        assert!((weights.complexity - 0.10).abs() < 1e-6);

        // Weights should sum to 1.0
        let total = weights.depth + weights.sibling + weights.homogeneity + weights.temporal + weights.complexity;
        assert!((total - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_dlm_weights_normalization() {
        // Unnormalized weights
        let weights = DLMWeights::new(1.0, 1.0, 1.0, 1.0, 1.0);

        // Should normalize to equal weights
        assert!((weights.depth - 0.2).abs() < 1e-6);
        assert!((weights.sibling - 0.2).abs() < 1e-6);
        assert!((weights.homogeneity - 0.2).abs() < 1e-6);
        assert!((weights.temporal - 0.2).abs() < 1e-6);
        assert!((weights.complexity - 0.2).abs() < 1e-6);
    }

    #[test]
    fn test_dlm_weights_presets() {
        let semantic = DLMWeights::semantic_focused();
        assert!(semantic.homogeneity > semantic.depth);
        assert!(semantic.homogeneity > semantic.sibling);

        let structural = DLMWeights::structural_focused();
        assert!(structural.depth > structural.homogeneity);
        assert!(structural.sibling > structural.complexity);

        let temporal = DLMWeights::temporal_focused();
        assert!(temporal.temporal > temporal.depth);
        assert!(temporal.temporal > temporal.homogeneity);

        let equal = DLMWeights::equal();
        assert!((equal.depth - 0.2).abs() < 1e-6);
        assert!((equal.sibling - 0.2).abs() < 1e-6);
    }

    #[test]
    fn test_dlm_weights_array_conversion() {
        let weights = DLMWeights::default();
        let arr = weights.to_array();

        let reconstructed = DLMWeights::from_array(arr);
        assert!((weights.depth - reconstructed.depth).abs() < 1e-6);
        assert!((weights.sibling - reconstructed.sibling).abs() < 1e-6);
        assert!((weights.homogeneity - reconstructed.homogeneity).abs() < 1e-6);
        assert!((weights.temporal - reconstructed.temporal).abs() < 1e-6);
        assert!((weights.complexity - reconstructed.complexity).abs() < 1e-6);
    }

    // ========== DLM Distance Methods Tests ==========

    #[test]
    fn test_5d_dlm_distance() {
        let a = TrajectoryCoordinate5D::new(0, 0, 1.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(2, 1, 0.8, 0.3, 2);

        let weights = DLMWeights::default();
        let dist = a.dlm_distance(&b, &weights);

        // Distance should be positive and reasonable
        assert!(dist > 0.0);
        assert!(dist < 5.0);
    }

    #[test]
    fn test_5d_dlm_distance_identical() {
        let coord = TrajectoryCoordinate5D::new(3, 2, 0.7, 0.5, 3);
        let weights = DLMWeights::default();

        assert!((coord.dlm_distance(&coord, &weights) - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_5d_weighted_manhattan() {
        let a = TrajectoryCoordinate5D::new(0, 0, 0.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(2, 2, 1.0, 1.0, 1);

        // With equal weights
        let weights = DLMWeights::equal();
        let dist = a.weighted_manhattan(&b, &weights);

        // Each dimension contributes: 2*0.2 + 2*0.2 + 1.0*0.2 + 1.0*0.2 + 0*0.2 = 1.2
        assert!((dist - 1.2).abs() < 1e-6);
    }

    #[test]
    fn test_5d_manhattan_distance() {
        let a = TrajectoryCoordinate5D::new(0, 0, 0.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(2, 2, 1.0, 1.0, 1);

        // Unweighted: 2 + 2 + 1.0 + 1.0 + 0 = 6.0
        let dist = a.manhattan_distance(&b);
        assert!((dist - 6.0).abs() < 1e-6);
    }

    #[test]
    fn test_5d_as_vector() {
        let coord = TrajectoryCoordinate5D::new(3, 2, 0.75, 0.5, 4);
        let vec = coord.as_vector();

        assert!((vec[0] - 3.0).abs() < 1e-6);
        assert!((vec[1] - 2.0).abs() < 1e-6);
        assert!((vec[2] - 0.75).abs() < 1e-6);
        assert!((vec[3] - 0.5).abs() < 1e-6);
        // ln(1+4) = ln(5) ≈ 1.609
        assert!((vec[4] - 5.0_f32.ln()).abs() < 1e-6);
    }

    #[test]
    fn test_5d_as_normalized_vector() {
        let coord = TrajectoryCoordinate5D::new(5, 3, 0.8, 0.6, 5);
        let vec = coord.as_normalized_vector(10, 6, 10);

        assert!((vec[0] - 0.5).abs() < 1e-6);  // 5/10
        assert!((vec[1] - 0.5).abs() < 1e-6);  // 3/6
        assert!((vec[2] - 0.8).abs() < 1e-6);  // homogeneity unchanged
        assert!((vec[3] - 0.6).abs() < 1e-6);  // temporal unchanged
        // ln(6)/ln(11) ≈ 0.748
        assert!(vec[4] > 0.7 && vec[4] < 0.8);
    }

    #[test]
    fn test_5d_cosine_similarity_identical() {
        let coord = TrajectoryCoordinate5D::new(3, 2, 0.8, 0.5, 2);
        let sim = coord.cosine_similarity(&coord);

        // Identical vectors should have cosine similarity of 1.0
        assert!((sim - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_5d_cosine_similarity_similar() {
        // Scaled version should have same direction
        let a = TrajectoryCoordinate5D::new(2, 1, 0.8, 0.4, 2);
        let b = TrajectoryCoordinate5D::new(4, 2, 0.8, 0.4, 2);  // Same direction, scaled

        let sim = a.cosine_similarity(&b);

        // Should be high but not exactly 1.0 due to log scaling of complexity
        assert!(sim > 0.95);
    }

    #[test]
    fn test_5d_cosine_similarity_different() {
        let a = TrajectoryCoordinate5D::new(10, 0, 0.0, 0.0, 1);
        let b = TrajectoryCoordinate5D::new(0, 10, 1.0, 1.0, 10);

        let sim = a.cosine_similarity(&b);

        // Very different directions should have low similarity
        assert!(sim < 0.5);
    }

    #[test]
    fn test_5d_hybrid_distance() {
        let a = TrajectoryCoordinate5D::new(2, 1, 0.9, 0.3, 1);
        let b = TrajectoryCoordinate5D::new(4, 2, 0.7, 0.6, 3);

        let weights = DLMWeights::default();

        // Pure Euclidean (cosine_weight = 0)
        let euclidean_only = a.hybrid_distance(&b, &weights, 0.0);

        // Pure cosine (cosine_weight = 1)
        let cosine_only = a.hybrid_distance(&b, &weights, 1.0);

        // Mixed (cosine_weight = 0.5)
        let mixed = a.hybrid_distance(&b, &weights, 0.5);

        // Mixed should be between the two extremes
        let min = euclidean_only.min(cosine_only);
        let max = euclidean_only.max(cosine_only);
        assert!(mixed >= min - 1e-6 && mixed <= max + 1e-6);
    }

    #[test]
    fn test_5d_hybrid_distance_identical() {
        let coord = TrajectoryCoordinate5D::new(3, 2, 0.8, 0.5, 2);
        let weights = DLMWeights::default();

        let dist = coord.hybrid_distance(&coord, &weights, 0.5);

        // Distance to self should be 0
        assert!((dist - 0.0).abs() < 1e-6);
    }
}