[lab4] More flexibility for passing dynamic vector types

This commit is contained in:
Andrew Golovashevich 2026-02-08 20:00:32 +03:00
parent 332ca62569
commit 5c8e1f9dc2
2 changed files with 48 additions and 17 deletions

View File

@ -1,13 +1,42 @@
fn compute_potential/*<const Is: usize, const Os: usize>*/( use std::ops::Index;
weights: &[&[f64/*; Is*/]/*; Os*/],
input_data: &[f64/*; Is*/], fn compute_potential<WA: Index<usize, Output = f64>>(
potential_data: &mut [f64/*; Is*/], weights: &[WA],
output_data: &mut [f64/*; Is*/], input_data: &[f64],
potential_data: &mut [f64],
output_data: &mut [f64],
f: impl Fn(f64) -> f64, f: impl Fn(f64) -> f64,
) { ) where
for<'a> &'a WA: IntoIterator<Item = &'a f64>,
{
for (i, n) in weights.iter().enumerate() { for (i, n) in weights.iter().enumerate() {
let P = input_data.iter().zip(n.iter()).map(|(x, w)| x * w).sum(); let P = input_data
.iter()
.zip(n.into_iter())
.map(|(x, w)| x * w)
.sum();
potential_data[i] = P; potential_data[i] = P;
output_data[i] = f(P); output_data[i] = f(P);
} }
} }
#[test]
fn test_compiles_static() {
let weights = [[0.0, 0.0], [1.0, 0.0]];
let input_data = [0.0, 0.0];
let mut potential_data = [0.0, 0.0];
let mut output_data = [0.0, 0.0];
compute_potential(&weights, &input_data, &mut potential_data, &mut output_data, |x| x);
}
#[test]
fn test_compiles_dynamic() {
let weights = [Vec::new(), Vec::new()];
let input_data = [0.0, 0.0];
let mut potential_data = [0.0, 0.0];
let mut output_data = [0.0, 0.0];
compute_potential(&weights, &input_data, &mut potential_data, &mut output_data, |x| x);
}

View File

@ -1,7 +1,9 @@
fn calc_error/*<const Cs: usize, const Ns: usize>*/( use std::ops::{Index, IndexMut};
next_errors: &[f64/*; Ns*/],
weights: &[&[f64/*; Cs*/]/*; Ns*/], fn calc_error<NWA: Index<usize, Output = f64>>(
current_errors: &mut [f64/*; Cs*/] next_errors: &[f64],
weights: &[NWA],
current_errors: &mut [f64],
) { ) {
for i in 0..current_errors.len() { for i in 0..current_errors.len() {
current_errors[i] = weights current_errors[i] = weights
@ -12,14 +14,14 @@ fn calc_error/*<const Cs: usize, const Ns: usize>*/(
} }
} }
fn apply_error/*<const Cs: usize, const Ns: usize>*/( fn apply_error<NWA: IndexMut<usize, Output = f64>>(
n: f64, n: f64,
errors: &[f64/*; Ns*/], errors: &[f64],
weights: &mut [&mut [f64/*; Cs*/]/*; Ns*/], weights: &mut [NWA],
current_potentials: &[f64/*; Cs*/], current_potentials: &[f64],
next_potentials: &[f64/*; Cs*/], next_potentials: &[f64],
f: impl Fn(f64) -> f64, f: impl Fn(f64) -> f64,
f1: impl Fn(f64) -> f64 f1: impl Fn(f64) -> f64,
) { ) {
for i in 0..current_potentials.len() { for i in 0..current_potentials.len() {
for j in 0..next_potentials.len() { for j in 0..next_potentials.len() {