Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205399

Convolution of polynomials returns unexpected value

$
0
0

I'm using R 3.6.2 (platform = x86_64-w64-mingw32)

In the convolution of the following two polynomial coefficient vectors, I would expect the first entry to be exactly 1.0, but the convolve function differs:

g <- c(1, -49, 1155, -17441, 189700, -1583071, 10545901, -57608692, 
263063351, -1018546561, 3380085631, -9693547553, 24176423345, 
-52691112850)
u <- c(1, -6, 11, -6)
convolve(g, rev(u), type = 'o')
# output
 [1]  1.000172e+00 -5.500020e+01  1.460000e+03 -2.491600e+04
 [5]  3.073450e+05 -2.920052e+06  2.223567e+07 -1.394361e+08
 [9]  7.342188e+08 -3.293898e+09  1.273071e+10 -4.275645e+10
[13]  1.256299e+11 -3.246592e+11  6.402486e+11 -7.246608e+11
[17]  3.161467e+11

Note that the first entry in the result is 1.000172, not 1.0.

Performing the same convolution in Python 3.7.4 delivers the expected answer:

import numpy as np
g = [1, -49, 1155, -17441, 189700, -1583071, 10545901, -57608692, 263063351, -1018546561, 3380085631, -9693547553, 24176423345, -52691112850]
u = [1, -6, 11, -6]
np.convolve(g,u)
array([            1,           -55,          1460,        -24916,
              307345,      -2920052,      22235673,    -139436079,
           734218840,   -3293897685,   12730714010,  -42756453616,
        125629929970, -324659189789,  640248619213, -724660781420,
        316146677100], dtype=int64)

Also when I use the convolveCpp example from the Rcpp vignette, I get the same results as in the Python above.

Is there a rounding or precision issue with convolve or the underlying fft?


Viewing all articles
Browse latest Browse all 205399

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>