1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
extern crate num;

use num::Float;

/// `step_function` is the kind of activation functions. That is
/// \\[
/// \text{step}(x) =
/// \begin{cases}
/// 1 & (x\gt 0) \\\\
/// 0 & (\text{otherwise})
/// \end{cases}
/// \\]
/// where \\(x\in\mathbb{R}\\).
///
/// # e.g.
///
/// ```
/// assert_eq!(true, deep_learning_playground::perceptron::single::step_function(0.1));
/// assert_eq!(false, deep_learning_playground::perceptron::single::step_function(-0.1));
/// ```
pub fn step_function<T: Float>(val: T) -> bool {
    if val <= T::zero() {
        false
    } else {
        true
    }
}

/// `logical_perceptron` is the logical perceptron.
/// It can generates some logical gates (`and_perceptron`, `nand_perceptron`, `or_perceptron`)
/// and generate a function that passes the sum of weighted signals
/// (\\(b+X\cdot W\\) where \\(b\in\mathbb{R}\\) is a bias
/// value, \\(W^{2\times 1}=\left(w_1,w_2\right)^T, X^{1\times 2}=\left(x_1,x_2\right)\\) and
/// \\(w_1\\) and \\(w_2\\) are weights of \\(x_1\\) and \\(x_2\\)) to the activation function.
///
/// # Arguments
///
/// * `w1` - Weights for signal `x1`
/// * `w2` - Weights for signal `x2`
/// * `bias` - Bias that determines the ease of firing of neurons
///
/// # e.g.
///
/// ```
/// assert_eq!(true, deep_learning_playground::perceptron::single::logical_perceptron(&0.5, &0.5, &-0.7)(true, true));
/// assert_eq!(false, deep_learning_playground::perceptron::single::logical_perceptron(&-0.5, &-0.5, &0.7)(true, true));
/// assert_eq!(true, deep_learning_playground::perceptron::single::logical_perceptron(&0.5, &0.5, &-0.2)(true, false));
/// ```
pub fn logical_perceptron<T: Float>(
    w1: &'static T,
    w2: &'static T,
    bias: &'static T,
) -> Box<dyn Fn(bool, bool) -> bool> {
    Box::new(move |x1: bool, x2: bool| -> bool {
        let f = |x| if x { T::one() } else { T::zero() };
        step_function(*bias + *w1 * f(x1) + *w2 * f(x2))
    })
}

/// `and_perceptron` generates the logical AND function.
/// Let \\(p\\) and \\(q\\) are logical variables, `and_perceptron` generates the function which
/// satisfies the following truth table.
///
/// | \\(p\\) | \\(q\\) | `and_perceptron()(`\\(p\\)`,`\\(q\\)`)` |
/// | -- | -- | -- |
/// | \\(1\\) | \\(1\\) | \\(1\\) |
/// | \\(1\\) | \\(0\\) | \\(0\\) |
/// | \\(0\\) | \\(1\\) | \\(0\\) |
/// | \\(0\\) | \\(0\\) | \\(0\\) |
///
/// # e.g.
///
/// ```
/// assert_eq!(true, deep_learning_playground::perceptron::single::and_perceptron()(true, true));
/// assert_eq!(false, deep_learning_playground::perceptron::single::and_perceptron()(false, true));
/// assert_eq!(false, deep_learning_playground::perceptron::single::and_perceptron()(false, false));
/// ```
pub fn and_perceptron() -> Box<dyn Fn(bool, bool) -> bool> {
    logical_perceptron(&0.5, &0.5, &-0.7)
}

/// `nand_perceptron` generates the logical NAND function.
/// Let \\(p\\) and \\(q\\) are logical variables, `nand_perceptron` generates the function which
/// satisfies the following truth table.
///
/// | \\(p\\) | \\(q\\) | `nand_perceptron()(`\\(p\\)`,`\\(q\\)`)` |
/// | -- | -- | -- |
/// | \\(1\\) | \\(1\\) | \\(0\\) |
/// | \\(1\\) | \\(0\\) | \\(1\\) |
/// | \\(0\\) | \\(1\\) | \\(1\\) |
/// | \\(0\\) | \\(0\\) | \\(1\\) |
///
/// # e.g.
///
/// ```
/// assert_eq!(true, deep_learning_playground::perceptron::single::nand_perceptron()(true, false));
/// assert_eq!(false, deep_learning_playground::perceptron::single::nand_perceptron()(true, true));
/// ```
pub fn nand_perceptron() -> Box<dyn Fn(bool, bool) -> bool> {
    logical_perceptron(&-0.5, &-0.5, &0.7)
}

/// `or_perceptron` generates the logical OR function.
/// Let \\(p\\) and \\(q\\) are logical variables, `or_perceptron()(`\\(p\\)`,`\\(q\\)`)` generates the function which
/// satisfies the following truth table.
///
/// | \\(p\\) | \\(q\\) | `or_perceptron()(`\\(p\\)`,`\\(q\\)`)` |
/// | -- | -- | -- |
/// | \\(1\\) | \\(1\\) | \\(1\\) |
/// | \\(1\\) | \\(0\\) | \\(1\\) |
/// | \\(0\\) | \\(1\\) | \\(1\\) |
/// | \\(0\\) | \\(0\\) | \\(0\\) |
///
/// # e.g.
///
/// ```
/// assert_eq!(true, deep_learning_playground::perceptron::single::or_perceptron()(true, false));
/// assert_eq!(false, deep_learning_playground::perceptron::single::or_perceptron()(false, false));
/// ```
pub fn or_perceptron() -> Box<dyn Fn(bool, bool) -> bool> {
    logical_perceptron(&0.5, &0.5, &-0.2)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_logical_gates_impl(
        logical_gate: &Box<dyn Fn(bool, bool) -> bool>,
        expected_answer: &[bool; 4],
    ) {
        let mut i: usize = 0;
        for b1 in [true, false].iter() {
            for b2 in [true, false].iter() {
                assert_eq!(logical_gate(*b1, *b2), expected_answer[i]);
                i += 1;
            }
        }
    }

    #[test]
    fn test_logical_gates() {
        test_logical_gates_impl(&and_perceptron(), &[true, false, false, false]);
        test_logical_gates_impl(&nand_perceptron(), &[false, true, true, true]);
        test_logical_gates_impl(&or_perceptron(), &[true, true, true, false]);
    }
}