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:
Neil Toronto 2012-11-17 22:58:49 -07:00
parent db500e8b58
commit 1e52736089
8 changed files with 465 additions and 356 deletions

View File

@ -137,6 +137,15 @@
(define bits (bf-precision)) (define bits (bf-precision))
(bf (random-bits bits) (- bits))) (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 (provide
;; Library stuffs ;; Library stuffs
mpfr-available? mpfr-available?
@ -159,10 +168,12 @@
integer->bigfloat integer->bigfloat
rational->bigfloat rational->bigfloat
real->bigfloat real->bigfloat
fl2->bigfloat
bigfloat->flonum bigfloat->flonum
bigfloat->integer bigfloat->integer
bigfloat->rational bigfloat->rational
bigfloat->real bigfloat->real
bigfloat->fl2
;; String conversion ;; String conversion
bigfloat->string bigfloat->string
string->bigfloat string->bigfloat

View File

@ -1001,24 +1001,6 @@
(set! 0ary-funs (list* #'phi.bf #'epsilon.bf #'-max.bf #'-min.bf #'+min.bf #'+max.bf (set! 0ary-funs (list* #'phi.bf #'epsilon.bf #'-max.bf #'-min.bf #'+min.bf #'+max.bf
0ary-funs))) 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 ;; Number Theoretic Functions
;; http://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions ;; http://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions

View File

@ -9,10 +9,9 @@ Discrete & Computational Geometry 18(3):305363, October 1997
|# |#
(require "../flonum-functions.rkt" (require "../flonum-functions.rkt"
"../flonum-syntax.rkt" "../flonum-syntax.rkt")
"../../bigfloat/bigfloat-struct.rkt")
(provide fl2 bigfloat->fl2 fl2->real fl2->bigfloat (provide fl2 fl2->real
fl2+ fl2- fl2*split-fl fl2* fl2/ fl2+ fl2- fl2*split-fl fl2* fl2/
flsqrt/error fl2sqrt) flsqrt/error fl2sqrt)
@ -31,11 +30,6 @@ Discrete & Computational Geometry 18(3):305363, October 1997
[(x y) [(x y)
(fast-fl+/error 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)) (: fl2->real (Flonum Flonum -> Real))
(define (fl2->real x2 x1) (define (fl2->real x2 x1)
(cond [(and (x1 . fl> . -inf.0) (x1 . fl< . +inf.0) (cond [(and (x1 . fl> . -inf.0) (x1 . fl< . +inf.0)
@ -43,10 +37,6 @@ Discrete & Computational Geometry 18(3):305363, October 1997
(+ (inexact->exact x2) (inexact->exact x1))] (+ (inexact->exact x2) (inexact->exact x1))]
[else (fl+ x1 x2)])) [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))) (: fl3->fl2 (Flonum Flonum Flonum -> (Values Flonum Flonum)))
(define (fl3->fl2 e3 e2 e1) (define (fl3->fl2 e3 e2 e1)
(values e3 (fl+ e2 e1))) (values e3 (fl+ e2 e1)))

View File

@ -7,9 +7,10 @@
;;; ;;;
(: divides? : Integer Integer -> Boolean) (: 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) (define (divides? a b)
(= (remainder b a) 0)) (cond [(zero? a) #f]
[else (= (remainder b a) 0)]))
; DEF (Coprime, relatively prime) ; DEF (Coprime, relatively prime)
; Two or more integers are called coprime, if their greatest common divisor is 1. ; Two or more integers are called coprime, if their greatest common divisor is 1.

View File

@ -23,8 +23,12 @@
(provide (all-defined-out)) (provide (all-defined-out))
(: current-modulus-param (Parameterof Positive-Integer)) (: current-modulus-param (Parameterof Integer Positive-Integer))
(define current-modulus-param (make-parameter 1)) (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)) (: current-modulus (-> Positive-Integer))
(begin-encourage-inline (begin-encourage-inline

View File

@ -107,8 +107,10 @@
; Example : (solve-chinese '(2 3 2) '(3 5 7)) = 23 ; 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) (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 ; the ns should be coprime
(let* ([n (apply * ns)] (let* ([n (apply * ns)]
[cs (map (λ: ([ni : Z]) (quotient n ni)) ns)] [cs (map (λ: ([ni : Z]) (quotient n ni)) ns)]
@ -245,35 +247,43 @@
(prev-prime n-2)))])) (prev-prime n-2)))]))
(: next-primes : Z N -> Zs) (: next-primes : Z Z -> Zs)
(define (next-primes m primes-wanted) (define (next-primes m primes-wanted)
(: loop : Z Z -> Zs) (cond
(define (loop n primes-wanted) [(primes-wanted . < . 0) (raise-argument-error 'next-primes "Natural" 1 m primes-wanted)]
(if (= primes-wanted 0) [else
'() (: loop : Z Z -> Zs)
(let ([next (next-prime n)]) (define (loop n primes-wanted)
(if next (if (= primes-wanted 0)
(cons next (loop next (sub1 primes-wanted))) '()
'())))) (let ([next (next-prime n)])
(loop m primes-wanted)) (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) (define (prev-primes m primes-wanted)
(: loop : Z Z -> Zs) (cond
(define (loop n primes-wanted) [(primes-wanted . < . 0) (raise-argument-error 'prev-primes "Natural" 1 m primes-wanted)]
(if (= primes-wanted 0) [else
'() (: loop : Z Z -> Zs)
(let ([prev (prev-prime n)]) (define (loop n primes-wanted)
(if prev (if (= primes-wanted 0)
(cons prev (loop prev (sub1 primes-wanted))) '()
'())))) (let ([prev (prev-prime n)])
(loop m primes-wanted)) (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) (define (nth-prime n)
(for/fold: ([p : Prime 2]) ([m (in-range n)]) (cond [(n . < . 0) (raise-argument-error 'nth-prime "Natural" n)]
(next-prime p))) [else
(for/fold: ([p : Prime 2]) ([m (in-range n)])
(next-prime p))]))
;;; ;;;

View File

@ -13,12 +13,13 @@
; The number s is called a squre root of a modulo n. ; The number s is called a squre root of a modulo n.
; p is prime ; p is prime
(: quadratic-character : Integer Integer -> Integer) (: quadratic-character : Integer Integer -> (U -1 0 1))
(define (quadratic-character a p) (define (quadratic-character a p)
(cond [(a . < . 0) (raise-argument-error 'quadratic-character "Natural" 0 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)] [(p . <= . 0) (raise-argument-error 'quadratic-character "Positive-Integer" 1 a p)]
[else (let ([l (modular-expt a (quotient (- p 1) 2) 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) (: quadratic-residue? : Integer Integer -> Boolean)
(define (quadratic-residue? a n) (define (quadratic-residue? a n)

File diff suppressed because it is too large Load Diff