thales 0.4.2

A comprehensive Computer Algebra System (CAS) library for symbolic mathematics, equation solving, calculus, and linear algebra
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
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
//! Coordinate system transformations.
//!
//! Provides transformations between different coordinate systems
//! (Cartesian, Polar, Spherical, Cylindrical) and complex number operations.

use nalgebra::{Matrix3, Vector2, Vector3};
use num_complex::Complex64;
use std::f64::consts::PI;

/// 2D Cartesian coordinates (x, y).
///
/// Represents a point in the 2D Cartesian plane with x and y coordinates.
/// Provides conversions to polar coordinates, complex numbers, and nalgebra vectors.
///
/// # Mathematical Representation
///
/// A point P in 2D Cartesian coordinates is represented as:
/// ```text
/// P = (x, y)
/// ```
///
/// # Examples
///
/// ```
/// use thales::transforms::Cartesian2D;
///
/// // Create a point at (3, 4)
/// let point = Cartesian2D::new(3.0, 4.0);
/// assert_eq!(point.x, 3.0);
/// assert_eq!(point.y, 4.0);
///
/// // Calculate distance from origin
/// let magnitude = point.magnitude();
/// assert!((magnitude - 5.0).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cartesian2D {
    /// x-coordinate (horizontal axis)
    pub x: f64,
    /// y-coordinate (vertical axis)
    pub y: f64,
}

impl Cartesian2D {
    /// Create new 2D Cartesian coordinates.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian2D;
    ///
    /// let point = Cartesian2D::new(3.0, 4.0);
    /// assert_eq!(point.x, 3.0);
    /// assert_eq!(point.y, 4.0);
    /// ```
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }

    /// Convert to polar coordinates.
    ///
    /// Converts Cartesian coordinates (x, y) to polar coordinates (r, θ) using:
    /// ```text
    /// r = √(x² + y²)
    /// θ = atan2(y, x)
    /// ```
    ///
    /// The angle θ is in radians, measured counterclockwise from the positive x-axis,
    /// and ranges from -π to π.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian2D;
    /// use std::f64::consts::PI;
    ///
    /// // Point at (1, 1) should be at 45 degrees (π/4 radians)
    /// let point = Cartesian2D::new(1.0, 1.0);
    /// let polar = point.to_polar();
    /// assert!((polar.r - std::f64::consts::SQRT_2).abs() < 1e-10);
    /// assert!((polar.theta - PI / 4.0).abs() < 1e-10);
    ///
    /// // Point on negative x-axis
    /// let point = Cartesian2D::new(-2.0, 0.0);
    /// let polar = point.to_polar();
    /// assert!((polar.r - 2.0).abs() < 1e-10);
    /// assert!((polar.theta - PI).abs() < 1e-10);
    /// ```
    pub fn to_polar(&self) -> Polar {
        let r = (self.x * self.x + self.y * self.y).sqrt();
        let theta = self.y.atan2(self.x);
        Polar::new(r, theta)
    }

    /// Convert to complex number.
    ///
    /// Represents the Cartesian point (x, y) as the complex number x + yi.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian2D;
    ///
    /// let point = Cartesian2D::new(3.0, 4.0);
    /// let complex = point.to_complex();
    /// assert_eq!(complex.re, 3.0);
    /// assert_eq!(complex.im, 4.0);
    /// ```
    pub fn to_complex(&self) -> Complex64 {
        Complex64::new(self.x, self.y)
    }

    /// Convert to nalgebra vector.
    ///
    /// Returns a 2D column vector `[x, y]ᵀ` for use with nalgebra linear algebra operations.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian2D;
    ///
    /// let point = Cartesian2D::new(3.0, 4.0);
    /// let vec = point.to_vector();
    /// assert_eq!(vec[0], 3.0);
    /// assert_eq!(vec[1], 4.0);
    /// ```
    pub fn to_vector(&self) -> Vector2<f64> {
        Vector2::new(self.x, self.y)
    }

    /// Distance from origin.
    ///
    /// Calculates the Euclidean distance from the origin (0, 0) to the point (x, y):
    /// ```text
    /// |P| = √(x² + y²)
    /// ```
    ///
    /// This is equivalent to the radius r in polar coordinates.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian2D;
    ///
    /// // 3-4-5 right triangle
    /// let point = Cartesian2D::new(3.0, 4.0);
    /// assert!((point.magnitude() - 5.0).abs() < 1e-10);
    ///
    /// // Point at origin
    /// let origin = Cartesian2D::new(0.0, 0.0);
    /// assert_eq!(origin.magnitude(), 0.0);
    /// ```
    pub fn magnitude(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
    }
}

/// Polar coordinates (r, θ).
///
/// Represents a point in the 2D plane using polar coordinates with radius r
/// and angle θ (theta) measured counterclockwise from the positive x-axis.
///
/// # Mathematical Representation
///
/// A point P in polar coordinates is represented as:
/// ```text
/// P = (r, θ)
/// where:
///   r ≥ 0 is the distance from the origin
///   θ is the angle in radians from the positive x-axis
/// ```
///
/// # Conversion Formulas
///
/// From Cartesian (x, y) to Polar (r, θ):
/// ```text
/// r = √(x² + y²)
/// θ = atan2(y, x)
/// ```
///
/// From Polar (r, θ) to Cartesian (x, y):
/// ```text
/// x = r cos(θ)
/// y = r sin(θ)
/// ```
///
/// # Examples
///
/// ```
/// use thales::transforms::{Polar, Cartesian2D};
/// use std::f64::consts::PI;
///
/// // Point at radius 5, angle 45 degrees (π/4 radians)
/// let polar = Polar::new(5.0, PI / 4.0);
/// assert_eq!(polar.r, 5.0);
/// assert!((polar.theta - PI / 4.0).abs() < 1e-10);
///
/// // Convert to Cartesian
/// let cartesian = polar.to_cartesian();
/// assert!((cartesian.x - 5.0 * (PI / 4.0).cos()).abs() < 1e-10);
/// assert!((cartesian.y - 5.0 * (PI / 4.0).sin()).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Polar {
    /// Radius (distance from origin)
    pub r: f64,
    /// Angle in radians (counterclockwise from positive x-axis)
    pub theta: f64,
}

impl Polar {
    /// Create new polar coordinates.
    ///
    /// # Arguments
    ///
    /// * `r` - Radius (distance from origin), typically r ≥ 0
    /// * `theta` - Angle in radians, measured counterclockwise from positive x-axis
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Polar;
    /// use std::f64::consts::PI;
    ///
    /// // Point at distance 3 from origin, 60 degrees (π/3 radians)
    /// let polar = Polar::new(3.0, PI / 3.0);
    /// assert_eq!(polar.r, 3.0);
    /// assert!((polar.theta - PI / 3.0).abs() < 1e-10);
    /// ```
    pub fn new(r: f64, theta: f64) -> Self {
        Self { r, theta }
    }

    /// Normalize angle to [0, 2π).
    ///
    /// Adjusts the angle θ to be in the standard range [0, 2π) radians.
    /// This is useful for comparing angles and for canonical representation.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Polar;
    /// use std::f64::consts::PI;
    ///
    /// // Angle greater than 2π
    /// let mut polar = Polar::new(5.0, 3.0 * PI);
    /// polar.normalize_angle();
    /// assert!((polar.theta - PI).abs() < 1e-10);
    ///
    /// // Negative angle
    /// let mut polar = Polar::new(5.0, -PI / 2.0);
    /// polar.normalize_angle();
    /// assert!((polar.theta - 3.0 * PI / 2.0).abs() < 1e-10);
    /// ```
    pub fn normalize_angle(&mut self) {
        self.theta = self.theta.rem_euclid(2.0 * PI);
    }

    /// Convert to Cartesian coordinates.
    ///
    /// Converts polar coordinates (r, θ) to Cartesian coordinates (x, y) using:
    /// ```text
    /// x = r cos(θ)
    /// y = r sin(θ)
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Polar;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 2, angle 0 (positive x-axis)
    /// let polar = Polar::new(2.0, 0.0);
    /// let cartesian = polar.to_cartesian();
    /// assert!((cartesian.x - 2.0).abs() < 1e-10);
    /// assert!((cartesian.y - 0.0).abs() < 1e-10);
    ///
    /// // Point at radius 1, angle π/2 (positive y-axis)
    /// let polar = Polar::new(1.0, PI / 2.0);
    /// let cartesian = polar.to_cartesian();
    /// assert!((cartesian.x - 0.0).abs() < 1e-10);
    /// assert!((cartesian.y - 1.0).abs() < 1e-10);
    ///
    /// // Round-trip conversion
    /// let original = Polar::new(5.0, PI / 4.0);
    /// let cartesian = original.to_cartesian();
    /// let polar = cartesian.to_polar();
    /// assert!((polar.r - original.r).abs() < 1e-10);
    /// assert!((polar.theta - original.theta).abs() < 1e-10);
    /// ```
    pub fn to_cartesian(&self) -> Cartesian2D {
        let x = self.r * self.theta.cos();
        let y = self.r * self.theta.sin();
        Cartesian2D::new(x, y)
    }

    /// Convert to complex number (polar form).
    ///
    /// Represents the polar coordinates as a complex number in polar form:
    /// ```text
    /// z = r e^(iθ) = r(cos(θ) + i sin(θ))
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Polar;
    /// use std::f64::consts::PI;
    ///
    /// let polar = Polar::new(5.0, PI / 6.0);
    /// let complex = polar.to_complex();
    /// assert!((complex.norm() - 5.0).abs() < 1e-10);
    /// assert!((complex.arg() - PI / 6.0).abs() < 1e-10);
    /// ```
    pub fn to_complex(&self) -> Complex64 {
        Complex64::from_polar(self.r, self.theta)
    }
}

/// 3D Cartesian coordinates (x, y, z).
///
/// Represents a point in 3D space using Cartesian coordinates with x, y, and z axes.
/// Provides conversions to spherical and cylindrical coordinate systems, and integration
/// with nalgebra's Vector3.
///
/// # Mathematical Representation
///
/// A point P in 3D Cartesian coordinates is represented as:
/// ```text
/// P = (x, y, z)
/// ```
///
/// # Coordinate System Diagram
///
/// ```text
///        z
//////        |
///        |    P(x,y,z)
///        |   /
///        |  /
///        | /
///        |/________→ y
///       /
///      /
///     /
//////    x
/// ```
///
/// # Examples
///
/// ```
/// use thales::transforms::Cartesian3D;
///
/// // Create a point at (3, 4, 5)
/// let point = Cartesian3D::new(3.0, 4.0, 5.0);
/// assert_eq!(point.x, 3.0);
/// assert_eq!(point.y, 4.0);
/// assert_eq!(point.z, 5.0);
///
/// // Calculate distance from origin
/// let magnitude = point.magnitude();
/// let expected = (3.0*3.0 + 4.0*4.0 + 5.0*5.0_f64).sqrt();
/// assert!((magnitude - expected).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cartesian3D {
    /// x-coordinate (horizontal axis)
    pub x: f64,
    /// y-coordinate (horizontal axis perpendicular to x)
    pub y: f64,
    /// z-coordinate (vertical axis)
    pub z: f64,
}

impl Cartesian3D {
    /// Create new 3D Cartesian coordinates.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian3D;
    ///
    /// let point = Cartesian3D::new(1.0, 2.0, 3.0);
    /// assert_eq!(point.x, 1.0);
    /// assert_eq!(point.y, 2.0);
    /// assert_eq!(point.z, 3.0);
    /// ```
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    /// Convert to spherical coordinates.
    ///
    /// Converts Cartesian coordinates (x, y, z) to spherical coordinates (r, θ, φ) using:
    /// ```text
    /// r = √(x² + y² + z²)
    /// θ = atan2(y, x)           [azimuthal angle in xy-plane]
    /// φ = acos(z / r)           [polar angle from z-axis]
    /// ```
    ///
    /// Uses the **physics convention** where:
    /// - r ≥ 0: radius (distance from origin)
    /// - θ ∈ [0, 2π): azimuthal angle in xy-plane from x-axis
    /// - φ ∈ [0, π]: polar angle from positive z-axis
    ///
    /// # Coordinate System Diagram
    ///
    /// ```text
    ///        z
    ///    ///        |    P
    ///        |   /|
    ///        |  / |
    ///        | /  | r·cos(φ)
    ///      φ |/   |
    ///        O----●----------→ y
    ///       /     |
    ///      /    r·sin(φ)
    ///     /    θ
    ///    ///    x
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian3D;
    /// use std::f64::consts::PI;
    ///
    /// // Point on positive z-axis
    /// let point = Cartesian3D::new(0.0, 0.0, 5.0);
    /// let spherical = point.to_spherical();
    /// assert!((spherical.r - 5.0).abs() < 1e-10);
    /// assert!((spherical.phi - 0.0).abs() < 1e-10);  // φ = 0 on z-axis
    ///
    /// // Point in xy-plane at 45 degrees
    /// let point = Cartesian3D::new(1.0, 1.0, 0.0);
    /// let spherical = point.to_spherical();
    /// assert!((spherical.r - std::f64::consts::SQRT_2).abs() < 1e-10);
    /// assert!((spherical.theta - PI / 4.0).abs() < 1e-10);
    /// assert!((spherical.phi - PI / 2.0).abs() < 1e-10);  // φ = π/2 in xy-plane
    /// ```
    pub fn to_spherical(&self) -> Spherical {
        let r = (self.x * self.x + self.y * self.y + self.z * self.z).sqrt();
        let theta = (self.y.atan2(self.x)).rem_euclid(2.0 * PI); // azimuthal angle
        let phi = if r == 0.0 {
            0.0
        } else {
            (self.z / r).acos() // polar angle from z-axis
        };
        Spherical::new(r, theta, phi)
    }

    /// Convert to cylindrical coordinates.
    ///
    /// Converts Cartesian coordinates (x, y, z) to cylindrical coordinates (ρ, φ, z) using:
    /// ```text
    /// ρ = √(x² + y²)
    /// φ = atan2(y, x)
    /// z = z
    /// ```
    ///
    /// # Coordinate System Diagram
    ///
    /// ```text
    ///        z
    ///    ///        |     P(x,y,z)
    ///        |    /|
    ///        |   / |
    ///        |  /  | z
    ///        | /   |
    ///        |/____●----------→ y
    ///       /      |
    ///      /     ρ (radial distance from z-axis)
    ///     /    φ
    ///    ///    x
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian3D;
    /// use std::f64::consts::PI;
    ///
    /// // Point at (3, 4, 5)
    /// let point = Cartesian3D::new(3.0, 4.0, 5.0);
    /// let cylindrical = point.to_cylindrical();
    /// assert!((cylindrical.rho - 5.0).abs() < 1e-10);  // √(3² + 4²) = 5
    /// assert!((cylindrical.z - 5.0).abs() < 1e-10);
    ///
    /// // Point on positive y-axis
    /// let point = Cartesian3D::new(0.0, 2.0, 3.0);
    /// let cylindrical = point.to_cylindrical();
    /// assert!((cylindrical.rho - 2.0).abs() < 1e-10);
    /// assert!((cylindrical.phi - PI / 2.0).abs() < 1e-10);
    /// assert!((cylindrical.z - 3.0).abs() < 1e-10);
    /// ```
    pub fn to_cylindrical(&self) -> Cylindrical {
        let rho = (self.x * self.x + self.y * self.y).sqrt();
        let phi = self.y.atan2(self.x);
        Cylindrical::new(rho, phi, self.z)
    }

    /// Convert to nalgebra vector.
    ///
    /// Returns a 3D column vector `[x, y, z]ᵀ` for use with nalgebra linear algebra operations.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian3D;
    ///
    /// let point = Cartesian3D::new(1.0, 2.0, 3.0);
    /// let vec = point.to_vector();
    /// assert_eq!(vec[0], 1.0);
    /// assert_eq!(vec[1], 2.0);
    /// assert_eq!(vec[2], 3.0);
    /// ```
    pub fn to_vector(&self) -> Vector3<f64> {
        Vector3::new(self.x, self.y, self.z)
    }

    /// Distance from origin.
    ///
    /// Calculates the Euclidean distance from the origin (0, 0, 0) to the point (x, y, z):
    /// ```text
    /// |P| = √(x² + y² + z²)
    /// ```
    ///
    /// This is equivalent to the radius r in spherical coordinates.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cartesian3D;
    ///
    /// // 3-4-5 right triangle in 3D space
    /// let point = Cartesian3D::new(0.0, 3.0, 4.0);
    /// assert!((point.magnitude() - 5.0).abs() < 1e-10);
    ///
    /// // Point at origin
    /// let origin = Cartesian3D::new(0.0, 0.0, 0.0);
    /// assert_eq!(origin.magnitude(), 0.0);
    ///
    /// // 3D Pythagorean quintuple (1, 2, 2) → √9 = 3
    /// let point = Cartesian3D::new(1.0, 2.0, 2.0);
    /// assert!((point.magnitude() - 3.0).abs() < 1e-10);
    /// ```
    pub fn magnitude(&self) -> f64 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }
}

/// Spherical coordinates (r, θ, φ).
///
/// Represents a point in 3D space using spherical coordinates with radius r,
/// azimuthal angle θ, and polar angle φ.
///
/// # Physics Convention
///
/// This implementation uses the **physics convention** (ISO 31-11), NOT the mathematics convention.
///
/// **Physics Convention** (used here):
/// - r ≥ 0: radial distance from origin
/// - θ ∈ [0, 2π): azimuthal angle in xy-plane from positive x-axis
/// - φ ∈ [0, π]: polar angle (inclination) from positive z-axis
///
/// **Mathematics Convention** (NOT used):
/// - r ≥ 0: radial distance
/// - θ ∈ [0, π]: polar angle from positive z-axis (equivalent to our φ)
/// - φ ∈ [0, 2π): azimuthal angle (equivalent to our θ)
///
/// # Coordinate System Diagram
///
/// ```text
///        z
//////        |    P
///        |   /|
///        |  / |
///        | /  |
///      φ |/)r |
///        O----●-------→ y
///       /  θ  |
///      /      ρ (projection onto xy-plane)
///     /
//////    x
///
/// where:
///   r = radius (distance OP)
///   θ = azimuthal angle (counterclockwise from x-axis in xy-plane)
///   φ = polar angle (angle from positive z-axis)
///   ρ = r·sin(φ) (projection of r onto xy-plane)
/// ```
///
/// # Conversion Formulas
///
/// From Cartesian (x, y, z) to Spherical (r, θ, φ):
/// ```text
/// r = √(x² + y² + z²)
/// θ = atan2(y, x)
/// φ = acos(z / r)
/// ```
///
/// From Spherical (r, θ, φ) to Cartesian (x, y, z):
/// ```text
/// x = r sin(φ) cos(θ)
/// y = r sin(φ) sin(θ)
/// z = r cos(φ)
/// ```
///
/// # Examples
///
/// ```
/// use thales::transforms::{Spherical, Cartesian3D};
/// use std::f64::consts::PI;
///
/// // Point on positive z-axis at distance 5
/// let spherical = Spherical::new(5.0, 0.0, 0.0);  // φ = 0 points along +z
/// let cartesian = spherical.to_cartesian();
/// assert!((cartesian.x - 0.0).abs() < 1e-10);
/// assert!((cartesian.y - 0.0).abs() < 1e-10);
/// assert!((cartesian.z - 5.0).abs() < 1e-10);
///
/// // Point in xy-plane at 45 degrees from x-axis
/// let spherical = Spherical::new(2.0, PI / 4.0, PI / 2.0);  // φ = π/2 is xy-plane
/// let cartesian = spherical.to_cartesian();
/// assert!((cartesian.x - std::f64::consts::SQRT_2).abs() < 1e-10);
/// assert!((cartesian.y - std::f64::consts::SQRT_2).abs() < 1e-10);
/// assert!((cartesian.z - 0.0).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Spherical {
    /// Radius (distance from origin), r ≥ 0
    pub r: f64,
    /// Azimuthal angle θ in radians (angle in xy-plane from x-axis), θ ∈ [0, 2π)
    pub theta: f64,
    /// Polar angle φ in radians (angle from positive z-axis), φ ∈ [0, π]
    pub phi: f64,
}

impl Spherical {
    /// Create new spherical coordinates.
    ///
    /// # Arguments
    ///
    /// * `r` - Radius (distance from origin), typically r ≥ 0
    /// * `theta` - Azimuthal angle in radians (angle in xy-plane from x-axis)
    /// * `phi` - Polar angle in radians (angle from positive z-axis)
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Spherical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 5, 30° azimuthal, 45° polar
    /// let spherical = Spherical::new(5.0, PI / 6.0, PI / 4.0);
    /// assert_eq!(spherical.r, 5.0);
    /// assert!((spherical.theta - PI / 6.0).abs() < 1e-10);
    /// assert!((spherical.phi - PI / 4.0).abs() < 1e-10);
    /// ```
    pub fn new(r: f64, theta: f64, phi: f64) -> Self {
        Self { r, theta, phi }
    }

    /// Convert to Cartesian coordinates.
    ///
    /// Converts spherical coordinates (r, θ, φ) to Cartesian coordinates (x, y, z) using:
    /// ```text
    /// x = r sin(φ) cos(θ)
    /// y = r sin(φ) sin(θ)
    /// z = r cos(φ)
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Spherical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 1 on positive x-axis
    /// let spherical = Spherical::new(1.0, 0.0, PI / 2.0);
    /// let cartesian = spherical.to_cartesian();
    /// assert!((cartesian.x - 1.0).abs() < 1e-10);
    /// assert!((cartesian.y - 0.0).abs() < 1e-10);
    /// assert!((cartesian.z - 0.0).abs() < 1e-10);
    ///
    /// // Point at radius 1 on positive z-axis
    /// let spherical = Spherical::new(1.0, 0.0, 0.0);
    /// let cartesian = spherical.to_cartesian();
    /// assert!((cartesian.x - 0.0).abs() < 1e-10);
    /// assert!((cartesian.y - 0.0).abs() < 1e-10);
    /// assert!((cartesian.z - 1.0).abs() < 1e-10);
    ///
    /// // Round-trip conversion
    /// let original = Spherical::new(3.0, PI / 3.0, PI / 6.0);
    /// let cartesian = original.to_cartesian();
    /// let spherical = cartesian.to_spherical();
    /// assert!((spherical.r - original.r).abs() < 1e-10);
    /// assert!((spherical.theta - original.theta).abs() < 1e-10);
    /// assert!((spherical.phi - original.phi).abs() < 1e-10);
    /// ```
    pub fn to_cartesian(&self) -> Cartesian3D {
        let x = self.r * self.phi.sin() * self.theta.cos();
        let y = self.r * self.phi.sin() * self.theta.sin();
        let z = self.r * self.phi.cos();
        Cartesian3D::new(x, y, z)
    }

    /// Convert to cylindrical coordinates.
    ///
    /// Converts spherical coordinates (r, θ, φ) to cylindrical coordinates (ρ, φ_cyl, z) using:
    /// ```text
    /// ρ = r sin(φ)
    /// φ_cyl = θ
    /// z = r cos(φ)
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Spherical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 2, θ=30°, φ=60°
    /// let spherical = Spherical::new(2.0, PI / 6.0, PI / 3.0);
    /// let cylindrical = spherical.to_cylindrical();
    /// assert!((cylindrical.rho - 2.0 * (PI / 3.0).sin()).abs() < 1e-10);
    /// assert!((cylindrical.phi - PI / 6.0).abs() < 1e-10);
    /// assert!((cylindrical.z - 2.0 * (PI / 3.0).cos()).abs() < 1e-10);
    ///
    /// // Point in xy-plane (φ = π/2)
    /// let spherical = Spherical::new(5.0, PI / 4.0, PI / 2.0);
    /// let cylindrical = spherical.to_cylindrical();
    /// assert!((cylindrical.rho - 5.0).abs() < 1e-10);
    /// assert!((cylindrical.z - 0.0).abs() < 1e-10);
    /// ```
    pub fn to_cylindrical(&self) -> Cylindrical {
        let rho = self.r * self.phi.sin();
        Cylindrical::new(rho, self.theta, self.r * self.phi.cos())
    }
}

/// Cylindrical coordinates (ρ, φ, z).
///
/// Represents a point in 3D space using cylindrical coordinates with radial distance ρ
/// from the z-axis, azimuthal angle φ, and height z along the z-axis.
///
/// # Mathematical Representation
///
/// A point P in cylindrical coordinates is represented as:
/// ```text
/// P = (ρ, φ, z)
/// where:
///   ρ ≥ 0 is the radial distance from the z-axis (radius in xy-plane)
///   φ is the azimuthal angle in radians from the positive x-axis
///   z is the height along the z-axis
/// ```
///
/// # Coordinate System Diagram
///
/// ```text
///        z
//////        |     P(ρ,φ,z)
///        |    /|
///        |   / |
///        |  /  | z (height)
///        | /   |
///        |/____|_____→ y
///       /      ●
///      /       |
///     /        ρ (radial distance from z-axis)
///    ↓       φ
///    x
///
/// Top view (looking down z-axis):
///
///      y
//////      |    P
///      |   /
///      |  /
///      | / ρ
///    φ |/)
///      |/________→ x
/// ```
///
/// # Conversion Formulas
///
/// From Cartesian (x, y, z) to Cylindrical (ρ, φ, z):
/// ```text
/// ρ = √(x² + y²)
/// φ = atan2(y, x)
/// z = z
/// ```
///
/// From Cylindrical (ρ, φ, z) to Cartesian (x, y, z):
/// ```text
/// x = ρ cos(φ)
/// y = ρ sin(φ)
/// z = z
/// ```
///
/// From Cylindrical (ρ, φ, z) to Spherical (r, θ, φ_sph):
/// ```text
/// r = √(ρ² + z²)
/// θ = φ
/// φ_sph = acos(z / r)
/// ```
///
/// # Examples
///
/// ```
/// use thales::transforms::{Cylindrical, Cartesian3D};
/// use std::f64::consts::PI;
///
/// // Point at radius 3 from z-axis, angle 60°, height 4
/// let cylindrical = Cylindrical::new(3.0, PI / 3.0, 4.0);
/// assert_eq!(cylindrical.rho, 3.0);
/// assert!((cylindrical.phi - PI / 3.0).abs() < 1e-10);
/// assert_eq!(cylindrical.z, 4.0);
///
/// // Convert to Cartesian
/// let cartesian = cylindrical.to_cartesian();
/// assert!((cartesian.x - 3.0 * (PI / 3.0).cos()).abs() < 1e-10);
/// assert!((cartesian.y - 3.0 * (PI / 3.0).sin()).abs() < 1e-10);
/// assert!((cartesian.z - 4.0).abs() < 1e-10);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cylindrical {
    /// Radial distance from z-axis (radius in xy-plane), ρ ≥ 0
    pub rho: f64,
    /// Azimuthal angle in radians (angle in xy-plane from x-axis)
    pub phi: f64,
    /// Height along z-axis
    pub z: f64,
}

impl Cylindrical {
    /// Create new cylindrical coordinates.
    ///
    /// # Arguments
    ///
    /// * `rho` - Radial distance from z-axis, typically ρ ≥ 0
    /// * `phi` - Azimuthal angle in radians (angle in xy-plane from x-axis)
    /// * `z` - Height along z-axis
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cylindrical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 2 from z-axis, 45°, height 3
    /// let cylindrical = Cylindrical::new(2.0, PI / 4.0, 3.0);
    /// assert_eq!(cylindrical.rho, 2.0);
    /// assert!((cylindrical.phi - PI / 4.0).abs() < 1e-10);
    /// assert_eq!(cylindrical.z, 3.0);
    /// ```
    pub fn new(rho: f64, phi: f64, z: f64) -> Self {
        Self { rho, phi, z }
    }

    /// Convert to Cartesian coordinates.
    ///
    /// Converts cylindrical coordinates (ρ, φ, z) to Cartesian coordinates (x, y, z) using:
    /// ```text
    /// x = ρ cos(φ)
    /// y = ρ sin(φ)
    /// z = z
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cylindrical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 5 on positive x-axis, height 2
    /// let cylindrical = Cylindrical::new(5.0, 0.0, 2.0);
    /// let cartesian = cylindrical.to_cartesian();
    /// assert!((cartesian.x - 5.0).abs() < 1e-10);
    /// assert!((cartesian.y - 0.0).abs() < 1e-10);
    /// assert!((cartesian.z - 2.0).abs() < 1e-10);
    ///
    /// // Point at radius 2 on positive y-axis, height 3
    /// let cylindrical = Cylindrical::new(2.0, PI / 2.0, 3.0);
    /// let cartesian = cylindrical.to_cartesian();
    /// assert!((cartesian.x - 0.0).abs() < 1e-10);
    /// assert!((cartesian.y - 2.0).abs() < 1e-10);
    /// assert!((cartesian.z - 3.0).abs() < 1e-10);
    ///
    /// // Round-trip conversion
    /// let original = Cylindrical::new(4.0, PI / 6.0, 5.0);
    /// let cartesian = original.to_cartesian();
    /// let cylindrical = cartesian.to_cylindrical();
    /// assert!((cylindrical.rho - original.rho).abs() < 1e-10);
    /// assert!((cylindrical.phi - original.phi).abs() < 1e-10);
    /// assert!((cylindrical.z - original.z).abs() < 1e-10);
    /// ```
    pub fn to_cartesian(&self) -> Cartesian3D {
        let x = self.rho * self.phi.cos();
        let y = self.rho * self.phi.sin();
        Cartesian3D::new(x, y, self.z)
    }

    /// Convert to spherical coordinates.
    ///
    /// Converts cylindrical coordinates (ρ, φ, z) to spherical coordinates (r, θ, φ_sph) using:
    /// ```text
    /// r = √(ρ² + z²)
    /// θ = φ
    /// φ_sph = acos(z / r)
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Cylindrical;
    /// use std::f64::consts::PI;
    ///
    /// // Point at radius 3, angle 30°, height 4
    /// let cylindrical = Cylindrical::new(3.0, PI / 6.0, 4.0);
    /// let spherical = cylindrical.to_spherical();
    /// assert!((spherical.r - 5.0).abs() < 1e-10);  // √(3² + 4²) = 5
    /// assert!((spherical.theta - PI / 6.0).abs() < 1e-10);
    ///
    /// // Point in xy-plane (z = 0)
    /// let cylindrical = Cylindrical::new(2.0, PI / 4.0, 0.0);
    /// let spherical = cylindrical.to_spherical();
    /// assert!((spherical.r - 2.0).abs() < 1e-10);
    /// assert!((spherical.phi - PI / 2.0).abs() < 1e-10);  // φ = π/2 for z=0
    /// ```
    pub fn to_spherical(&self) -> Spherical {
        let r = (self.rho * self.rho + self.z * self.z).sqrt();
        let phi = if r == 0.0 { 0.0 } else { (self.z / r).acos() };
        Spherical::new(r, self.phi, phi)
    }
}

/// Complex number operations and transformations.
///
/// Provides utilities for working with complex numbers including conversions between
/// Cartesian (x + yi) and polar (r∠θ) forms, as well as operations leveraging polar
/// representation such as De Moivre's theorem for computing powers and roots.
///
/// # Complex Number Representations
///
/// **Cartesian form**: z = x + yi
/// - Real part: x = Re(z)
/// - Imaginary part: y = Im(z)
/// - From num_complex::Complex64
///
/// **Polar form**: z = r∠θ = r e^(iθ)
/// - Magnitude: r = |z| = √(x² + y²)
/// - Argument: θ = arg(z) = atan2(y, x)
/// - Euler's formula: e^(iθ) = cos(θ) + i sin(θ)
///
/// # Integration with num_complex
///
/// This struct provides convenience methods that integrate with the `num_complex::Complex64`
/// type and the [`Polar`] coordinate struct, enabling seamless conversions and operations
/// across both representations.
///
/// # Applications
///
/// Complex number operations in polar form are essential for:
/// - **Signal processing**: Frequency domain analysis, Fourier transforms
/// - **Electrical engineering**: AC circuit analysis (impedance, phasors)
/// - **Control systems**: Transfer functions, stability analysis
/// - **Quantum mechanics**: Wave functions, probability amplitudes
/// - **Computer graphics**: Rotations and transformations in 2D
///
/// # Examples
///
/// ```
/// use thales::transforms::ComplexOps;
/// use num_complex::Complex64;
/// use std::f64::consts::PI;
///
/// // Convert complex number to polar form
/// let z = Complex64::new(3.0, 4.0);
/// let polar = ComplexOps::to_polar(z);
/// assert!((polar.r - 5.0).abs() < 1e-10);  // magnitude = √(3² + 4²) = 5
/// assert!((polar.theta - (4.0_f64).atan2(3.0)).abs() < 1e-10);
///
/// // Convert back from polar to Cartesian
/// let z_back = ComplexOps::from_polar(polar);
/// assert!((z_back.re - 3.0).abs() < 1e-10);
/// assert!((z_back.im - 4.0).abs() < 1e-10);
///
/// // Compute power using De Moivre's theorem
/// let z = Complex64::new(1.0, 1.0);  // 1 + i
/// let z_cubed = ComplexOps::de_moivre(z, 3.0);
/// // (1+i)³ = -2 + 2i
/// assert!((z_cubed.re - -2.0).abs() < 1e-10);
/// assert!((z_cubed.im - 2.0).abs() < 1e-10);
/// ```
pub struct ComplexOps;

impl ComplexOps {
    /// Convert complex number to polar form (r, θ).
    ///
    /// Converts a complex number from Cartesian form (x + yi) to polar form (r∠θ).
    ///
    /// # Conversion Formulas
    ///
    /// ```text
    /// r = |z| = √(x² + y²)    (magnitude)
    /// θ = arg(z) = atan2(y, x) (argument)
    /// ```
    ///
    /// The argument θ is in radians, measured counterclockwise from the positive real axis,
    /// and ranges from -π to π.
    ///
    /// # Arguments
    ///
    /// * `c` - Complex number in Cartesian form (num_complex::Complex64)
    ///
    /// # Returns
    ///
    /// [`Polar`] coordinates (r, θ) representing the same complex number
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::ComplexOps;
    /// use num_complex::Complex64;
    /// use std::f64::consts::PI;
    ///
    /// // Convert 1 + i to polar form
    /// let z = Complex64::new(1.0, 1.0);
    /// let polar = ComplexOps::to_polar(z);
    /// assert!((polar.r - std::f64::consts::SQRT_2).abs() < 1e-10);
    /// assert!((polar.theta - PI / 4.0).abs() < 1e-10);  // 45 degrees
    ///
    /// // Pure real number
    /// let z = Complex64::new(5.0, 0.0);
    /// let polar = ComplexOps::to_polar(z);
    /// assert!((polar.r - 5.0).abs() < 1e-10);
    /// assert!((polar.theta - 0.0).abs() < 1e-10);
    ///
    /// // Pure imaginary number
    /// let z = Complex64::new(0.0, 3.0);
    /// let polar = ComplexOps::to_polar(z);
    /// assert!((polar.r - 3.0).abs() < 1e-10);
    /// assert!((polar.theta - PI / 2.0).abs() < 1e-10);  // 90 degrees
    ///
    /// // Negative real number (angle = π)
    /// let z = Complex64::new(-2.0, 0.0);
    /// let polar = ComplexOps::to_polar(z);
    /// assert!((polar.r - 2.0).abs() < 1e-10);
    /// assert!((polar.theta - PI).abs() < 1e-10);  // 180 degrees
    /// ```
    pub fn to_polar(c: Complex64) -> Polar {
        Polar::new(c.norm(), c.arg())
    }

    /// Convert polar form to complex number.
    ///
    /// Converts polar coordinates (r∠θ) to Cartesian form (x + yi).
    ///
    /// # Conversion Formulas
    ///
    /// Using Euler's formula:
    /// ```text
    /// z = r e^(iθ) = r(cos(θ) + i sin(θ))
    /// x = r cos(θ)  (real part)
    /// y = r sin(θ)  (imaginary part)
    /// ```
    ///
    /// # Arguments
    ///
    /// * `p` - [`Polar`] coordinates (r, θ)
    ///
    /// # Returns
    ///
    /// Complex number in Cartesian form (num_complex::Complex64)
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::{ComplexOps, Polar};
    /// use std::f64::consts::PI;
    ///
    /// // Convert polar form to Cartesian
    /// let polar = Polar::new(5.0, PI / 3.0);  // r=5, θ=60°
    /// let z = ComplexOps::from_polar(polar);
    /// assert!((z.re - 5.0 * (PI / 3.0).cos()).abs() < 1e-10);
    /// assert!((z.im - 5.0 * (PI / 3.0).sin()).abs() < 1e-10);
    ///
    /// // Round-trip conversion
    /// let original = Polar::new(3.0, PI / 4.0);
    /// let complex = ComplexOps::from_polar(original);
    /// let polar = ComplexOps::to_polar(complex);
    /// assert!((polar.r - original.r).abs() < 1e-10);
    /// assert!((polar.theta - original.theta).abs() < 1e-10);
    ///
    /// // Unit circle point at 90 degrees
    /// let polar = Polar::new(1.0, PI / 2.0);
    /// let z = ComplexOps::from_polar(polar);
    /// assert!((z.re - 0.0).abs() < 1e-10);
    /// assert!((z.im - 1.0).abs() < 1e-10);
    /// ```
    pub fn from_polar(p: Polar) -> Complex64 {
        Complex64::from_polar(p.r, p.theta)
    }

    /// De Moivre's theorem: (r∠θ)^n = r^n∠(nθ).
    ///
    /// Computes powers (or fractional powers for roots) of complex numbers using
    /// De Moivre's theorem, which states that raising a complex number to a power
    /// in polar form multiplies the magnitude by the power and the angle by the power.
    ///
    /// # Mathematical Background
    ///
    /// **De Moivre's Theorem**: For a complex number z = r∠θ and any real number n:
    /// ```text
    /// z^n = (r∠θ)^n = r^n∠(nθ)
    /// ```
    ///
    /// In Cartesian form, this is equivalent to:
    /// ```text
    /// (r(cos(θ) + i sin(θ)))^n = r^n(cos(nθ) + i sin(nθ))
    /// ```
    ///
    /// **Special Cases**:
    /// - Integer powers: (1+i)² = 2i, (1+i)³ = -2+2i
    /// - Fractional powers (roots): z^(1/n) gives the principal nth root
    /// - Negative powers: z^(-1) = 1/z (multiplicative inverse)
    ///
    /// # Applications
    ///
    /// - **Trigonometric identities**: Deriving formulas like cos(3θ) = 4cos³(θ) - 3cos(θ)
    /// - **Signal processing**: Computing harmonics and frequency components
    /// - **Electrical engineering**: AC circuit analysis with complex impedances
    /// - **Quantum mechanics**: Time evolution of wave functions
    ///
    /// # Arguments
    ///
    /// * `c` - Complex number to raise to power (num_complex::Complex64)
    /// * `n` - Exponent (can be integer, fractional, or negative)
    ///
    /// # Returns
    ///
    /// Result of c^n in Cartesian form
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::ComplexOps;
    /// use num_complex::Complex64;
    /// use std::f64::consts::PI;
    ///
    /// // Square of 1+i: (1+i)² = 2i
    /// let z = Complex64::new(1.0, 1.0);
    /// let z_squared = ComplexOps::de_moivre(z, 2.0);
    /// assert!((z_squared.re - 0.0).abs() < 1e-10);
    /// assert!((z_squared.im - 2.0).abs() < 1e-10);
    ///
    /// // Cube of 1+i: (1+i)³ = -2+2i
    /// let z_cubed = ComplexOps::de_moivre(z, 3.0);
    /// assert!((z_cubed.re - -2.0).abs() < 1e-10);
    /// assert!((z_cubed.im - 2.0).abs() < 1e-10);
    ///
    /// // Square root (principal): (1+i)^(1/2)
    /// let z_sqrt = ComplexOps::de_moivre(z, 0.5);
    /// // Verify by squaring: z_sqrt² should equal z
    /// let z_back = ComplexOps::de_moivre(z_sqrt, 2.0);
    /// assert!((z_back.re - z.re).abs() < 1e-10);
    /// assert!((z_back.im - z.im).abs() < 1e-10);
    ///
    /// // Fourth power of unit circle point at 45°
    /// let z = Complex64::from_polar(1.0, PI / 4.0);  // e^(iπ/4)
    /// let z_fourth = ComplexOps::de_moivre(z, 4.0);   // e^(iπ) = -1
    /// assert!((z_fourth.re - -1.0).abs() < 1e-10);
    /// assert!((z_fourth.im - 0.0).abs() < 1e-10);
    ///
    /// // Negative power (reciprocal): (2+0i)^(-1) = 0.5
    /// let z = Complex64::new(2.0, 0.0);
    /// let z_inv = ComplexOps::de_moivre(z, -1.0);
    /// assert!((z_inv.re - 0.5).abs() < 1e-10);
    /// assert!((z_inv.im - 0.0).abs() < 1e-10);
    /// ```
    pub fn de_moivre(c: Complex64, n: f64) -> Complex64 {
        let polar = Self::to_polar(c);
        let r_n = polar.r.powf(n);
        let theta_n = polar.theta * n;
        Complex64::from_polar(r_n, theta_n)
    }

    /// Compute all nth roots of a complex number using De Moivre's theorem.
    ///
    /// For any complex number z and positive integer n, there are exactly n
    /// distinct nth roots equally spaced around a circle in the complex plane.
    ///
    /// # Mathematical Background
    ///
    /// For z = r∠θ, the n distinct nth roots are:
    /// ```text
    /// z_k = r^(1/n) ∠ ((θ + 2πk) / n)  for k = 0, 1, 2, ..., n-1
    /// ```
    ///
    /// These roots are evenly distributed at angles 2π/n radians apart on a circle
    /// of radius r^(1/n) centred at the origin.
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::ComplexOps;
    /// use num_complex::Complex64;
    ///
    /// // Find all cube roots of 8 (real number): 2, -1+√3i, -1-√3i
    /// let z = Complex64::new(8.0, 0.0);
    /// let roots = ComplexOps::nth_root(z, 3);
    /// assert_eq!(roots.len(), 3);
    ///
    /// // Find all square roots of -1: i and -i
    /// let z = Complex64::new(-1.0, 0.0);
    /// let roots = ComplexOps::nth_root(z, 2);
    /// assert_eq!(roots.len(), 2);
    /// ```
    ///
    /// # Arguments
    ///
    /// * `c` - Complex number to find roots of
    /// * `n` - Root degree (must be a positive integer; returns empty vec for n ≤ 0)
    ///
    /// # Returns
    ///
    /// Vector of all n distinct nth roots ordered by increasing angle.
    pub fn nth_root(c: Complex64, n: i32) -> Vec<Complex64> {
        if n <= 0 {
            return vec![];
        }
        let polar = Self::to_polar(c);
        let r_n = polar.r.powf(1.0 / f64::from(n));
        let n_usize = n as usize;
        (0..n_usize)
            .map(|k| {
                let theta_k = (polar.theta + 2.0 * std::f64::consts::PI * k as f64) / f64::from(n);
                Complex64::from_polar(r_n, theta_k)
            })
            .collect()
    }
}

/// Homogeneous transformation matrix for 2D transformations.
///
/// Represents 2D transformations (translation, rotation, scaling) using
/// homogeneous coordinates and a 3×3 transformation matrix.
///
/// # Mathematical Representation
///
/// A point (x, y) in homogeneous coordinates is represented as [x, y, 1]ᵀ.
/// Transformations are represented as 3×3 matrices:
///
/// ```text
/// ┌         ┐   ┌   ┐   ┌    ┐
/// │ a  b  tx│   │ x │   │ x' │
/// │ c  d  ty│ × │ y │ = │ y' │
/// │ 0  0  1 │   │ 1 │   │ 1  │
/// └         ┘   └   ┘   └    ┘
/// ```
///
/// # Common Transformations
///
/// **Translation** by (dx, dy):
/// ```text
/// ┌         ┐
/// │ 1  0  dx│
/// │ 0  1  dy│
/// │ 0  0  1 │
/// └         ┘
/// ```
///
/// **Rotation** by θ radians counterclockwise:
/// ```text
/// ┌                    ┐
/// │ cos(θ)  -sin(θ)  0 │
/// │ sin(θ)   cos(θ)  0 │
/// │   0        0     1 │
/// └                    ┘
/// ```
///
/// **Scaling** by (sx, sy):
/// ```text
/// ┌         ┐
/// │ sx  0  0│
/// │ 0  sy  0│
/// │ 0   0  1│
/// └         ┘
/// ```
///
/// # Integration with nalgebra
///
/// This struct wraps nalgebra's `Matrix3<f64>` for efficient linear algebra
/// operations. Transformations can be composed through matrix multiplication.
///
/// # Examples
///
/// ```
/// use thales::transforms::{Transform2D, Cartesian2D};
///
/// // Identity transformation leaves points unchanged
/// let identity = Transform2D::identity();
/// let point = Cartesian2D::new(3.0, 4.0);
/// let transformed = identity.apply(point);
/// assert_eq!(transformed.x, point.x);
/// assert_eq!(transformed.y, point.y);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform2D {
    matrix: Matrix3<f64>,
}

impl Transform2D {
    /// Identity transformation.
    ///
    /// Creates the identity transformation that leaves all points unchanged.
    /// The identity matrix is:
    /// ```text
    /// ┌       ┐
    /// │ 1 0 0 │
    /// │ 0 1 0 │
    /// │ 0 0 1 │
    /// └       ┘
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::{Transform2D, Cartesian2D};
    ///
    /// let identity = Transform2D::identity();
    /// let point = Cartesian2D::new(5.0, -3.0);
    /// let result = identity.apply(point);
    /// assert_eq!(result.x, 5.0);
    /// assert_eq!(result.y, -3.0);
    /// ```
    pub fn identity() -> Self {
        Self {
            matrix: Matrix3::identity(),
        }
    }

    /// Translation transformation.
    ///
    /// Creates a transformation that translates points by (dx, dy):
    /// ```text
    /// (x, y) → (x + dx, y + dy)
    /// ```
    ///
    /// Matrix form:
    /// ```text
    /// ┌         ┐
    /// │ 1  0  dx│
    /// │ 0  1  dy│
    /// │ 0  0  1 │
    /// └         ┘
    /// ```
    ///
    /// # Arguments
    ///
    /// * `dx` - Translation in x direction
    /// * `dy` - Translation in y direction
    pub fn translation(dx: f64, dy: f64) -> Self {
        Self {
            matrix: Matrix3::new(1.0, 0.0, dx, 0.0, 1.0, dy, 0.0, 0.0, 1.0),
        }
    }

    /// Rotation transformation (angle in radians).
    ///
    /// Creates a transformation that rotates points counterclockwise by θ radians
    /// around the origin:
    /// ```text
    /// x' = x cos(θ) - y sin(θ)
    /// y' = x sin(θ) + y cos(θ)
    /// ```
    ///
    /// Matrix form:
    /// ```text
    /// ┌                    ┐
    /// │ cos(θ)  -sin(θ)  0 │
    /// │ sin(θ)   cos(θ)  0 │
    /// │   0        0     1 │
    /// └                    ┘
    /// ```
    ///
    /// # Arguments
    ///
    /// * `theta` - Rotation angle in radians (counterclockwise)
    pub fn rotation(theta: f64) -> Self {
        let (s, c) = theta.sin_cos();
        Self {
            matrix: Matrix3::new(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0),
        }
    }

    /// Scaling transformation.
    ///
    /// Creates a transformation that scales points by (sx, sy) in the x and y directions:
    /// ```text
    /// (x, y) → (sx·x, sy·y)
    /// ```
    ///
    /// Matrix form:
    /// ```text
    /// ┌         ┐
    /// │ sx  0  0│
    /// │ 0  sy  0│
    /// │ 0   0  1│
    /// └         ┘
    /// ```
    ///
    /// # Arguments
    ///
    /// * `sx` - Scale factor in x direction
    /// * `sy` - Scale factor in y direction
    pub fn scaling(sx: f64, sy: f64) -> Self {
        Self {
            matrix: Matrix3::new(sx, 0.0, 0.0, 0.0, sy, 0.0, 0.0, 0.0, 1.0),
        }
    }

    /// Apply transformation to point.
    ///
    /// Transforms a point using homogeneous coordinates:
    /// ```text
    /// ┌         ┐   ┌   ┐   ┌    ┐
    /// │ a  b  tx│   │ x │   │ x' │
    /// │ c  d  ty│ × │ y │ = │ y' │
    /// │ 0  0  1 │   │ 1 │   │ 1  │
    /// └         ┘   └   ┘   └    ┘
    /// ```
    ///
    /// The result is (x', y') after dividing by the homogeneous coordinate.
    ///
    /// # Arguments
    ///
    /// * `point` - The point to transform
    ///
    /// # Returns
    ///
    /// The transformed point
    pub fn apply(&self, point: Cartesian2D) -> Cartesian2D {
        use nalgebra::Vector3;
        let h = Vector3::new(point.x, point.y, 1.0);
        let result = self.matrix * h;
        Cartesian2D::new(result[0] / result[2], result[1] / result[2])
    }

    /// Compose with another transformation.
    ///
    /// Composes two transformations through matrix multiplication.
    /// The result transformation T = self × other applies `other` first, then `self`:
    /// ```text
    /// T(p) = self(other(p))
    /// ```
    ///
    /// Note: Matrix multiplication is not commutative, so order matters.
    ///
    /// # Arguments
    ///
    /// * `other` - The transformation to compose with
    ///
    /// # Returns
    ///
    /// The composed transformation
    pub fn compose(&self, other: &Transform2D) -> Transform2D {
        Transform2D {
            matrix: self.matrix * other.matrix,
        }
    }
}

/// Rotation matrices for 3D transformations.
///
/// Provides methods to create 3×3 rotation matrices for rotating points in 3D space
/// around the coordinate axes or arbitrary axes. Returns nalgebra Matrix3 for integration
/// with linear algebra operations.
///
/// # Mathematical Background
///
/// Rotation matrices are orthogonal matrices that preserve distances and angles.
/// For any rotation matrix R:
/// - R^T R = I (orthogonal)
/// - det(R) = 1 (proper rotation, not reflection)
///
/// # Integration with nalgebra
///
/// All methods return `nalgebra::Matrix3<f64>` which can be:
/// - Multiplied with Vector3 to rotate points
/// - Composed through matrix multiplication
/// - Inverted by transposition (R^(-1) = R^T)
///
/// # Examples
///
/// ```
/// use thales::transforms::{Rotation3D, Cartesian3D};
/// use std::f64::consts::PI;
///
/// // Rotate point around z-axis by 90 degrees
/// let rot = Rotation3D::around_z(PI / 2.0);
/// let point = Cartesian3D::new(1.0, 0.0, 0.0);
/// let vec = point.to_vector();
/// let rotated = rot * vec;
/// assert!((rotated[0] - 0.0).abs() < 1e-10);
/// assert!((rotated[1] - 1.0).abs() < 1e-10);
/// assert!((rotated[2] - 0.0).abs() < 1e-10);
/// ```
pub struct Rotation3D;

impl Rotation3D {
    /// Rotation around x-axis.
    ///
    /// Creates a rotation matrix for rotating points counterclockwise around the x-axis
    /// by angle θ when looking from positive x towards the origin (right-hand rule).
    ///
    /// # Matrix Form
    ///
    /// ```text
    /// Rx(θ) = ┌                   ┐
    ///         │ 1     0        0  │
    ///         │ 0  cos(θ)  -sin(θ)│
    ///         │ 0  sin(θ)   cos(θ)│
    ///         └                   ┘
    /// ```
    ///
    /// # Coordinate Transformation
    ///
    /// ```text
    /// x' = x
    /// y' = y cos(θ) - z sin(θ)
    /// z' = y sin(θ) + z cos(θ)
    /// ```
    ///
    /// # Coordinate System Diagram
    ///
    /// ```text
    ///   Looking down the x-axis (from +x towards origin):
    ///
    ///         z
    ///    ///         |     P'
    ///         |    /
    ///       θ |   /
    ///         |  /
    ///         | /___P
    ///         |/________→ y
    ///
    ///   Positive rotation is counterclockwise (right-hand rule)
    /// ```
    ///
    /// # Arguments
    ///
    /// * `theta` - Rotation angle in radians (counterclockwise when looking from +x)
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Rotation3D;
    /// use nalgebra::Vector3;
    /// use std::f64::consts::PI;
    ///
    /// // Rotate 90° around x-axis: (0,1,0) → (0,0,1)
    /// let rot = Rotation3D::around_x(PI / 2.0);
    /// let vec = Vector3::new(0.0, 1.0, 0.0);
    /// let result = rot * vec;
    /// assert!((result[0] - 0.0).abs() < 1e-10);
    /// assert!((result[1] - 0.0).abs() < 1e-10);
    /// assert!((result[2] - 1.0).abs() < 1e-10);
    /// ```
    pub fn around_x(theta: f64) -> Matrix3<f64> {
        Matrix3::new(
            1.0,
            0.0,
            0.0,
            0.0,
            theta.cos(),
            -theta.sin(),
            0.0,
            theta.sin(),
            theta.cos(),
        )
    }

    /// Rotation around y-axis.
    ///
    /// Creates a rotation matrix for rotating points counterclockwise around the y-axis
    /// by angle θ when looking from positive y towards the origin (right-hand rule).
    ///
    /// # Matrix Form
    ///
    /// ```text
    /// Ry(θ) = ┌                   ┐
    ///         │  cos(θ)  0  sin(θ)│
    ///         │    0     1    0   │
    ///         │ -sin(θ)  0  cos(θ)│
    ///         └                   ┘
    /// ```
    ///
    /// # Coordinate Transformation
    ///
    /// ```text
    /// x' = x cos(θ) + z sin(θ)
    /// y' = y
    /// z' = -x sin(θ) + z cos(θ)
    /// ```
    ///
    /// # Coordinate System Diagram
    ///
    /// ```text
    ///   Looking down the y-axis (from +y towards origin):
    ///
    ///         z
    ///    ///         |     P
    ///         |    /
    ///       θ |   /
    ///         |  /
    ///         | /___P'
    ///         |/________→ x
    ///
    ///   Positive rotation is counterclockwise (right-hand rule)
    /// ```
    ///
    /// # Arguments
    ///
    /// * `theta` - Rotation angle in radians (counterclockwise when looking from +y)
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Rotation3D;
    /// use nalgebra::Vector3;
    /// use std::f64::consts::PI;
    ///
    /// // Rotate 90° around y-axis: (1,0,0) → (0,0,-1)
    /// let rot = Rotation3D::around_y(PI / 2.0);
    /// let vec = Vector3::new(1.0, 0.0, 0.0);
    /// let result = rot * vec;
    /// assert!((result[0] - 0.0).abs() < 1e-10);
    /// assert!((result[1] - 0.0).abs() < 1e-10);
    /// assert!((result[2] - -1.0).abs() < 1e-10);
    /// ```
    pub fn around_y(theta: f64) -> Matrix3<f64> {
        Matrix3::new(
            theta.cos(),
            0.0,
            theta.sin(),
            0.0,
            1.0,
            0.0,
            -theta.sin(),
            0.0,
            theta.cos(),
        )
    }

    /// Rotation around z-axis.
    ///
    /// Creates a rotation matrix for rotating points counterclockwise around the z-axis
    /// by angle θ when looking from positive z towards the origin (right-hand rule).
    ///
    /// # Matrix Form
    ///
    /// ```text
    /// Rz(θ) = ┌                   ┐
    ///         │ cos(θ)  -sin(θ)  0│
    ///         │ sin(θ)   cos(θ)  0│
    ///         │   0        0     1│
    ///         └                   ┘
    /// ```
    ///
    /// # Coordinate Transformation
    ///
    /// ```text
    /// x' = x cos(θ) - y sin(θ)
    /// y' = x sin(θ) + y cos(θ)
    /// z' = z
    /// ```
    ///
    /// # Coordinate System Diagram
    ///
    /// ```text
    ///   Looking down the z-axis (from +z towards origin):
    ///
    ///         y
    ///    ///         |     P'
    ///         |    /
    ///       θ |   /
    ///         |  /
    ///         | /___P
    ///         |/________→ x
    ///
    ///   Positive rotation is counterclockwise (right-hand rule)
    /// ```
    ///
    /// # Arguments
    ///
    /// * `theta` - Rotation angle in radians (counterclockwise when looking from +z)
    ///
    /// # Examples
    ///
    /// ```
    /// use thales::transforms::Rotation3D;
    /// use nalgebra::Vector3;
    /// use std::f64::consts::PI;
    ///
    /// // Rotate 90° around z-axis: (1,0,0) → (0,1,0)
    /// let rot = Rotation3D::around_z(PI / 2.0);
    /// let vec = Vector3::new(1.0, 0.0, 0.0);
    /// let result = rot * vec;
    /// assert!((result[0] - 0.0).abs() < 1e-10);
    /// assert!((result[1] - 1.0).abs() < 1e-10);
    /// assert!((result[2] - 0.0).abs() < 1e-10);
    ///
    /// // Compose rotations: 90° + 90° = 180°
    /// let rot1 = Rotation3D::around_z(PI / 2.0);
    /// let rot2 = Rotation3D::around_z(PI / 2.0);
    /// let combined = rot2 * rot1;
    /// let vec = Vector3::new(1.0, 0.0, 0.0);
    /// let result = combined * vec;
    /// assert!((result[0] - -1.0).abs() < 1e-10);
    /// assert!((result[1] - 0.0).abs() < 1e-10);
    /// ```
    pub fn around_z(theta: f64) -> Matrix3<f64> {
        Matrix3::new(
            theta.cos(),
            -theta.sin(),
            0.0,
            theta.sin(),
            theta.cos(),
            0.0,
            0.0,
            0.0,
            1.0,
        )
    }

    /// Rotation around arbitrary axis (Rodrigues' rotation formula).
    ///
    /// **TODO**: Not yet implemented. Returns identity matrix as placeholder.
    ///
    /// When implemented, will create a rotation matrix for rotating points around
    /// an arbitrary unit axis vector by angle θ using Rodrigues' rotation formula.
    ///
    /// # Rodrigues' Rotation Formula
    ///
    /// For a unit vector **k** = (kx, ky, kz) and angle θ:
    /// ```text
    /// R = I + sin(θ)K + (1 - cos(θ))K²
    ///
    /// where K is the cross-product matrix:
    /// K = ┌              ┐
    ///     │  0   -kz   ky│
    ///     │  kz   0   -kx│
    ///     │ -ky   kx   0 │
    ///     └              ┘
    /// ```
    ///
    /// # Arguments
    ///
    /// * `_axis` - Unit vector defining rotation axis (should be normalized)
    /// * `_theta` - Rotation angle in radians (right-hand rule)
    pub fn around_axis(_axis: Vector3<f64>, _theta: f64) -> Matrix3<f64> {
        // TODO: Implement Rodrigues' formula
        Matrix3::identity()
    }
}

// TODO: Add quaternion representations for 3D rotations
// TODO: Add homogeneous 3D transformations
// TODO: Add projection transformations (orthographic, perspective)
// TODO: Add coordinate frame conversions
// TODO: Add support for reference frame transformations
// TODO: Add geodetic coordinate systems (lat/lon/alt)
// TODO: Add support for non-Euclidean geometries

#[cfg(test)]
mod tests {
    use super::*;
    use std::f64::consts::FRAC_PI_2;

    const EPS: f64 = 1e-10;

    fn approx_eq(a: f64, b: f64) -> bool {
        (a - b).abs() < EPS
    }

    // --- identity ---

    #[test]
    fn identity_leaves_point_unchanged() {
        let p = Cartesian2D::new(3.0, 7.0);
        let result = Transform2D::identity().apply(p);
        assert!(approx_eq(result.x, 3.0));
        assert!(approx_eq(result.y, 7.0));
    }

    // --- translation ---

    #[test]
    fn translation_moves_origin_to_offset() {
        let t = Transform2D::translation(3.0, 4.0);
        let result = t.apply(Cartesian2D::new(0.0, 0.0));
        assert!(approx_eq(result.x, 3.0));
        assert!(approx_eq(result.y, 4.0));
    }

    #[test]
    fn translation_adds_offset_to_point() {
        let t = Transform2D::translation(1.0, -2.0);
        let result = t.apply(Cartesian2D::new(5.0, 6.0));
        assert!(approx_eq(result.x, 6.0));
        assert!(approx_eq(result.y, 4.0));
    }

    // --- rotation ---

    #[test]
    fn rotation_90_maps_unit_x_to_unit_y() {
        let t = Transform2D::rotation(FRAC_PI_2);
        let result = t.apply(Cartesian2D::new(1.0, 0.0));
        assert!(approx_eq(result.x, 0.0));
        assert!(approx_eq(result.y, 1.0));
    }

    #[test]
    fn rotation_zero_is_identity() {
        let t = Transform2D::rotation(0.0);
        let p = Cartesian2D::new(3.0, 4.0);
        let result = t.apply(p);
        assert!(approx_eq(result.x, 3.0));
        assert!(approx_eq(result.y, 4.0));
    }

    // --- scaling ---

    #[test]
    fn scaling_multiplies_coordinates() {
        let t = Transform2D::scaling(2.0, 2.0);
        let result = t.apply(Cartesian2D::new(2.0, 3.0));
        assert!(approx_eq(result.x, 4.0));
        assert!(approx_eq(result.y, 6.0));
    }

    #[test]
    fn scaling_non_uniform() {
        let t = Transform2D::scaling(3.0, 0.5);
        let result = t.apply(Cartesian2D::new(2.0, 4.0));
        assert!(approx_eq(result.x, 6.0));
        assert!(approx_eq(result.y, 2.0));
    }

    // --- compose ---

    #[test]
    fn compose_identity_is_neutral() {
        let t = Transform2D::translation(5.0, -3.0);
        let composed = t.compose(&Transform2D::identity());
        let p = Cartesian2D::new(1.0, 2.0);
        let direct = t.apply(p);
        let via_compose = composed.apply(p);
        assert!(approx_eq(direct.x, via_compose.x));
        assert!(approx_eq(direct.y, via_compose.y));
    }

    #[test]
    fn compose_translate_then_rotate_differs_from_rotate_then_translate() {
        let tr = Transform2D::translation(1.0, 0.0);
        let rot = Transform2D::rotation(FRAC_PI_2);

        // rotate first, then translate: rot.compose(tr) applies tr then rot
        let rot_then_tr = rot.compose(&tr);
        // translate first, then rotate: tr.compose(rot) applies rot then tr
        let tr_then_rot = tr.compose(&rot);

        let p = Cartesian2D::new(0.0, 0.0);
        let r1 = rot_then_tr.apply(p);
        let r2 = tr_then_rot.apply(p);

        // Results must differ, confirming non-commutativity
        assert!(!approx_eq(r1.x, r2.x) || !approx_eq(r1.y, r2.y));
    }

    #[test]
    fn compose_two_translations_adds_offsets() {
        let t1 = Transform2D::translation(1.0, 2.0);
        let t2 = Transform2D::translation(3.0, 4.0);
        let composed = t1.compose(&t2);
        let result = composed.apply(Cartesian2D::new(0.0, 0.0));
        assert!(approx_eq(result.x, 4.0));
        assert!(approx_eq(result.y, 6.0));
    }

    // --- nth_root ---

    const COMPLEX_EPS: f64 = 1e-10;

    fn complex_approx_eq(a: num_complex::Complex64, b: num_complex::Complex64) -> bool {
        (a.re - b.re).abs() < COMPLEX_EPS && (a.im - b.im).abs() < COMPLEX_EPS
    }

    #[test]
    fn nth_root_square_roots_of_one_are_one_and_minus_one() {
        let z = num_complex::Complex64::new(1.0, 0.0);
        let roots = ComplexOps::nth_root(z, 2);
        assert_eq!(roots.len(), 2);
        // roots must be 1 and -1 (in some order)
        let has_pos_one = roots
            .iter()
            .any(|r| complex_approx_eq(*r, num_complex::Complex64::new(1.0, 0.0)));
        let has_neg_one = roots
            .iter()
            .any(|r| complex_approx_eq(*r, num_complex::Complex64::new(-1.0, 0.0)));
        assert!(has_pos_one, "missing root +1");
        assert!(has_neg_one, "missing root -1");
    }

    #[test]
    fn nth_root_cube_roots_of_one_lie_on_unit_circle_spaced_120_degrees_apart() {
        use std::f64::consts::PI;
        let z = num_complex::Complex64::new(1.0, 0.0);
        let roots = ComplexOps::nth_root(z, 3);
        assert_eq!(roots.len(), 3);
        // All roots must have magnitude 1
        for r in &roots {
            assert!(
                (r.norm() - 1.0).abs() < COMPLEX_EPS,
                "root magnitude != 1: {r}"
            );
        }
        // Angles must be 0, 2π/3, 4π/3
        let expected_angles = [0.0_f64, 2.0 * PI / 3.0, 4.0 * PI / 3.0];
        for expected in expected_angles {
            let found = roots.iter().any(|r| {
                let angle = r.arg().rem_euclid(2.0 * PI);
                (angle - expected).abs() < COMPLEX_EPS
            });
            assert!(found, "missing root at angle {expected}");
        }
    }

    #[test]
    fn nth_root_sqrt_of_minus_one_gives_i_and_minus_i() {
        let z = num_complex::Complex64::new(-1.0, 0.0);
        let roots = ComplexOps::nth_root(z, 2);
        assert_eq!(roots.len(), 2);
        let has_i = roots
            .iter()
            .any(|r| complex_approx_eq(*r, num_complex::Complex64::new(0.0, 1.0)));
        let has_neg_i = roots
            .iter()
            .any(|r| complex_approx_eq(*r, num_complex::Complex64::new(0.0, -1.0)));
        assert!(has_i, "missing root +i");
        assert!(has_neg_i, "missing root -i");
    }

    #[test]
    fn nth_root_fourth_roots_of_16_include_two_and_minus_two_and_two_i() {
        let z = num_complex::Complex64::new(16.0, 0.0);
        let roots = ComplexOps::nth_root(z, 4);
        assert_eq!(roots.len(), 4);
        let expected: &[num_complex::Complex64] = &[
            num_complex::Complex64::new(2.0, 0.0),
            num_complex::Complex64::new(-2.0, 0.0),
            num_complex::Complex64::new(0.0, 2.0),
            num_complex::Complex64::new(0.0, -2.0),
        ];
        for exp in expected {
            let found = roots.iter().any(|r| complex_approx_eq(*r, *exp));
            assert!(found, "missing expected root {exp}");
        }
    }

    #[test]
    fn nth_root_all_roots_raised_to_n_recover_original() {
        let cases: &[(num_complex::Complex64, i32)] = &[
            (num_complex::Complex64::new(1.0, 0.0), 2),
            (num_complex::Complex64::new(1.0, 0.0), 3),
            (num_complex::Complex64::new(-1.0, 0.0), 2),
            (num_complex::Complex64::new(16.0, 0.0), 4),
            (num_complex::Complex64::new(3.0, 4.0), 3),
        ];
        for &(z, n) in cases {
            let roots = ComplexOps::nth_root(z, n);
            assert_eq!(roots.len(), n as usize);
            for root in roots {
                let recovered = root.powi(n);
                assert!(
                    complex_approx_eq(recovered, z),
                    "root^{n} = {recovered} != {z} (root = {root})"
                );
            }
        }
    }

    #[test]
    fn nth_root_non_positive_n_returns_empty() {
        let z = num_complex::Complex64::new(1.0, 0.0);
        assert!(ComplexOps::nth_root(z, 0).is_empty());
        assert!(ComplexOps::nth_root(z, -1).is_empty());
    }
}