forked from mathnet/mathnet-numerics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigRational.fs
More file actions
511 lines (423 loc) · 15.8 KB
/
BigRational.fs
File metadata and controls
511 lines (423 loc) · 15.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
// First version copied from the F# Power Pack
// https://raw.github.com/fsharp/powerpack/master/src/FSharp.PowerPack/math/q.fs
// (c) Microsoft Corporation. All rights reserved
#nowarn "44" // OK to use the "compiler only" function RangeGeneric
#nowarn "52" // The value has been copied to ensure the original is not mutated by this operation
namespace MathNet.Numerics
open System
open System.Numerics
open System.Globalization
// invariants: (p,q) in lowest form, q >= 0
[<Sealed>]
type BigRationalLarge (p : BigInteger, q : BigInteger) =
//
member __.IsNegative =
sign p < 0
//
member __.IsPositive =
sign p > 0
//
member __.Numerator = p
//
member __.Denominator = q
//
member __.Sign =
sign p
override __.GetHashCode () =
// This hash code must be identical to the hash for BigInteger when the numbers coincide.
if q.IsOne then p.GetHashCode ()
else (p.GetHashCode () <<< 3) + q.GetHashCode ()
override __.ToString () =
if q.IsOne then
p.ToString ()
else
p.ToString () + "/" + q.ToString ()
//
static member Equals (x : BigRationalLarge, y : BigRationalLarge) =
// normal form, so structural equality
x.Numerator = y.Numerator && x.Denominator = y.Denominator
//
static member Compare (x : BigRationalLarge, y : BigRationalLarge) =
compare (x.Numerator * y.Denominator) (y.Numerator * x.Denominator)
//
static member ToDouble (num : BigRationalLarge) =
float num.Numerator / float num.Denominator
//
static member Normalize (p : BigInteger, q : BigInteger) =
if q.IsZero then
(* throw for any x/0 *)
raise <| System.DivideByZeroException ()
elif q.IsOne then
BigRationalLarge (p, q)
else
let k = BigInteger.GreatestCommonDivisor (p, q)
let p = p / k
let q = q / k
if sign q < 0 then
BigRationalLarge (-p, -q)
else
BigRationalLarge (p, q)
//
static member Create (p : int, q : int) =
BigRationalLarge.Normalize (bigint p, bigint q)
//
static member Create (p, q) =
BigRationalLarge.Normalize (p, q)
/// Return the given rational number
static member (~+) (n1 : BigRationalLarge) = n1
/// Return the negation of a rational number
static member (~-) (num : BigRationalLarge) =
// still coprime, bq >= 0
BigRationalLarge (-num.Numerator, num.Denominator)
/// Return the sum of two rational numbers
static member (+) (x : BigRationalLarge, y : BigRationalLarge) =
BigRationalLarge.Normalize ((x.Numerator * y.Denominator) + (y.Numerator * x.Denominator), x.Denominator * y.Denominator)
/// Return the difference of two rational numbers
static member (-) (x : BigRationalLarge, y : BigRationalLarge) =
BigRationalLarge.Normalize ((x.Numerator * y.Denominator) - (y.Numerator * x.Denominator), x.Denominator * y.Denominator)
/// Return the product of two rational numbers
static member (*) (x : BigRationalLarge, y : BigRationalLarge) =
BigRationalLarge.Normalize (x.Numerator * y.Numerator, x.Denominator * y.Denominator)
/// Return the ratio of two rational numbers
static member (/) (x : BigRationalLarge, y : BigRationalLarge) =
BigRationalLarge.Normalize (x.Numerator * y.Denominator, x.Denominator * y.Numerator)
//
static member Reciprocal (num : BigRationalLarge) =
BigRationalLarge.Normalize (num.Denominator, num.Numerator)
//
static member Pow (num : BigRationalLarge, n : int) =
// p,q powers still coprime
if n < 0 then BigRationalLarge.Normalize (BigInteger.Pow (num.Denominator, -n), BigInteger.Pow (num.Numerator, -n))
else BigRationalLarge (BigInteger.Pow (num.Numerator, n), BigInteger.Pow (num.Denominator, n))
//
static member FromBigInteger z =
BigRationalLarge.Create (z, BigInteger.One)
//
static member FromInt32 n =
BigRationalLarge.Create (n, 1)
/// Returns the integer part of a rational number.
static member ToBigInteger (num : BigRationalLarge) =
// have p = d.q + r, |r| < |q|
let d, r = BigInteger.DivRem (num.Numerator, num.Denominator)
if r < BigInteger.Zero then
// p = (d-1).q + (r+q)
d - BigInteger.One
else
// p = d.q + r
d
//
static member Parse (str : string) =
let len = str.Length
if len = 0 then
invalidArg "str" "empty string"
let j = str.IndexOf '/'
if j >= 0 then
let p = BigInteger.Parse (str.Substring (0, j))
let q = BigInteger.Parse (str.Substring (j + 1, len - j - 1))
BigRationalLarge.Create (p, q)
else
let p = BigInteger.Parse str
BigRationalLarge.Create (p, BigInteger.One)
override this.Equals (that : obj) =
match that with
| :? BigRationalLarge as that ->
BigRationalLarge.Equals (this, that)
| _ -> false
interface System.IComparable with
member this.CompareTo (obj : obj) =
match obj with
| :? BigRationalLarge as other ->
BigRationalLarge.Compare (this, other)
| _ ->
invalidArg "obj" "the object does not have the correct type"
interface System.IComparable<BigRationalLarge> with
member this.CompareTo other =
BigRationalLarge.Compare (this, other)
/// The type of arbitrary-sized rational numbers.
[<CustomEquality; CustomComparison>]
[<StructuredFormatDisplay("{StructuredDisplayString}N")>]
type BigRational =
//
| Z of BigInteger
//
| Q of BigRationalLarge
/// Return the numerator of the normalized rational number
member this.Numerator =
match this with
| Z z -> z
| Q q -> q.Numerator
/// Return the denominator of the normalized rational number
member this.Denominator =
match this with
| Z _ -> BigInteger.One
| Q q -> q.Denominator
/// Return a boolean indicating if this rational number is strictly negative
member this.IsNegative =
match this with
| Z z -> sign z < 0
| Q q -> q.IsNegative
/// Return a boolean indicating if this rational number is strictly positive
member this.IsPositive =
match this with
| Z z -> sign z > 0
| Q q -> q.IsPositive
/// Indicates whether this number is an integer; denominator is one
member this.IsInteger =
match this with
| Z z -> true
| Q q -> q.Denominator.IsOne
/// Indicates whether this number is equal to zero.
member this.IsZero =
match this with
| Z z -> z.IsZero
| Q q -> q.Numerator.IsZero
/// Indicates whether this number is equal to one.
member this.IsOne =
match this with
| Z z -> z.IsOne
| Q q -> q.Denominator.IsOne && q.Numerator.IsOne
/// Return the sign of a rational number; 0, +1 or -1
member this.Sign =
if this.IsNegative then -1
elif this.IsPositive then 1
else 0
override this.Equals (obj : obj) =
match obj with
| :? BigRational as other ->
BigRational.(=)(this, other)
| _ -> false
override this.GetHashCode () =
// nb. Q and Z hash codes must match up - see notes above
match this with
| Z z -> z.GetHashCode ()
| Q q -> q.GetHashCode ()
override this.ToString () =
match this with
| Z z ->
z.ToString ()
| Q q ->
q.ToString ()
member this.StructuredDisplayString =
this.ToString ()
/// Return the result of converting the string to a rational number
static member Parse (str : string) =
Q (BigRationalLarge.Parse str)
// TODO : Optimize this by implementing a proper comparison function (so we only do one comparison instead of two).
interface System.IComparable with
member this.CompareTo (obj : obj) =
match obj with
| :? BigRational as other ->
if BigRational.(<)(this, other) then -1
elif BigRational.(=)(this, other) then 0
else 1
| _ ->
invalidArg "obj" "The objects are not comparable."
/// Return the result of converting the given integer to a rational number
static member FromInt (x : int) =
Z (bigint x)
/// Return the result of converting the given big integer to a rational number
static member FromBigInt x = Z x
static member FromIntFraction (numerator: int, denominator: int) =
Q (BigRationalLarge.Create (numerator, denominator))
static member FromBigIntFraction (numerator: BigInteger, denominator: BigInteger) =
Q (BigRationalLarge.Create (numerator, denominator))
// See https://docs.microsoft.com/en-us/dotnet/api/system.decimal.getbits
static member FromDecimal(n : decimal) =
let parts = Decimal.GetBits(n)
assert(parts.Length = 4)
let toBigInt = uint32 >> bigint
let lo = (toBigInt parts.[0]) <<< 0
let mid = (toBigInt parts.[1]) <<< 32
let hi = (toBigInt parts.[2]) <<< 64
let sign = if (parts.[3] &&& 0x80000000) = 0 then 1I else -1I
let scale = (parts.[3] >>> 16) &&& 0x7F
BigRational.FromBigIntFraction(
sign * (lo + mid + hi),
BigInteger.Pow(10I, scale))
/// Get zero as a rational number
static member Zero =
BigRational.FromInt 0
/// Get one as a rational number
static member One =
BigRational.FromInt 1
/// Return the sum of two rational numbers
static member ( + ) (n1, n2) =
match n1, n2 with
| Z z, Z zz ->
Z (z + zz)
| Q q, Q qq ->
Q (q + qq)
| Z z, Q qq ->
Q (BigRationalLarge.FromBigInteger z + qq)
| Q q, Z zz ->
Q (q + BigRationalLarge.FromBigInteger zz)
/// Return the difference of two rational numbers
static member ( - ) (n1, n2) =
match n1, n2 with
| Z z, Z zz ->
Z (z - zz)
| Q q, Q qq ->
Q (q - qq)
| Z z, Q qq ->
Q (BigRationalLarge.FromBigInteger z - qq)
| Q q, Z zz ->
Q (q - BigRationalLarge.FromBigInteger zz)
/// Return the product of two rational numbers
static member ( * ) (n1, n2) =
match n1, n2 with
| Z z, Z zz ->
Z (z * zz)
| Q q, Q qq ->
Q (q * qq)
| Z z, Q qq ->
Q (BigRationalLarge.FromBigInteger z * qq)
| Q q, Z zz ->
Q (q * BigRationalLarge.FromBigInteger zz)
/// Return the ratio of two rational numbers
static member ( / ) (n1, n2) =
match n1, n2 with
| Z z, Z zz ->
Q (BigRationalLarge.Create (z, zz))
| Q q, Q qq ->
Q (q / qq)
| Z z, Q qq ->
Q (BigRationalLarge.FromBigInteger z / qq)
| Q q, Z zz ->
Q (q / BigRationalLarge.FromBigInteger zz)
/// Return the negation of a rational number
static member ( ~- ) n =
match n with
| Z z -> Z (-z)
| Q q -> Q (-q)
/// Return the given rational number
static member ( ~+ ) (n : BigRational) = n
/// This operator is for use from other .NET languages
static member op_Equality (n, nn) =
match n,nn with
| Z z, Z zz ->
BigInteger.(=) (z,zz)
| Q q, Q qq ->
BigRationalLarge.Equals (q, qq)
| Z z, Q qq ->
BigRationalLarge.Equals (BigRationalLarge.FromBigInteger z, qq)
| Q q, Z zz ->
BigRationalLarge.Equals (q, BigRationalLarge.FromBigInteger zz)
/// This operator is for use from other .NET languages
static member op_Inequality (n, nn) =
not <| BigRational.op_Equality (n, nn)
/// This operator is for use from other .NET languages
static member op_LessThan (n, nn) =
match n, nn with
| Z z, Z zz ->
z < zz
| Q q, Q qq ->
q < qq
| Z z, Q qq ->
BigRationalLarge.FromBigInteger z < qq
| Q q, Z zz ->
q < BigRationalLarge.FromBigInteger zz
/// This operator is for use from other .NET languages
static member op_LessThanOrEqual (n, nn) =
match n, nn with
| Z z, Z zz ->
z <= zz
| Q q, Q qq ->
q <= qq
| Z z, Q qq ->
BigRationalLarge.FromBigInteger z <= qq
| Q q, Z zz ->
q <= BigRationalLarge.FromBigInteger zz
/// This operator is for use from other .NET languages
static member op_GreaterThan (n, nn) =
match n, nn with
| Z z, Z zz ->
z > zz
| Q q, Q qq ->
q > qq
| Z z, Q qq ->
BigRationalLarge.FromBigInteger z > qq
| Q q, Z zz ->
q > BigRationalLarge.FromBigInteger zz
/// This operator is for use from other .NET languages
static member op_GreaterThanOrEqual (n, nn) =
match n, nn with
| Z z, Z zz ->
z >= zz
| Q q, Q qq ->
q >= qq
| Z z, Q qq ->
BigRationalLarge.FromBigInteger z >= qq
| Q q, Z zz ->
q >= BigRationalLarge.FromBigInteger zz
/// Return the absolute value of a rational number
static member Abs (n : BigRational) =
if n.IsNegative then -n else n
/// Returns the multiplicative inverse of a rational number
static member Reciprocal (n) =
match n with
| Z z ->
Q (BigRationalLarge.Create (BigInteger.One, z))
| Q q ->
Q (BigRationalLarge.Reciprocal q)
/// Return the result of raising the given rational number to the given power
static member Pow (n, i : int) =
match n with
| Z z when i > 0 ->
Z (BigInteger.Pow (z, i))
| Z z ->
Q (BigRationalLarge.Pow (BigRationalLarge.FromBigInteger z, i))
| Q q ->
Q (BigRationalLarge.Pow (q, i))
static member PowN (n, i : int) = BigRational.Pow(n, i)
/// Return the result of converting the given rational number to a floating point number
static member ToDouble (n : BigRational) =
match n with
| Z z ->
float z
| Q q ->
BigRationalLarge.ToDouble q
/// Return the result of converting the given rational number to a big integer
static member ToBigInt (n : BigRational) =
match n with
| Z z -> z
| Q q ->
BigRationalLarge.ToBigInteger q
/// Return the result of converting the given rational number to an integer
static member ToInt32 (n : BigRational) =
match n with
| Z z ->
int z
| Q q ->
int (BigRationalLarge.ToBigInteger q)
/// Return the result of converting the given rational number to an integer
static member op_Explicit (n : BigRational) =
BigRational.ToInt32 n
/// Return the result of converting the given rational number to a big integer
static member op_Explicit (n : BigRational) =
BigRational.ToBigInt n
/// Return the result of converting the given rational number to a floating point number
static member op_Explicit (n : BigRational) =
BigRational.ToDouble n
//
[<RequireQualifiedAccess>]
module NumericLiteralN =
let private zero = BigRational.Zero
let private one = BigRational.One
//
let FromZero () = zero
//
let FromOne () = one
//
let FromInt32 x =
BigRational.FromInt x
//
let FromInt64 (x : int64) =
BigInteger (x)
|> BigRational.FromBigInt
//
let FromString str =
BigRational.Parse str
//
type BigNum = BigRational
//
type bignum = BigRational