geo 0.33.0

Geospatial primitives and algorithms
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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
//!
//! _k_-means clustering partitions `n` observations into _k_ clusters in which each observation belongs
//! to the cluster with the nearest mean (centroid). Unlike DBSCAN, _k_-means requires specifying the
//! number of clusters in advance.
//!
//! Uses Lloyd's algorithm with `k-means++` initialisation for improved cluster quality, accelerated
//! using [Hamerly's triangle inequality bounds (2010)](https://cs.baylor.edu/~hamerly/papers/2014_pca_chapter_hamerly_drake.pdf). This implementation is optimised for
//! 2D points with moderate cluster counts (k=2-50).
//!
//!
//! ## Hamerly's Algorithm
//!
//! This implementation uses Hamerly's triangle inequality bounds to skip unnecessary distance
//! calculations. The acceleration is most effective under the following conditions:
//!
//! - **Well-separated clusters**: Greater separation → tighter bounds → more pruning
//! - **More iterations**: Bounds tighten progressively, so longer convergence provides more savings
//! - **Moderate k**: Provides pruning opportunities whilst keeping O(k²) centroid distance overhead manageable
//!
//! For larger _k_ (> 50), the O(k²) centroid-to-centroid distance computation can dominate.
//!
//! # Parameters
//!
//! - `k`: The number of clusters to form. Must be > 0 and <= number of points. **NB**: More than `k` clusters may be returned if `max_radius` is specified.
//! - `max_radius` (optional): Maximum radius for clusters. If specified, clusters exceeding this radius will be subdivided,
//!   potentially resulting in more than `k` clusters.
//! - `max_iter` (optional): Maximum number of iterations. Default: 300.
//! - `tolerance` (optional): Absolute tolerance for convergence based on centroid movement (in coordinate units). Default: 0.0001.
//!
//! # Returns
//!
//! Returns `Result<Vec<usize>, KMeansError<T>>`:
//! - `Ok(labels)`: A vector of cluster labels (0-based indices), one for each input point
//! - `Err(KMeansError::InvalidK)`: If `k` is 0 or greater than the number of points
//! - `Err(KMeansError::EmptyCluster)`: If an empty cluster cannot be recovered during iteration
//! - `Err(KMeansError::InitializationFailed)`: If k-means++ initialisation fails due to degenerate data (e.g., all points at same location, NaN/infinite coordinates)
//! - `Err(KMeansError::MaxIterationsReached)`: If max_iter is reached before convergence. The error contains the partial result with convergence metadata.
//!
//! # Algorithm
//!
//! The algorithm works as follows:
//! 1. **Initialisation**: Uses [k-means++](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf) for better initial centroids
//! 2. **Assignment**: Assign each point to the cluster with the nearest centroid using Hamerly's triangle inequality bounds
//!    - Maintains upper and lower bounds on distances to avoid redundant calculations
//!    - Only computes actual distances when bounds don't rule out assignment changes
//! 3. **Update**: Recalculate centroids as the mean of all points in each cluster
//! 4. **Bounds update**: Update distance bounds based on centroid movement using triangle inequality
//! 5. **Convergence check**: Stop if:
//!    - Centroids have moved less than the tolerance threshold, OR
//!    - No points have changed cluster assignment, OR
//!    - Maximum iterations reached
//! 6. Repeat steps 2-5 until convergence
//! 7. **max_radius constraint** (if specified): Split any clusters with radius exceeding max_radius
//!
//! # Notes
//!
//! ## Local Minima
//!
//! _k_-means converges to a local minimum, not necessarily the global optimum. The final clustering
//! depends on the initial centroid placement. Although _k_-means++ provides good initialisation,
//! it can still occasionally place multiple initial centroids within the same "natural" cluster,
//! leading to suboptimal results. For critical applications, consider running _k_-means multiple
//! times with different seeds and selecting the result with lowest inertia (sum of squared
//! distances to centroids).
//!
//! ## Empty Clusters
//!
//! Empty clusters may rarely occur during iteration if all points are reassigned away from a centroid.
//! When this happens, the algorithm attempts to recover by reassigning the farthest point from its
//! current centroid to the empty cluster (following scikit-learn's approach). If recovery fails,
//! the function returns an error.
//!
//! ## Complexity
//!
//! - **Initialization (k-means++)**: O(n·k²)
//! - **Per iteration**: O(k²) centroid distances + O(n) for point assignments with Hamerly pruning
//! - **Overall**: Effectively O(i·n) for moderate k, where i is iterations to convergence
//!
//! For large datasets with _k_ > 50, consider hierarchical or density-based clustering (DBSCAN).
//!
//! # Examples
//!
//! ## Basic clustering with MultiPoint
//!
//! ```
//! use geo::{KMeans, MultiPoint, point};
//!
//! let points = MultiPoint::new(vec![
//!     point!(x: 0.0, y: 0.0),
//!     point!(x: 1.0, y: 0.0),
//!     point!(x: 0.0, y: 1.0),
//!     point!(x: 10.0, y: 10.0),
//!     point!(x: 11.0, y: 10.0),
//!     point!(x: 10.0, y: 11.0),
//! ]);
//!
//! let labels = points.kmeans(2).unwrap();
//!
//! // Two clusters should be found
//! let cluster_0_count = labels.iter().filter(|&l| *l == 0).count();
//! let cluster_1_count = labels.iter().filter(|&l| *l == 1).count();
//! assert_eq!(cluster_0_count + cluster_1_count, 6);
//! ```
//!
//! ## Using with a Vec of Points
//!
//! ```
//! use geo::{KMeans, point};
//!
//! let points = vec![
//!     point!(x: 0.0, y: 0.0),
//!     point!(x: 1.0, y: 1.0),
//!     point!(x: 1.0, y: 0.0),
//!     point!(x: 0.0, y: 1.0),
//! ];
//!
//! let labels = points.kmeans(2).unwrap();
//!
//! // All points should be assigned to one of two clusters
//! assert_eq!(labels.len(), 4);
//! ```
//!
//! # Errors
//!
//! Returns `KMeansError<T>` if:
//! - `k` is 0 or greater than the number of points (`InvalidK`)
//! - An empty cluster cannot be recovered during iteration (`EmptyCluster`)
//! - Initialisation fails due to degenerate data (`InitializationFailed`)
//! - Maximum iterations reached before convergence (`MaxIterationsReached`)

mod error;
pub use error::{KMeansError, KMeansInitError};

use crate::algorithm::Vector2DOps;
use crate::line_measures::Distance;
use crate::line_measures::metric_spaces::Euclidean;
use crate::{Centroid, GeoFloat, MultiPoint, Point};

use rand::RngExt;
use rand::SeedableRng;
use rand::distr::weighted::WeightedIndex;
use rand::prelude::Distribution;

/// Parameters for k-means clustering with builder pattern.
///
/// # Examples
///
/// ```
/// use geo::{KMeans, KMeansParams, point};
///
/// let points = vec![
///     point!(x: 0.0, y: 0.0),
///     point!(x: 1.0, y: 1.0),
///     point!(x: 10.0, y: 10.0),
///     point!(x: 11.0, y: 11.0),
/// ];
///
/// // Use builder pattern for advanced configuration
/// let params = KMeansParams::new(2)
///     .seed(42)
///     .max_iter(500)
///     .tolerance(0.001);
///
/// let labels = points.kmeans_with_params(params).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct KMeansParams<T: GeoFloat> {
    k: usize,
    seed: Option<u64>,
    max_iter: usize,
    tolerance: T,
    max_radius: Option<T>,
}

impl<T: GeoFloat> KMeansParams<T> {
    /// Create new k-means parameters with the specified number of clusters.
    ///
    /// Uses default values for optional parameters:
    /// - No seed (random initialisation)
    /// - max_iter: 300
    /// - tolerance: 0.0001
    /// - No max_radius constraint
    ///
    /// This function is not available on WASM targets
    #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
    pub fn new(k: usize) -> Self {
        let mut s = Self::new_with_seed(k, 42);
        s.seed = None;
        s
    }

    /// Create new k-means parameters with the specified number of clusters with an explicit random seed.
    ///
    /// The seed can either be a constant to get identical values across executions or it can be
    /// a random value.
    ///
    /// Uses default values for optional parameters:
    /// - max_iter: 300
    /// - tolerance: 0.0001
    /// - No max_radius constraint
    ///
    /// Prefer using this function if you want to support WASM targets
    pub fn new_with_seed(k: usize, seed: u64) -> Self {
        Self {
            k,
            seed: Some(seed),
            max_iter: 300, // same as scikit-learn
            tolerance: T::from(0.0001).expect("tolerance must be representable in float type"),
            max_radius: None,
        }
    }

    /// Set the random seed for reproducible results.
    ///
    /// When set, k-means will produce identical results across multiple runs with the same
    /// input data and parameters.
    pub fn seed(mut self, seed: u64) -> Self {
        self.seed = Some(seed);
        self
    }

    /// Set the maximum number of iterations.
    ///
    /// Default: 300
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set the convergence tolerance.
    ///
    /// The algorithm stops when the maximum distance any centroid moves is less than this
    /// threshold (measured in coordinate units).
    /// Default: 0.0001
    pub fn tolerance(mut self, tolerance: T) -> Self {
        self.tolerance = tolerance;
        self
    }

    /// Set the maximum cluster radius.
    ///
    /// When set, clusters exceeding this radius will be subdivided, potentially resulting
    /// in more than k clusters.
    pub fn max_radius(mut self, max_radius: T) -> Self {
        self.max_radius = Some(max_radius);
        self
    }
}

pub trait KMeans<T>
where
    T: GeoFloat,
{
    /// Perform _k_-means clustering on the points.
    ///
    /// Returns a vector of cluster assignments, one for each input point.
    /// Each element is a cluster ID in the range `0..k`.
    ///
    /// To specify configuration options, use [`kmeans_with_params`](Self::kmeans_with_params).
    ///
    /// The output depends on initial conditions - see [`Self::kmeans_with_seed`] or [`Self::kmeans_with_params`]
    /// to pass in a random or predetermined seed, otherwise a deterministic seed will be used.
    ///
    /// # Errors
    ///
    /// Returns `KMeansError::InvalidK` if `k` is 0 or greater than the number of points.
    /// Returns `KMeansError::EmptyCluster` if a cluster becomes empty and cannot be recovered.
    /// Returns `KMeansError::MaxIterationsReached` if max_iter is reached before convergence.
    ///
    /// See the [module-level documentation](self) for details on the algorithm and parameters.
    ///
    /// This function is only supported on non-WASM targets. Prefer using [kmeans_with_seed](Self::kmeans_with_seed)
    /// on WASM targets instead.
    #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
    fn kmeans(&self, k: usize) -> Result<Vec<usize>, KMeansError<T>> {
        self.kmeans_with_params(KMeansParams::new(k))
    }

    /// Perform _k_-means clustering on the points.
    ///
    /// Returns a vector of cluster assignments, one for each input point.
    /// Each element is a cluster ID in the range `0..k`.
    ///
    /// To specify configuration options, use [`kmeans_with_params`](Self::kmeans_with_params).
    ///
    /// # Errors
    ///
    /// Returns `KMeansError::InvalidK` if `k` is 0 or greater than the number of points.
    /// Returns `KMeansError::EmptyCluster` if a cluster becomes empty and cannot be recovered.
    /// Returns `KMeansError::MaxIterationsReached` if max_iter is reached before convergence.
    ///
    /// See the [module-level documentation](self) for details on the algorithm and parameters.
    fn kmeans_with_seed(&self, k: usize, seed: u64) -> Result<Vec<usize>, KMeansError<T>> {
        self.kmeans_with_params(KMeansParams::new_with_seed(k, seed))
    }

    /// Perform _k_-means clustering with specific configuration options.
    ///
    /// Returns a vector of cluster assignments, one for each input point.
    /// Each element is a cluster ID in the range `0..k`.
    ///
    /// # Examples
    ///
    /// ```
    /// use geo::{KMeans, KMeansParams, point};
    ///
    /// let points = vec![
    ///     point!(x: 0.0, y: 0.0),
    ///     point!(x: 1.0, y: 1.0),
    ///     point!(x: 10.0, y: 10.0),
    ///     point!(x: 11.0, y: 11.0),
    /// ];
    ///
    /// let params = KMeansParams::new(2)
    ///     .seed(42)           // Reproducible results
    ///     .max_iter(500)      // More iterations
    ///     .tolerance(0.001);  // Less strict convergence
    ///
    /// let labels = points.kmeans_with_params(params).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `KMeansError::InvalidK` if `k` is 0 or greater than the number of points.
    /// Returns `KMeansError::EmptyCluster` if a cluster becomes empty and cannot be recovered.
    /// Returns `KMeansError::InitializationFailed` if initialisation fails due to degenerate data.
    /// Returns `KMeansError::MaxIterationsReached` if max_iter is reached before convergence.
    ///
    /// See the [module-level documentation](self) for details on the algorithm and parameters.
    fn kmeans_with_params(&self, params: KMeansParams<T>) -> Result<Vec<usize>, KMeansError<T>>;
}

/// Configuration for k-means algorithm
#[derive(Debug, Clone)]
struct KMeansConfig<T: GeoFloat> {
    params: KMeansParams<T>,
    max_split_depth: usize,
}

#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
impl<T: GeoFloat> Default for KMeansConfig<T> {
    fn default() -> Self {
        Self {
            params: KMeansParams::new(0),
            max_split_depth: 10,
        }
    }
}

impl<T: GeoFloat> From<KMeansParams<T>> for KMeansConfig<T> {
    fn from(params: KMeansParams<T>) -> Self {
        Self {
            params,
            max_split_depth: 10,
        }
    }
}

fn kmeans_impl<T>(
    points: &[Point<T>],
    params: KMeansParams<T>,
) -> Result<Vec<usize>, KMeansError<T>>
where
    T: GeoFloat,
{
    let n = points.len();
    let k = params.k;

    // Handle edge cases
    if n == 0 {
        return Ok(Vec::new());
    }

    if k == 0 || k > n {
        return Err(KMeansError::InvalidK { k, n });
    }

    if k == 1 {
        // All points in one cluster
        return Ok(vec![0; n]);
    }

    if k == n {
        // Each point is its own cluster
        return Ok((0..n).collect());
    }

    let config = KMeansConfig::from(params);

    kmeans_impl_with_config(points, config)
}

fn kmeans_impl_with_config<T>(
    points: &[Point<T>],
    config: KMeansConfig<T>,
) -> Result<Vec<usize>, KMeansError<T>>
where
    T: GeoFloat,
{
    let n = points.len();
    let k = config.params.k;

    // Precompute squared norms of all points (done once, reused throughout)
    // This is the key optimisation from scikit-learn's implementation
    let point_sq_norms: Vec<T> = points.iter().map(|p| p.0.magnitude_squared()).collect();

    // Initialise centroids using k-means++
    let mut centroids = kmeans_plusplus_init(points, k, config.params.seed)?;

    // Precompute squared norms of initial centroids
    let mut centroid_sq_norms: Vec<T> = centroids.iter().map(|c| c.0.magnitude_squared()).collect();

    // Track cluster assignments and Hamerly bounds
    let mut assignments = vec![0; n];
    let mut upper_bounds = vec![T::infinity(); n];
    let mut lower_bounds = vec![T::zero(); n];

    // First iteration: assign all points to initial (k-means++) centroids
    for (i, (point, assignment)) in points.iter().zip(assignments.iter_mut()).enumerate() {
        let (nearest_idx, _, _) = find_nearest_and_second_nearest(
            *point,
            &centroids,
            point_sq_norms[i],
            &centroid_sq_norms,
        );
        *assignment = nearest_idx;
    }

    // Update centroids to be cluster means (initial centroids from k-means++ are data points,
    // not true cluster means). This is required before the first convergence check because
    // otherwise points at the initial centroid locations have upper_bound=0 and get pruned.
    centroids = update_centroids(points, &mut assignments, &centroids, k, 0)?;
    centroid_sq_norms = centroids.iter().map(|c| c.0.magnitude_squared()).collect();

    // Initialise Hamerly bounds based on true cluster centroids
    for (i, ((point, &assignment), (upper, lower))) in points
        .iter()
        .zip(assignments.iter())
        .zip(upper_bounds.iter_mut().zip(lower_bounds.iter_mut()))
        .enumerate()
    {
        // Calculate actual distances to all centroids
        let (_, nearest_sq_dist, second_nearest_sq_dist) = find_nearest_and_second_nearest(
            *point,
            &centroids,
            point_sq_norms[i],
            &centroid_sq_norms,
        );
        // Upper bound is distance to assigned centroid
        let sq_dist_to_assigned = squared_distance_using_norms(
            *point,
            centroids[assignment],
            point_sq_norms[i],
            centroid_sq_norms[assignment],
        );
        *upper = sq_dist_to_assigned.sqrt();
        *lower = second_nearest_sq_dist.sqrt();
        // After centroids are updated to be the mean of their clusters, a point's
        // assigned centroid might no longer be its geometrically closest centroid.
        // In this case, `nearest_sq_dist` (to the new closest) will be less than
        // `sq_dist_to_assigned`. The lower bound must be the distance to the true
        // second-closest centroid, so we take the minimum of the old second-closest
        // and the new `nearest_sq_dist`.
        if nearest_sq_dist < sq_dist_to_assigned {
            *lower = nearest_sq_dist.sqrt().min(*lower);
        }
    }

    // Track convergence state
    let mut final_max_delta = T::zero();
    let mut final_changed_count = 0;
    let mut converged = false;

    // Iterate until convergence
    for iter in 0..config.params.max_iter {
        let mut changed_count = 0;

        // Compute distance from each centroid to its nearest other centroid
        let centroid_distances = compute_centroid_distances(&centroids);

        // Assignment step using Hamerly's algorithm
        for (i, point) in points.iter().enumerate() {
            let assigned_idx = assignments[i];

            // Pruning condition 1: upper bound <= lower bound
            if upper_bounds[i] <= lower_bounds[i] {
                continue;
            }

            // Pruning condition 2: upper bound <= s[assigned]/2
            if upper_bounds[i] <= centroid_distances[assigned_idx] / T::from(2.0).unwrap() {
                continue;
            }

            // Tighten upper bound using optimised squared distance
            let sq_dist_to_assigned = squared_distance_using_norms(
                *point,
                centroids[assigned_idx],
                point_sq_norms[i],
                centroid_sq_norms[assigned_idx],
            );
            upper_bounds[i] = sq_dist_to_assigned.sqrt();

            // Recheck after tightening
            if upper_bounds[i] <= lower_bounds[i] {
                continue;
            }

            // Must check all centroids using optimised distance calculation
            let (new_nearest_idx, new_nearest_sq_dist, new_second_nearest_sq_dist) =
                find_nearest_and_second_nearest(
                    *point,
                    &centroids,
                    point_sq_norms[i],
                    &centroid_sq_norms,
                );

            if new_nearest_idx != assigned_idx {
                assignments[i] = new_nearest_idx;
                changed_count += 1;
            }

            // Store actual distances (sqrt of squared distances)
            upper_bounds[i] = new_nearest_sq_dist.sqrt();
            lower_bounds[i] = new_second_nearest_sq_dist.sqrt();
        }

        // Check for convergence: no assignments changed
        if changed_count == 0 {
            converged = true;
            break;
        }

        // Update step: recalculate centroids
        let new_centroids = update_centroids(points, &mut assignments, &centroids, k, iter)?;

        // Recompute centroid squared norms after update
        centroid_sq_norms = new_centroids
            .iter()
            .map(|c| c.0.magnitude_squared())
            .collect();

        // Compute centroid movement deltas
        let deltas: Vec<T> = centroids
            .iter()
            .zip(new_centroids.iter())
            .map(|(old, new)| Euclidean.distance(*old, *new))
            .collect();

        let max_delta = deltas.iter().fold(T::zero(), |a, &b| a.max(b));
        final_max_delta = max_delta;
        final_changed_count = changed_count;

        // Check for convergence: centroids haven't moved much
        if max_delta < config.params.tolerance {
            converged = true;
            break;
        }

        // Update bounds based on centroid movement
        for ((upper, lower), &assigned_idx) in upper_bounds
            .iter_mut()
            .zip(lower_bounds.iter_mut())
            .zip(assignments.iter())
        {
            *upper = *upper + deltas[assigned_idx];
            *lower = (*lower - max_delta).max(T::zero());
        }

        centroids = new_centroids;
    }

    // Check if we hit max_iter without converging
    if !converged {
        return Err(KMeansError::MaxIterationsReached {
            assignments,
            iterations: config.params.max_iter,
            max_centroid_shift: final_max_delta,
            tolerance: config.params.tolerance,
            changed_assignments: final_changed_count,
        });
    }

    // Apply max_radius constraint if specified
    if let Some(max_radius) = config.params.max_radius {
        apply_max_radius_constraint(
            points,
            &mut assignments,
            max_radius,
            config.max_split_depth,
            config.params.seed,
        )?;
    }

    Ok(assignments)
}

/// Initialise centroids using k-means++ algorithm
///
/// k-means++ improves upon random initialisation by choosing initial centroids
/// that are spread out, leading to better cluster quality and faster convergence.
///
/// Algorithm:
/// 1. Choose first centroid uniformly at random
/// 2. For each remaining centroid:
///    - Calculate squared distance from each point to its nearest existing centroid
///    - Choose next centroid with probability proportional to this squared distance (D² weighting)
///    - This favours points that are far from existing centroids
///
/// Reference: Arthur & Vassilvitskii (2007), ["k-means++: the Advantages of Careful Seeding"](https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf)
fn kmeans_plusplus_init<T>(
    points: &[Point<T>],
    k: usize,
    seed: Option<u64>,
) -> Result<Vec<Point<T>>, KMeansError<T>>
where
    T: GeoFloat,
{
    let n = points.len();
    let mut rng = rand_pcg::Pcg32::seed_from_u64(seed.unwrap_or(0));
    let mut centroids = Vec::with_capacity(k);

    // Choose first centroid uniformly at random
    // this would panic if n is set to 0, but we handle that case in kmeans_impl
    let first_idx = rng.random_range(0..n);
    centroids.push(points[first_idx]);

    // Choose remaining centroids using D² weighting: probability proportional to
    // squared distance to nearest centroid
    for _ in 1..k {
        // Calculate minimum squared distance from each point to nearest existing centroid,
        // validating and converting to f64 in a single pass
        let distances_f64: Vec<f64> = points
            .iter()
            .map(|point| {
                // Find minimum squared distance to any centroid
                let mut min_sq_dist = T::infinity();

                for centroid in centroids.iter() {
                    let dist = Euclidean.distance(*point, *centroid);

                    // Check for invalid values immediately (before .min() can hide them)
                    if dist.is_nan() {
                        return Err(KMeansError::InitializationFailed(
                            KMeansInitError::NaNCoordinate,
                        ));
                    }
                    if dist.is_infinite() {
                        return Err(KMeansError::InitializationFailed(
                            KMeansInitError::InfiniteCoordinate,
                        ));
                    }

                    let sq_dist = dist * dist;
                    min_sq_dist = min_sq_dist.min(sq_dist);
                }

                // Convert to f64 (guaranteed to succeed after validation above)
                Ok(min_sq_dist
                    .to_f64()
                    .expect("Valid distance should convert to f64"))
            })
            .collect::<Result<Vec<f64>, KMeansError<T>>>()?;

        // Choose next centroid with probability proportional to squared distance
        let dist = WeightedIndex::new(&distances_f64).map_err(|e| {
            // Check if all weights are zero (degenerate data case)
            let all_zero = distances_f64.iter().all(|&d| d == 0.0);
            if all_zero {
                KMeansError::InitializationFailed(KMeansInitError::DegenerateData)
            } else {
                KMeansError::InitializationFailed(KMeansInitError::WeightedDistributionFailed {
                    error: e,
                })
            }
        })?;
        let next_idx = dist.sample(&mut rng);
        centroids.push(points[next_idx]);
    }

    Ok(centroids)
}

/// Compute the distance from each centroid to its nearest other centroid
///
/// Used in Hamerly's algorithm for bounds-based pruning
fn compute_centroid_distances<T>(centroids: &[Point<T>]) -> Vec<T>
where
    T: GeoFloat,
{
    let k = centroids.len();
    let mut distances = vec![T::infinity(); k];

    // Compute each pairwise distance once (upper triangle only)
    (0..k)
        .flat_map(|i| ((i + 1)..k).map(move |j| (i, j)))
        .for_each(|(i, j)| {
            let dist = Euclidean.distance(centroids[i], centroids[j]);
            distances[i] = distances[i].min(dist);
            distances[j] = distances[j].min(dist);
        });

    distances
}

/// Compute squared Euclidean distance using precomputed squared norms
///
/// Uses the algebraic identity: ||a - b||² = ||a||² + ||b||² - 2⟨a,b⟩
///
/// This optimisation (from scikit-learn) avoids redundant squared norm calculations
/// in the O(n·k) inner loop. By precomputing ||a||² and ||b||² once, we only need
/// to compute the dot product ⟨a,b⟩ for each point-centroid pair.
///
/// # Returns
/// The squared Euclidean distance (no sqrt needed for distance comparisons)
#[inline]
fn squared_distance_using_norms<T>(
    point: Point<T>,
    centroid: Point<T>,
    point_sq_norm: T,
    centroid_sq_norm: T,
) -> T
where
    T: GeoFloat,
{
    // ||a - b||² = ||a||² + ||b||² - 2⟨a,b⟩
    let dot_prod = point.0.dot_product(centroid.0);
    point_sq_norm + centroid_sq_norm - (T::from(2.0).unwrap() * dot_prod)
}

/// Find the nearest and second-nearest centroids to a point using precomputed squared norms
///
/// Returns (nearest_index, nearest_squared_distance, second_nearest_squared_distance)
///
/// # Arguments
/// * `point` - The point to find nearest centroids for
/// * `centroids` - Slice of centroid points
/// * `point_sq_norm` - Precomputed ||point||²
/// * `centroid_sq_norms` - Precomputed ||centroid||² for each centroid
///
/// Note: Returns squared distances (no sqrt) since we only need them for comparison
fn find_nearest_and_second_nearest<T>(
    point: Point<T>,
    centroids: &[Point<T>],
    point_sq_norm: T,
    centroid_sq_norms: &[T],
) -> (usize, T, T)
where
    T: GeoFloat,
{
    let mut nearest_idx = 0;
    let mut nearest_sq_dist = T::infinity();
    let mut second_nearest_sq_dist = T::infinity();

    for (idx, (centroid, &centroid_sq_norm)) in
        centroids.iter().zip(centroid_sq_norms.iter()).enumerate()
    {
        let sq_dist =
            squared_distance_using_norms(point, *centroid, point_sq_norm, centroid_sq_norm);
        if sq_dist < nearest_sq_dist {
            second_nearest_sq_dist = nearest_sq_dist;
            nearest_sq_dist = sq_dist;
            nearest_idx = idx;
        } else if sq_dist < second_nearest_sq_dist {
            second_nearest_sq_dist = sq_dist;
        }
    }

    (nearest_idx, nearest_sq_dist, second_nearest_sq_dist)
}

/// Find the point that is farthest from its assigned centroid
///
/// Used for reassigning points to empty clusters. Returns None if no points are found.
/// Returns (point_index, distance) for the farthest point.
fn find_farthest_point<T>(
    points: &[Point<T>],
    assignments: &[usize],
    centroids: &[Point<T>],
) -> Option<(usize, T)>
where
    T: GeoFloat,
{
    let mut farthest_idx = None;
    let mut farthest_dist = T::zero();

    for (i, (point, &cluster_id)) in points.iter().zip(assignments.iter()).enumerate() {
        if cluster_id < centroids.len() {
            let dist = Euclidean.distance(*point, centroids[cluster_id]);
            if dist > farthest_dist {
                farthest_dist = dist;
                farthest_idx = Some(i);
            }
        }
    }

    farthest_idx.map(|idx| (idx, farthest_dist))
}

/// Update centroids based on current assignments
///
/// If an empty cluster is encountered, attempts to reassign the farthest point to that cluster.
/// Returns an error if an empty cluster cannot be recovered.
fn update_centroids<T>(
    points: &[Point<T>],
    assignments: &mut [usize],
    centroids: &[Point<T>],
    k: usize,
    iteration: usize,
) -> Result<Vec<Point<T>>, KMeansError<T>>
where
    T: GeoFloat,
{
    // We're manually calculating centroids rather than using e.g. MultiPoint::centroid()
    // This provides better perf for k-means:
    // - O(1) empty cluster reassignment using arithmetic on sums/counts
    // - No intermediate MultiPoint allocations per iteration

    // Accumulate sums and counts for each cluster in a single pass
    let mut sums: Vec<(T, T)> = vec![(T::zero(), T::zero()); k];
    let mut counts: Vec<usize> = vec![0; k];

    for (point, &cluster_id) in points.iter().zip(assignments.iter()) {
        if cluster_id < k {
            sums[cluster_id].0 = sums[cluster_id].0 + point.x();
            sums[cluster_id].1 = sums[cluster_id].1 + point.y();
            counts[cluster_id] += 1;
        }
    }

    // Handle empty clusters by reassigning the farthest point
    for cluster_id in 0..k {
        if counts[cluster_id] == 0 {
            // Find the point farthest from its current centroid
            if let Some((farthest_idx, _)) = find_farthest_point(points, assignments, centroids) {
                let old_cluster = assignments[farthest_idx];
                let point = points[farthest_idx];

                // Remove from old cluster
                sums[old_cluster].0 = sums[old_cluster].0 - point.x();
                sums[old_cluster].1 = sums[old_cluster].1 - point.y();
                counts[old_cluster] -= 1;

                // Add to new cluster
                sums[cluster_id].0 = sums[cluster_id].0 + point.x();
                sums[cluster_id].1 = sums[cluster_id].1 + point.y();
                counts[cluster_id] += 1;

                // Update assignment
                assignments[farthest_idx] = cluster_id;
            } else {
                // No points available to reassign: this is an unrecoverable error!
                return Err(KMeansError::EmptyCluster {
                    iteration,
                    cluster_id,
                });
            }
        }
    }

    // Compute centroids directly from sums and counts
    let new_centroids = sums
        .iter()
        .zip(counts.iter())
        .map(|(&(sum_x, sum_y), &count)| {
            let count_t = T::from(count).expect("Cluster count must be representable as float");
            Point::new(sum_x / count_t, sum_y / count_t)
        })
        .collect();

    Ok(new_centroids)
}

fn apply_max_radius_constraint<T>(
    points: &[Point<T>],
    assignments: &mut [usize],
    max_radius: T,
    remaining_depth: usize,
    seed: Option<u64>,
) -> Result<(), KMeansError<T>>
where
    T: GeoFloat,
{
    if remaining_depth == 0 {
        return Ok(());
    }

    // Find next available cluster ID. unwrap_or(0) is correct: if assignments is empty,
    // we start numbering clusters from 0
    let mut next_cluster_id = assignments.iter().max().map(|&a| a + 1).unwrap_or(0);

    let max_cluster = assignments.iter().max().copied().unwrap_or(0);
    let mut cluster_ids = Vec::with_capacity(max_cluster + 1);
    let mut seen = vec![false; max_cluster + 1];

    for &assignment in &*assignments {
        if !seen[assignment] {
            seen[assignment] = true;
            cluster_ids.push(assignment);
        }
    }

    for cluster_id in cluster_ids {
        let cluster_points: Vec<(usize, Point<T>)> = points
            .iter()
            .enumerate()
            .filter(|(idx, _)| assignments[*idx] == cluster_id)
            .map(|(idx, &point)| (idx, point))
            .collect();

        if cluster_points.is_empty() {
            continue;
        }

        let cluster_point_vec: Vec<Point<T>> = cluster_points.iter().map(|(_, p)| *p).collect();
        let multipoint = MultiPoint::new(cluster_point_vec);
        let centroid = multipoint
            .centroid()
            .expect("MultiPoint cannot be empty after filtering non-empty cluster");

        let max_dist = multipoint
            .iter()
            .map(|p| Euclidean.distance(&centroid, p))
            .fold(T::zero(), T::max);

        if max_dist > max_radius {
            let params = if let Some(s) = seed {
                KMeansParams::new_with_seed(2, s)
            } else {
                #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
                {
                    KMeansParams::new(2)
                }
                #[cfg(all(target_family = "wasm", target_os = "unknown"))]
                {
                    unreachable!(
                        "Geo only supports KMeansParams with an explicit seed on WASM Targets"
                    )
                }
            };
            let sub_assignments = kmeans_impl(&multipoint.0, params)?;

            for ((original_idx, _), &sub_assignment) in
                cluster_points.iter().zip(sub_assignments.iter())
            {
                assignments[*original_idx] = if sub_assignment == 0 {
                    cluster_id
                } else {
                    next_cluster_id
                };
            }
            next_cluster_id += 1;
        }
    }

    Ok(())
}

impl<T> KMeans<T> for MultiPoint<T>
where
    T: GeoFloat,
{
    fn kmeans_with_params(&self, params: KMeansParams<T>) -> Result<Vec<usize>, KMeansError<T>> {
        kmeans_impl(&self.0, params)
    }
}

impl<T> KMeans<T> for &MultiPoint<T>
where
    T: GeoFloat,
{
    fn kmeans_with_params(&self, params: KMeansParams<T>) -> Result<Vec<usize>, KMeansError<T>> {
        kmeans_impl(&self.0, params)
    }
}

impl<T> KMeans<T> for [Point<T>]
where
    T: GeoFloat,
{
    fn kmeans_with_params(&self, params: KMeansParams<T>) -> Result<Vec<usize>, KMeansError<T>> {
        kmeans_impl(self, params)
    }
}

impl<T> KMeans<T> for &[Point<T>]
where
    T: GeoFloat,
{
    fn kmeans_with_params(&self, params: KMeansParams<T>) -> Result<Vec<usize>, KMeansError<T>> {
        kmeans_impl(self, params)
    }
}

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

    #[test]
    fn test_kmeans_empty() {
        let points: Vec<Point<f64>> = vec![];
        let labels = points.kmeans(2).unwrap();
        assert_eq!(labels.len(), 0);
    }

    #[test]
    fn test_kmeans_single_point() {
        let points = [point!(x: 0.0, y: 0.0)];
        let labels = points.kmeans(1).unwrap();
        assert_eq!(labels, vec![0]);
    }

    #[test]
    fn test_kmeans_k_zero() {
        let points = [point!(x: 0.0, y: 0.0), point!(x: 1.0, y: 1.0)];
        let result = points.kmeans(0);
        assert!(matches!(result, Err(KMeansError::InvalidK { k: 0, n: 2 })));
    }

    #[test]
    fn test_kmeans_k_too_large() {
        let points = [point!(x: 0.0, y: 0.0), point!(x: 1.0, y: 1.0)];
        let result = points.kmeans(10);
        assert!(matches!(result, Err(KMeansError::InvalidK { k: 10, n: 2 })));
    }

    #[test]
    fn test_kmeans_k_equals_n() {
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 1.0),
            point!(x: 2.0, y: 2.0),
        ];
        let labels = points.kmeans(3).unwrap();
        // Each point should be in its own cluster
        assert_eq!(labels, vec![0, 1, 2]);
    }

    #[test]
    fn test_kmeans_single_cluster() {
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            point!(x: 1.0, y: 1.0),
        ];
        let labels = points.kmeans(1).unwrap();

        // All points should be in cluster 0
        assert!(labels.iter().all(|&label| label == 0));
    }

    #[test]
    fn test_kmeans_two_clusters() {
        let points = [
            // Cluster 1
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            // Cluster 2
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            point!(x: 10.0, y: 11.0),
        ];
        let labels = points.kmeans(2).unwrap();

        // First three points should be in the same cluster
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[1], labels[2]);

        // Last three points should be in the same cluster
        assert_eq!(labels[3], labels[4]);
        assert_eq!(labels[4], labels[5]);

        // The two groups should be in different clusters
        assert_ne!(labels[0], labels[3]);
    }

    #[test]
    fn test_kmeans_multipoint() {
        let points = MultiPoint::new(vec![
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            point!(x: 10.0, y: 11.0),
        ]);

        let labels = points.kmeans(2).unwrap();

        // Two distinct clusters
        let cluster_0_count = labels.iter().filter(|&&l| l == 0).count();
        let cluster_1_count = labels.iter().filter(|&&l| l == 1).count();
        assert_eq!(cluster_0_count + cluster_1_count, 6);
    }

    #[test]
    fn test_kmeans_three_clusters() {
        // Use a fixed seed for deterministic testing. K-means can converge to local minima
        // depending on initialisation, so we use a known-good seed to test algorithm correctness.
        let points = [
            // Cluster 1
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            // Cluster 2
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            // Cluster 3
            point!(x: 20.0, y: 20.0),
            point!(x: 21.0, y: 20.0),
        ];
        let params = KMeansParams::new(3).seed(42);
        let labels = points.kmeans_with_params(params).unwrap();

        // Each pair should be in the same cluster
        assert_eq!(labels[0], labels[1]);
        assert_eq!(labels[2], labels[3]);
        assert_eq!(labels[4], labels[5]);

        // All three pairs should be in different clusters
        assert_ne!(labels[0], labels[2]);
        assert_ne!(labels[2], labels[4]);
        assert_ne!(labels[0], labels[4]);
    }

    #[test]
    fn test_kmeans_identical_points() {
        // Test with duplicate points at the same location
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 0.0, y: 0.0),
            point!(x: 0.0, y: 0.0),
        ];
        let labels = points.kmeans(1).unwrap();

        // All points should be in the same cluster
        assert!(labels.iter().all(|&label| label == 0));
    }

    #[test]
    fn test_kmeans_linear_cluster() {
        // Test a linear arrangement of points
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 2.0, y: 0.0),
            point!(x: 3.0, y: 0.0),
            point!(x: 4.0, y: 0.0),
        ];
        let labels = points.kmeans(2).unwrap();

        // Two distinct clusters should exist
        let mut unique_clusters: Vec<_> = labels.to_vec();
        unique_clusters.sort_unstable();
        unique_clusters.dedup();
        assert_eq!(unique_clusters.len(), 2);
    }

    #[test]
    fn test_kmeans_degenerate_all_same_location() {
        // Test with all points at the same location and k > 1
        // This should fail during initialisation because all distances will be zero
        let points = [
            point!(x: 5.0, y: 5.0),
            point!(x: 5.0, y: 5.0),
            point!(x: 5.0, y: 5.0),
            point!(x: 5.0, y: 5.0),
        ];
        let result = points.kmeans(2);

        // Should return InitializationFailed error because all distances are zero
        assert!(matches!(
            result,
            Err(KMeansError::InitializationFailed(
                KMeansInitError::DegenerateData
            ))
        ));
    }

    #[test]
    fn test_kmeans_nan_coordinates() {
        // Test with NaN coordinates
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: f64::NAN, y: 1.0),
            point!(x: 2.0, y: 2.0),
        ];
        let result = points.kmeans(2);

        // Should return InitializationFailed error due to NaN
        assert!(matches!(
            result,
            Err(KMeansError::InitializationFailed(
                KMeansInitError::NaNCoordinate
            ))
        ));
    }

    #[test]
    fn test_kmeans_infinite_coordinates() {
        // Test with infinite coordinates
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: f64::INFINITY, y: 1.0),
            point!(x: 2.0, y: 2.0),
        ];
        let result = points.kmeans(2);

        // Should return InitializationFailed error due to infinity
        assert!(matches!(
            result,
            Err(KMeansError::InitializationFailed(
                KMeansInitError::InfiniteCoordinate
            ))
        ));
    }

    #[test]
    fn test_kmeans_reproducibility_with_seed() {
        // Test that using the same seed produces identical results
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            point!(x: 10.0, y: 11.0),
        ];

        let params = KMeansParams::new(2).seed(42);

        let result1 = points.kmeans_with_params(params.clone()).unwrap();
        let result2 = points.kmeans_with_params(params).unwrap();

        // Same seed should produce identical results
        assert_eq!(result1, result2);
    }

    #[test]
    fn test_kmeans_reproducibility_with_seed_and_max_radius() {
        // Test that seed produces reproducible results even when max_radius triggers splitting
        // Create a cluster that will exceed max_radius
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 100.0, y: 0.0),
            point!(x: 0.0, y: 100.0),
            point!(x: 100.0, y: 100.0),
            point!(x: 50.0, y: 50.0),
        ];

        let params = KMeansParams::new(1).seed(42).max_radius(40.0); // Will trigger splitting since centroid is at (50, 50) and max point distance is ~70

        let result1 = points.kmeans_with_params(params.clone()).unwrap();
        let result2 = points.kmeans_with_params(params).unwrap();

        // Same seed should produce identical results even with max_radius splitting
        assert_eq!(result1, result2);
    }

    #[test]
    fn test_kmeans_builder_pattern() {
        // Test the builder pattern with all options
        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            point!(x: 10.0, y: 11.0),
        ];

        // Use builder pattern to configure all parameters
        let params = KMeansParams::new(2).seed(42).max_iter(100).tolerance(0.001);

        let labels = points.kmeans_with_params(params).unwrap();

        // Should successfully cluster into 2 groups
        let unique_labels: std::collections::HashSet<_> = labels.iter().copied().collect();
        assert_eq!(unique_labels.len(), 2);
    }

    #[test]
    fn test_kmeans_converges_with_sufficient_iterations() {
        // Test that with sufficient iterations, we get Ok (no MaxIterationsReached)

        // Note: Testing MaxIterationsReached is challenging because k-means++ initialization
        // combined with Hamerly's algorithm converges very efficiently, often in 1-2 iterations
        // even with complex data. The MaxIterationsReached error path is exercised when max_iter
        // is set too low for the data, but creating a deterministic test case that reliably
        // triggers this without being flaky is difficult.

        let points = [
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.0, y: 1.0),
            point!(x: 10.0, y: 10.0),
            point!(x: 11.0, y: 10.0),
            point!(x: 10.0, y: 11.0),
        ];

        // Use high max_iter which should be enough to converge
        let params = KMeansParams::new(2).seed(42).max_iter(100);

        let result = points.kmeans_with_params(params);

        // Should converge successfully
        assert!(result.is_ok());
    }

    #[test]
    fn test_kmeans_max_radius() {
        use std::collections::HashSet;

        // Create one tight cluster and one elongated cluster
        let points = [
            // Tight cluster (radius approximately 0.7)
            point!(x: 0.0, y: 0.0),
            point!(x: 1.0, y: 0.0),
            point!(x: 0.5, y: 0.5),
            point!(x: 0.0, y: 1.0),
            point!(x: 1.0, y: 1.0),
            // Elongated cluster spread along x-axis (radius approximately 30)
            point!(x: 20.0, y: 0.0),
            point!(x: 30.0, y: 0.0),
            point!(x: 40.0, y: 0.0),
            point!(x: 50.0, y: 0.0),
            point!(x: 60.0, y: 0.0),
            point!(x: 70.0, y: 0.0),
            point!(x: 80.0, y: 0.0),
        ];

        // Ask for k=2 clusters but set max_radius to force splitting of elongated cluster
        let params = KMeansParams::new(2).seed(42).max_radius(15.0);

        let labels = points.kmeans_with_params(params).unwrap();

        // Should produce more than k clusters due to max_radius constraint
        let unique_labels: HashSet<_> = labels.iter().copied().collect();
        assert!(
            unique_labels.len() > 2,
            "Expected more than 2 clusters due to max_radius splitting, got {}",
            unique_labels.len()
        );

        // Verify no cluster exceeds max_radius
        for &cluster_id in &unique_labels {
            let cluster_points: Vec<Point<f64>> = points
                .iter()
                .zip(labels.iter())
                .filter(|&(_, &label)| label == cluster_id)
                .map(|(&p, _)| p)
                .collect();

            if cluster_points.is_empty() {
                continue;
            }

            let multipoint = MultiPoint::new(cluster_points);
            let centroid = multipoint.centroid().unwrap();

            let max_dist = multipoint
                .iter()
                .map(|p| Euclidean.distance(&centroid, p))
                .fold(0.0, f64::max);

            // Allow small numerical tolerance (epsilon) for floating point comparisons
            // The splitting algorithm may not achieve exact max_radius due to the way
            // k-means with k=2 splits clusters
            let epsilon = 2.0;
            assert!(
                max_dist <= 15.0 + epsilon,
                "Cluster {} has radius {:.2} significantly exceeding max_radius 15.0",
                cluster_id,
                max_dist
            );
        }

        // Verify tight cluster (first 5 points) stayed together
        let tight_cluster_labels: HashSet<_> = labels[0..5].iter().copied().collect();
        assert_eq!(
            tight_cluster_labels.len(),
            1,
            "Tight cluster should remain as one cluster, but has {} different labels",
            tight_cluster_labels.len()
        );

        // Verify elongated cluster (last 7 points) was split into multiple clusters
        let elongated_cluster_labels: HashSet<_> = labels[5..].iter().copied().collect();
        assert!(
            elongated_cluster_labels.len() >= 2,
            "Elongated cluster should be split into at least 2 clusters, got {}",
            elongated_cluster_labels.len()
        );
    }
}