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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Some popular activation functions and their derivatives.

#![allow(non_camel_case_types)]
use libc::c_float;

extern {

    // the activation functions
    fn CGL_utils_sigmoid(x: c_float) -> c_float;
    fn CGL_utils_sigmoid_derivative(x: c_float) -> c_float;
    fn CGL_utils_relu(x: c_float) -> c_float;
    fn CGL_utils_relu_derivative(x: c_float) -> c_float;
    fn CGL_utils_tanh(x: c_float) -> c_float;
    fn CGL_utils_tanh_derivative(x: c_float) -> c_float;
    fn CGL_utils_step(x: c_float) -> c_float;
    fn CGL_utils_step_derivative(x: c_float) -> c_float;
    fn CGL_utils_relu_leaky(x: c_float) -> c_float;
    fn CGL_utils_relu_leaky_derivative(x: c_float) -> c_float;
    fn CGL_utils_relu_smooth(x: c_float) -> c_float;
    fn CGL_utils_relu_smooth_derivative(x: c_float) -> c_float;
}


/// Calculates the sigmoid activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::sigmoid(x);
/// println!("sigmoid({}) = {}", x, result);
/// ```
pub fn sigmoid(x: f32) -> f32  {
    unsafe {
        CGL_utils_sigmoid(x as c_float) as f32
    }
}

/// Calculates the derivative of the sigmoid activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::sigmoid_derivative(x);
/// println!("sigmoid_derivative({}) = {}", x, result);
/// ```
pub fn sigmoid_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_sigmoid_derivative(x as c_float) as f32
    }
}

/// Calculates the ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu(x);
/// println!("relu({}) = {}", x, result);
/// ```
pub fn relu(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu(x as c_float) as f32
    }
}

/// Calculates the derivative of the ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu_derivative(x);
/// println!("relu_derivative({}) = {}", x, result);
/// ```
pub fn relu_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu_derivative(x as c_float) as f32
    }
}

/// Calculates the hyperbolic tangent activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::tanh(x);
/// println!("tanh({}) = {}", x, result);
/// ```
pub fn tanh(x: f32) -> f32  {
    unsafe {
        CGL_utils_tanh(x as c_float) as f32
    }
}

/// Calculates the derivative of the hyperbolic tangent activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::tanh_derivative(x);
/// println!("tanh_derivative({}) = {}", x, result);
/// ```
pub fn tanh_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_tanh_derivative(x as c_float) as f32
    }
}

/// Calculates the step activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::step(x);
/// println!("step({}) = {}", x, result);
/// ```
pub fn step(x: f32) -> f32  {
    unsafe {
        CGL_utils_step(x as c_float) as f32
    }
}

/// Calculates the derivative of the step activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::step_derivative(x);
/// println!("step_derivative({}) = {}", x, result);
/// ```
pub fn step_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_step_derivative(x as c_float) as f32
    }
}

/// Calculates the leaky ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu_leaky(x);
/// println!("relu_leaky({}) = {}", x, result);
/// ```
pub fn relu_leaky(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu_leaky(x as c_float) as f32
    }
}

/// Calculates the derivative of the leaky ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu_leaky_derivative(x);
/// println!("relu_leaky_derivative({}) = {}", x, result);
/// ```
pub fn relu_leaky_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu_leaky_derivative(x as c_float) as f32
    }
}

/// Calculates the smooth ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu_smooth(x);
/// println!("relu_smooth({}) = {}", x, result);
/// ```
pub fn relu_smooth(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu_smooth(x as c_float) as f32
    }
}

/// Calculates the derivative of the smooth ReLU activation function for a given input.
///
/// # Arguments
///
/// * `x` - A 32-bit float input value.
///
/// # Example
///
/// ```
/// let x = 0.5;
/// let result = cgl_rs::math::activations::relu_smooth_derivative(x);
/// println!("relu_smooth_derivative({}) = {}", x, result);
/// ```
pub fn relu_smooth_derivative(x: f32) -> f32  {
    unsafe {
        CGL_utils_relu_smooth_derivative(x as c_float) as f32
    }
}