forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumeric.java
More file actions
633 lines (553 loc) · 21.4 KB
/
Copy pathNumeric.java
File metadata and controls
633 lines (553 loc) · 21.4 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 Charles O Nutter <headius@headius.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.util;
import java.math.BigInteger;
import org.joni.Regex;
import org.jcodings.specific.ASCIIEncoding;
import org.jruby.Ruby;
import org.jruby.RubyBignum;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyInteger;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import static org.jruby.javasupport.util.RuntimeHelpers.invokedynamic;
import static org.jruby.runtime.invokedynamic.MethodNames.OP_CMP;
public class Numeric {
public static final boolean CANON = true;
/** f_add
*
*/
public static IRubyObject f_add(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum)y).getLongValue() == 0) return x;
if (x instanceof RubyFixnum && ((RubyFixnum)x).getLongValue() == 0) return y;
return x.callMethod(context, "+", y);
}
/** f_cmp
*
*/
public static IRubyObject f_cmp(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
long c = ((RubyFixnum)x).getLongValue() - ((RubyFixnum)y).getLongValue();
if (c > 0) {
return RubyFixnum.one(context.runtime);
} else if (c < 0) {
return RubyFixnum.minus_one(context.runtime);
}
return RubyFixnum.zero(context.runtime);
}
return invokedynamic(context, x, OP_CMP, y);
}
/** f_div
*
*/
public static IRubyObject f_div(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum)y).getLongValue() == 1) return x;
return x.callMethod(context, "/", y);
}
/** f_gt_p
*
*/
public static IRubyObject f_gt_p(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
return ((RubyFixnum)x).getLongValue() > ((RubyFixnum)y).getLongValue() ? context.runtime.getTrue() : context.runtime.getFalse();
}
return x.callMethod(context, ">", y);
}
/** f_lt_p
*
*/
public static IRubyObject f_lt_p(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
return ((RubyFixnum)x).getLongValue() < ((RubyFixnum)y).getLongValue() ? context.runtime.getTrue() : context.runtime.getFalse();
}
return x.callMethod(context, "<", y);
}
/** f_mod
*
*/
public static IRubyObject f_mod(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "%", y);
}
/** f_mul
*
*/
public static IRubyObject f_mul(ThreadContext context, IRubyObject x, IRubyObject y) {
Ruby runtime = context.runtime;
if (y instanceof RubyFixnum) {
long iy = ((RubyFixnum)y).getLongValue();
if (iy == 0) {
if (x instanceof RubyFixnum || x instanceof RubyBignum) return RubyFixnum.zero(runtime);
} else if (iy == 1) {
return x;
}
} else if (x instanceof RubyFixnum) {
long ix = ((RubyFixnum)x).getLongValue();
if (ix == 0) {
if (y instanceof RubyFixnum || y instanceof RubyBignum) return RubyFixnum.zero(runtime);
} else if (ix == 1) {
return y;
}
}
return x.callMethod(context, "*", y);
}
/** f_sub
*
*/
public static IRubyObject f_sub(ThreadContext context, IRubyObject x, IRubyObject y) {
if (y instanceof RubyFixnum && ((RubyFixnum)y).getLongValue() == 0) return x;
return x.callMethod(context, "-", y);
}
/** f_xor
*
*/
public static IRubyObject f_xor(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "^", y);
}
/** f_abs
*
*/
public static IRubyObject f_abs(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "abs");
}
/** f_abs2
*
*/
public static IRubyObject f_abs2(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "abs2");
}
/** f_arg
*
*/
public static IRubyObject f_arg(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "arg");
}
/** f_conjugate
*
*/
public static IRubyObject f_conjugate(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "conjugate");
}
/** f_denominator
*
*/
public static IRubyObject f_denominator(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "denominator");
}
/** f_exact_p
*
*/
public static IRubyObject f_exact_p(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "exact?");
}
/** f_numerator
*
*/
public static IRubyObject f_numerator(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "numerator");
}
/** f_polar
*
*/
public static IRubyObject f_polar(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "polar");
}
/** f_real_p
*
*/
public static IRubyObject f_real_p(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "real?");
}
/** f_integer_p
*
*/
public static IRubyObject f_integer_p(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "integer?");
}
/** f_divmod
*
*/
public static IRubyObject f_divmod(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "divmod", y);
}
/** f_floor
*
*/
public static IRubyObject f_floor(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "floor");
}
/** f_inspect
*
*/
public static IRubyObject f_inspect(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "inspect");
}
/** f_negate
*
*/
public static IRubyObject f_negate(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "-@");
}
/** f_to_f
*
*/
public static IRubyObject f_to_f(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "to_f");
}
/** f_to_i
*
*/
public static IRubyObject f_to_i(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "to_i");
}
/** f_to_r
*
*/
public static IRubyObject f_to_r(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "to_r");
}
/** f_to_s
*
*/
public static IRubyObject f_to_s(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "to_s");
}
/** f_truncate
*
*/
public static IRubyObject f_truncate(ThreadContext context, IRubyObject x) {
return x.callMethod(context, "truncate");
}
/** f_equal
*
* Note: This may not return a value which is a boolean. other.== can
* return non-boolean (which unless it is nil it will be isTrue()).
*
*/
public static IRubyObject f_equal(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
return context.runtime.newBoolean(((RubyFixnum) x).getLongValue() == ((RubyFixnum) y).getLongValue());
}
return x.callMethod(context, "==", y);
}
/** f_expt
*
*/
public static IRubyObject f_expt(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "**", y);
}
/** f_idiv
*
*/
public static IRubyObject f_idiv(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "div", y);
}
/** f_quo
*
*/
public static IRubyObject f_quo(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "quo", y);
}
/** f_rshift
*
*/
public static IRubyObject f_rshift(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, ">>", y);
}
/** f_lshift
*
*/
public static IRubyObject f_lshift(ThreadContext context, IRubyObject x, IRubyObject y) {
return x.callMethod(context, "<<", y);
}
/** f_negative_p
*
*/
public static boolean f_negative_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyFixnum) return ((RubyFixnum)x).getLongValue() < 0;
return x.callMethod(context, "<", RubyFixnum.zero(context.runtime)).isTrue();
}
/** f_zero_p
*
*/
public static boolean f_zero_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyFixnum) return ((RubyFixnum)x).getLongValue() == 0;
return x.callMethod(context, "==", RubyFixnum.zero(context.runtime)).isTrue();
}
/** f_one_p
*
*/
public static boolean f_one_p(ThreadContext context, IRubyObject x) {
if (x instanceof RubyFixnum) return ((RubyFixnum)x).getLongValue() == 1;
return x.callMethod(context, "==", RubyFixnum.one(context.runtime)).isTrue();
}
/** i_gcd
*
*/
public static long i_gcd(long x, long y) {
if (x < 0) x = -x;
if (y < 0) y = -y;
if (x == 0) return y;
if (y == 0) return x;
while (x > 0) {
long t = x;
x = y % x;
y = t;
}
return y;
}
/** f_gcd
*
*/
public static IRubyObject f_gcd(ThreadContext context, IRubyObject x, IRubyObject y) {
if (x instanceof RubyFixnum && y instanceof RubyFixnum) {
return RubyFixnum.newFixnum(context.runtime, i_gcd(((RubyFixnum)x).getLongValue(), ((RubyFixnum)y).getLongValue()));
}
if (f_negative_p(context, x)) x = f_negate(context, x);
if (f_negative_p(context, y)) y = f_negate(context, y);
if (f_zero_p(context, x)) return y;
if (f_zero_p(context, y)) return x;
for (;;) {
if (x instanceof RubyFixnum) {
if (((RubyFixnum)x).getLongValue() == 0) return y;
if (y instanceof RubyFixnum) {
return RubyFixnum.newFixnum(context.runtime, i_gcd(((RubyFixnum)x).getLongValue(), ((RubyFixnum)y).getLongValue()));
}
}
IRubyObject z = x;
x = f_mod(context, y, x);
y = z;
}
}
/** f_lcm
*
*/
public static IRubyObject f_lcm(ThreadContext context, IRubyObject x, IRubyObject y) {
if (f_zero_p(context, x) || f_zero_p(context, y)) {
return RubyFixnum.zero(context.runtime);
}
return f_abs(context, f_mul(context, f_div(context, x, f_gcd(context, x, y)), y));
}
public static long i_ilog2(ThreadContext context, IRubyObject x) {
long q = (x.callMethod(context, "size").convertToInteger().getLongValue() - 8) * 8 + 1;
if (q > 0) {
x = f_rshift(context, x, RubyFixnum.newFixnum(context.runtime, q));
}
long fx = x.convertToInteger().getLongValue();
long r = -1;
while (fx != 0) {
fx >>= 1;
r += 1;
}
return q + r;
}
public static double ldexp(double f, long e) {
return f * Math.pow(2.0, e);
}
public static double frexp(double mantissa, long[]e) {
short sign = 1;
long exponent = 0;
if (Double.isInfinite(mantissa) || Double.isNaN(mantissa)) {
return mantissa;
}
if (mantissa != 0.0) {
if (mantissa < 0) {
mantissa = -mantissa;
sign = -1;
}
for (; mantissa < 0.5; mantissa *= 2.0, exponent -=1) { }
for (; mantissa >= 1.0; mantissa *= 0.5, exponent +=1) { }
}
e[0] = exponent;
return sign * mantissa;
}
private static long SQRT_LONG_MAX = ((long)1) << ((8 * 8 - 1) / 2);
static boolean fitSqrtLong(long n) {
return n < SQRT_LONG_MAX && n >= -SQRT_LONG_MAX;
}
public static IRubyObject int_pow(ThreadContext context, long x, long y) {
boolean neg = x < 0;
long z = 1;
if (neg) x = -x;
if ((y & 1) != 0) {
z = x;
} else {
neg = false;
}
y &= ~1;
Ruby runtime = context.runtime;
do {
while (y % 2 == 0) {
if (!fitSqrtLong(x)) {
IRubyObject v = RubyBignum.newBignum(runtime, RubyBignum.fix2big(RubyFixnum.newFixnum(runtime, x))).op_pow(context, RubyFixnum.newFixnum(runtime, y));
if (z != 1) v = RubyBignum.newBignum(runtime, RubyBignum.fix2big(RubyFixnum.newFixnum(runtime, neg ? -z : z))).op_mul19(context, v);
return v;
}
x *= x;
y >>= 1;
}
BigInteger bigX = BigInteger.valueOf(x);
BigInteger bigXZ = bigX.multiply(bigX);
if (bigXZ.divide(bigX).longValue() != z) {
IRubyObject v = RubyBignum.newBignum(runtime, RubyBignum.fix2big(RubyFixnum.newFixnum(runtime, x))).op_pow(context, RubyFixnum.newFixnum(runtime, y));
if (z != 1) v = RubyBignum.newBignum(runtime, RubyBignum.fix2big(RubyFixnum.newFixnum(runtime, neg ? -z : z))).op_mul19(context, v);
return v;
}
z = bigXZ.longValue();
} while(--y != 0);
if (neg) z = -z;
return RubyFixnum.newFixnum(runtime, z);
}
public static boolean k_exact_p(IRubyObject x) {
return !(x instanceof RubyFloat);
}
public static boolean k_inexact_p(IRubyObject x) {
return x instanceof RubyFloat;
}
public static final class ComplexPatterns {
public static final Regex comp_pat0, comp_pat1, comp_pat2, underscores_pat;
static {
String WS = "\\s*";
String DIGITS = "(?:\\d(?:_\\d|\\d)*)";
String NUMERATOR = "(?:" + DIGITS + "?\\.)?" + DIGITS + "(?:[eE][-+]?" + DIGITS + ")?";
String DENOMINATOR = DIGITS;
String NUMBER = "[-+]?" + NUMERATOR + "(?:\\/" + DENOMINATOR + ")?";
String NUMBERNOS = NUMERATOR + "(?:\\/" + DENOMINATOR + ")?";
String PATTERN0 = "\\A" + WS + "(" + NUMBER + ")@(" + NUMBER + ")" + WS;
String PATTERN1 = "\\A" + WS + "([-+])?(" + NUMBER + ")?[iIjJ]" + WS;
String PATTERN2 = "\\A" + WS + "(" + NUMBER + ")(([-+])(" + NUMBERNOS + ")?[iIjJ])?" + WS;
comp_pat0 = new Regex(PATTERN0.getBytes(), 0, PATTERN0.length(), 0, ASCIIEncoding.INSTANCE);
comp_pat1 = new Regex(PATTERN1.getBytes(), 0, PATTERN1.length(), 0, ASCIIEncoding.INSTANCE);
comp_pat2 = new Regex(PATTERN2.getBytes(), 0, PATTERN2.length(), 0, ASCIIEncoding.INSTANCE);
underscores_pat = new Regex("_+".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE);
}
}
public static final class RationalPatterns {
public static final Regex rat_pat, an_e_pat, a_dot_pat;
static {
String WS = "\\s*";
String DIGITS = "(?:\\d(?:_\\d|\\d)*)";
String NUMERATOR = "(?:" + DIGITS + "?\\.)?" + DIGITS + "(?:[eE][-+]?" + DIGITS + ")?";
String DENOMINATOR = DIGITS;
String PATTERN = "\\A" + WS + "([-+])?(" + NUMERATOR + ")(?:\\/(" + DENOMINATOR + "))?" + WS;
rat_pat = new Regex(PATTERN.getBytes(), 0, PATTERN.length(), 0, ASCIIEncoding.INSTANCE);
an_e_pat = new Regex("[Ee]".getBytes(), 0, 4, 0, ASCIIEncoding.INSTANCE);
a_dot_pat = new Regex("\\.".getBytes(), 0, 2, 0, ASCIIEncoding.INSTANCE);
}
}
/*
The algorithm here is the method described in CLISP. Bruno Haible has
graciously given permission to use this algorithm. He says, "You can use
it, if you present the following explanation of the algorithm."
Algorithm (recursively presented):
If x is a rational number, return x.
If x = 0.0, return 0.
If x < 0.0, return (- (rationalize (- x))).
If x > 0.0:
Call (integer-decode-float x). It returns a m,e,s=1 (mantissa,
exponent, sign).
If m = 0 or e >= 0: return x = m*2^e.
Search a rational number between a = (m-1/2)*2^e and b = (m+1/2)*2^e
with smallest possible numerator and denominator.
Note 1: If m is a power of 2, we ought to take a = (m-1/4)*2^e.
But in this case the result will be x itself anyway, regardless of
the choice of a. Therefore we can simply ignore this case.
Note 2: At first, we need to consider the closed interval [a,b].
but since a and b have the denominator 2^(|e|+1) whereas x itself
has a denominator <= 2^|e|, we can restrict the search to the open
interval (a,b).
So, for given a and b (0 < a < b) we are searching a rational number
y with a <= y <= b.
Recursive algorithm fraction_between(a,b):
c := (ceiling a)
if c < b
then return c ; because a <= c < b, c integer
else
; a is not integer (otherwise we would have had c = a < b)
k := c-1 ; k = floor(a), k < a < b <= k+1
return y = k + 1/fraction_between(1/(b-k), 1/(a-k))
; note 1 <= 1/(b-k) < 1/(a-k)
You can see that we are actually computing a continued fraction expansion.
Algorithm (iterative):
If x is rational, return x.
Call (integer-decode-float x). It returns a m,e,s (mantissa,
exponent, sign).
If m = 0 or e >= 0, return m*2^e*s. (This includes the case x = 0.0.)
Create rational numbers a := (2*m-1)*2^(e-1) and b := (2*m+1)*2^(e-1)
(positive and already in lowest terms because the denominator is a
power of two and the numerator is odd).
Start a continued fraction expansion
p[-1] := 0, p[0] := 1, q[-1] := 1, q[0] := 0, i := 0.
Loop
c := (ceiling a)
if c >= b
then k := c-1, partial_quotient(k), (a,b) := (1/(b-k),1/(a-k)),
goto Loop
finally partial_quotient(c).
Here partial_quotient(c) denotes the iteration
i := i+1, p[i] := c*p[i-1]+p[i-2], q[i] := c*q[i-1]+q[i-2].
At the end, return s * (p[i]/q[i]).
This rational number is already in lowest terms because
p[i]*q[i-1]-p[i-1]*q[i] = (-1)^i.
*/
public static IRubyObject[] nurat_rationalize_internal(ThreadContext context, IRubyObject[] ary) {
IRubyObject a, b, p, q;
a = ary[0];
b = ary[1];
IRubyObject c, k, t, p0, p1, p2, q0, q1, q2;
RubyFixnum zero = RubyFixnum.zero(context.runtime);
RubyFixnum one = RubyFixnum.one(context.runtime);
p0 = q1 = zero;
p1 = q0 = one;
while (true) {
c = a.callMethod(context, "ceil");
if (f_lt_p(context, c, b).isTrue()) {
break;
}
k = f_sub(context, c, one);
p2 = f_add(context, f_mul(context, k, p1), p0);
q2 = f_add(context, f_mul(context, k, q1), q0);
t = f_quo(context, one, f_sub(context, b, k));
b = f_quo(context, one, f_sub(context, a, k));
a = t;
p0 = p1;
q0 = q1;
p1 = p2;
q1 = q2;
}
p = f_add(context, f_mul(context, c, p1), p0);
q = f_add(context, f_mul(context, c, q1), q0);
IRubyObject[] v = new IRubyObject[]{p, q};
return v;
}
public static void checkInteger(ThreadContext context, IRubyObject obj) {
if (!(obj instanceof RubyInteger)) {
throw context.runtime.newTypeError("not an integer");
}
}
}