diff --git a/include/mcl/lagrange.hpp b/include/mcl/lagrange.hpp index feca80d..18e0597 100644 --- a/include/mcl/lagrange.hpp +++ b/include/mcl/lagrange.hpp @@ -63,15 +63,20 @@ void LagrangeInterpolation(bool *pb, G& out, const F *S, const G *vec, size_t k) /* out = f(x) = c[0] + c[1] * x + c[2] * x^2 + ... + c[cSize - 1] * x^(cSize - 1) - @retval 0 if succeed else -1 + @retval 0 if succeed else -1 (if cSize == 0) */ template void evaluatePolynomial(bool *pb, G& out, const G *c, size_t cSize, const T& x) { - if (cSize < 2) { + if (cSize == 0) { *pb = false; return; } + if (cSize == 1) { + out = c[0]; + *pb = true; + return; + } G y = c[cSize - 1]; for (int i = (int)cSize - 2; i >= 0; i--) { G::mul(y, y, x); diff --git a/test/bench.hpp b/test/bench.hpp index 12868d3..cc1639e 100644 --- a/test/bench.hpp +++ b/test/bench.hpp @@ -187,4 +187,6 @@ void testLagrange() CYBOZU_TEST_EQUAL(s, c[0]); mcl::LagrangeInterpolation(s, x, y, 1); CYBOZU_TEST_EQUAL(s, y[0]); + mcl::evaluatePolynomial(y[0], c, 1, x[0]); + CYBOZU_TEST_EQUAL(y[0], c[0]); }