only-brain 0.3.1

A simple Neural Network library, without the learning part.
Documentation
use std::ops;

use nalgebra::{SimdRealField, SVector};

/// This module provides a vector type `BVector` that is optimized for compile-time
/// dimension and uses SIMD operations for performance.
#[derive(Debug, Clone, PartialEq)]
pub struct BVector<T: SimdRealField + Copy, const N: usize> {
    pub data_vector: SVector<T, N>,
}

impl<T: SimdRealField + Copy, const N: usize> BVector<T, N> {
    /// Constructs a vector filled with `element` of a dimension `N`.
    pub fn from_element(element: T) -> Self {
        BVector {
            data_vector: SVector::<T, N>::from_element(element),
        }
    }

    /// Constructs the vector from fixed-size array.
    pub fn from_array(data: [T; N]) -> Self
    where
        T: Copy,
    {
        BVector {
            data_vector: SVector::<T, N>::from_row_slice(&data),
        }
    }

    pub fn dot(&self, other: &Self) -> T {
        self.data_vector.dot(&other.data_vector)
    }

    pub fn len(&self) -> usize {
        N
    }

    /// Whether the vector has no elements, which is only the case when `N` is 0.
    pub fn is_empty(&self) -> bool {
        N == 0
    }

    pub fn get(&self, index: usize) -> T {
        assert!(index < N, "Index out of bounds");
        self.data_vector[index]
    }
}

impl<T: SimdRealField + Copy, const N: usize> ops::Add for BVector<T, N> {
    type Output = BVector<T, N>;

    fn add(self, rhs: Self) -> Self::Output {
        BVector {
            data_vector: self.data_vector + rhs.data_vector,
        }
    }
}

/// Macro to construct a `BVector` with compile-time dimension inferred from the
/// number of elements provided.
///
/// Example:
/// - `let v = bvector![1.0, 2.0, 3.0]; // BVector<f64, 3>`
/// - `let v = bvector![2.0, -1.5]; // BVector<f64, 2>`
#[macro_export]
macro_rules! bvector {
    ($($x:expr),+ $(,)?) => {
        {
            // Let the compiler infer both element type `T` and length `N`.
            $crate::BVector::from_array([$( $x ),+])
        }
    };
}