gmp 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/README.rdoc +8 -8
- data/ext/gmpf.c +24 -13
- data/ext/gmpq.c +5 -0
- data/ext/gmpz.c +429 -344
- data/ext/ruby_gmp.h +10 -8
- data/manual.pdf +0 -0
- data/manual.tex +167 -53
- data/test/README +1 -1
- data/test/tc_fib_fac_nextprime.rb +6 -6
- data/test/tc_z_jac_leg_rem.rb +73 -0
- data/test/unit_tests.rb +1 -0
- metadata +3 -2
data/ext/ruby_gmp.h
CHANGED
@@ -62,11 +62,12 @@ typedef __mpf_struct MP_FLOAT;
|
|
62
62
|
#define mpf_temp_free(var) { mpf_clear(var); free(var); }
|
63
63
|
#define prec_max(prec,var) {if(mpf_get_prec(var) > prec) prec = mpf_get_prec(var); }
|
64
64
|
|
65
|
-
#define EXPECTED_ZQFXBD "Expected GMP::Z, GMP::Q, GMP::F,
|
66
|
-
#define EXPECTED_ZQFXB "Expected GMP::Z, GMP::Q, GMP::F,
|
67
|
-
#define EXPECTED_ZXB "Expected GMP::Z,
|
68
|
-
#define EXPECTED_ZX "Expected GMP::Z or
|
69
|
-
#define EXPECTED_X "Expected
|
65
|
+
#define EXPECTED_ZQFXBD "Expected GMP::Z, GMP::Q, GMP::F, Fixnum, Bignum or Float"
|
66
|
+
#define EXPECTED_ZQFXB "Expected GMP::Z, GMP::Q, GMP::F, Fixnum or Bignum"
|
67
|
+
#define EXPECTED_ZXB "Expected GMP::Z, Fixnum or Bignum"
|
68
|
+
#define EXPECTED_ZX "Expected GMP::Z or Fixnum"
|
69
|
+
#define EXPECTED_X "Expected Fixnum"
|
70
|
+
#define EXPECTED_Z "Expected GMP::Z"
|
70
71
|
#define typeerror(expected) rb_raise(rb_eTypeError, EXPECTED_##expected)
|
71
72
|
#define typeerror_as(expected, argname) rb_raise(rb_eTypeError, EXPECTED_##expected " as " argname)
|
72
73
|
|
@@ -105,6 +106,10 @@ extern VALUE r_gmpz_mul(VALUE self, VALUE arg);
|
|
105
106
|
extern VALUE r_gmpzsg_pow(VALUE klass, VALUE base, VALUE exp);
|
106
107
|
|
107
108
|
// Number Theoretic Functions
|
109
|
+
extern VALUE r_gmpz_is_probab_prime(int argc, VALUE* argv, VALUE self);
|
110
|
+
extern VALUE r_gmpz_jacobi(VALUE self, VALUE b);
|
111
|
+
extern VALUE r_gmpzsg_jacobi(VALUE klass, VALUE a, VALUE b);
|
112
|
+
extern VALUE r_gmpz_legendre(VALUE self, VALUE p);
|
108
113
|
extern VALUE r_gmpz_remove(VALUE self, VALUE arg);
|
109
114
|
|
110
115
|
// Integer Comparisons
|
@@ -114,10 +119,7 @@ extern VALUE r_gmpz_cmpabs(VALUE self, VALUE arg);
|
|
114
119
|
|
115
120
|
// _unsorted_
|
116
121
|
extern VALUE r_gmpz_div(VALUE self, VALUE arg);
|
117
|
-
extern VALUE r_gmpz_is_probab_prime(int argc, VALUE* argv, VALUE self);
|
118
122
|
extern VALUE r_gmpz_popcount(VALUE self);
|
119
|
-
extern VALUE r_gmpz_jacobi(VALUE self);
|
120
|
-
extern VALUE r_gmpz_legendre(VALUE self);
|
121
123
|
extern VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to);
|
122
124
|
extern VALUE r_gmpz_getbit(VALUE self, VALUE bitnr);
|
123
125
|
extern VALUE r_gmpz_scan0(VALUE self, VALUE bitnr);
|
data/manual.pdf
CHANGED
Binary file
|
data/manual.tex
CHANGED
@@ -18,13 +18,15 @@
|
|
18
18
|
\addtolength{\heavyrulewidth}{\heavyrulewidth}
|
19
19
|
\def\qquad{\quad\quad}
|
20
20
|
\def\qqqquad{\quad\quad\quad\quad}
|
21
|
+
\def\cc{\colon\colon}
|
22
|
+
\frenchspacing
|
21
23
|
\begin{document}
|
22
24
|
|
23
25
|
\begin{tabular}{p{1.0in} p{\titrwidth}}
|
24
26
|
\huge{gmp} &\\
|
25
27
|
\midrule[3pt]
|
26
28
|
\multicolumn{2}{r}{\large{Ruby bindings to the GMP library}}\\
|
27
|
-
\multicolumn{2}{r}{\large{Edition 0.2.
|
29
|
+
\multicolumn{2}{r}{\large{Edition 0.2.2}}\\
|
28
30
|
\multicolumn{2}{r}{\large{21 December 2009}}
|
29
31
|
\end{tabular}
|
30
32
|
|
@@ -164,11 +166,11 @@ There shouldn't be any errors, or warnings.
|
|
164
166
|
\subsection{Classes}
|
165
167
|
The gmp gem includes the namespace \texttt{GMP} and three classes within \texttt{GMP}:
|
166
168
|
\begin{itemize}
|
167
|
-
\item \texttt{GMP::Z} - Methods for signed integer arithmetic. There are at least
|
169
|
+
\item \texttt{GMP::Z} - Methods for signed integer arithmetic. There are at least 45
|
168
170
|
methods here (still accounting).
|
169
|
-
\item \texttt{GMP::Q} - Methods for rational number arithmetic. There are at least
|
171
|
+
\item \texttt{GMP::Q} - Methods for rational number arithmetic. There are at least 11
|
170
172
|
methods here (still accounting).
|
171
|
-
\item \texttt{GMP::F} - Methods for floating-point arithmetic. There are at least
|
173
|
+
\item \texttt{GMP::F} - Methods for floating-point arithmetic. There are at least 6
|
172
174
|
methods here (still accounting).
|
173
175
|
\end{itemize}
|
174
176
|
|
@@ -277,20 +279,8 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
277
279
|
\textbf{+} & & \textit{integer} + \textit{numeric} $\rightarrow$ \textit{numeric} \\
|
278
280
|
\cmidrule(r){2-3}
|
279
281
|
& \multicolumn{2}{p{\defnwidth}}{
|
280
|
-
Returns the sum of \textit{integer} and \textit{numeric}. \textit{numeric} can be
|
281
|
-
|
282
|
-
\textit{Bignum}.
|
283
|
-
}
|
284
|
-
\end{tabular}
|
285
|
-
\newline\newline
|
286
|
-
|
287
|
-
\begin{tabular}{p{\methwidth} l r}
|
288
|
-
\toprule
|
289
|
-
\textbf{add} & & \textit{integer}.add(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
290
|
-
\cmidrule(r){2-3}
|
291
|
-
& \multicolumn{2}{p{\defnwidth}}{
|
292
|
-
Returns the sum of \textit{integer} and \textit{numeric}. \textit{numeric} can be a
|
293
|
-
member of \textit{GMP::Z}, \textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or
|
282
|
+
Returns the sum of \textit{integer} and \textit{numeric}. \textit{numeric} can be an
|
283
|
+
instance of \textit{GMP::Z}, \textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or
|
294
284
|
\textit{Bignum}.
|
295
285
|
}
|
296
286
|
\end{tabular}
|
@@ -302,8 +292,8 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
302
292
|
\textit{numeric} \\
|
303
293
|
\cmidrule(r){2-3}
|
304
294
|
& \multicolumn{2}{p{\defnwidth}}{
|
305
|
-
Sums \textit{integer} and \textit{numeric}, in place. \textit{numeric} can be
|
306
|
-
|
295
|
+
Sums \textit{integer} and \textit{numeric}, in place. \textit{numeric} can be an
|
296
|
+
instance of \textit{GMP::Z}, \textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or
|
307
297
|
\textit{Bignum}.
|
308
298
|
}
|
309
299
|
\end{tabular}
|
@@ -312,12 +302,11 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
312
302
|
\begin{tabular}{p{\methwidth} l r}
|
313
303
|
\toprule
|
314
304
|
\textbf{-} & & \textit{integer} - \textit{numeric} $\rightarrow$ \textit{numeric} \\
|
315
|
-
& & \textit{integer}.sub(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
316
305
|
& & \textit{integer}.sub!(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
317
306
|
\cmidrule(r){2-3}
|
318
307
|
& \multicolumn{2}{p{\defnwidth}}{
|
319
308
|
Returns the difference of \textit{integer} and \textit{numeric}. The destructive method
|
320
|
-
calculates the difference in place. \textit{numeric} can be
|
309
|
+
calculates the difference in place. \textit{numeric} can be an instance of
|
321
310
|
\textit{GMP::Z}, \textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or \textit{Bignum}.
|
322
311
|
Here are some examples:\newline
|
323
312
|
|
@@ -344,8 +333,19 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
344
333
|
\cmidrule(r){2-3}
|
345
334
|
& \multicolumn{2}{p{\defnwidth}}{
|
346
335
|
Returns the product of \textit{integer} and \textit{numeric}. The destructive method
|
347
|
-
calculates the product in place. \textit{numeric} can be
|
348
|
-
\textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or \textit{Bignum}.
|
336
|
+
calculates the product in place. \textit{numeric} can be an instance of
|
337
|
+
\textit{GMP::Z}, \textit{Fixnum}, \textit{GMP::Q}, \textit{GMP::F}, or \textit{Bignum}.
|
338
|
+
}
|
339
|
+
\end{tabular}
|
340
|
+
\newline\newline
|
341
|
+
|
342
|
+
\begin{tabular}{p{\methwidth} l r}
|
343
|
+
\toprule
|
344
|
+
\textbf{\textless\textless} & & \textit{integer} \textless\textless \textit{numeric} $\rightarrow$ \textit{integer} \\
|
345
|
+
\cmidrule(r){2-3}
|
346
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
347
|
+
Returns \textit{integer} times 2 to the \textit{numeric} power. This can also be
|
348
|
+
defined as a left shift by \textit{numeric} bits.
|
349
349
|
}
|
350
350
|
\end{tabular}
|
351
351
|
\newline\newline
|
@@ -378,60 +378,57 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
378
378
|
|
379
379
|
\begin{tabular}{p{\methwidth} l r}
|
380
380
|
\toprule
|
381
|
-
\textbf{tdiv} & &
|
381
|
+
\textbf{tdiv} & & $integer$.tdiv($numeric$) $\rightarrow$ $integer$\\
|
382
382
|
\cmidrule(r){2-3}
|
383
383
|
& \multicolumn{2}{p{\defnwidth}}{
|
384
|
-
Returns the division of
|
385
|
-
|
386
|
-
The return object's class is always \textit{GMP::Z}.
|
384
|
+
Returns the division of $integer$ by $numeric$, truncated. $numeric$ can be an instance
|
385
|
+
of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always $GMP::Z$.
|
387
386
|
}
|
388
387
|
\end{tabular}
|
389
388
|
\newline\newline
|
390
389
|
|
391
390
|
\begin{tabular}{p{\methwidth} l r}
|
392
391
|
\toprule
|
393
|
-
\textbf{fdiv} & &
|
392
|
+
\textbf{fdiv} & & $integer$.fdiv($numeric$) $\rightarrow$ $integer$\\
|
394
393
|
\cmidrule(r){2-3}
|
395
394
|
& \multicolumn{2}{p{\defnwidth}}{
|
396
|
-
Returns the division of
|
397
|
-
|
398
|
-
The return object's class is always \textit{GMP::Z}.
|
395
|
+
Returns the division of $integer$ by $numeric$, floored. $numeric$ can be an instance
|
396
|
+
of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always $GMP::Z$.
|
399
397
|
}
|
400
398
|
\end{tabular}
|
401
399
|
\newline\newline
|
402
400
|
|
403
401
|
\begin{tabular}{p{\methwidth} l r}
|
404
402
|
\toprule
|
405
|
-
\textbf{cdiv} & &
|
403
|
+
\textbf{cdiv} & & $integer$.cdiv($numeric$) $\rightarrow$ $integer$\\
|
406
404
|
\cmidrule(r){2-3}
|
407
405
|
& \multicolumn{2}{p{\defnwidth}}{
|
408
|
-
Returns the ceiling division of
|
409
|
-
|
410
|
-
The return object's class is always \textit{GMP::Z}.
|
406
|
+
Returns the ceiling division of $integer$ by $numeric$. $numeric$ can be an instance of
|
407
|
+
$GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always $GMP::Z$.
|
411
408
|
}
|
412
409
|
\end{tabular}
|
413
410
|
\newline\newline
|
414
411
|
|
415
412
|
\begin{tabular}{p{\methwidth} l r}
|
416
413
|
\toprule
|
417
|
-
\textbf{tmod} & &
|
414
|
+
\textbf{tmod} & & $integer$.tmod($numeric$) $\rightarrow$ $integer$\\
|
418
415
|
\cmidrule(r){2-3}
|
419
416
|
& \multicolumn{2}{p{\defnwidth}}{
|
420
|
-
Returns the remainder after truncated division of
|
421
|
-
|
422
|
-
|
417
|
+
Returns the remainder after truncated division of $integer$ by $numeric$. $numeric$ can
|
418
|
+
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
419
|
+
$GMP::Z$.
|
423
420
|
}
|
424
421
|
\end{tabular}
|
425
422
|
\newline\newline
|
426
423
|
|
427
424
|
\begin{tabular}{p{\methwidth} l r}
|
428
425
|
\toprule
|
429
|
-
\textbf{fmod} & &
|
426
|
+
\textbf{fmod} & & $integer$.fmod($numeric$) $\rightarrow$ $integer$\\
|
430
427
|
\cmidrule(r){2-3}
|
431
428
|
& \multicolumn{2}{p{\defnwidth}}{
|
432
|
-
Returns the remainder after floored division of
|
433
|
-
|
434
|
-
|
429
|
+
Returns the remainder after floored division of $integer$ by $numeric$. $numeric$ can
|
430
|
+
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
431
|
+
$GMP::Z$.
|
435
432
|
}
|
436
433
|
\end{tabular}
|
437
434
|
\newline\newline
|
@@ -441,9 +438,9 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
441
438
|
\textbf{cmod} & & \textit{integer}.cmod(\textit{numeric}) $\rightarrow$ \textit{integer}\\
|
442
439
|
\cmidrule(r){2-3}
|
443
440
|
& \multicolumn{2}{p{\defnwidth}}{
|
444
|
-
Returns the remainder after ceilinged division of
|
445
|
-
|
446
|
-
|
441
|
+
Returns the remainder after ceilinged division of $integer$ by $numeric$. $numeric$ can
|
442
|
+
be an instance of $GMP::Z$, $Fixnum$, $Bignum$. The return object's class is always
|
443
|
+
$GMP::Z$.
|
447
444
|
}
|
448
445
|
\end{tabular}
|
449
446
|
|
@@ -454,7 +451,7 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
454
451
|
\textbf{root} & & \textit{integer}.root(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
455
452
|
\cmidrule(r){2-3}
|
456
453
|
& \multicolumn{2}{p{\defnwidth}}{
|
457
|
-
Returns the integer part of the
|
454
|
+
Returns the integer part of the $numeric$'th root of $integer$.
|
458
455
|
}
|
459
456
|
\end{tabular}
|
460
457
|
\newline\newline
|
@@ -465,7 +462,7 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
465
462
|
& & \textit{integer}.sqrt!(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
466
463
|
\cmidrule(r){2-3}
|
467
464
|
& \multicolumn{2}{p{\defnwidth}}{
|
468
|
-
Returns the truncated integer part of the square root of
|
465
|
+
Returns the truncated integer part of the square root of $integer$.
|
469
466
|
}
|
470
467
|
\end{tabular}
|
471
468
|
\newline\newline
|
@@ -475,9 +472,35 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
475
472
|
\textbf{sqrtrem} & & \textit{integer}.sqrtrem $\rightarrow$ \textit{sqrt}, \textit{rem} \\
|
476
473
|
\cmidrule(r){2-3}
|
477
474
|
& \multicolumn{2}{p{\defnwidth}}{
|
478
|
-
Returns the truncated integer part of the square root of
|
479
|
-
|
480
|
-
|
475
|
+
Returns the truncated integer part of the square root of $integer$ as $sqrt$ and the
|
476
|
+
remainder, $integer - sqrt * sqrt$, as $rem$, which will be zero if $integer$ is a
|
477
|
+
perfect square.
|
478
|
+
}
|
479
|
+
\end{tabular}
|
480
|
+
\newline\newline
|
481
|
+
|
482
|
+
\begin{tabular}{p{\methwidth} l r}
|
483
|
+
\toprule
|
484
|
+
\textbf{power?} & & \textit{integer}.power? $\rightarrow$ \textit{true} \textbar\ \textit{false} \\
|
485
|
+
\cmidrule(r){2-3}
|
486
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
487
|
+
Returns true if $integer$ is a perfect power, i.e., if there exist integers $a$ and
|
488
|
+
$b$, with $b > 1$, such that $integer$ equals $a$ raised to the power $b$.
|
489
|
+
\newline\newline
|
490
|
+
Under this definition both 0 and 1 are considered to be perfect powers. Negative values
|
491
|
+
of integers are accepted, but of course can only be odd perfect powers.
|
492
|
+
}
|
493
|
+
\end{tabular}
|
494
|
+
\newline\newline
|
495
|
+
|
496
|
+
\begin{tabular}{p{\methwidth} l r}
|
497
|
+
\toprule
|
498
|
+
\textbf{square?} & & \textit{integer}.square? $\rightarrow$ \textit{true} \textbar\ \textit{false} \\
|
499
|
+
\cmidrule(r){2-3}
|
500
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
501
|
+
Returns true if $integer$ is a perfect square, i.e., if the square root of
|
502
|
+
$integer$ is an integer. Under this definition both 0 and 1 are considered to be
|
503
|
+
perfect squares.
|
481
504
|
}
|
482
505
|
\end{tabular}
|
483
506
|
|
@@ -489,7 +512,98 @@ There is also a convenience method available, \texttt{GMP::Z()}.\\
|
|
489
512
|
& & \textit{integer}.pow(\textit{numeric}) $\rightarrow$ \textit{numeric} \\
|
490
513
|
\cmidrule(r){2-3}
|
491
514
|
& \multicolumn{2}{p{\defnwidth}}{
|
492
|
-
Returns
|
515
|
+
Returns $integer$ raised to the $numeric$ power.
|
516
|
+
}
|
517
|
+
\end{tabular}
|
518
|
+
\newline\newline
|
519
|
+
|
520
|
+
\begin{tabular}{p{\methwidth} l r}
|
521
|
+
\toprule
|
522
|
+
\textbf{powmod} & & \textit{integer}.powmod(\textit{exp}, \textit{mod}) $\rightarrow$ \textit{integer} \\
|
523
|
+
\cmidrule(r){2-3}
|
524
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
525
|
+
Returns $integer$ raised to the $exp$ power, modulo $mod$. Negative $exp$ is supported
|
526
|
+
if an inverse, $integer^{-1}$ modulo $mod$, exists. If an inverse doesn't exist then a
|
527
|
+
divide by zero exception is raised.
|
528
|
+
}
|
529
|
+
\end{tabular}
|
530
|
+
|
531
|
+
\subsection{Number Theoretic Functions}
|
532
|
+
|
533
|
+
\begin{tabular}{p{\methwidth} l r}
|
534
|
+
\toprule
|
535
|
+
\textbf{is\_probab\_prime?} & & $integer$.is\_probab\_prime?($reps = 5$) $\rightarrow$ 0, 1, or 2 \\
|
536
|
+
\cmidrule(r){2-3}
|
537
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
538
|
+
Determine whether $integer$ is prime. Returns 2 if $integer$ is definitely prime,
|
539
|
+
returns 1 if $integer$ is probably prime (without being certain), or returns 0 if
|
540
|
+
$integer$ is definitely composite.
|
541
|
+
\newline\newline
|
542
|
+
This function does some trial divisions, then some Miller-Rabin probabilistic primality
|
543
|
+
tests. $reps$ controls how many such tests are done, 5 to 10 is a reasonable number,
|
544
|
+
more will reduce the chances of a composite being returned as �probably prime�.
|
545
|
+
\newline\newline
|
546
|
+
Miller-Rabin and similar tests can be more properly called compositeness tests. Numbers
|
547
|
+
which fail are known to be composite but those which pass might be prime or might be
|
548
|
+
composite. Only a few composites pass, hence those which pass are considered probably
|
549
|
+
prime.
|
550
|
+
}
|
551
|
+
\end{tabular}
|
552
|
+
\newline\newline
|
553
|
+
|
554
|
+
\begin{tabular}{p{\methwidth} l r}
|
555
|
+
\toprule
|
556
|
+
\textbf{next\_prime} & & $integer$.next\_prime $\rightarrow$ $prime$ \\
|
557
|
+
& & $integer$.nextprime $\rightarrow$ $prime$ \\
|
558
|
+
& & $integer$.next\_prime! $\rightarrow$ $prime$ \\
|
559
|
+
& & $integer$.nextprime! $\rightarrow$ $prime$ \\
|
560
|
+
\cmidrule(r){2-3}
|
561
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
562
|
+
Returns the next prime greater than $integer$. The destructive method sets $integer$ to
|
563
|
+
the next prime greater than $integer$.
|
564
|
+
\newline\newline
|
565
|
+
This function uses a probabilistic algorithm to identify primes. For practical purposes
|
566
|
+
it's adequate, the chance of a composite passing will be extremely small.
|
567
|
+
}
|
568
|
+
\end{tabular}
|
569
|
+
\newline\newline
|
570
|
+
|
571
|
+
\begin{tabular}{p{\methwidth} l r}
|
572
|
+
\toprule
|
573
|
+
\textbf{jacobi} & & $a$.jacobi($b$) $\rightarrow$ $integer$ \\
|
574
|
+
& & GMP::Z.jacobi($a$, $b$) $\rightarrow$ $integer$ \\
|
575
|
+
\cmidrule(r){2-3}
|
576
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
577
|
+
Returns the Jacobi symbol $(a/b)$. This is defined only for $b$ odd. If $b$ is even, a
|
578
|
+
range exception will be raised.
|
579
|
+
\newline\newline
|
580
|
+
$GMP::Z.jacobi$ (the instance method) requires $b$ to be an instance of $GMP::Z$.
|
581
|
+
\newline
|
582
|
+
$GMP::Z\#jacobi$ (the class method) requires $a$ and $b$ each to be an instance of
|
583
|
+
$GMP::Z$, $Fixnum$, or $Bignum$.
|
584
|
+
}
|
585
|
+
\end{tabular}
|
586
|
+
\newline\newline
|
587
|
+
|
588
|
+
\begin{tabular}{p{\methwidth} l r}
|
589
|
+
\toprule
|
590
|
+
\textbf{legendre} & & $a$.legendre($b$) $\rightarrow$ $integer$ \\
|
591
|
+
\cmidrule(r){2-3}
|
592
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
593
|
+
Returns the Legendre symbol $(a/b)$. This is defined only for $p$ an odd positive
|
594
|
+
prime. If $p$ is even, negative, or composite, a range exception will be raised.
|
595
|
+
}
|
596
|
+
\end{tabular}
|
597
|
+
\newline\newline
|
598
|
+
|
599
|
+
\begin{tabular}{p{\methwidth} l r}
|
600
|
+
\toprule
|
601
|
+
\textbf{remove} & & $n$.remove($factor$) $\rightarrow$ ($integer$, $times$) \\
|
602
|
+
\cmidrule(r){2-3}
|
603
|
+
& \multicolumn{2}{p{\defnwidth}}{
|
604
|
+
Remove all occurrences of the factor $factor$ from $n$. $factor$ can be an instance of
|
605
|
+
$GMP::Z$, $Fixnum$, or $Bignum$. $integer$ is the resulting integer, an instance of
|
606
|
+
$GMP::Z$. $times$ is how many times $factor$ was removed, a $Fixnum$.
|
493
607
|
}
|
494
608
|
\end{tabular}
|
495
609
|
|
data/test/README
CHANGED
@@ -41,11 +41,11 @@ class TC_fib_fac_nextprime < Test::Unit::TestCase
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_nextprime
|
44
|
-
assert_equal(@z11, @z7.
|
45
|
-
assert_equal(@z11, @z8.nextprime,
|
46
|
-
assert_equal(@z11, @z8.
|
47
|
-
assert_equal(@z13, @z11.nextprime,
|
48
|
-
assert_equal(@z17, @z13.
|
49
|
-
assert_equal(@z19, @z17.nextprime,
|
44
|
+
assert_equal(@z11, @z7.next_prime, "GMP::Z.nextprime should work.")
|
45
|
+
assert_equal(@z11, @z8.nextprime, "GMP::Z.nextprime should work.")
|
46
|
+
assert_equal(@z11, @z8.next_prime, "GMP::Z.nextprime should work.")
|
47
|
+
assert_equal(@z13, @z11.nextprime, "GMP::Z.nextprime should work.")
|
48
|
+
assert_equal(@z17, @z13.next_prime, "GMP::Z.nextprime should work.")
|
49
|
+
assert_equal(@z19, @z17.nextprime, "GMP::Z.nextprime should work.")
|
50
50
|
end
|
51
51
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TC_Z_Jacobi_Legendre_Remove < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@one = GMP::Z(1)
|
6
|
+
@two = GMP::Z(2)
|
7
|
+
@three = GMP::Z(3)
|
8
|
+
@four = GMP::Z(4)
|
9
|
+
@five = GMP::Z(5)
|
10
|
+
@nine = GMP::Z(9)
|
11
|
+
@forty_five = GMP::Z(45)
|
12
|
+
@neg_one = GMP::Z(-1)
|
13
|
+
end
|
14
|
+
|
15
|
+
# I don't actually know the Jacobi symbol, so these are just from Wolfram Alpha.
|
16
|
+
def test_jacobi
|
17
|
+
assert_equal(@one.jacobi(@one), 1, "GMP::Z should jacobi(GMP::Z) correctly")
|
18
|
+
assert_equal(@two.jacobi(@one), 1, "GMP::Z should jacobi(GMP::Z) correctly")
|
19
|
+
assert_equal(@forty_five.jacobi(@one), 1, "GMP::Z should jacobi(GMP::Z) correctly")
|
20
|
+
assert_equal(@one.jacobi(@three), 1, "GMP::Z should jacobi(GMP::Z) correctly")
|
21
|
+
assert_equal(@two.jacobi(@three), -1, "GMP::Z should jacobi(GMP::Z) correctly")
|
22
|
+
assert_equal(@three.jacobi(@three), 0, "GMP::Z should jacobi(GMP::Z) correctly")
|
23
|
+
assert_equal(@forty_five.jacobi(@three), 0, "GMP::Z should jacobi(GMP::Z) correctly")
|
24
|
+
|
25
|
+
assert_equal(GMP::Z.jacobi(@one, @one), 1, "GMP::Z should jacobi(GMP::Z, GMP::Z) correctly")
|
26
|
+
assert_equal(GMP::Z.jacobi(@four, @one), 1, "GMP::Z should jacobi(GMP::Z, GMP::Z) correctly")
|
27
|
+
assert_equal(GMP::Z.jacobi(@two, @three), -1, "GMP::Z should jacobi(GMP::Z, GMP::Z) correctly")
|
28
|
+
assert_equal(GMP::Z.jacobi(@three, @three), 0, "GMP::Z should jacobi(GMP::Z, GMP::Z) correctly")
|
29
|
+
|
30
|
+
assert_equal(GMP::Z.jacobi(1, @one), 1, "GMP::Z should jacobi(Fixnum, GMP::Z) correctly")
|
31
|
+
assert_equal(GMP::Z.jacobi(4, @one), 1, "GMP::Z should jacobi(Fixnum, GMP::Z) correctly")
|
32
|
+
assert_equal(GMP::Z.jacobi(2, @three), -1, "GMP::Z should jacobi(Fixnum, GMP::Z) correctly")
|
33
|
+
assert_equal(GMP::Z.jacobi(3, @three), 0, "GMP::Z should jacobi(Fixnum, GMP::Z) correctly")
|
34
|
+
|
35
|
+
assert_equal(GMP::Z.jacobi(@one, 1), 1, "GMP::Z should jacobi(GMP::Z, Fixnum) correctly")
|
36
|
+
assert_equal(GMP::Z.jacobi(@four, 1), 1, "GMP::Z should jacobi(GMP::Z, Fixnum) correctly")
|
37
|
+
assert_equal(GMP::Z.jacobi(@two, 3), -1, "GMP::Z should jacobi(GMP::Z, Fixnum) correctly")
|
38
|
+
assert_equal(GMP::Z.jacobi(@three, 3), 0, "GMP::Z should jacobi(GMP::Z, Fixnum) correctly")
|
39
|
+
|
40
|
+
assert_equal(GMP::Z.jacobi(1, 1), 1, "GMP::Z should jacobi(Fixnum, Fixnum) correctly")
|
41
|
+
assert_equal(GMP::Z.jacobi(4, 1), 1, "GMP::Z should jacobi(Fixnum, Fixnum) correctly")
|
42
|
+
assert_equal(GMP::Z.jacobi(2, 3), -1, "GMP::Z should jacobi(Fixnum, Fixnum) correctly")
|
43
|
+
assert_equal(GMP::Z.jacobi(3, 3), 0, "GMP::Z should jacobi(Fixnum, Fixnum) correctly")
|
44
|
+
|
45
|
+
assert_raise(RangeError) { @two.jacobi(@two) }
|
46
|
+
assert_raise(RangeError) { @three.jacobi(@neg_one) }
|
47
|
+
assert_raise(RangeError) { GMP::Z.jacobi(@two, @two) }
|
48
|
+
assert_raise(RangeError) { GMP::Z.jacobi(@three, @neg_one) }
|
49
|
+
assert_raise(RangeError) { GMP::Z.jacobi(@two, 2) }
|
50
|
+
assert_raise(RangeError) { GMP::Z.jacobi(3, @neg_one) }
|
51
|
+
assert_raise(RangeError) { GMP::Z.jacobi(3, -1) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_legendre
|
55
|
+
assert_equal(@one.legendre(@three), 1, "GMP::Z should legendre(GMP::Z) correctly")
|
56
|
+
assert_equal(@two.legendre(@three), -1, "GMP::Z should legendre(GMP::Z) correctly")
|
57
|
+
|
58
|
+
assert_raise(RangeError) { @one.legendre(@one) }
|
59
|
+
assert_raise(RangeError) { @one.legendre(@two) }
|
60
|
+
assert_raise(RangeError) { @one.legendre(@neg_one) }
|
61
|
+
assert_raise(RangeError) { @one.legendre(@four) }
|
62
|
+
assert_raise(RangeError) { @one.legendre(@nine) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_remove
|
66
|
+
assert_equal(@nine, @forty_five.remove(5)[0], "GMP::Z should remove(GMP::Z) correctly.")
|
67
|
+
assert_equal(@nine, @forty_five.remove(@five)[0], "GMP::Z should remove(GMP::Z) correctly.")
|
68
|
+
assert_equal(@five, @forty_five.remove(@three)[0], "GMP::Z should remove(GMP::Z) correctly.")
|
69
|
+
assert_equal(1, @forty_five.remove(5)[1], "GMP::Z should remove(GMP::Z) correctly.")
|
70
|
+
assert_equal(1, @forty_five.remove(@five)[1], "GMP::Z should remove(GMP::Z) correctly.")
|
71
|
+
assert_equal(2, @forty_five.remove(@three)[1], "GMP::Z should remove(GMP::Z) correctly.")
|
72
|
+
end
|
73
|
+
end
|