;;;; math-utils-vector-test.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '23 (import test) (import (only (chicken format) format) (test-utils gloss)) ;;; (test-begin "Math Utils Vector") (import (math-utils vector)) ;;; (import (chicken string)) (import (chicken fixnum)) (import (chicken random)) (import (only vector-lib vector-unfold)) ;; ;https://en.wikipedia.org/wiki/Cosine_similarity (define (cosine-similarity/reference a b) (/ (dot-product a b) (* (absolute-magnitude a) (absolute-magnitude b))) ) #; (repeat-random (100) (let*-random ((len 1000) (a (vector-of real len)) (b (vector-of real len)) ) (test "\"fast\" cosine-similarity eqv cosine-similarity reference" (cosine-similarity/reference a b) (cosine-similarity a b)) ) ) (define (make-random-real-vector len #!optional (zeros? #f) (rnd pseudo-random-real)) (vector-unfold (lambda _ #;(i . seeds) (let loop ((trys 10)) (let ((n (rnd))) (if (or (not (zero? n)) zeros?) n (if (fx< trys 0) 1 (loop (fx- trys 1)))) ) ) ) len) ) ;; (test-group "cosine-similarity \"fast\" = reference w/ random length & real values" (let loop ((cnt (pseudo-random-integer 25))) (unless (fx= 0 cnt) (let* ((len (pseudo-random-integer 10000)) (a (make-random-real-vector len)) (b (make-random-real-vector len)) ) (test (conc "Length " len) (exact->inexact (cosine-similarity/reference a b)) (exact->inexact (cosine-similarity a b))) (loop (fx- cnt 1)) ) ) ) ) (test-group "operations" (test 10 (dot-product #(1 2 3) #(3 2 1))) (test 3 (cross-product #(1) #(3))) (test -4 (cross-product #(1 2) #(3 2))) (test #(-4 8 -4) (cross-product #(1 2 3) #(3 2 1))) ) (test-group "vector apply ops" (test #(4 4 4) (vector-sum #(1 2 3) #(3 2 1))) (test #(3 4 3) (vector-mul #(1 2 3) #(3 2 1))) (test "cmp" #(-2 0 2) (vector-dif #(1 2 3) #(3 2 1))) ) (test-group "compare" (test "diff len" 1 (vector-compare #(1 2 3 4) #(3 2 1))) (test "<" -2 (vector-compare #(1 2 3) #(3 2 1))) (test "=" 0 (vector-compare #(3 2 1) #(3 2 1))) (test ">" 2 (vector-compare #(3 2 1) #(1 2 3))) (test "multi <" -5 (vector-compare #(-2 0 2) #(3 2 1) #(1 2 3))) (test "multi =" 0 (vector-compare #(1 2 3) #(1 2 3) #(1 2 3))) (test "multi >" 2 (vector-compare #(3 2 1) #(1 2 3) #(-2 0 2))) ) ;;; (test-end "Math Utils Vector") (test-exit)