Skip to content

Commit

Permalink
Simplify vector operations in natural_cubic.rs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nakashima-hikaru committed Jun 16, 2024
1 parent 78eb4b1 commit ab0ff72
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions crates/qlab-math/src/interpolation/spline/natural_cubic.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -72,11 +72,7 @@ impl<V: Value> Interpolator<NaturalCubic<V>, V> for NaturalCubic<V> {
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());
Expand All @@ -86,7 +82,7 @@ impl<V: Value> Interpolator<NaturalCubic<V>, V> for NaturalCubic<V> {
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 {
Expand Down

0 comments on commit ab0ff72

Please sign in to comment.