-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigComplex.cs
More file actions
476 lines (419 loc) · 21 KB
/
Copy pathBigComplex.cs
File metadata and controls
476 lines (419 loc) · 21 KB
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
//========================================================================
// Badger Maths Library
// Copyright (C) 2018-2025 Mike Conroy
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//========================================================================
using System;
using System.Numerics;
using System.Text;
namespace Badger.Maths.Algebra
{
/// <summary>
/// The BigComplex struct is a structure which represents a complex number using Badger.Maths BigFloat class
/// for the underlying real and imaginary parts
/// </summary>
public readonly struct BigComplex : ICloneable, IComparable, IComparable<BigComplex>, IEquatable<BigComplex>
{
#region Fields
/// <summary>
/// internal private field for the real part of the complex number
/// </summary>
private readonly BigFloat _real;
/// <summary>
/// internal private field for the imaginary part of the complex number
/// </summary>
private readonly BigFloat _imaginary;
#endregion
#region Constructors
/// <summary>
/// Constructs a new complex number using the supplied real and imaginary parts
/// </summary>
/// <param name="realpart">The real part (a) of the complex number a + bi</param>
/// <param name="imaginarypart">The imaginary part (b) of the complex number a + bi</param>
/// <remarks></remarks>
public BigComplex(BigFloat realpart, BigFloat imaginarypart)
{
this._real = realpart;
this._imaginary = imaginarypart;
}
/// <summary>
/// Constructs a new complex number using the supplied BigComplex numer
/// </summary>
/// <param name="complex">A BigComplex number</param>
/// <remarks></remarks>
public BigComplex(BigComplex complex)
{
this._real = complex.Real;
this._imaginary = complex.Imaginary;
}
/// <summary>
/// Constructs a new complex number using the supplied Complex numer
/// </summary>
/// <param name="complex">A Complex number</param>
/// <remarks></remarks>
public BigComplex(Complex complex)
{
this._real = new BigFloat(complex.Real);
this._imaginary = new BigFloat(complex.Imaginary);
}
#endregion
#region Properties / Accessors
/// <summary>
/// The Real part of this imaginary number
/// </summary>
public BigFloat Real
{
get { return this._real; }
}
/// <summary>
/// The Imaginary part of this imaginary number
/// </summary>
public BigFloat Imaginary
{
get { return this._imaginary; }
}
/// <summary>
/// Returns the modulus of this BigComplex number
/// The modulus is the positive real scalar which measures the distance from the origin
/// </summary>
/// <remarks>This method uses <see cref="BigFloat.Sqrt(BigFloat, double)"/> which
/// requires a tolerance to be provided, this is set to 1e-20 by this method</remarks>
public BigFloat Modulus
{
get
{
BigFloat result = this._real * this._real + this._imaginary * this._imaginary;
return BigFloat.Sqrt(result, 1e-20);
}
}
/// <summary>
/// Returns the Argument of this BigComplex structure. The argument measures the angle that the line from the origin to the
/// point z makes with the real axis. It is measured in an anticlockwise direction. The argument is returned in radians
/// </summary>
public BigFloat Argument
{
get { return BigComplex.Atan2(this); }
}
/// <summary>
/// Gets the complex conjugate of this <see cref="ComplexNumber"/>
/// </summary>
public BigComplex Conjugate()
{
return new BigComplex(this._imaginary, -this._real);
}
#endregion
#region Static Arithmetic Operators
/// <summary>
/// Compares the two supplied BigComplex Structures and returns <c>true</c> if the first is less than the second
/// </summary>
/// <param name="left">The first BigComplex structure in the equation "left < right"</param>
/// <param name="right">The second BigComplex structure in the equation "left < right"</param>
/// <returns></returns>
/// <remarks>Internally this method uses the <see cref="BigComplex.CompareTo(BigComplex)">
/// method and hence the comparison is based on proximity to the origin which is determined via the Modulus property</remarks>
public static bool operator <(BigComplex left, BigComplex right)
{
return left.CompareTo(right) < 0;
}
/// <summary>
/// Compares the two supplied BigComplex Structures and returns <c>true</c> if the first is less than or equal to the second
/// </summary>
/// <param name="left">The first BigComplex structure in the equation "left <= right"</param>
/// <param name="right">The second BigComplex structure in the equation "left <= right"</param>
/// <returns>Internally this method uses the <see cref="BigComplex.CompareTo(BigComplex)">
/// method and hence the comparison is based on proximity to the origin which is determined via the Modulus property</returns>
public static bool operator <=(BigComplex left, BigComplex right)
{
return left.CompareTo(right) <= 0;
}
/// <summary>
/// Compares the two supplied BigComplex Structures and returns <c>true</c> if the first is greater than the second
/// </summary>
/// <param name="left">The first BigComplex structure in the equation "left > right"</param>
/// <param name="right">The second BigComplex structure in the equation "left > right"</param>
/// <returns></returns>
/// <remarks>Internally this method uses the <see cref="BigComplex.CompareTo(BigComplex)">
/// method and hence the comparison is based on proximity to the origin which is determined via the Modulus property</remarks>
public static bool operator >(BigComplex left, BigComplex right)
{
return left.CompareTo(right) > 0;
}
/// <summary>
/// Compares the two supplied BigComplex Structures and returns <c>true</c> if the first is greater than or equal to the second
/// </summary>
/// <param name="left">The first BigComplex structure in the equation "left >= right"</param>
/// <param name="right">The second BigComplex structure in the equation "left >= right"</param>
/// <returns>Internally this method uses the <see cref="BigComplex.CompareTo(BigComplex)">
/// method and hence the comparison is based on proximity to the origin which is determined via the Modulus property</returns>
public static bool operator >=(BigComplex left, BigComplex right)
{
return left.CompareTo(right) >= 0;
}
/// <summary>
/// Tests if the two supplied BigComplex structure instances have the same properties
/// </summary>
/// <param name="left">The first instance of a BigComplex structure for the comparison</param>
/// <param name="right">The second instance of a BigComplex structure for the comparison</param>
/// <returns>True if both instances have the same real and imaginary properties, false otherwise</returns>
public static bool operator ==(BigComplex left, BigComplex right)
{
return left.Real == right.Real && left.Imaginary == right.Imaginary;
}
/// <summary>
/// Tests if the two supplied BigComplex structure instances have different properties
/// </summary>
/// <param name="left">The first instance of a BigComplex structure for the comparison</param>
/// <param name="right">The second instance of a BigComplex structure for the comparison</param>
/// <returns>True if the two instances have the different real or imaginary properties, false otherwise</returns>
public static bool operator !=(BigComplex left, BigComplex right)
{
return !(left == right);
}
/// <summary>
/// The unary - operator, negates the value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="BigComplex"/> that will be negated</param>
/// <returns>A negated <see cref="BigComplex"/></returns>
public static BigComplex operator -(BigComplex value)
{
return BigComplex.Negate(value);
}
/// <summary>
/// The unary + operator, returns the value of <paramref name="value"/>, i.e. it is a no-op
/// </summary>
/// <param name="value">The <see cref="BigComplex"/> that will be subject to the unary + operation</param>
/// <returns><paramref name="value"/></returns>
/// <remarks>This operator is implemented for consistency with the - operator, it is a no-op,
/// it does not change the value of <paramref name="value"/></remarks>
public static BigComplex operator +(BigComplex value)
{
return value;
}
/// <summary>
/// Adds two BigComplex structures
/// </summary>
/// <param name="left">The first of the two BigComplex structure instances to sum</param>
/// <param name="right">The second of the two BigComplex structure instances to sum</param>
/// <returns>Returns a BigComplex structure which represents the sum of left + right</returns>
public static BigComplex operator +(BigComplex left, BigComplex right)
{
return new BigComplex(left.Real + right.Real, left.Imaginary + right.Imaginary);
}
/// <summary>
/// Subtracts two BigComplex structures
/// </summary>
/// <param name="left">The first of the two BigComplex structure instances to subtract</param>
/// <param name="right">The second of the two BigComplex structure instances to subtract</param>
/// <returns>Returns a BigComplex structure which represents the calculation of left - right</returns>
public static BigComplex operator -(BigComplex left, BigComplex right)
{
return left + (-right);
}
/// <summary>
/// Multiplies two BigComplex structures
/// </summary>
/// <param name="left">The first of the two BigComplex structure instances to multiply</param>
/// <param name="right">The second of the two BigComplex structure instances to multiply</param>
/// <returns>Returns a BigComplex structure which represents the calculation of left * right</returns>
public static BigComplex operator *(BigComplex left, BigComplex right)
{
BigFloat real = left.Real * right.Real - left.Imaginary * right.Imaginary;
BigFloat imaginary = left.Imaginary * right.Real + left.Real * right.Imaginary;
return new BigComplex(real, imaginary);
}
/// <summary>
/// Divides two BigComplex structures
/// </summary>
/// <param name="left">The first of the two BigComplex structure instances to divide</param>
/// <param name="right">The second of the two BigComplex structure instances to divide</param>
/// <returns>Returns a BigComplex structure which represents the calculation of left / right</returns>
public static BigComplex operator /(BigComplex left, BigComplex right)
{
BigFloat denominator = BigFloat.Pow(right.Real, 2) + BigFloat.Pow(right.Imaginary, 2);
BigFloat real = (left.Real * right.Real + left.Imaginary * right.Imaginary) / denominator;
BigFloat imaginary = (left.Imaginary * right.Real - left.Real * right.Imaginary) / denominator;
return new BigComplex(real, imaginary);
}
#endregion
#region Static Arithmetic Methods
/// <summary>
/// Negates the supplied <see cref="BigComplex"/> structure
/// </summary>
/// <param name="value">The <see cref="BigComplex"/> to be negated</param>
/// <returns>A new <see cref="BigComplex"/> struct that is the negative of <paramref name="value"/></returns>
///
public static BigComplex Negate(BigComplex value)
{
return new BigComplex(-value.Real, -value.Imaginary);
}
/// <summary>
/// Calculates the atan2 value of the supplied BigComplex number
/// </summary>
/// <param name="value">The <see cref="BigComplex"/> for which the atan2 will be calculated</param>
/// <returns>The 2 argument arctangent of a complex number</returns>
/// <remarks>This function is particularly useful because it resolves the ambiguity that can arise
/// when finding the angle. The standard arctangent function only gives angles in the range <c>-pi/2</c> to
/// <c>pi/2</c> (quadrants I and IV). However, with <c>atan2</c>, the angle is calculated for all four
/// quadrants by taking the signs of both <c>x</c> and <c>y</c> into account. This ensures it outputs the
/// correct angle in the range <c>-pi</c> to <c>pi</c></remarks>
public static BigFloat Atan2(BigComplex value)
{
if (value.Real == BigFloat.Zero)
{
if (value.Imaginary > BigFloat.Zero)
{
return BigFloat.Divide(BigFloat.Pi, new BigFloat(2)); // π/2
}
else if (value.Imaginary < BigFloat.Zero)
{
return -BigFloat.Pi / new BigFloat(2); // -π/2
}
return BigFloat.Zero;
}
BigFloat atan = BigFloat.Atan(value.Imaginary / value.Real);
if (value.Real > BigFloat.Zero)
{
return atan;
}
else if (value.Imaginary >= BigFloat.Zero)
{
return atan + BigFloat.Pi; // π
}
else
{
return atan - BigFloat.Pi; // -π
}
}
/// <summary>
/// Tests if the two supplied BigComplex structure instances have the same properties
/// </summary>
/// <param name="left">The first instance of an <see cref="BigComplex"/> struct for the comparison</param>
/// <param name="right">The second instance of an <see cref="BigComplex"/> struct for the comparison</param>
/// <returns><c>True</c> if the two instances have the same <see cref="BigComplex.Real"/> and
/// <see cref="BigComplex.Imaginary"/> properties</returns>
public static bool IsEqual(BigComplex left, BigComplex right)
{
return left.Real == right.Real && left.Imaginary == right.Imaginary;
}
#endregion
#region ICloneable Support
/// <summary>
/// Creates a new instance of a BigComplex structure which has the same real and imaginary properties as this instance
/// </summary>
/// <returns>A deep copy of this instance of a BigComplex structure with identical real and imaginary properties</returns>
public Object Clone()
{
return new BigComplex(this);
}
#endregion
#region IComparable Support
/// <summary>
/// Compares this instance of a BigComplex structure to a specified object instance and returns an integer that indicates whether the value of this instance is less than,
/// equal to, or greater than the value of the specified object instance
/// </summary>
/// The object to compare with this instance of a BigComplex structure<param name="obj"></param>
/// <returns>Less than zero if ??????, zero if they are the same, and greater than zero if ?????</returns>
public int CompareTo(object? obj)
{
if ((obj == null))
{
throw new ArgumentNullException(nameof(obj), "The object you have tried to compare to this instance is Null (Nothing in VB).");
}
else if ((!object.ReferenceEquals(this.GetType(), obj.GetType())))
{
try
{
return this.CompareTo((BigComplex)obj);
}
catch (InvalidCastException ex)
{
throw new InvalidCastException("The object you provided cannot be cast to an BigComplex object.", ex);
}
}
else
{
return this.CompareTo((BigComplex)obj);
}
}
/// <summary>
/// Compares this instance of a BigComplex structure to a specified object instance and returns an integer that indicates whether the value of this instance is less than,
/// equal to, or greater than the value of the specified object instance
/// </summary>
/// <param name="obj">The object to compare with this instance of a BigComplex structure</param>
/// <returns>Less than zero if this instance is closer to the origin than the supplied version, zero if they are the same distance, and greater than zero if this
/// instance is farther from the origin than the supplied version (the Modulus property is used)</returns>
public int CompareTo(BigComplex other)
{
// Multiply by 100000 in order to increase differentiation
return Convert.ToInt32((this.Modulus - other.Modulus) * 100000);
}
#endregion
#region IEquatable Support
/// <summary>
/// Tests if the supplied object, <paramref name="obj">obj</paramref>, is an instance of a BigComplex structure and if
/// so tests whether it has the same properties as this instance of a BigComplex structure
/// </summary>
/// <param name="obj">A BigComplex object to test for property equivalence with this instance</param>
/// <returns><c>True</c> if both instances have the same real and imaginary properties, <c>False</c> otherwise</returns>
/// <remarks>Internally this method uses the equality operator, ==</remarks>
public override bool Equals(object? obj)
{
if ((obj == null) || (!object.ReferenceEquals(this.GetType(), obj.GetType())))
return false;
return (this == (BigComplex)obj);
}
/// <summary>
/// Tests if the supplied object has the same properties as this instance of a BigComplex structure
/// </summary>
/// <param name="obj">A BigComplex object to test for property equivalence with this instance</param>
/// <returns><c>True</c> if both instances have the same real and imaginary properties, <c>False</c> otherwise</returns>
/// <remarks>Internally this method uses the equality operator, ==</remarks>
public bool Equals(BigComplex other)
{
return this == other;
}
#endregion
#region Object Methods
/// <summary>
/// Returns a hash code for this instance of a BigComplex structure
/// </summary>
/// <returns>A single hash code calculated from the individual hash codes of the real and imaginary parts</returns>
public override int GetHashCode() => HashCode.Combine(this._real.GetHashCode(), this._imaginary.GetHashCode());
/// <summary>
/// Returns a string representation of this instance of a BigComplex structure
/// </summary>
/// <returns>A <see cref="string"/> representation of this <see cref="BigComplex"/></returns>
public override string ToString()
{
// Copied from Science Library SCI, https://sourceforge.net/projects/scinet/?source=typ_redirect
StringBuilder z = new();
_ = z.Append('(');
_ = z.Append(this._real.ToString());
if (this._imaginary > new BigFloat(0))
_ = z.Append(" + ");
else if (this._imaginary < new BigFloat(0))
_ = z.Append(" - ");
if (this._imaginary != new BigFloat(0))
{
_ = z.Append(BigFloat.Abs(this._imaginary).ToString());
_ = z.Append('i');
}
_ = z.Append(')');
return z.ToString();
}
#endregion
}
}