functinos,apply-tbf and apply-tbf/boolean: Work on Boolean vectors.

These functions initially used rest arguments, but it's actually quite
impractical.
This commit is contained in:
Sergiu Ivanov 2020-07-07 21:41:45 +02:00
parent 2ed5f84338
commit c4ba018b5b

View file

@ -32,8 +32,8 @@
[tbf-w (-> tbf? (vectorof number?))] [tbf-w (-> tbf? (vectorof number?))]
[tbf-θ (-> tbf? number?)] [tbf-θ (-> tbf? number?)]
[vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))] [vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))]
[apply-tbf (->* (tbf?) #:rest (listof (or/c 0 1)) (or/c 0 1))] [apply-tbf (-> tbf? (vectorof (or/c 0 1)) (or/c 0 1))]
[apply-tbf/boolean (->* (tbf?) #:rest (listof boolean?) boolean?)])) [apply-tbf/boolean (-> tbf? (vectorof boolean?) boolean?)]))
(module+ test (module+ test
(require rackunit)) (require rackunit))
@ -253,11 +253,12 @@
;;; Applying a TBF consists in multiplying the weights by the ;;; Applying a TBF consists in multiplying the weights by the
;;; corresponding inputs and comparing the sum of the products to the ;;; corresponding inputs and comparing the sum of the products to the
;;; threshold. ;;; threshold.
(define (apply-tbf tbf . inputs) (define/contract (apply-tbf tbf inputs)
(-> tbf? (vectorof (or/c 0 1)) (or/c 0 1))
(any->01 (any->01
(> (>
;; The scalar product between the inputs and the weights ;; The scalar product between the inputs and the weights
(for/sum ([x (in-list inputs)] (for/sum ([x (in-vector inputs)]
[w (in-vector (tbf-w tbf))]) [w (in-vector (tbf-w tbf))])
(* x w)) (* x w))
(tbf-θ tbf)))) (tbf-θ tbf))))
@ -265,15 +266,15 @@
(module+ test (module+ test
(test-case "apply-tbf" (test-case "apply-tbf"
(define f1 (tbf #(2 -2) 1)) (define f1 (tbf #(2 -2) 1))
(check-equal? (tabulate/01 (λ (x y) (apply-tbf f1 x y))) (check-equal? (tabulate/01 (λ (x y) (apply-tbf f1 (vector x y))))
'((0 0 0) (0 1 0) (1 0 1) (1 1 0))))) '((0 0 0) (0 1 0) (1 0 1) (1 1 0)))))
;;; Like apply-tbf, but takes Boolean values as inputs and outputs a ;;; Like apply-tbf, but takes Boolean values as inputs and outputs a
;;; boolean value. ;;; boolean value.
(define (apply-tbf/boolean tbf . inputs) (define (apply-tbf/boolean tbf inputs)
(01->boolean (apply ((curry apply-tbf) tbf) (map any->01 inputs)))) (01->boolean (apply-tbf tbf (vector-map any->01 inputs))))
(module+ test (module+ test
(define f1 (tbf #(2 -2) 1)) (define f1 (tbf #(2 -2) 1))
(check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 x y))) (check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 (vector x y))))
'((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f)))) '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))