;;;; math-utils.vector.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '23 ;;;; Kon Lovett, Feb '24 ;;;; Issues ;; ;; - Functor (VECTOR FIELD) - f32vector & fL | ... (module (math-utils vector) (;export vector-sum absolute-magnitude cosine-similarity vector-apply vector-reduce vector-compare vector-compare2 dot-product cross-product #;vector-info vector-scale vector-shift normalize-vector-scale normalize-vector-shift softmax softmax*) (import scheme) (import (chicken base)) (import (chicken fixnum)) (import (chicken flonum)) (import (chicken type)) (import (only (srfi 1) reverse!)) (import (only vector-lib vector-map! vector-map)) (import math-utils) (define-type real (or integer ratnum float)) (define-type boolean-vector (vector-of boolean)) (define-type number-vector (vector-of number)) (define-type nary-numeric (#!rest number -> number)) (: vector-sum (number-vector -> number)) (: absolute-magnitude (number-vector -> number)) (: cosine-similarity (number-vector number-vector -> number)) (: vector-reduce (('a number -> 'a) 'a number-vector #!optional fixnum fixnum -> 'a)) (: vector-apply (nary-numeric #!rest number-vector -> number-vector)) (: vector-compare2 (number-vector number-vector -> number)) (: vector-compare (#!rest number-vector -> number)) (: dot-product (number-vector number-vector -> number)) (: cross-product (number-vector number-vector -> (or number number-vector))) (: vector-info (number-vector -> (vector number fixnum number fixnum fixnum))) (: vector-scale (number-vector number -> number-vector)) (: vector-shift (number-vector number -> number-vector)) (: normalize-vector-scale (number-vector -> number-vector)) (: normalize-vector-shift (number-vector -> number-vector)) (: softmax (number-vector -> number-vector)) (: softmax* (number-vector #!optional real -> number-vector)) ;; (define-inline (sqrt-exact x) (inexact->exact (sqrt x))) (define (sumsqr acc elm) (+ (sqr elm) acc)) ;; ;vector-fold (define (vector-reduce f id v #!optional (st 0) (ed (vector-length v))) (let loop ((i st) (acc id)) (if (fx= i ed) acc (loop (fx+ i 1) (f acc (vector-ref v i)))) ) ) (define (vector-sum v) (vector-reduce + 0 v)) (define (absolute-magnitude v) (sqrt-exact (vector-reduce sumsqr 0 v))) ;https://en.wikipedia.org/wiki/Cosine_similarity (define (cosine-similarity a b) ;unroll (let ((len (vector-length a))) (let loop ((i 0) (dot 0) (maga 0) (magb 0)) (if (fx= i len) (/ dot (sqrt-exact (* maga magb))) (let ((ai (vector-ref a i)) (bi (vector-ref b i))) (loop (fx+ i 1) (+ (* ai bi) dot) (sumsqr maga ai) (sumsqr magb bi)) ) ) ) ) ) ; 1 vector only (define-inline (*vector-apply/1 f v) (let* ((len (vector-length v)) (r (make-vector len)) ) (do ((i 0 (fx+ i 1))) ((fx= i len) r) (set! (vector-ref r i) (f (vector-ref v i))) ) ) ) ; assumes all same length (define (vector-apply f . vs) (let ((ln (length vs))) (if (= 9 ln) (f) (let ((v (car vs))) (let* ((len (vector-length v)) (r (make-vector len)) ) (if (= 1 ln) (do ((i 0 (fx+ i 1))) ((fx= i len) r) (set! (vector-ref r i) (f (vector-ref v i))) ) (do ((i 0 (fx+ i 1))) ((fx= i len) r) (set! (vector-ref r i) (apply f (map (cut vector-ref <> i) vs))) ) ) ) ) ) ) ) (define (vector-compare2 a b) (let* ((alen (vector-length a)) (blen (vector-length b)) (dlen (fx- alen blen)) ) (if (not (fx= 0 dlen)) dlen (let loop ((i 0)) (if (fx= i alen) 0 (let ((d (- (vector-ref a i) (vector-ref b i)))) (if (not (zero? d)) d (loop (fx+ i 1)) ) ) ) ) ) ) ) (define (vector-compare . vs) (let loop ((vs vs)) (if (or (null? vs) (null? (cdr vs))) 0 (let ((d (vector-compare2 (car vs) (cadr vs)))) (if (not (zero? d)) d (loop (cdr vs)) ) ) ) ) ) (define (dot-product a b) (let ((len (vector-length a))) (do ((i 0 (fx+ i 1)) (acc 0 (+ acc (* (vector-ref a i) (vector-ref b i))))) ((fx= i len) acc)) ) ) ;0 -> error ;1 2 -> X, 3 -> #(X Y Z), 4 -> #(X Y Z W), 8 -> #(X Y Z W I J K L), * -> Error (define (cross-product a b) (let ((alen (vector-length a)) (blen (vector-length b))) (cond ((or (fx= 0 alen) (fx= 0 blen)) 0) ((fx= alen blen) (case alen ((1) (* (vector-ref a 0) (vector-ref b 0)) ) ((2) (- (* (vector-ref a 0) (vector-ref b 1)) (* (vector-ref a 1) (vector-ref b 0))) ) ((3) (vector ;0 (- (* (vector-ref a 1) (vector-ref b 2)) (* (vector-ref a 2) (vector-ref b 1))) ;1 (- (* (vector-ref a 2) (vector-ref b 0)) (* (vector-ref a 0) (vector-ref b 2))) ;2 (- (* (vector-ref a 0) (vector-ref b 1)) (* (vector-ref a 1) (vector-ref b 0)))) ) ((4) ;{ {a[0] * b[0]} - {a[1] * b[1]} - {a[2] * b[2]} - {a[3] * b[3]} } ;{ {a[0] * b[1]} + {a[1] * b[0]} + {a[2] * b[3]} - {a[3] * b[2]} } ;{ {a[0] * b[2]} - {a[1] * b[3]} + {a[2] * b[0]} + {a[3] * b[1]} } ;{ {a[0] * b[3]} + {a[1] * b[2]} - {a[2] * b[1]} + {a[3] * b[0]} } (vector ;0 (- (* (vector-ref a 0) (vector-ref b 0)) (* (vector-ref a 1) (vector-ref b 1)) (* (vector-ref a 2) (vector-ref b 2)) (* (vector-ref a 3) (vector-ref b 3))) ;1 (- (+ (+ (* (vector-ref a 0) (vector-ref b 1)) (* (vector-ref a 1) (vector-ref b 0))) (* (vector-ref a 2) (vector-ref b 3))) (* (vector-ref a 3) (vector-ref b 2))) ;2 (+ (+ (- (* (vector-ref a 0) (vector-ref b 2)) (* (vector-ref a 1) (vector-ref b 3))) (* (vector-ref a 2) (vector-ref b 0))) (* (vector-ref a 3) (vector-ref b 1))) ;3 (+ (- (+ (* (vector-ref a 0) (vector-ref b 3)) (* (vector-ref a 1) (vector-ref b 2))) (* (vector-ref a 2) (vector-ref b 1))) (* (vector-ref a 3) (vector-ref b 0)))) ) ((8) ;{ {a[0] * b[0]} - {a[1] * b[1]} - {a[2] * b[2]} - {a[3] * b[3]} - {b[4] * a[4]} - {b[5] * a[5]} - {b[6] * a[6]} - {b[7] * a[7]} } ;{ {a[0] * b[1]} + {a[1] * b[0]} + {a[2] * b[3]} - {a[3] * b[2]} - {b[4] * a[5]} + {b[5] * a[4]} + {b[6] * a[7]} - {b[7] * a[6]} } ;{ {a[0] * b[2]} - {a[1] * b[3]} + {a[2] * b[0]} + {a[3] * b[1]} - {b[4] * a[6]} - {b[5] * a[7]} + {b[6] * a[4]} + {b[7] * a[5]} } ;{ {a[0] * b[3]} + {a[1] * b[2]} - {a[2] * b[1]} + {a[3] * b[0]} - {b[4] * a[7]} + {b[5] * a[6]} - {b[6] * a[5]} + {b[7] * a[4]} } ;{ {b[4] * a[0]} - {b[5] * a[1]} - {b[6] * a[2]} - {b[7] * a[3]} + {a[4] * b[0]} + {a[5] * b[1]} + {a[6] * b[2]} + {a[7] * b[3]} } ;{ {b[4] * a[1]} + {b[5] * a[0]} + {b[6] * a[3]} - {b[7] * a[2]} - {a[4] * b[1]} + {a[5] * b[0]} - {a[6] * b[3]} + {a[7] * b[2]} } ;{ {b[4] * a[2]} - {b[5] * a[3]} + {b[6] * a[0]} + {b[7] * a[1]} - {a[4] * b[2]} + {a[5] * b[3]} + {a[6] * b[0]} - {a[7] * b[1]} } ;{ {b[4] * a[3]} + {b[5] * a[2]} - {b[6] * a[1]} + {b[7] * a[0]} - {a[4] * b[3]} - {a[5] * b[2]} + {a[6] * b[1]} + {a[7] * b[0]} } (vector ;0 (- (* (vector-ref a 0) (vector-ref b 0)) (* (vector-ref a 1) (vector-ref b 1)) (* (vector-ref a 2) (vector-ref b 2)) (* (vector-ref a 3) (vector-ref b 3)) (* (vector-ref b 4) (vector-ref a 4)) (* (vector-ref b 5) (vector-ref a 5)) (* (vector-ref b 6) (vector-ref a 6)) (* (vector-ref b 7) (vector-ref a 7))) ;1 (- (+ (+ (- (- (+ (+ (* (vector-ref a 0) (vector-ref b 1)) (* (vector-ref a 1) (vector-ref b 0))) (* (vector-ref a 2) (vector-ref b 3))) (* (vector-ref a 3) (vector-ref b 2))) (* (vector-ref b 4) (vector-ref a 5))) (* (vector-ref b 5) (vector-ref a 4))) (* (vector-ref b 6) (vector-ref a 7))) (* (vector-ref b 7) (vector-ref a 6))) ;2 (+ (+ (- (- (+ (+ (- (* (vector-ref a 0) (vector-ref b 2)) (* (vector-ref a 1) (vector-ref b 3))) (* (vector-ref a 2) (vector-ref b 0))) (* (vector-ref a 3) (vector-ref b 1))) (* (vector-ref b 4) (vector-ref a 6))) (* (vector-ref b 5) (vector-ref a 7))) (* (vector-ref b 6) (vector-ref a 4))) (* (vector-ref b 7) (vector-ref a 5))) ;3 (+ (- (+ (- (+ (- (+ (* (vector-ref a 0) (vector-ref b 3)) (* (vector-ref a 1) (vector-ref b 2))) (* (vector-ref a 2) (vector-ref b 1))) (* (vector-ref a 3) (vector-ref b 0))) (* (vector-ref b 4) (vector-ref a 7))) (* (vector-ref b 5) (vector-ref a 6))) (* (vector-ref b 6) (vector-ref a 5))) (* (vector-ref b 7) (vector-ref a 4))) ;4 (+ (+ (+ (+ (- (- (- (* (vector-ref b 4) (vector-ref a 0)) (* (vector-ref b 5) (vector-ref a 1))) (* (vector-ref b 6) (vector-ref a 2))) (* (vector-ref b 7) (vector-ref a 3))) (* (vector-ref a 4) (vector-ref b 0))) (* (vector-ref a 5) (vector-ref b 1))) (* (vector-ref a 6) (vector-ref b 2))) (* (vector-ref a 7) (vector-ref b 3))) ;5 (+ (- (+ (- (- (+ (+ (* (vector-ref b 4) (vector-ref a 1)) (* (vector-ref b 5) (vector-ref a 0))) (* (vector-ref b 6) (vector-ref a 3))) (* (vector-ref b 7) (vector-ref a 2))) (* (vector-ref a 4) (vector-ref b 1))) (* (vector-ref a 5) (vector-ref b 0))) (* (vector-ref a 6) (vector-ref b 3))) (* (vector-ref a 7) (vector-ref b 2))) ;6 (- (+ (+ (- (+ (+ (- (* (vector-ref b 4) (vector-ref a 2)) (* (vector-ref b 5) (vector-ref a 3))) (* (vector-ref b 6) (vector-ref a 0))) (* (vector-ref b 7) (vector-ref a 1))) (* (vector-ref a 4) (vector-ref b 2))) (* (vector-ref a 5) (vector-ref b 3))) (* (vector-ref a 6) (vector-ref b 0))) (* (vector-ref a 7) (vector-ref b 1))) ;7 (+ (+ (- (- (+ (- (+ (* (vector-ref b 4) (vector-ref a 3)) (* (vector-ref b 5) (vector-ref a 2))) (* (vector-ref b 6) (vector-ref a 1))) (* (vector-ref b 7) (vector-ref a 0))) (* (vector-ref a 4) (vector-ref b 3))) (* (vector-ref a 5) (vector-ref b 2))) (* (vector-ref a 6) (vector-ref b 1))) (* (vector-ref a 7) (vector-ref b 0)))) ) (else (error 'cross-product "undefined for this length" alen blen) ) ) ) (else (error 'cross-product "undefined for varying length" alen blen)) ) ) ) ;; Normalize (define-inline (zero~? n e) (or (zero? n) (< (abs n) e))) ; #(max xcnt min ncnt 0s) (define (vector-info v) (define (maxmincnt mc a) (let ((mx (vector-ref mc 0)) (mn (vector-ref mc 2))) (cond ((= mx a) (set! (vector-ref mc 1) (+ (vector-ref mc 1) 1))) ((< mx a) (set! (vector-ref mc 0) a) (set! (vector-ref mc 1) 1))) (cond ((= mn a) (set! (vector-ref mc 3) (+ (vector-ref mc 3) 1))) ((> mn a) (set! (vector-ref mc 2) a) (set! (vector-ref mc 3) 1))) (cond ((zero? a) (set! (vector-ref mc 4) (+ (vector-ref mc 4) 1)))) ) mc ) (vector-reduce maxmincnt (vector -inf.0 0 +inf.0 0 0) v) ) ;reduce range -> (... 1] (define (vector-scale v x) (*vector-apply/1 (cut * <> x) v)) ;bias towards 0 (define (vector-shift v x) (*vector-apply/1 (cute + <> x) v)) (define (normalize-vector-scale v) (vector-scale v (/ (vector-ref (vector-info v) 0))) ) ;????? (define (normalize-vector-shift v) (vector-shift v (- (vector-ref (vector-info v) 2))) ) ;; softargmax - normalized exponential function ;1 ~= summ(vmax(V)) but 1 ?= summ(V), normalize (define (softmax v) (let ((ve (*vector-apply/1 exp v))) (*vector-apply/1 (cute / <> (vector-sum ve)) ve) ) ) (define (softmax* v #!optional (t 1) (e 1e-05)) (cond ((= t 1) (softmax v)) ((zero? t) ;(zero~? t e) ;what if > 1 1? ;vector-map! - segmentation violation (vector-map (lambda (i v) (cond ((exact? v) (if (= v 1) 1 0)) (else (if (= v 1.0) 1.0 0.0))) ) (normalize-vector-scale v)) ) ((infinite? t) ;(< (/ e) t) (make-vector (vector-length v) 0) ) (else (softmax (vector-scale v (/ t)))) ) ) ) ;(math-utils vector)