diff --git a/functions.rkt b/functions.rkt index ebf6ff1..1e7e79c 100644 --- a/functions.rkt +++ b/functions.rkt @@ -27,7 +27,7 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf) (module+ test (require typed/rackunit)) @@ -368,6 +368,22 @@ (test-case "boolean->01/vector" (check-equal? (boolean->01/vector #(#t #f #f)) #(1 0 0)))) + (: apply-tbf (-> tbf (Vectorof (U Zero One)) (U Zero One))) + (define (apply-tbf tbf inputs) + (any->01 + (> + ;; The scalar product between the inputs and the weights. + (for/sum ([x (in-vector inputs)] + [w (in-vector (tbf-w tbf))]) : Real + (* x w)) + (tbf-θ tbf)))) + + (module+ test + (test-case "apply-tbf" + (define f1 (tbf #(2 -2) 1)) + (check-equal? (tabulate/pv/01 2 (pvλ (x y) (apply-tbf f1 (vector x y)))) + '((0 0 0) (0 1 0) (1 0 1) (1 1 0))))) + (module untyped racket (module+ test (require rackunit)) @@ -454,7 +470,7 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -463,7 +479,6 @@ (provide ;; Functions (contract-out - [apply-tbf (-> tbf? (vectorof (or/c 0 1)) (or/c 0 1))] [apply-tbf/boolean (-> tbf? (vectorof boolean?) boolean?)] [list->tbf (-> (cons/c number? (cons/c number? (listof number?))) tbf?)] [lists->tbfs (-> (listof (listof number?)) (listof tbf?))] @@ -486,26 +501,6 @@ ;;; Threshold Boolean functions ;;; =========================== -;;; 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-vector 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 (vector 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) diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 4460808..1a11c42 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -392,6 +392,19 @@ Converts a Boolean vector to a vector of zeros and ones. (boolean->01/vector #(#t #f #f)) ]} +@defproc[(apply-tbf [t tbf] [inputs (Vectorof (U Zero One))]) (U Zero One)]{ + +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. If the product is +above the threshold, the function is 1, otherwise it is 0. + +@examples[#:eval functions-evaluator +(define simple-tbf (tbf #(2 -2) 1)) +(tabulate/pv/01 2 (pvλ (x y) (apply-tbf simple-tbf (vector x y)))) +]} + @section[#:tag "fuctions/untyped"]{Untyped definitions} @defmodule[(submod dds/functions typed untyped)]