Skip to content

Fix 4th/5th derivative coefficients in NaturalQuinticSpline::Evaluate#119

Open
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers:fix/natural-quintic-spline-derivatives
Open

Fix 4th/5th derivative coefficients in NaturalQuinticSpline::Evaluate#119
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers:fix/natural-quintic-spline-derivatives

Conversation

@CodeReclaimers
Copy link
Contributor

Lines 219 and 225 use factor 60 (= 5*4*3, correct for the 3rd derivative) where 120 (= 5!, needed for the 4th and 5th derivatives) is required. The 4th and 5th derivatives are returned at half their correct values.

Fix: introduce r120 = 120 and use it in place of r60 on lines 219 and 225.

Test (CMakeLists.txt)
cmake_minimum_required(VERSION 3.14)
project(test_issue_6_1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(GTE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../GTE")
add_executable(test_issue_6_1 test_issue_6_1.cpp)
target_include_directories(test_issue_6_1 PRIVATE "${GTE_ROOT}")
Test (test_issue_6_1.cpp)
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <Mathematics/NaturalQuinticSpline.h>

static bool test_derivatives_vs_coefficients()
{
    using Real = double;
    using Vec = gte::Vector<1, Real>;

    std::vector<Real> times = { 0.0, 1.0, 2.0 };
    Vec v0, v1, v2;
    v0[0] = 0.0; v1[0] = 1.0; v2[0] = 0.0;
    std::vector<Vec> f0 = { v0, v1, v2 };
    Vec d0, d1, d2;
    d0[0] = 1.0; d1[0] = 0.0; d2[0] = -1.0;
    std::vector<Vec> f1 = { d0, d1, d2 };

    gte::NaturalQuinticSpline<1, Real> spline(true, f0, f1, times);
    auto const& c = spline.GetPolynomials()[0];
    Real c4 = c[4][0], c5 = c[5][0];

    // Analytically: p''''(u) = 24*c4 + 120*c5*u, p'''''(u) = 120*c5
    Real u = 0.7;
    Real d4_expected = 24.0 * c4 + 120.0 * c5 * u;
    Real d5_expected = 120.0 * c5;

    Vec jet[6];
    spline.Evaluate(0.7, 5, jet);
    return std::fabs(jet[4][0] - d4_expected) < 1e-10
        && std::fabs(jet[5][0] - d5_expected) < 1e-10;
}

int main()
{
    return test_derivatives_vs_coefficients() ? EXIT_SUCCESS : EXIT_FAILURE;
}

…uate

Lines 219 and 225 used factor 60 (correct for 3rd derivative: 5*4*3)
where 120 (= 5*4*3*2 = 5!) is needed for the 4th and 5th derivatives.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant