complexible 0.2.4

A minimal and user-friendly Rust library complex-number operations, bringing the joy of complex mathematics to Rustaceans everywhere!
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
//! # `angle` Module
//!
//! This module provides Radian and Degree structs for working with angles in Rust.
//! Angle measurements are essential in many mathematical and engineering applications,
//! such as trigonometry, geometry, and physics. The Radian and Degree structs offer
//! a convenient way to represent, convert, and manipulate angle values in both radians and degrees.
//!
//! The module includes methods for creating angle values from radians and degrees, converting
//! between radians and degrees, and performing arithmetic operations on angles.
//!
//! Embrace the flexibility and precision offered by the angle module for your angle-related calculations!
//!
//! # `z` Module
//!
//! This module provides CartesianComplexNumber and PolarComplexNumber structs for representing
//! complex numbers in Cartesian and polar coordinates, respectively.
//!
//! Complex numbers can be represented in various forms, and the choice of representation can have a significant
//! impact on the efficiency and simplicity of certain operations. For example, multiplication and division are
//! more straightforward in polar form, while addition and subtraction are more natural in Cartesian form.
//!
//! The z module allows you to seamlessly work with complex numbers in both Cartesian and polar coordinates
//! and easily convert between the two representations. Take advantage of the z module to harness the full
//! power and flexibility of complex number representations in your calculations!

pub mod angle;
pub mod z;

use angle::*;
use std::fmt;
use z::*;

/// Represents an angle, stored in both degrees and radians.
///
/// # Fields
///
/// * `d` - The angle value in degrees, represented as a `Degree` struct.
/// * `r` - The angle value in radians, represented as a `Radian` struct.
#[derive(Debug)]
pub struct Angle {
    pub d: Degree,
    pub r: Radian,
}
impl Angle {
    /// Creates a new `Angle` value from a given angle value in degrees.
    ///
    /// # Arguments
    ///
    /// * `d` - The value of the angle in degrees.
    ///
    /// # Example
    ///
    /// ```  
    /// use complexible::complex_numbers::*;
    /// let angle = Angle::from_degrees(45.0);
    /// ```
    pub fn from_degrees(d: f64) -> Angle {
        let d = Degree::from(d);
        let r = d.to_radians();
        Angle { d, r }
    }

    /// Creates a new `Angle` value from a given angle value in radians.
    ///
    /// # Arguments
    ///
    /// * `r` - The value of the angle in radians.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    /// let angle = Angle::from_radians(std::f64::consts::PI);
    /// ```
    pub fn from_radians(r: f64) -> Angle {
        let r = Radian::from(r);
        let d = r.to_degrees();
        Angle { d, r }
    }
}
impl Clone for Angle {
    fn clone(&self) -> Angle {
        Angle::from_degrees(self.d.value)
    }
}

/// Represents a complex number in both Cartesian and polar form.
///
/// # Fields
///
/// * `cartesian` - The complex number in Cartesian form, represented as a `CartesianComplexNumber` struct.
/// * `polar` - The complex number in polar form, represented as a `PolarComplexNumber` struct.
#[derive(Debug)]
pub struct ComplexNumber {
    cartesian: CartesianComplexNumber,
    polar: PolarComplexNumber,
}

impl ComplexNumber {
    /// Creates a new `ComplexNumber` from its Cartesian coordinates.
    ///
    /// # Arguments
    ///
    /// * `real` - The real part of the complex number.
    /// * `imaginary` - The imaginary part of the complex number.
    ///
    /// # Example
    ///
    /// ```  
    /// use complexible::complex_numbers::*;
    /// let complex = ComplexNumber::from_cartesian(1.0, 1.0); //1 + 1 J
    /// ```
    pub fn from_cartesian(real: f64, imaginary: f64) -> ComplexNumber {
        let cartesian = CartesianComplexNumber { real, imaginary };
        let polar = cartesian.to_polar();
        ComplexNumber { cartesian, polar }
    }

    /// Creates a new `ComplexNumber` from its polar coordinates.
    ///
    /// # Arguments
    ///
    /// * `magnitude` - The magnitude (or absolute value) of the complex number.
    /// * `angle` - The angle of the complex number, represented as an `Angle` struct.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let angle = Angle::from_degrees(45.0);
    /// let complex = ComplexNumber::from_polar(1.0, angle);
    /// ```
    pub fn from_polar(magnitude: f64, angle: Angle) -> ComplexNumber {
        let polar = PolarComplexNumber { magnitude, angle };
        let cartesian = polar.to_cartesian();
        ComplexNumber { cartesian, polar }
    }

    /// Creates a new `ComplexNumber` from a real number.
    ///
    /// # Arguments
    ///
    /// * `real` - The real part of the complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_real(1.0);
    /// ```
    pub fn from_real(real: f64) -> ComplexNumber {
        ComplexNumber::from_cartesian(real, 0.0)
    }

    /// Returns the absolute value (or magnitude) of the complex number.
    ///
    /// # Returns
    ///
    /// The absolute value (or magnitude) of the complex number as a `f64`.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(3.0, 4.0);
    /// assert_eq!(complex.abs(), 5.0);
    /// ```
    pub fn abs(&self) -> f64 {
        self.polar.magnitude
    }

    /// Returns the angle (in radians) of the complex number.
    ///
    /// # Returns
    ///
    /// The angle (in radians) of the complex number as a `f64`.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(1.0, 1.0);
    /// assert_eq!(complex.angle_in_rads(), 0.7854);
    /// ```
    pub fn angle_in_rads(&self) -> f64 {
        round_five_zeros(self.polar.angle.r.value)
    }

    /// Returns the angle (in degrees) of the complex number.
    ///
    /// # Returns
    ///
    /// The angle (in degrees) of the complex number as a `f64`.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(1.0, 1.0);
    /// assert_eq!(complex.angle_in_degs(), 45.0);
    /// ```
    pub fn angle_in_degs(&self) -> f64 {
        round_three_zeros(self.polar.angle.d.value)
    }

    /// Returns the angle of the complex number as an `Angle` struct.
    ///
    /// # Returns
    ///
    /// An `Angle` struct representing the angle of the complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(1.0, 1.0);
    /// let angle = complex.angle_in_angle();
    /// assert_eq!(angle.d.value, 45.0);
    /// ```
    pub fn angle_in_angle(&self) -> Angle {
        self.polar.angle.clone()
    }

    /// Returns the real part of the complex number.
    ///
    /// # Returns
    ///
    /// The real part of the complex number as a `f64`.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(3.0, 4.0);
    /// assert_eq!(complex.real(), 3.0);
    /// ```
    pub fn real(&self) -> f64 {
        self.cartesian.real
    }

    /// Returns the imaginary part of the complex number.
    ///
    /// # Returns
    ///
    /// The imaginary part of the complex number as a `f64`.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let complex = ComplexNumber::from_cartesian(3.0, 4.0);
    /// assert_eq!(complex.imag(), 4.0);
    /// ```
    pub fn imag(&self) -> f64 {
        self.cartesian.imaginary
    }

    /// Adds the given complex number to this complex number.
    ///
    /// # Arguments
    ///
    /// * `z2` - A reference to the complex number to add to this complex number.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of adding the two complex numbers.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_cartesian(1.0, 2.0);
    /// let z2 = ComplexNumber::from_cartesian(3.0, 4.0);
    /// let result = z1.add(&z2);
    /// assert_eq!(result.real(), 4.0);
    /// assert_eq!(result.imag(), 6.0);
    /// ```
    pub fn add(&self, z2: &ComplexNumber) -> ComplexNumber {
        let real = self.real() + z2.real();
        let imaginary = self.imag() + z2.imag();
        ComplexNumber::from_cartesian(real, imaginary)
    }

    /// Subtracts the given complex number from this complex number.
    ///
    /// # Arguments
    ///
    /// * `z2` - A reference to the complex number to subtract from this complex number.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of subtracting the two complex numbers.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_cartesian(3.0, 4.0);
    /// let z2 = ComplexNumber::from_cartesian(1.0, 2.0);
    /// let result = z1.sub(&z2);
    /// assert_eq!(result.real(), 2.0);
    /// assert_eq!(result.imag(), 2.0);
    /// ```
    pub fn sub(&self, z2: &ComplexNumber) -> ComplexNumber {
        let real = self.real() - z2.real();
        let imaginary = self.imag() - z2.imag();
        ComplexNumber::from_cartesian(real, imaginary)
    }

    /// Multiplies this complex number by the given complex number.
    ///
    /// # Arguments
    ///
    /// * `z2` - A reference to the complex number to multiply by.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of multiplying the two complex numbers.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let z2 = ComplexNumber::from_polar(3.0, Angle::from_degrees(45.0));
    /// let result = z1.mul(&z2);
    /// assert_eq!(result.abs(), 6.0);
    /// assert_eq!(result.angle_in_degs(), 75.0);
    /// ```
    pub fn mul(&self, z2: &ComplexNumber) -> ComplexNumber {
        let magnitude = self.abs() * z2.abs();
        let angle = Angle::from_radians(self.angle_in_rads() + z2.angle_in_rads());
        ComplexNumber::from_polar(magnitude, angle)
    }

    /// Multiplies this complex number by the given scalar value.
    ///
    /// # Arguments
    ///
    /// * `n` - The scalar value to multiply by.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of multiplying this complex number by the scalar value.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.mul_n(3.0);
    /// assert_eq!(result.abs(), 6.0);
    /// assert_eq!(result.angle_in_degs(), 30.0);
    /// ```
    pub fn mul_n(&self, n: f64) -> ComplexNumber {
        let magnitude = self.abs() * n;
        ComplexNumber::from_polar(magnitude, self.angle_in_angle())
    }

    /// Divides this complex number by the given complex number.
    ///
    /// # Arguments
    ///
    /// * `z2` - A reference to the complex number to divide by.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of dividing this complex number by the given complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let z2 = ComplexNumber::from_polar(3.0, Angle::from_degrees(45.0));
    /// let result = z1.div(&z2);
    /// assert_eq!(result.abs(), 2.0/3.0);
    /// assert_eq!(result.angle_in_degs(), -15.0);
    /// ```
    pub fn div(&self, z2: &ComplexNumber) -> ComplexNumber {
        let magnitude = self.abs() / z2.abs();
        let angle = Angle::from_radians(self.angle_in_rads() - z2.angle_in_rads());
        ComplexNumber::from_polar(magnitude, angle)
    }

    /// Raises this complex number to the given power.
    ///
    /// # Arguments
    ///
    /// * `n` - The power to raise this complex number to.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of raising this complex number to the given power.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.pow(2.0);
    /// assert_eq!(result.abs(), 4.0);
    /// assert_eq!(result.angle_in_degs(), 60.0);
    /// ```
    pub fn pow(&self, n: f64) -> ComplexNumber {
        let magnitude = self.abs().powf(n);
        let angle = Angle::from_radians(self.angle_in_rads() * n);
        ComplexNumber::from_polar(magnitude, angle)
    }

    /// Calculates the nth root of this complex number.
    ///
    /// # Arguments
    ///
    /// * `n` - The root to calculate.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the result of taking the nth root of this complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.nth_root(2.0);
    /// assert_eq!(result.abs(), 1.4142135623730951);
    /// assert_eq!(result.angle_in_degs(), 15.0);
    /// ```
    pub fn nth_root(&self, n: f64) -> ComplexNumber {
        let magnitude: f64 = self.abs().powf(1.0 / n);
        let angle = Angle::from_radians(self.angle_in_rads() / n);
        ComplexNumber::from_polar(magnitude, angle)
    }

    /// Calculates the natural logarithm of this complex number.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the natural logarithm of this complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.ln();
    /// assert_eq!(result.real(), 0.6002830669264718);
    /// assert_eq!(result.imag(), 0.3465735902799726);
    /// ```
    ///
    pub fn ln(&self) -> ComplexNumber {
        let magnitude = self.abs().ln();
        ComplexNumber::from_polar(magnitude, self.angle_in_angle())
    }
    /// Calculates the base-10 logarithm of this complex number.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the base-10 logarithm of this complex number.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.log10();
    /// assert_eq!(result.real(), 0.26069962354612713);
    /// assert_eq!(result.imag(), 0.15051499783199057);
    /// ```
    pub fn log10(&self) -> ComplexNumber {
        let magnitude = self.abs().log10();
        ComplexNumber::from_polar(magnitude, self.angle_in_angle())
    }
    /// Calculates the logarithm of this complex number with respect to an arbitrary base.
    ///
    /// # Arguments
    ///
    /// * `arb` - The base to calculate the logarithm with respect to.
    ///
    /// # Returns
    ///
    /// A new `ComplexNumber` object that represents the logarithm of this complex number with respect to the given base.
    ///
    /// # Example
    ///
    /// ```
    /// use complexible::complex_numbers::*;
    ///
    /// let z1 = ComplexNumber::from_polar(2.0, Angle::from_degrees(30.0));
    /// let result = z1.log(2.0);
    /// assert_eq!(result.real(), 0.8660254037844387);
    /// assert_eq!(result.imag(), 0.49999999999999994);
    /// ```
    pub fn log(&self, arb: f64) -> ComplexNumber {
        let magnitude = self.abs().log(arb);
        ComplexNumber::from_polar(magnitude, self.angle_in_angle())
    }


    pub fn print_cartesian(&self) {
        print!("cartesian form: {} + {} j", self.real(), self.imag());
    }
    pub fn print_polar(&self) {
        println!(
            "polar form (radian): {} e ^ {} ㎭ j",
            self.abs(),
            self.angle_in_rads()
        );
        println!(
            "polar form (degree): {} e ^ {}° j",
            self.abs(),
            self.angle_in_degs()
        );
    } 
}
impl fmt::Display for ComplexNumber {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mag_as_sqrt = self.polar.magnitude.powi(2);
        writeln!(
            f,
            "
            Pretty Values: 
            Cartesian Form (Pretty): {:.1} + {:.1} j 
            Polar Form (Pretty): √{:.0} e ^ {:.1}㎭ j
            Polar Form (Pretty): √{:.0} e ^ {:.1}° j 
            
            Precision Values:
            Cartesian Form (Pretty): {} + {} j 
            Polar Form (Pretty): {} e ^ {}㎭ j
            Polar Form (Pretty): {} e ^ {}° j ",
            self.cartesian.real,
            self.cartesian.imaginary,
            mag_as_sqrt,
            self.polar.angle.r.value,
            mag_as_sqrt,
            self.polar.angle.d.value,
            self.cartesian.real,
            self.cartesian.imaginary,
            self.polar.magnitude,
            self.polar.angle.r.value,
            self.polar.magnitude,
            self.polar.angle.d.value,
        )
    }
}

impl PartialEq for ComplexNumber {
    fn eq(&self, other: &Self) -> bool {
        let c1 = round_five_zeros(self.abs()) == round_five_zeros(other.abs());
        let c2 = round_five_zeros(self.angle_in_degs()) == round_five_zeros(other.angle_in_degs());
        let c3 = round_five_zeros(self.angle_in_rads()) == round_five_zeros(other.angle_in_rads());
        let c4 = round_five_zeros(self.imag()) == round_five_zeros(other.imag());
        let c5 = round_five_zeros(self.real()) == round_five_zeros(other.real());

        c1 && c2 && c3 && c4 && c5
    }
}
fn round_five_zeros(n: f64) -> f64 {
    let five_zeros = 1_00000_f64;
    (n * five_zeros).round() / five_zeros
}
fn round_three_zeros(n: f64) -> f64 {
    let three_zeros = 1_000_f64;
    (n * three_zeros).round() / three_zeros
}