From 2ed5f84338de862019f9c47c92fa3eb9e78730f0 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 11 Jun 2020 00:27:11 +0200 Subject: [PATCH] functions: Add apply-tbf and apply-tbf/boolean. --- functions.rkt | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/functions.rkt b/functions.rkt index 5c3fc08..5ba88d2 100644 --- a/functions.rkt +++ b/functions.rkt @@ -31,7 +31,9 @@ [random-boolean-function/list (-> number? procedure?)] [tbf-w (-> tbf? (vectorof 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/boolean (->* (tbf?) #:rest (listof boolean?) boolean?)])) (module+ test (require rackunit)) @@ -245,3 +247,33 @@ (module+ test (test-case "boolean->0-1" (check-equal? (vector-boolean->01 #(#t #f #f)) #(1 0 0)))) + +;;; Applies the TBF to its inputs. +;;; +;;; Applying a TBF consists in multiplying the weights by the +;;; corresponding inputs and comparing the sum of the products to the +;;; threshold. +(define (apply-tbf tbf . inputs) + (any->01 + (> + ;; The scalar product between the inputs and the weights + (for/sum ([x (in-list inputs)] + [w (in-vector (tbf-w tbf))]) + (* x w)) + (tbf-θ tbf)))) + +(module+ test + (test-case "apply-tbf" + (define f1 (tbf #(2 -2) 1)) + (check-equal? (tabulate/01 (λ (x y) (apply-tbf f1 x y))) + '((0 0 0) (0 1 0) (1 0 1) (1 1 0))))) + +;;; Like apply-tbf, but takes Boolean values as inputs and outputs a +;;; boolean value. +(define (apply-tbf/boolean tbf . inputs) + (01->boolean (apply ((curry apply-tbf) tbf) (map any->01 inputs)))) + +(module+ test + (define f1 (tbf #(2 -2) 1)) + (check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 x y))) + '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))