aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2019-02-20 09:29:47 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2019-02-20 09:29:47 +0800
commitb401313c71119e48f9e2edd4484c416b7ca349cc (patch)
treed62144c6a23d5e423b7d0daa3f77247ca7e66d54
parent08148dcf605117f495545cdf3ce29afb1e9c963e (diff)
downloadtangerine-mcl-b401313c71119e48f9e2edd4484c416b7ca349cc.tar.gz
tangerine-mcl-b401313c71119e48f9e2edd4484c416b7ca349cc.tar.zst
tangerine-mcl-b401313c71119e48f9e2edd4484c416b7ca349cc.zip
evaluatePolynomial sets out = c[0] if cSize = 1
-rw-r--r--include/mcl/lagrange.hpp9
-rw-r--r--test/bench.hpp2
2 files changed, 9 insertions, 2 deletions
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<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);
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]);
}