;;;; math-utils-test.scm -*- Scheme -*- ;;;; Kon Lovett, Oct '22 (import test) (test-begin "Math Utils") ;;; (import math-utils) ;; (define (fibonacci/naive n) (case n ((0) 0) ((1) 1) (else (+ (fibonacci/naive (- n 1)) (fibonacci/naive (- n 2)))) ) ) (test-group "seqs" (test 2 (average 1 3)) (test 3 (average 1 3 5)) (test 3 (average '(1 3 5))) (test "true fib" (fibonacci/naive 10) (fibonacci 10)) (test "approx fib" (fibonacci/naive 10) (inexact->exact (floor (fibonacci* 10)))) (test 3628800 (factorial 10)) #; (factorial- a fall) #; (factorial+ a rise) (test 137/60 (harmonic 4)) ) (define (approx-points b e pts) (map (lambda (c) (cons (exact->inexact (car c)) (exact->inexact (round (+ (* b (car c)) e))))) pts)) (test-group "stats" (define area-3ln (trapezoid (lambda (x) (* 3 (log x))) 3 4)) (define some-pts '((0 . 0) (1 . 2) (2 . 4) (3 . 8) (4 . 16))) (define data-pts '((1.47 . 52.21) (1.50 . 53.12) (1.52 . 54.48) (1.55 . 55.84) (1.57 . 57.20) (1.60 . 58.57) (1.63 . 59.93) (1.65 . 61.29) (1.68 . 63.11) (1.70 . 64.47) (1.73 . 66.28) (1.75 . 68.10) (1.78 . 69.92) (1.80 . 72.19) (1.83 . 74.46))) (define data-pts/exact (map (lambda (x) (cons (inexact->exact (car x)) (inexact->exact (cdr x)))) data-pts)) (test-assert (procedure? area-3ln)) (test 3.74781341992827 (area-3ln 10)) (test 3.74802173521736 (area-3ln 10000)) (test 9/2 (average 4 5)) (test 9/2 (average '(4 5))) (test 9/2 (average 4 5 4 5)) (test '(19/5 -8/5) (receive (least-squares some-pts))) ;exact since do not need fp~= (test '(1702812740636016600843530210081488/27790957638923528730017463330997 -305560554657646811776139815548877388711888796291/7822459154182827706963815203832551052025004032) (receive (least-squares data-pts/exact))) #; (print (approx-points 1702812740636016600843530210081488/27790957638923528730017463330997 -305560554657646811776139815548877388711888796291/7822459154182827706963815203832551052025004032 data-pts/exact)) #; (cross-ratio a b c d) (test 70 (binomial 8 4)) ) (test-group "interest" (test 1938.84 (compound-interest 0.043 4 6 1500)) ) (test-group "log-with-base" (define log256 (log-with-base 256)) (test-assert (procedure? log256)) (test "log/base 256" 9 (inexact->exact (round (log256 995972255318008407568)))) ) (test-group "coprime" (test-assert (coprime? 2 1)) (test-assert (coprime? 3 1)) (test-assert (coprime? 81 19)) (test-assert (coprime? 1 1)) (test-assert (not (coprime? 9 3))) (test-assert (coprime? (* 3 7) (* 5 19))) (test-assert (not (coprime? (* 3 7 5) (* 5 19 2)))) (test-assert (coprime? 6 10 15)) ) (test-group "pairwise coprime" (test-assert (pairwise-coprime? 10 7 33 13)) (test-assert (not (pairwise-coprime? 10 7 33 14))) (test-assert (not (pairwise-coprime? 6 10 15))) ) (test-group "*coprime" (test-assert (fxcoprime? 2 1)) (test-assert (fxcoprime? 3 1)) (test-assert (fxcoprime? 81 19)) (test-assert (fxcoprime? 1 1)) (test-assert (not (fxcoprime? 9 3))) (test-assert (fxcoprime? (* 3 7) (* 5 19))) (test-assert (not (fxcoprime? (* 3 7 5) (* 5 19 2)))) ) (test-group "to-places" (test 3.141 (to-places 3 (truncate (acos -1)))) (test 3.0 (to-places 0 (truncate (acos -1)))) (test 3.14159 (to-places 5 (truncate (acos -1)))) ) ;;; (test-end "Math Utils") (test-exit)