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
//! # Coordinate System Transformations Guide
//!
//! This guide covers coordinate system transformations in thales, from 2D Cartesian-Polar
//! conversions to 3D transformations between Cartesian, Spherical, and Cylindrical systems.
//! Learn how to convert between coordinate systems, work with complex numbers in polar form,
//! and apply these transformations to real-world problems.
//!
//! ## Quick Start: 2D Transformations
//!
//! The simplest use case is converting between Cartesian and Polar coordinates:
//!
//! ```rust,ignore
//! use thales::{Cartesian2D, Polar};
//!
//! // Create a point in Cartesian coordinates
//! let point = Cartesian2D::new(3.0, 4.0);
//!
//! // Convert to Polar coordinates
//! let polar = point.to_polar();
//! assert!((polar.r - 5.0).abs() < 1e-10); // r = √(3² + 4²) = 5
//! assert!((polar.theta - 0.927295218).abs() < 1e-6); // θ = arctan(4/3) ≈ 0.927 rad
//!
//! // Convert back to Cartesian (round-trip)
//! let back = polar.to_cartesian();
//! assert!((back.x - 3.0).abs() < 1e-10);
//! assert!((back.y - 4.0).abs() < 1e-10);
//! ```
//!
//! All transformations are bidirectional and preserve geometric properties through
//! round-trip conversions.
//!
//! ## 2D Coordinate Systems
//!
//! ### Cartesian Coordinates
//!
//! Cartesian coordinates represent points using perpendicular x and y axes:
//!
//! ```rust,ignore
//! use thales::Cartesian2D;
//!
//! let origin = Cartesian2D::new(0.0, 0.0);
//! let point = Cartesian2D::new(3.0, 4.0);
//!
//! // Direct field access
//! println!("x = {}, y = {}", point.x, point.y);
//! ```
//!
//! Cartesian coordinates are ideal for:
//! - Linear motion and forces
//! - Rectangular geometries
//! - Grid-based computations
//!
//! ### Polar Coordinates
//!
//! Polar coordinates represent points using radius r and angle θ:
//!
//! ```rust,ignore
//! use thales::{Polar, Cartesian2D};
//! use std::f64::consts::PI;
//!
//! // Create point at radius 5, angle π/4 (45 degrees)
//! let polar = Polar::new(5.0, PI / 4.0);
//!
//! // Convert to Cartesian
//! let cartesian = polar.to_cartesian();
//! assert!((cartesian.x - 3.535533906).abs() < 1e-6); // 5 * cos(π/4)
//! assert!((cartesian.y - 3.535533906).abs() < 1e-6); // 5 * sin(π/4)
//! ```
//!
//! Polar coordinates are ideal for:
//! - Circular motion and rotation
//! - Angular velocities
//! - Radar and navigation systems
//!
//! ### When to Use Each System
//!
//! | Scenario | Recommended System | Reason |
//! |----------|-------------------|---------|
//! | Robot motion on flat surface | Cartesian | Linear coordinates match actuators |
//! | Satellite orbit calculations | Polar | Natural for circular paths |
//! | Screen pixel positioning | Cartesian | Matches display hardware |
//! | Radar tracking | Polar | Direct r and θ measurements |
//!
//! ## 3D Coordinate Systems
//!
//! ### Cartesian 3D Coordinates
//!
//! Three-dimensional Cartesian coordinates extend 2D with a z-axis:
//!
//! ```rust,ignore
//! use thales::Cartesian3D;
//!
//! let point = Cartesian3D::new(1.0, 1.0, 1.0);
//! println!("Point at ({}, {}, {})", point.x, point.y, point.z);
//! ```
//!
//! ### Spherical Coordinates
//!
//! Spherical coordinates use radius r, polar angle θ, and azimuthal angle φ:
//!
//! ```rust,ignore
//! use thales::{Cartesian3D, Spherical};
//!
//! // Convert Cartesian to Spherical
//! let cart = Cartesian3D::new(1.0, 1.0, 1.0);
//! let spherical = cart.to_spherical();
//!
//! // r = √(x² + y² + z²)
//! assert!((spherical.r - 1.732050808).abs() < 1e-6);
//!
//! // Round-trip conversion
//! let back = spherical.to_cartesian();
//! assert!((back.x - 1.0).abs() < 1e-10);
//! assert!((back.y - 1.0).abs() < 1e-10);
//! assert!((back.z - 1.0).abs() < 1e-10);
//! ```
//!
//! Spherical coordinates are ideal for:
//! - Astronomy and celestial mechanics
//! - Electromagnetic fields
//! - Global positioning (latitude/longitude)
//!
//! ### Cylindrical Coordinates
//!
//! Cylindrical coordinates combine polar coordinates (r, θ) with height z:
//!
//! ```rust,ignore
//! use thales::{Cartesian3D, Cylindrical};
//!
//! // Convert Cartesian to Cylindrical
//! let cart = Cartesian3D::new(3.0, 4.0, 5.0);
//! let cylindrical = cart.to_cylindrical();
//!
//! // rho = √(x² + y²), z remains the same
//! assert!((cylindrical.rho - 5.0).abs() < 1e-10);
//! assert!((cylindrical.z - 5.0).abs() < 1e-10);
//!
//! // Convert back to Cartesian
//! let back = cylindrical.to_cartesian();
//! assert!((back.x - 3.0).abs() < 1e-10);
//! assert!((back.y - 4.0).abs() < 1e-10);
//! assert!((back.z - 5.0).abs() < 1e-10);
//! ```
//!
//! Cylindrical coordinates are ideal for:
//! - Objects with rotational symmetry
//! - Pipes and cylinders
//! - Rotating machinery
//!
//! ### 3D System Selection
//!
//! | Scenario | Recommended System | Reason |
//! |----------|-------------------|---------|
//! | Satellite orbits | Spherical | Natural for radial distance |
//! | Drilling operations | Cylindrical | Rotational + depth |
//! | 3D printing | Cartesian | Matches linear actuators |
//! | Antenna radiation patterns | Spherical | Angular distribution |
//!
//! ## Complex Number Operations
//!
//! Complex numbers can be represented in rectangular (a + bi) or polar (r∠θ) form.
//! The [`ComplexOps`](crate::ComplexOps) trait provides polar form operations:
//!
//! ### Polar Form Conversions
//!
//! ```rust,ignore
//! use thales::{ComplexOps, Polar};
//! use num_complex::Complex64;
//!
//! let z = Complex64::new(3.0, 4.0); // 3 + 4i
//!
//! // Convert to polar form
//! let polar = ComplexOps::to_polar(z);
//! assert!((polar.r - 5.0).abs() < 1e-10); // |z| = √(3² + 4²) = 5
//! assert!((polar.theta - 0.927295218).abs() < 1e-6); // arg(z) = arctan(4/3)
//!
//! // Convert back to rectangular form
//! let back = ComplexOps::from_polar(polar);
//! assert!((back.re - 3.0).abs() < 1e-10);
//! assert!((back.im - 4.0).abs() < 1e-10);
//! ```
//!
//! ### De Moivre's Theorem
//!
//! De Moivre's theorem states that (r∠θ)ⁿ = rⁿ∠(nθ):
//!
//! ```rust,ignore
//! use thales::ComplexOps;
//! use num_complex::Complex64;
//!
//! let z = Complex64::new(1.0, 1.0); // 1 + i
//!
//! // Compute z² using De Moivre's theorem
//! let z_squared = ComplexOps::de_moivre(z, 2.0);
//!
//! // (1 + i)² = 1 + 2i - 1 = 2i
//! assert!((z_squared.re - 0.0).abs() < 1e-10);
//! assert!((z_squared.im - 2.0).abs() < 1e-10);
//!
//! // Compute z^(1/2) (square root)
//! let z_sqrt = ComplexOps::de_moivre(z, 0.5);
//! assert!((z_sqrt.re - 1.098684).abs() < 1e-5);
//! assert!((z_sqrt.im - 0.455090).abs() < 1e-5);
//! ```
//!
//! De Moivre's theorem is particularly useful for:
//! - Computing integer powers of complex numbers
//! - Finding nth roots of complex numbers
//! - Trigonometric identities
//!
//! ### Complex Conjugate and Modulus
//!
//! The `num_complex` crate provides standard complex operations:
//!
//! ```rust,ignore
//! use num_complex::Complex64;
//!
//! let z = Complex64::new(3.0, 4.0); // 3 + 4i
//!
//! // Complex conjugate: z* = 3 - 4i
//! let conj = z.conj();
//! assert_eq!(conj.re, 3.0);
//! assert_eq!(conj.im, -4.0);
//!
//! // Modulus (magnitude): |z| = √(3² + 4²) = 5
//! let modulus = z.norm();
//! assert!((modulus - 5.0).abs() < 1e-10);
//!
//! // Complex multiplication
//! let w = Complex64::new(1.0, 2.0);
//! let product = z * w; // (3+4i)(1+2i) = 3+6i+4i-8 = -5+10i
//! assert_eq!(product.re, -5.0);
//! assert_eq!(product.im, 10.0);
//! ```
//!
//! ## Round-Trip Conversions
//!
//! All coordinate transformations preserve geometric information through round-trip conversions:
//!
//! ### 2D Round-Trip
//!
//! ```rust,ignore
//! use thales::{Cartesian2D, Polar};
//!
//! let original = Cartesian2D::new(3.0, 4.0);
//! let polar = original.to_polar();
//! let back = polar.to_cartesian();
//!
//! assert!((back.x - original.x).abs() < 1e-10);
//! assert!((back.y - original.y).abs() < 1e-10);
//! ```
//!
//! ### 3D Spherical Round-Trip
//!
//! ```rust,ignore
//! use thales::{Cartesian3D, Spherical};
//!
//! let original = Cartesian3D::new(1.0, 2.0, 3.0);
//! let spherical = original.to_spherical();
//! let back = spherical.to_cartesian();
//!
//! assert!((back.x - original.x).abs() < 1e-10);
//! assert!((back.y - original.y).abs() < 1e-10);
//! assert!((back.z - original.z).abs() < 1e-10);
//! ```
//!
//! ### 3D Cylindrical Round-Trip
//!
//! ```rust,ignore
//! use thales::{Cartesian3D, Cylindrical};
//!
//! let original = Cartesian3D::new(3.0, 4.0, 5.0);
//! let cylindrical = original.to_cylindrical();
//! let back = cylindrical.to_cartesian();
//!
//! assert!((back.x - original.x).abs() < 1e-10);
//! assert!((back.y - original.y).abs() < 1e-10);
//! assert!((back.z - original.z).abs() < 1e-10);
//! ```
//!
//! Numerical precision is maintained within floating-point error bounds (typically < 1e-10).
//!
//! ## Common Use Cases
//!
//! ### Use Case 1: Robot Navigation
//!
//! Convert sensor data (polar) to control coordinates (Cartesian):
//!
//! ```rust,ignore
//! use thales::{Polar, Cartesian2D};
//!
//! // Sensor detects obstacle at 5m, 45 degrees
//! let obstacle_polar = Polar::new(5.0, 0.785398); // π/4 radians
//! let obstacle_cart = obstacle_polar.to_cartesian();
//!
//! // Plan path in Cartesian coordinates
//! println!("Obstacle at ({:.2}, {:.2})", obstacle_cart.x, obstacle_cart.y);
//! ```
//!
//! ### Use Case 2: Physics Simulation
//!
//! Calculate orbital mechanics using spherical coordinates:
//!
//! ```rust,ignore
//! use thales::{Cartesian3D, Spherical};
//!
//! // Satellite position in Cartesian
//! let position = Cartesian3D::new(6378.0, 0.0, 0.0); // km from Earth center
//!
//! // Convert to spherical for orbital calculations
//! let spherical = position.to_spherical();
//! let altitude = spherical.r - 6371.0; // Subtract Earth radius
//!
//! println!("Satellite altitude: {:.1} km", altitude);
//! ```
//!
//! ### Use Case 3: Signal Processing
//!
//! Analyze frequency response using complex polar form:
//!
//! ```rust,ignore
//! use thales::ComplexOps;
//! use num_complex::Complex64;
//!
//! // Frequency response: H(ω) = 1 / (1 + iω)
//! let omega = 1.0;
//! let h = Complex64::new(1.0, 0.0) / Complex64::new(1.0, omega);
//!
//! // Magnitude and phase in polar form
//! let polar = ComplexOps::to_polar(h);
//! println!("Magnitude: {:.3}, Phase: {:.3} rad", polar.r, polar.theta);
//! ```
//!
//! ### Use Case 4: 3D Graphics
//!
//! Convert camera position from spherical to Cartesian:
//!
//! ```rust,ignore
//! use thales::{Spherical, Cartesian3D};
//! use std::f64::consts::PI;
//!
//! // Camera at radius 10, looking down at 30° from horizontal
//! let camera_spherical = Spherical::new(10.0, PI/6.0, PI/4.0);
//! let camera_position = camera_spherical.to_cartesian();
//!
//! println!("Camera at ({:.2}, {:.2}, {:.2})",
//! camera_position.x, camera_position.y, camera_position.z);
//! ```
//!
//! ### Use Case 5: Antenna Design
//!
//! Calculate radiation pattern in cylindrical coordinates:
//!
//! ```rust,ignore
//! use thales::{Cylindrical, Cartesian3D};
//! use std::f64::consts::PI;
//!
//! // Antenna element at radius 0.5m, 30° angle, height 2m
//! let element = Cylindrical::new(0.5, PI/6.0, 2.0);
//! let position = element.to_cartesian();
//!
//! println!("Element position: ({:.3}, {:.3}, {:.3})",
//! position.x, position.y, position.z);
//! ```
//!
//! ## Performance Considerations
//!
//! All coordinate transformations are O(1) operations involving a fixed number
//! of trigonometric functions:
//!
//! - Cartesian → Polar: 1 atan2, 1 hypot (√)
//! - Polar → Cartesian: 1 sin, 1 cos
//! - Cartesian3D → Spherical: 1 atan2, 2 hypot (√)
//! - Spherical → Cartesian3D: 2 sin, 2 cos
//! - Cartesian3D → Cylindrical: 1 atan2, 1 hypot (√)
//! - Cylindrical → Cartesian3D: 1 sin, 1 cos
//!
//! For performance-critical applications:
//! 1. Minimize coordinate system changes within tight loops
//! 2. Batch transformations to improve cache locality
//! 3. Use Cartesian coordinates for linear algebra operations
//! 4. Consider precomputing common angles (sin/cos lookup tables)
//!
//! ## See Also
//!
//! - [`crate::Cartesian2D`] - 2D Cartesian coordinate representation
//! - [`crate::Polar`] - 2D polar coordinate representation
//! - [`crate::Cartesian3D`] - 3D Cartesian coordinate representation
//! - [`crate::Spherical`] - 3D spherical coordinate representation
//! - [`crate::Cylindrical`] - 3D cylindrical coordinate representation
//! - [`crate::ComplexOps`] - Complex number operations in polar form
//! - [`num_complex::Complex64`] - Complex number representation (external crate)