Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG → CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Everything below is merged to `main` but not yet tagged/released.
every dimensionality and included in the corresponding `Strategy*Enum` types.

### Changed
- **Breaking:** `find_nearest_index` is renamed to `locate_lower_index` and, along with
the other grid/index search helpers (`step_index` -> `locate_step_index`,
`uniform_lower_index` -> `locate_lower_index_uniform`, `exact_index`,
`check_uniform_grid`), moves from `strategy::traits` to a new `strategy::utils`
module — `traits` now holds only the `Strategy1D`/`2D`/`3D`/`ND` trait definitions.
No deprecation shim, matching the other breaking renames in this release.
`locate_lower_index` also now clamps out-of-range points to `[0, len - 2]` itself,
rather than relying on each `Linear` call site to inline the same clamp before
calling it.
- **Breaking:** `LeftNearest` and `RightNearest` are removed. Migrate to
`Step::from(StepDirection::Lower)` / `Step::from(StepDirection::Upper)`, or the
leaner `StepLower` / `StepUpper` markers.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2024, Alliance for Sustainable Energy, LLC
Copyright (c) 2024, Alliance for Energy Innovation, LLC

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
29 changes: 8 additions & 21 deletions src/interpolator/n/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ where
if grid[dim].is_empty() {
continue;
}
// Binary search for an exact match: find_nearest_index returns the lower bracket,
// so the point can only be exactly equal to grid[lower] or grid[lower+1].
let lower = if &point[dim] < grid[dim].first().unwrap() {
0
} else if &point[dim] > grid[dim].last().unwrap() {
grid[dim].len() - 2
} else {
find_nearest_index(grid[dim].view(), &point[dim])
};
let lower = locate_lower_index(grid[dim].view(), &point[dim]);
let pos = exact_index(grid[dim].view(), lower, &point[dim]);
if let Some(pos) = pos {
point.remove(dim);
Expand All @@ -57,13 +49,7 @@ where
for dim in 0..n {
// Extrapolation is checked previously in Interpolator::interpolate,
// meaning by now, point is within grid bounds or extrapolation is enabled
let lower_idx = if &point[dim] < grid[dim].first().unwrap() {
0
} else if &point[dim] > grid[dim].last().unwrap() {
grid[dim].len() - 2
} else {
find_nearest_index(grid[dim].view(), &point[dim])
};
let lower_idx = locate_lower_index(grid[dim].view(), &point[dim]);
let interp_diff = (point[dim] - grid[dim][lower_idx])
/ (grid[dim][lower_idx + 1] - grid[dim][lower_idx]);
lower_idxs.push(lower_idx);
Expand Down Expand Up @@ -123,7 +109,8 @@ where
let mut interp_diffs = Vec::with_capacity(n);
for (grid_dim, &point_dim) in data.grid.iter().zip(point.iter()) {
let step = grid_dim[1] - grid_dim[0];
let lower_idx = uniform_lower_index(grid_dim[0], step, grid_dim.len(), point_dim);
let lower_idx =
locate_lower_index_uniform(grid_dim[0], step, grid_dim.len(), point_dim);
let diff = (point_dim - grid_dim[lower_idx]) / step;
lower_idxs.push(lower_idx);
interp_diffs.push(diff);
Expand Down Expand Up @@ -169,7 +156,7 @@ where
// dimensionality reduction needed — the distance comparison handles exact matches correctly.
let mut idx = vec![0usize; n];
for dim in 0..n {
let lower_idx = find_nearest_index(data.grid[dim].view(), &point[dim]);
let lower_idx = locate_lower_index(data.grid[dim].view(), &point[dim]);
idx[dim] = if point[dim] - data.grid[dim][lower_idx]
< data.grid[dim][lower_idx + 1] - point[dim]
{
Expand Down Expand Up @@ -212,7 +199,7 @@ where
let n = data.values.ndim();
let mut idx = vec![0usize; n];
for dim in 0..n {
idx[dim] = step_index(self.dir(dim), data.grid[dim].view(), &point[dim]);
idx[dim] = locate_step_index(self.dir(dim), data.grid[dim].view(), &point[dim]);
}
Ok(data.values.view()[idx.as_slice()])
}
Expand All @@ -236,7 +223,7 @@ where
let n = data.values.ndim();
let mut idx = vec![0usize; n];
for dim in 0..n {
idx[dim] = step_index(StepDirection::Lower, data.grid[dim].view(), &point[dim]);
idx[dim] = locate_step_index(StepDirection::Lower, data.grid[dim].view(), &point[dim]);
}
Ok(data.values.view()[idx.as_slice()])
}
Expand All @@ -259,7 +246,7 @@ where
let n = data.values.ndim();
let mut idx = vec![0usize; n];
for dim in 0..n {
idx[dim] = step_index(StepDirection::Upper, data.grid[dim].view(), &point[dim]);
idx[dim] = locate_step_index(StepDirection::Upper, data.grid[dim].view(), &point[dim]);
}
Ok(data.values.view()[idx.as_slice()])
}
Expand Down
27 changes: 12 additions & 15 deletions src/interpolator/one/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ where
) -> Result<D::Elem, InterpolateError> {
// Extrapolation is checked previously in Interpolator::interpolate,
// meaning by now, point is within grid bounds or extrapolation is enabled
let x_l = if &point[0] < data.grid[0].first().unwrap() {
0
} else if &point[0] > data.grid[0].last().unwrap() {
data.grid[0].len() - 2
} else {
find_nearest_index(data.grid[0].view(), &point[0])
};
let x_u = x_l + 1;
let x_diff = (point[0] - data.grid[0][x_l]) / (data.grid[0][x_u] - data.grid[0][x_l]);
Ok(data.values[x_l] * (D::Elem::one() - x_diff) + data.values[x_u] * x_diff)
match locate_axis(data.grid[0].view(), &point[0]) {
AxisLocation::Exact(i) => Ok(data.values[i]),
AxisLocation::Interp { lower, frac } => {
let upper = lower + 1;
Ok(data.values[lower] * (D::Elem::one() - frac) + data.values[upper] * frac)
}
}
}

/// Returns `true`.
Expand All @@ -48,7 +45,7 @@ where
) -> Result<D::Elem, InterpolateError> {
let grid = data.grid[0].view();
let step = grid[1] - grid[0];
let x_l = uniform_lower_index(grid[0], step, grid.len(), point[0]);
let x_l = locate_lower_index_uniform(grid[0], step, grid.len(), point[0]);
let x_u = x_l + 1;
let x_diff = (point[0] - grid[x_l]) / step;
Ok(data.values[x_l] * (D::Elem::one() - x_diff) + data.values[x_u] * x_diff)
Expand All @@ -70,7 +67,7 @@ where
data: &InterpData1D<D>,
point: &[D::Elem; 1],
) -> Result<D::Elem, InterpolateError> {
let x_l = find_nearest_index(data.grid[0].view(), &point[0]);
let x_l = locate_lower_index(data.grid[0].view(), &point[0]);
let x_u = x_l + 1;
let i = if point[0] - data.grid[0][x_l] < data.grid[0][x_u] - point[0] {
x_l
Expand Down Expand Up @@ -107,7 +104,7 @@ where
data: &InterpData1D<D>,
point: &[D::Elem; 1],
) -> Result<D::Elem, InterpolateError> {
Ok(data.values[step_index(self.dir(0), data.grid[0].view(), &point[0])])
Ok(data.values[locate_step_index(self.dir(0), data.grid[0].view(), &point[0])])
}

fn allow_extrapolate(&self) -> bool {
Expand All @@ -125,7 +122,7 @@ where
data: &InterpData1D<D>,
point: &[D::Elem; 1],
) -> Result<D::Elem, InterpolateError> {
Ok(data.values[step_index(StepDirection::Lower, data.grid[0].view(), &point[0])])
Ok(data.values[locate_step_index(StepDirection::Lower, data.grid[0].view(), &point[0])])
}

/// Returns `false`.
Expand All @@ -144,7 +141,7 @@ where
data: &InterpData1D<D>,
point: &[D::Elem; 1],
) -> Result<D::Elem, InterpolateError> {
Ok(data.values[step_index(StepDirection::Upper, data.grid[0].view(), &point[0])])
Ok(data.values[locate_step_index(StepDirection::Upper, data.grid[0].view(), &point[0])])
}

/// Returns `false`.
Expand Down
Loading
Loading