use crate::matrix::fill;
use crate::matrix::Matrix;
pub enum Map
{
MatrixMultiply(usize, usize),
Sigmoid(usize)
}
fn matrix_multiply(a: Matrix, x: Matrix) -> Matrix
{
Matrix::wrap(a, x.rows) * x
}
fn sigmoid(_a: Matrix, x: Matrix) -> Matrix
{
let mut y = x;
for i in 0..y.elements.len()
{
let u: f64 = -1.0 * y[(i, 0)];
y[(i, 0)] = 1.0 / (1.0 + u.exp());
}
y
}
pub struct Layer
{
pub a: Matrix,
pub x: Matrix,
pub feedforwardptr: fn(Matrix, Matrix) -> Matrix,
}
impl Layer
{
pub fn new(map: Map) -> Self
{
match map
{
Map::MatrixMultiply(i, o) =>
{
let x = Matrix::new(i, 1);
let mut a = Matrix::new(i * o, 1);
fill(&mut a.elements);
Self {a, x, feedforwardptr: matrix_multiply}
}
Map::Sigmoid(d) =>
{
let x = Matrix::new(d, 1);
let a = Matrix {rows: 0, cols: 0, elements: Vec::new()};
Self {a, x, feedforwardptr: sigmoid}
}
}
}
pub fn feedforward(&self) -> Matrix
{
(self.feedforwardptr)(self.a.clone(), self.x.clone())
}
}