-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMpfrComplex.cs
More file actions
524 lines (462 loc) · 24.8 KB
/
Copy pathMpfrComplex.cs
File metadata and controls
524 lines (462 loc) · 24.8 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
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
//========================================================================
// 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.Numerics;
using System.Text;
using Sdcb.Arithmetic.Mpfr;
namespace Badger.Maths.Algebra
{
/// <summary>
/// The MpfrComplex struct is a structure which represents a complex number using an MPFR library (Sdcb.Arithmetic.Mpfr)
/// for the underlying real and imaginary parts
/// </summary>
/// <seealso href="https://github.com/sdcb/Sdcb.Arithmetic">MpfrFloat</seealso>
public readonly struct MpfrComplex : ICloneable, IComparable, IComparable<MpfrComplex>, IEquatable<MpfrComplex>
{
#region Fields
/// <summary>
/// The real part of this MpfrComplex number
/// </summary>
private readonly MpfrFloat _real;
/// <summary>
/// The imaginary part of this MpfrComplex number
/// </summary>
private readonly MpfrFloat _imaginary;
/// <summary>
/// Determines the number of bits used to represent the floating-point number.
/// The higher the precision, the more accurate the number representation, but also the more memory it requires
/// </summary>
private readonly int _precision;
/// <summary>
/// The rounding mode used for the MPFR library
/// </summary>
private readonly MpfrRounding _roundingMode = MpfrRounding.ToEven;
#endregion
#region Constructors
/// <summary>
/// Constructs a new MpfrComplex 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>
/// <param name="precision">The number of bits used to represent the floating-point number</param>
/// <param name="rounding">The rounding mode used for the <see cref="MpfrComplex"/>, see <see cref="MpfrRounding"/></param>
/// <remarks></remarks>
public MpfrComplex(MpfrFloat realpart, MpfrFloat imaginarypart, int precision = 1000, MpfrRounding rounding = MpfrRounding.ToEven)
{
this._real = realpart;
this._imaginary = imaginarypart;
this._precision = precision;
this._roundingMode = rounding;
}
/// <summary>
/// Constructs a new MpfrComplex number using the supplied BigComplex numer
/// </summary>
/// <param name="complex">A MpfrComplex number</param>
/// <param name="precision">The number of bits used to represent the floating-point number</param>
/// <param name="rounding">The rounding mode used for the <see cref="MpfrComplex"/>, see <see cref="MpfrRounding"/></param>
/// <remarks></remarks>
public MpfrComplex(MpfrComplex complex, int precision = 1000, MpfrRounding rounding = MpfrRounding.ToEven)
{
this._real = complex.Real;
this._imaginary = complex.Imaginary;
this._precision = precision;
this._roundingMode = rounding;
}
/// <summary>
/// Constructs a new MpfrComplex number using the supplied System.Numerics.Complex number
/// </summary>
/// <param name="complex">A System.Numerics.Complex number</param>
/// <param name="precision">The number of bits used to represent the floating-point number</param>
/// <param name="rounding">The rounding mode used for the <see cref="MpfrComplex"/>, see <see cref="MpfrRounding"/></param>
/// <remarks></remarks>
public MpfrComplex(Complex complex, int precision = 1000, MpfrRounding rounding = MpfrRounding.ToEven)
{
this._real = MpfrFloat.From(complex.Real, precision);
this._imaginary = MpfrFloat.From(complex.Imaginary, precision);
this._precision = precision;
this._roundingMode = rounding;
}
#endregion
#region Properties / Accessors
/// <summary>
/// The Real part of this imaginary number
/// </summary>
public MpfrFloat Real
{
get { return this._real; }
}
/// <summary>
/// The Imaginary part of this imaginary number
/// </summary>
public MpfrFloat Imaginary
{
get { return this._imaginary; }
}
/// <summary>
/// The number of bits used to represent the floating-point number.
/// The higher the precision, the more accurate the number representation, but also the more memory it requires
/// </summary>
public int Precision
{
get { return this._precision; }
}
/// <summary>
/// The rounding mode used for the MPFR library
/// </summary>
public MpfrRounding RoundingMode
{
get { return this._roundingMode; }
}
/// <summary>
/// Returns the modulus of this MpfrComplex number
/// The modulus is the positive real scalar which measures the distance from the origin.
/// </summary>
public MpfrFloat Modulus
{
get
{
MpfrFloat tResult, tImaginary;
tResult = MpfrFloat.Square(this._real, this._precision, this._roundingMode);
tImaginary = MpfrFloat.Square(this._imaginary, this._precision, this._roundingMode);
tResult += tImaginary;
return MpfrFloat.Sqrt(tResult, this._precision, this._roundingMode);
}
}
/// <summary>
/// Returns the Argument of this MpfrComplex structure. The argument measures the angle that the line from the origin to the
/// point z makes with the real axis. The argument is returned in radians
/// </summary>
public MpfrFloat Argument
{
get
{
return MpfrFloat.Atan2(this._imaginary, this._real, this._precision, this._roundingMode);
}
}
/// <summary>
/// Gets the complex conjugate of this <see cref="ComplexNumber"/>.
/// </summary>
public MpfrComplex Conjugate
{
get
{
return new MpfrComplex(this._real, new MpfrFloat(0) - this._imaginary, this._precision, this._roundingMode);
}
}
#endregion
#region Static Arithmetic Operators
/// <summary>
/// Tests if the two supplied MpfrComplex structure instances have the same properties
/// </summary>
/// <param name="left">The first instance of a MpfrComplex structure for the comparison</param>
/// <param name="right">The second instance of a MpfrComplex structure for the comparison</param>
/// <returns>True if both instances have the same real and imaginary properties, false otherwise</returns>
/// <remarks><para>The <c>sdcb.Arithmetic.Mpfr</c> library uses the <c>mpfr_equal_p</c> method to determine
/// equality. <c>mpfr_equal_p</c> performs a bitwise comparison, meaning that the numbers are considered
/// equal only if their entire binary representations (including sign, exponent, and mantissa) match.</para>
/// <para>Consider using the <see cref="IsEqual(MpfrComplex, MpfrComplex, double)"/> function to
/// perform an equality test within a specified precision.</para></remarks>
public static bool operator ==(MpfrComplex left, MpfrComplex right)
{
return left.Real.Equals(right.Real) && left.Imaginary.Equals(right.Imaginary);
}
/// <summary>
/// Tests if the two supplied MpfrComplex structure instances have different properties
/// </summary>
/// <param name="left">The first instance of a MpfrComplex structure for the comparison</param>
/// <param name="right">The second instance of a MpfrComplex structure for the comparison</param>
/// <returns><c>True</c> if the two instances have different real and/or imaginary properties, <c>false</c> otherwise</returns>
/// <remarks><para>The <c>sdcb.Arithmetic.Mpfr</c> library uses the <c>mpfr_equal_p</c> method to determine
/// equality. <c>mpfr_equal_p</c> performs a bitwise comparison, meaning that the numbers are considered
/// equal only if their entire binary representations (including sign, exponent, and mantissa) match.</para>
/// <para>Consider using the <see cref="IsEqual(MpfrComplex, MpfrComplex, double)"/> function to
/// perform an equality test within a specified precision.</para></remarks>
public static bool operator !=(MpfrComplex left, MpfrComplex right)
{
return !(left == right);
}
/// <summary>
/// The unary - operator, negates the value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="MpfrComplex"/> that will be negated</param>
/// <returns>A negated <see cref="MpfrComplex"/></returns>
public static MpfrComplex operator -(MpfrComplex value)
{
return MpfrComplex.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="MpfrComplex"/> 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 MpfrComplex operator +(MpfrComplex value)
{
return value;
}
/// <summary>
/// Adds two MpfrComplex structures
/// </summary>
/// <param name="left">The first of the two MpfrComplex structure instances to sum</param>
/// <param name="right">The second of the two MpfrComplex structure instances to sum</param>
/// <remarks>The <see cref="Precision"/> and <see cref="RoundingMode"/> of <paramref name="left"/> are used for the new MpfrComplex</remarks>
/// <returns>Returns a MpfrComplex structure which represents the sum of left + right</returns>
public static MpfrComplex operator +(MpfrComplex left, MpfrComplex right)
{
return new MpfrComplex(left.Real + right.Real, left.Imaginary + right.Imaginary, left.Precision, left.RoundingMode);
}
/// <summary>
/// Subtracts two MpfrComplex structures
/// </summary>
/// <param name="left">The first of the two MpfrComplex structure instances to subtract</param>
/// <param name="right">The second of the two MpfrComplex structure instances to subtract</param>
/// <remarks>The <see cref="Precision"/> and <see cref="RoundingMode"/> of <paramref name="left"/> are used for the new MpfrComplex</remarks>
/// <returns>Returns a MpfrComplex structure which represents the calculation of left - right</returns>
public static MpfrComplex operator -(MpfrComplex left, MpfrComplex right)
{
return left + (-right);
}
/// <summary>
/// Multiplies two MpfrComplex structures
/// </summary>
/// <param name="left">The first of the two MpfrComplex structure instances to multiply</param>
/// <param name="right">The second of the two MpfrComplex structure instances to multiply</param>
/// <remarks>The <see cref="Precision"/> and <see cref="RoundingMode"/> of <paramref name="left"/> are used for the new MpfrComplex</remarks>
/// <returns>Returns a MpfrComplex structure which represents the calculation of left * right</returns>
public static MpfrComplex operator *(MpfrComplex left, MpfrComplex right)
{
MpfrFloat NewReal = (left.Real * right.Real) - (left.Imaginary * right.Imaginary);
MpfrFloat NewImaginary = (left.Real * right.Imaginary) + (left.Imaginary * right.Real);
return new MpfrComplex(NewReal, NewImaginary, left.Precision, left.RoundingMode);
}
/// <summary>
/// Divides two MpfrComplex structures
/// </summary>
/// <param name="left">The first of the two MpfrComplex structure instances to divide</param>
/// <param name="right">The second of the two MpfrComplex structure instances to divide</param>
/// <remarks>The <see cref="Precision"/> and <see cref="RoundingMode"/> of <paramref name="left"/> are used for the new MpfrComplex</remarks>
/// <returns>Returns a MpfrComplex structure which represents the calculation of left / right</returns>
public static MpfrComplex operator /(MpfrComplex left, MpfrComplex right)
{
MpfrFloat denominator;
MpfrFloat t1, t2, t3;
MpfrFloat NewReal, NewImaginary;
t1 = MpfrFloat.Square(right.Real, right.Precision, right.RoundingMode);
t2 = MpfrFloat.Square(right.Imaginary, right.Precision, right.RoundingMode);
denominator = t1 + t2;
// Calculate real part
t1 = left.Real * right.Real;
t2 = left.Imaginary * right.Imaginary;
t3 = t1 + t2;
NewReal = t3 / denominator;
//Calculate imaginary part
t1 = left.Imaginary * right.Real;
t2 = left.Real * right.Imaginary;
t3 = t1 - t2;
NewImaginary = t3 / denominator;
return new MpfrComplex(NewReal, NewImaginary, left.Precision, left.RoundingMode);
}
/// <summary>
/// Tests if the first supplied MpfrComplex structure is less than the second supplied MpfrComplex structure
/// </summary>
/// <param name="left">The MpfrComplex instance to compare with <paramref name="right"/></param>
/// <param name="right">The MpfrComplex instance to compare with <paramref name="left"/></param>
/// <returns><c>True</c> if <paramref name="left"/> is closer to the origin than <paramref name="right"/>, otherwise <c>false</c></returns>
public static bool operator <(MpfrComplex left, MpfrComplex right)
{
if (left.CompareTo(right) < 0) return true;
return false;
}
/// <summary>
/// Tests if the first supplied MpfrComplex structure is greater than the second supplied MpfrComplex structure
/// </summary>
/// <param name="left">The MpfrComplex instance to compare with <paramref name="right"/></param>
/// <param name="right">The MpfrComplex instance to compare with <paramref name="left"/></param>
/// <returns><c>True</c> if <paramref name="left"/> is further from the origin than <paramref name="right"/>, otherwise <c>false</c></returns>
public static bool operator >(MpfrComplex left, MpfrComplex right)
{
if (left.CompareTo(right) > 0) return true;
return false;
}
/// <summary>
/// Tests if the first supplied MpfrComplex structure is less than or equal to the second supplied MpfrComplex structure
/// </summary>
/// <param name="left">The MpfrComplex instance to compare with <paramref name="right"/></param>
/// <param name="right">The MpfrComplex instance to compare with <paramref name="left"/></param>
/// <returns><c>True</c> if <paramref name="left"/> is the same distance or closer to the origin than <paramref name="right"/>, otherwise <c>false</c></returns>
public static bool operator <=(MpfrComplex left, MpfrComplex right)
{
if (left.CompareTo(right) <= 0) return true;
return false;
}
/// <summary>
/// Tests if the first supplied MpfrComplex structure is greater than or equal to the second supplied MpfrComplex structure
/// </summary>
/// <param name="left">The MpfrComplex instance to compare with <paramref name="right"/></param>
/// <param name="right">The MpfrComplex instance to compare with <paramref name="left"/></param>
/// <returns></returns>
public static bool operator >=(MpfrComplex left, MpfrComplex right)
{
if (left.CompareTo(right) >= 0) return true;
return false;
}
#endregion
#region Static Arithmetic Methods
/// <summary>
/// Negates the supplied <see cref="MpfrComplex"/> structure
/// </summary>
/// <param name="value">The <see cref="MpfrComplex"/> to be negated</param>
/// <returns>A new <see cref="MpfrComplex"/> struct that is the negative of <paramref name="value"/></returns>
///
public static MpfrComplex Negate(MpfrComplex value)
{
return new MpfrComplex(-value.Real, -value.Imaginary, value.Precision, value.RoundingMode);
}
/// <summary>
/// Tests if the two supplied MpfrComplex structure instances have the same properties to within a given <paramref name="tolerance"/>
/// </summary>
/// <param name="left">The first instance of an MpfrComplex structure for the comparison</param>
/// <param name="right">The second instance of an MpfrComplex structure for the comparison</param>
/// <param name="tolerance">If two MpfrComplex structures have the same properties to within a defined
/// <paramref name="tolerance"/> then they are considered equal.</param>
/// <returns><c>True</c> if the two instances have the same real or imaginary properties (within the defined
/// <paramref name="tolerance"/>, <c>false</c> otherwise</returns>
public static bool IsEqual(MpfrComplex left, MpfrComplex right, double tolerance = 1e-25)
{
if (MpfrFloat.Subtract(left.Real, right.Real) > tolerance || MpfrFloat.Subtract(left.Imaginary, right.Imaginary) > tolerance)
return false;
return true;
}
#endregion
#region ICloneable Support
/// <summary>
/// Creates a new instance of a MpfrComplex structure which has the same real and imaginary properties as this instance
/// </summary>
/// <returns>A deep copy of this instance of a MpfrComplex structure with identical real and imaginary properties</returns>
public Object Clone()
{
return new MpfrComplex(this, this._precision, this._roundingMode);
}
#endregion
#region IComparable Support
/// <summary>
/// Compares this instance of a MpfrComplex 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 an MpfrComplex 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 <see cref="Modulus"/> property is used)</returns>
/// <exception cref="ArgumentNullException">This exception is thrown if <paramref name="obj"/> is <code>Null</code> (<code>Nothing</code> in VB)</exception>
/// <exception cref="InvalidCastException">This exception is thrown if <paramref name="obj"/> cannot be cast to an <c cref="MpfrComplex"/></exception>
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((MpfrComplex)obj);
}
catch (InvalidCastException ex)
{
throw new InvalidCastException("The object you provided cannot be cast to an MPFRComplex object.", ex);
}
}
else
{
return this.CompareTo((MpfrComplex)obj);
}
}
/// <summary>
/// Compares this instance of a MpfrComplex 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 MpfrComplex 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 <see cref="Modulus"/> property is used)</returns>
public int CompareTo(MpfrComplex other)
{
// Multiply by 100000 in order to increase differentiation
MpfrFloat tTemp;
tTemp = this.Modulus - other.Modulus;
tTemp *= new MpfrFloat(10000);
return Convert.ToInt32(tTemp);
}
#endregion
#region IEquatable Support
/// <summary>
/// Tests if the supplied object, <paramref name="obj">obj</paramref>, is an instance of a MpfrComplex structure and if
/// so tests whether it has the same properties as this instance of an MpfrComplex structure
/// </summary>
/// <param name="obj">An MpfrComplex 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 == (MpfrComplex)obj);
}
/// <summary>
/// Tests if the supplied object has the same properties as this instance of a MpfrComplex structure
/// </summary>
/// <param name="obj">A MpfrComplex 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(MpfrComplex other)
{
return this == other;
}
#endregion
#region Object Methods
/// <summary>
/// Returns a hash code for this instance of a MpfrComplex 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 MpfrComplex structure
/// </summary>
/// <returns>A string representation of this instance of a MpfrComplex structure</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 MpfrFloat(0))
z.Append(" + ");
else if (this._imaginary <= new MpfrFloat(0))
z.Append(" - ");
if (this._imaginary != new MpfrFloat(0))
{
MpfrFloat tTemp = MpfrFloat.Abs(this._imaginary, this._precision, this._roundingMode);
z.Append(tTemp.ToString());
z.Append('i');
}
z.Append(')');
return z.ToString();
}
#endregion
}
}