Print(x/steps, mgf(x/steps), mgf(x/steps)/mgf(x/steps), mgf_sum(x/steps)/mgf(x/steps), mgf_fsum(x/steps)/mgf(x/steps), mgf_horner(x/steps)/mgf(x/steps), mgf_horner_sum(x/steps)/mgf(x/steps), mgf_horner_fsum(x/steps)/mgf(x/steps)) Return sum(p2(i) * x ** i for i in range(MAX_DEGREE, 0, -1))īut this rounding has an effect on the results: steps = 15 Return sum(p2(i) * x ** i for i in range(1, MAX_DEGREE)) Return math.fsum(p2(i) * x ** i for i in range(MAX_DEGREE, 0, -1)) Return math.fsum(p2(i) * x ** i for i in range(1, MAX_DEGREE)) Return sum((p_k(k, n) for k in range(1, n)), 1) you can use the built-in sum or math.fsum if you rewrite the formula's a bit.instead of reversed(range(1, MAX_DEGREE)) you can use range(MAX_DEGREE - 1, 0, -1).The main difference is the direction of the summation, which affects the float rounding errors. If you deconstruct the algorithms, your mgf and mgf_horner don't seem to do a lot different. I think most savings are already achieved by the memoization. Mpmath.cplot(mgf_horner, points=NB_POINTS, re=, im=) All these modifications led to speed ups, and I want to know if there are more optimizations I could do. I first wrote a naive version, added memoization of the sequence, changed the naive evaluation of the polynomial with the Horner algorithm. The more points I want (and the larger the degree is), the longer the plot takes. I want to plot (on the complex plane, with mpmath) the power series whose general term is the number of partitions of an integer.
0 Comments
Leave a Reply. |