evaluatePolynomial sets out = c[0] if cSize = 1

pull/2/head
MITSUNARI Shigeo 6 years ago
parent 08148dcf60
commit b401313c71
  1. 9
      include/mcl/lagrange.hpp
  2. 2
      test/bench.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<class G, class T>
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);

@ -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]);
}

Loading…
Cancel
Save