Type apply-tbf.

This commit is contained in:
Sergiu Ivanov 2022-04-21 14:20:22 +02:00
parent 027022524b
commit 1863b0829c
2 changed files with 31 additions and 23 deletions

View file

@ -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)

View file

@ -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)]