From ab0ff728b31d689e173df29849d05df8d73ec3e1 Mon Sep 17 00:00:00 2001 From: hnakashima Date: Sun, 16 Jun 2024 21:57:06 +0900 Subject: [PATCH] Simplify vector operations in natural_cubic.rs Changed the way vectors are handled in the natural_cubic.rs file resulting in more readable and efficient code. The new implementation reduces the use of separate operations and duplicates, and avoids the unnecessary creation of new containers for the intermediate results. --- .../src/interpolation/spline/natural_cubic.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/qlab-math/src/interpolation/spline/natural_cubic.rs b/crates/qlab-math/src/interpolation/spline/natural_cubic.rs index 7888398..215c6f1 100644 --- a/crates/qlab-math/src/interpolation/spline/natural_cubic.rs +++ b/crates/qlab-math/src/interpolation/spline/natural_cubic.rs @@ -1,7 +1,7 @@ use crate::interpolation::spline::Value; use crate::interpolation::{find_index_at_left_boundary, Interpolator, Point2DWithSlope}; use crate::linear_algebra::tridiagonal_matrix::TridiagonalMatrix; -use nalgebra::{DVector, Dim, Dyn, VecStorage, U1}; +use nalgebra::DVector; use qlab_error::InterpolationError; #[derive(Default)] @@ -72,11 +72,7 @@ impl Interpolator, V> for NaturalCubic { for raw_point in raw_points.iter().take(raw_points.len() - 1).skip(1) { y.push(raw_point.1); } - let rhs = VecStorage::new(Dyn::from_usize(raw_points.len() - 2), U1, (c * y).unwrap()); - let mut rhs = DVector::from_data(rhs); - let m = VecStorage::new(Dyn::from_usize(raw_points.len() - 2), U1, m); - let m = DVector::from_data(m); - rhs += m; + let rhs = DVector::from((c * y).unwrap()) + DVector::from(m); let y2 = b.solve(rhs.as_slice()); let mut derivatives = Vec::with_capacity(raw_points.len()); derivatives.push(V::zero()); @@ -86,7 +82,7 @@ impl Interpolator, V> for NaturalCubic { derivatives.push(V::zero()); let mut temp = raw_points[0].0; - let mut points = Vec::new(); + let mut points = Vec::with_capacity(raw_points.len()); for (&(x, y), dydx) in raw_points.iter().zip(derivatives) { let point = Point2DWithSlope::new(x, y, dydx); if point.coordinate.x < temp {