From 768c351e11304e6d5978872ad538614c687d11c1 Mon Sep 17 00:00:00 2001 From: Tobia Marcucci Date: Fri, 4 Oct 2024 19:48:40 -0700 Subject: [PATCH] Fixes typo in integral --- pybezier/bezier_curve.py | 2 +- tests/test_bezier_curve.py | 12 +++++++----- tests/test_composite_bezier_curve.py | 12 +++++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pybezier/bezier_curve.py b/pybezier/bezier_curve.py index 26135b8..5afa46e 100644 --- a/pybezier/bezier_curve.py +++ b/pybezier/bezier_curve.py @@ -98,7 +98,7 @@ def derivative(self) -> Self: return BezierCurve(points, self.initial_time, self.final_time) def integral(self, initial_condition : np.ndarray | None = None) -> Self: - points = self.points * self.duration / self.degree + points = self.points * self.duration / (self.degree + 1) points = np.vstack([np.zeros(self.dimension), points]) points = np.cumsum(points, axis=0) if initial_condition is not None: diff --git a/tests/test_bezier_curve.py b/tests/test_bezier_curve.py index 90d4745..d5de406 100644 --- a/tests/test_bezier_curve.py +++ b/tests/test_bezier_curve.py @@ -119,12 +119,14 @@ def test_integral(self): initial_conditions = [None, np.ones(self.curve.dimension)] for initial_condition in initial_conditions: integral = self.curve.integral(initial_condition) - value = 0 if initial_condition is None else initial_condition - np.testing.assert_array_almost_equal(integral(self.initial_time), value) - time_step = 1e-6 + value = integral(self.initial_time) + target_value = 0 if initial_condition is None else initial_condition + np.testing.assert_array_almost_equal(value, target_value) + time_step = 1e-3 for time in np.linspace(self.initial_time, self.final_time - time_step): - numerical_integral = integral(time) + time_step * self.curve(time) - np.testing.assert_array_almost_equal(integral(time + time_step), numerical_integral) + value = self.curve(time + time_step / 2) + target_value = (integral(time + time_step) - integral(time)) / time_step + np.testing.assert_array_almost_equal(value, target_value) def test_domain_split(self): split_time = (self.initial_time + self.final_time) / 2 diff --git a/tests/test_composite_bezier_curve.py b/tests/test_composite_bezier_curve.py index cc67a9f..6033f63 100644 --- a/tests/test_composite_bezier_curve.py +++ b/tests/test_composite_bezier_curve.py @@ -130,12 +130,14 @@ def test_integral(self): initial_conditions = [None, np.ones(self.composite_curve.dimension)] for initial_condition in initial_conditions: integral = self.composite_curve.integral(initial_condition) - value = 0 if initial_condition is None else initial_condition - np.testing.assert_array_almost_equal(integral(self.initial_time), value) - time_step = 1e-6 + value = integral(self.initial_time) + target_value = 0 if initial_condition is None else initial_condition + np.testing.assert_array_almost_equal(value, target_value) + time_step = 1e-3 for time in np.linspace(self.initial_time, self.final_time - time_step): - numerical_integral = integral(time) + time_step * self.composite_curve(time) - np.testing.assert_array_almost_equal(integral(time + time_step), numerical_integral) + value = self.composite_curve(time + time_step / 2) + target_value = (integral(time + time_step) - integral(time)) / time_step + np.testing.assert_array_almost_equal(value, target_value) def test_knot_points(self): for i, point in enumerate(self.composite_curve.knot_points()):