Documentation style changes
Fixes after merge weirdness from pull request (specifically, removed `bfrandom' from "mpfr.rkt" again) Removed dependence of math/flonum on math/bigfloat (better build parallelization) Changed `divides?' to return #f when its first argument is 0 Made return type of `quadratic-character' more precise Made argument types more permissive: * second argument to `solve-chinese' * second argument to `next-primes' * second argument to `prev-primes'
This commit is contained in:
parent
db500e8b58
commit
1e52736089
|
@ -137,6 +137,15 @@
|
|||
(define bits (bf-precision))
|
||||
(bf (random-bits bits) (- bits)))
|
||||
|
||||
(: bigfloat->fl2 (Bigfloat -> (Values Flonum Flonum)))
|
||||
(define (bigfloat->fl2 x)
|
||||
(define x2 (bigfloat->flonum x))
|
||||
(values x2 (bigfloat->flonum (bf- x (flonum->bigfloat x2)))))
|
||||
|
||||
(: fl2->bigfloat (Flonum Flonum -> Bigfloat))
|
||||
(define (fl2->bigfloat x2 x1)
|
||||
(bf+ (flonum->bigfloat x1) (flonum->bigfloat x2)))
|
||||
|
||||
(provide
|
||||
;; Library stuffs
|
||||
mpfr-available?
|
||||
|
@ -159,10 +168,12 @@
|
|||
integer->bigfloat
|
||||
rational->bigfloat
|
||||
real->bigfloat
|
||||
fl2->bigfloat
|
||||
bigfloat->flonum
|
||||
bigfloat->integer
|
||||
bigfloat->rational
|
||||
bigfloat->real
|
||||
bigfloat->fl2
|
||||
;; String conversion
|
||||
bigfloat->string
|
||||
string->bigfloat
|
||||
|
|
|
@ -1001,24 +1001,6 @@
|
|||
(set! 0ary-funs (list* #'phi.bf #'epsilon.bf #'-max.bf #'-min.bf #'+min.bf #'+max.bf
|
||||
0ary-funs)))
|
||||
|
||||
;; ===================================================================================================
|
||||
;; Extra functions
|
||||
|
||||
(define (random-bits bits)
|
||||
(let loop ([bits bits] [acc 0])
|
||||
(cond [(= 0 bits) acc]
|
||||
[else
|
||||
(define new-bits (min 24 bits))
|
||||
(loop (- bits new-bits)
|
||||
(bitwise-ior (random (arithmetic-shift 1 new-bits))
|
||||
(arithmetic-shift acc new-bits)))])))
|
||||
|
||||
(define (bfrandom)
|
||||
(define bits (bf-precision))
|
||||
(bf (random-bits bits) (- bits)))
|
||||
|
||||
(provide bfrandom)
|
||||
|
||||
;; ===================================================================================================
|
||||
;; Number Theoretic Functions
|
||||
;; http://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions
|
||||
|
|
|
@ -9,10 +9,9 @@ Discrete & Computational Geometry 18(3):305–363, October 1997
|
|||
|#
|
||||
|
||||
(require "../flonum-functions.rkt"
|
||||
"../flonum-syntax.rkt"
|
||||
"../../bigfloat/bigfloat-struct.rkt")
|
||||
"../flonum-syntax.rkt")
|
||||
|
||||
(provide fl2 bigfloat->fl2 fl2->real fl2->bigfloat
|
||||
(provide fl2 fl2->real
|
||||
fl2+ fl2- fl2*split-fl fl2* fl2/
|
||||
flsqrt/error fl2sqrt)
|
||||
|
||||
|
@ -31,11 +30,6 @@ Discrete & Computational Geometry 18(3):305–363, October 1997
|
|||
[(x y)
|
||||
(fast-fl+/error x y)]))
|
||||
|
||||
(: bigfloat->fl2 (Bigfloat -> (Values Flonum Flonum)))
|
||||
(define (bigfloat->fl2 x)
|
||||
(define x2 (bigfloat->flonum x))
|
||||
(values x2 (bigfloat->flonum (bf- x (bf x2)))))
|
||||
|
||||
(: fl2->real (Flonum Flonum -> Real))
|
||||
(define (fl2->real x2 x1)
|
||||
(cond [(and (x1 . fl> . -inf.0) (x1 . fl< . +inf.0)
|
||||
|
@ -43,10 +37,6 @@ Discrete & Computational Geometry 18(3):305–363, October 1997
|
|||
(+ (inexact->exact x2) (inexact->exact x1))]
|
||||
[else (fl+ x1 x2)]))
|
||||
|
||||
(: fl2->bigfloat (Flonum Flonum -> Bigfloat))
|
||||
(define (fl2->bigfloat x2 x1)
|
||||
(bf+ (bf x1) (bf x2)))
|
||||
|
||||
(: fl3->fl2 (Flonum Flonum Flonum -> (Values Flonum Flonum)))
|
||||
(define (fl3->fl2 e3 e2 e1)
|
||||
(values e3 (fl+ e2 e1)))
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
;;;
|
||||
|
||||
(: divides? : Integer Integer -> Boolean)
|
||||
; For b<>0: ( a divides b <=> exists k s.t. a*k=b )
|
||||
; a divides b <=> exists unique k s.t. a*k=b
|
||||
(define (divides? a b)
|
||||
(= (remainder b a) 0))
|
||||
(cond [(zero? a) #f]
|
||||
[else (= (remainder b a) 0)]))
|
||||
|
||||
; DEF (Coprime, relatively prime)
|
||||
; Two or more integers are called coprime, if their greatest common divisor is 1.
|
||||
|
|
|
@ -23,8 +23,12 @@
|
|||
|
||||
(provide (all-defined-out))
|
||||
|
||||
(: current-modulus-param (Parameterof Positive-Integer))
|
||||
(define current-modulus-param (make-parameter 1))
|
||||
(: current-modulus-param (Parameterof Integer Positive-Integer))
|
||||
(define current-modulus-param
|
||||
(make-parameter
|
||||
1 (λ: ([n : Integer])
|
||||
(cond [(n . <= . 0) (raise-argument-error 'with-modulus "Positive-Integer" n)]
|
||||
[else n]))))
|
||||
|
||||
(: current-modulus (-> Positive-Integer))
|
||||
(begin-encourage-inline
|
||||
|
|
|
@ -107,8 +107,10 @@
|
|||
|
||||
; Example : (solve-chinese '(2 3 2) '(3 5 7)) = 23
|
||||
|
||||
(: solve-chinese : Zs (Listof N+) -> N)
|
||||
(: solve-chinese : Zs (Listof Z) -> N)
|
||||
(define (solve-chinese as ns)
|
||||
(unless (andmap positive? ns)
|
||||
(raise-argument-error 'solve-chinese "(Listof Positive-Integer)" 1 as ns))
|
||||
; the ns should be coprime
|
||||
(let* ([n (apply * ns)]
|
||||
[cs (map (λ: ([ni : Z]) (quotient n ni)) ns)]
|
||||
|
@ -245,35 +247,43 @@
|
|||
(prev-prime n-2)))]))
|
||||
|
||||
|
||||
(: next-primes : Z N -> Zs)
|
||||
(: next-primes : Z Z -> Zs)
|
||||
(define (next-primes m primes-wanted)
|
||||
(: loop : Z Z -> Zs)
|
||||
(define (loop n primes-wanted)
|
||||
(if (= primes-wanted 0)
|
||||
'()
|
||||
(let ([next (next-prime n)])
|
||||
(if next
|
||||
(cons next (loop next (sub1 primes-wanted)))
|
||||
'()))))
|
||||
(loop m primes-wanted))
|
||||
(cond
|
||||
[(primes-wanted . < . 0) (raise-argument-error 'next-primes "Natural" 1 m primes-wanted)]
|
||||
[else
|
||||
(: loop : Z Z -> Zs)
|
||||
(define (loop n primes-wanted)
|
||||
(if (= primes-wanted 0)
|
||||
'()
|
||||
(let ([next (next-prime n)])
|
||||
(if next
|
||||
(cons next (loop next (sub1 primes-wanted)))
|
||||
'()))))
|
||||
(loop m primes-wanted)]))
|
||||
|
||||
(: prev-primes : Z N -> Zs)
|
||||
(: prev-primes : Z Z -> Zs)
|
||||
(define (prev-primes m primes-wanted)
|
||||
(: loop : Z Z -> Zs)
|
||||
(define (loop n primes-wanted)
|
||||
(if (= primes-wanted 0)
|
||||
'()
|
||||
(let ([prev (prev-prime n)])
|
||||
(if prev
|
||||
(cons prev (loop prev (sub1 primes-wanted)))
|
||||
'()))))
|
||||
(loop m primes-wanted))
|
||||
(cond
|
||||
[(primes-wanted . < . 0) (raise-argument-error 'prev-primes "Natural" 1 m primes-wanted)]
|
||||
[else
|
||||
(: loop : Z Z -> Zs)
|
||||
(define (loop n primes-wanted)
|
||||
(if (= primes-wanted 0)
|
||||
'()
|
||||
(let ([prev (prev-prime n)])
|
||||
(if prev
|
||||
(cons prev (loop prev (sub1 primes-wanted)))
|
||||
'()))))
|
||||
(loop m primes-wanted)]))
|
||||
|
||||
|
||||
(: nth-prime : N -> Prime)
|
||||
(: nth-prime : Z -> Prime)
|
||||
(define (nth-prime n)
|
||||
(for/fold: ([p : Prime 2]) ([m (in-range n)])
|
||||
(next-prime p)))
|
||||
(cond [(n . < . 0) (raise-argument-error 'nth-prime "Natural" n)]
|
||||
[else
|
||||
(for/fold: ([p : Prime 2]) ([m (in-range n)])
|
||||
(next-prime p))]))
|
||||
|
||||
|
||||
;;;
|
||||
|
|
|
@ -13,12 +13,13 @@
|
|||
; The number s is called a squre root of a modulo n.
|
||||
|
||||
; p is prime
|
||||
(: quadratic-character : Integer Integer -> Integer)
|
||||
(: quadratic-character : Integer Integer -> (U -1 0 1))
|
||||
(define (quadratic-character a p)
|
||||
(cond [(a . < . 0) (raise-argument-error 'quadratic-character "Natural" 0 a p)]
|
||||
[(p . <= . 0) (raise-argument-error 'quadratic-character "Positive-Integer" 1 a p)]
|
||||
[else (let ([l (modular-expt a (quotient (- p 1) 2) p)])
|
||||
(if (<= 0 l 1) l -1))]))
|
||||
(cond [(or (eqv? l 0) (eqv? l 1)) l]
|
||||
[else -1]))]))
|
||||
|
||||
(: quadratic-residue? : Integer Integer -> Boolean)
|
||||
(define (quadratic-residue? a n)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user