pub const WGSL_TRAPZ_F64: &str = include_str!("../shaders/math/trapz_f64.wgsl");
use crate::error::{BarracudaError, Result};
pub fn trapz(y: &[f64], x: &[f64]) -> Result<f64> {
if y.len() != x.len() {
return Err(BarracudaError::InvalidInput {
message: format!("y and x must have same length: {} vs {}", y.len(), x.len()),
});
}
let n = y.len();
if n == 0 {
return Ok(0.0);
}
if n == 1 {
return Ok(0.0);
}
for i in 1..n {
if x[i] < x[i - 1] {
return Err(BarracudaError::InvalidInput {
message: format!(
"x must be non-decreasing: x[{}] = {} < x[{}] = {}",
i,
x[i],
i - 1,
x[i - 1]
),
});
}
}
let mut integral = 0.0;
for i in 0..n - 1 {
let dx = x[i + 1] - x[i];
integral = (0.5 * (y[i] + y[i + 1])).mul_add(dx, integral);
}
Ok(integral)
}
pub fn trapz_product(f: &[f64], g1: &[f64], g2: &[f64], x: &[f64], weights: &[f64]) -> Result<f64> {
if f.len() != g1.len() || f.len() != g2.len() || f.len() != x.len() || f.len() != weights.len()
{
return Err(BarracudaError::InvalidInput {
message: format!(
"All arrays must have same length: f={}, g1={}, g2={}, x={}, weights={}",
f.len(),
g1.len(),
g2.len(),
x.len(),
weights.len()
),
});
}
let n = f.len();
if n == 0 {
return Ok(0.0);
}
if n == 1 {
return Ok(f[0] * g1[0] * g2[0] * weights[0]);
}
let mut integral = 0.0;
for i in 0..n - 1 {
let product_i = f[i] * g1[i] * g2[i] * weights[i];
let product_i1 = f[i + 1] * g1[i + 1] * g2[i + 1] * weights[i + 1];
let dx = x[i + 1] - x[i];
integral = (0.5 * (product_i + product_i1)).mul_add(dx, integral);
}
Ok(integral)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trapz_linear() {
let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
let y = vec![0.0, 1.0, 2.0, 3.0, 4.0];
let result = trapz(&y, &x).unwrap();
assert!((result - 8.0).abs() < 1e-10);
}
#[test]
fn test_trapz_quadratic() {
let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
let y: Vec<f64> = x.iter().map(|xi| xi * xi).collect();
let result = trapz(&y, &x).unwrap();
let expected = 64.0 / 3.0;
assert!((result - expected).abs() < 0.7); }
#[test]
fn test_trapz_constant() {
let x = vec![0.0, 1.0, 2.0, 3.0, 4.0];
let y = vec![5.0; 5];
let result = trapz(&y, &x).unwrap();
assert!((result - 20.0).abs() < 1e-10);
}
#[test]
fn test_trapz_nonuniform_grid() {
let x = vec![0.0, 1.0, 3.0, 6.0, 10.0];
let y = vec![0.0, 1.0, 3.0, 6.0, 10.0];
let result = trapz(&y, &x).unwrap();
let expected = 50.0; assert!((result - expected).abs() < 0.1);
}
#[test]
fn test_trapz_empty() {
let result = trapz(&[], &[]).unwrap();
assert_eq!(result, 0.0);
}
#[test]
fn test_trapz_single_point() {
let result = trapz(&[42.0], &[1.0]).unwrap();
assert_eq!(result, 0.0);
}
#[test]
fn test_trapz_mismatched_lengths() {
let result = trapz(&[1.0, 2.0, 3.0], &[0.0, 1.0]);
assert!(result.is_err());
}
#[test]
fn test_trapz_non_increasing() {
let x = vec![0.0, 2.0, 1.0, 3.0]; let y = vec![0.0, 2.0, 1.0, 3.0];
let result = trapz(&y, &x);
assert!(result.is_err());
}
#[test]
fn test_trapz_product_simple() {
let n = 5;
let x: Vec<f64> = (0..n).map(|i| i as f64).collect();
let f = vec![1.0; n];
let g1 = x.clone();
let g2 = x.clone();
let weights = vec![1.0; n];
let result = trapz_product(&f, &g1, &g2, &x, &weights).unwrap();
let expected = 64.0 / 3.0; assert!((result - expected).abs() < 0.7); }
#[test]
fn test_trapz_product_weights() {
let x = vec![0.0, 1.0, 2.0];
let f = vec![1.0, 2.0, 3.0];
let g1 = vec![1.0, 1.0, 1.0];
let g2 = vec![1.0, 1.0, 1.0];
let weights = vec![2.0, 2.0, 2.0];
let result = trapz_product(&f, &g1, &g2, &x, &weights).unwrap();
assert!((result - 8.0).abs() < 1e-10);
}
#[test]
fn test_trapz_product_mismatched_lengths() {
let result = trapz_product(
&[1.0, 2.0],
&[1.0, 2.0],
&[1.0, 2.0],
&[0.0, 1.0],
&[1.0], );
assert!(result.is_err());
}
}