Skip to content

Commit a85b6c1

Browse files
committed
fix(spline): validate interpolator initialization before interpolation
1 parent 4115539 commit a85b6c1

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

cxx/include/pyinterp/math/interpolate/bivariate.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ class BivariateBase {
9292
return z;
9393
}
9494

95+
/// @brief Check if the interpolator is valid (coefficients computed)
96+
/// @return True if the interpolator is valid, false otherwise.
97+
[[nodiscard]] constexpr auto is_valid() const noexcept -> bool {
98+
return is_valid_;
99+
}
100+
95101
protected:
96102
/// @brief Compute coefficients from stored data
97103
/// @return true if successful, false otherwise.
@@ -109,12 +115,6 @@ class BivariateBase {
109115
/// @return Z-values of the data points.
110116
[[nodiscard]] constexpr auto za() const -> const Matrix<T>& { return za_; }
111117

112-
/// @brief Check if the interpolator is valid (coefficients computed)
113-
/// @return True if the interpolator is valid, false otherwise.
114-
[[nodiscard]] constexpr auto is_valid() const noexcept -> bool {
115-
return is_valid_;
116-
}
117-
118118
private:
119119
/// Stored X-coordinates of the data points
120120
Vector<T> xa_;

cxx/include/pyinterp/math/interpolate/bivariate/spline.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <stdexcept>
1212

13+
#include "pyinterp/math/fill.hpp"
1314
#include "pyinterp/math/interpolate/bivariate.hpp"
1415
#include "pyinterp/math/interpolate/univariate.hpp"
1516

@@ -59,10 +60,11 @@ class Spline : public BivariateBase<T> {
5960
const auto& ya = this->ya();
6061
const auto& za = this->za();
6162

62-
// Validate grid dimensions
63-
if (za.size() == 0) {
64-
throw std::runtime_error("Cannot interpolate on empty grid");
63+
// Check if interpolator was properly initialized
64+
if (!this->is_valid()) [[unlikely]] {
65+
return math::Fill<T>::value();
6566
}
67+
6668
return interpolate_y_then_x(xa, ya, za, x, y);
6769
}
6870

cxx/tests/math/interpolate/bivariate/spline.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ TEST(Spline, EmptyGrid) {
225225
Eigen::Matrix<double, Eigen::Dynamic, 1> ya(0);
226226

227227
auto spline = Spline<double>(std::make_unique<univariate::Linear<double>>());
228+
EXPECT_TRUE(std::isnan(spline(1.0, 1.0)));
228229
spline.prepare(xa, ya, za);
229-
EXPECT_THROW(static_cast<void>(spline(1.0, 1.0)), std::runtime_error);
230+
EXPECT_TRUE(std::isnan(spline(1.0, 1.0)));
230231
}
231232

232233
// Test with larger grid to verify capacity management

0 commit comments

Comments
 (0)