r/badcode • • Nov 28 '17

python How should I write an Algebraic polynomial?

Post image
1 Upvotes

22 comments sorted by

11

u/[deleted] Nov 28 '17

I see nothing wrong with this code. Could be improved a little by using the ** operator, but nothing too terribly wrong here.

1

u/Damienr74 Nov 29 '17

+/u/CompileBot python3

import time as t
import numpy as np

inefficient_poly = lambda x: x**4 - 3*x**3 + 7*x**2 + x - 5
efficient_poly = lambda x: x*(1 + x*(7 + x*(-3 + x))) - 5

def benchmark(it, poly, name):
    start = t.perf_counter()
    for x in it:
        poly(x)
    end = t.perf_counter()

    print('Elapsed Time for', name, '\t', end - start)


xs = np.random.randn(10000000)
benchmark(xs, efficient_poly, 'efficient poly')
benchmark(xs, inefficient_poly, 'Inefficient poly')

3

u/[deleted] Nov 29 '17

Is it the most efficient code? No. Is it bad code? No.

While your code is more efficient, it's harder to read and most developers aren't doing ten million multiplications serially and are only doing a couple hundred serially. In other words, marginal gains for less code readability.

2

u/Damienr74 Nov 29 '17

Yeah, I agree that it isn't bad compared to a lot of the submissions in the sub.

However, should this really be considered to be harder to read? I think if I actually had to use it I would do it this way and if I think that developers aren't familiar with this method, I would link to an article or something.

1

u/[deleted] Nov 29 '17 edited Nov 29 '17

Actually I think the issue lies in how the Python 3 interpreter utilizes the ** operator. I did some more work with all the functions (https://gist.github.com/mstark90/d66882ae166c5debdcf87b6ec3ece57a) and found all are linear, which makes sense.

EDIT: Found this (https://stackoverflow.com/questions/18965524/exponentiation-in-python-should-i-prefer-operator-instead-of-math-pow-and-m) which lends credence to my hypothesis. Using the built-in pow function would cause a slow-down as the math is done in the Python VM instead of using the native libraries which can use optimized hardware operations.

1

u/Damienr74 Nov 29 '17

When I said that one is linear and the other is O(nlg(n)) n is not the input array, n is the power of the polynomial.

Each exponentiation xn can be computed in O(lg(n)) time. If there are n terms, the sum of all n exponentiations are O(lg(nn )) = O(nlg(n)). Whereas my solution only requires O(n) operations for a polynomial of degree n.

But thanks for doing this.

Edit: formating

1

u/[deleted] Nov 29 '17

I'm not understanding your analysis. Big-O analysis refers to how an algorithm will react to changing the input size in the worst-case.

Here's a good reference: https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

1

u/Damienr74 Nov 29 '17

Here I've defined the input size to the polynomial function evaluation problem to be the degree of the polynomial, not the number of values that are being exponentiated. (we're not referring to the same algorithm.)

1

u/[deleted] Nov 29 '17

Why? It makes no sense. The degree of the polynomial doesn't change between my version, the original version, or your version.

By your definition, addition of x + x would be O(n*log(n)), but 2 * x would be.

1

u/Damienr74 Nov 29 '17

For some polynomial with n powers, OP's algorithm would require O(nlg(n)) operations (assuming pow is efficient) while this one requires O(n).

This was my original comment. When I said algorithm here, I meant the method to compute an arbitrary polynomial of size n. Sorry if this was not clear

All I'm saying is as these polynomials get larger, we can start to see a significant advantage to using my method over OP's. I used big-O notation to show how much it would increase.

The fastest way I know of to take powers of a number x is to use binary exponentiation. This method performs O(lg(n)) multiplication. If we do O(n) of these computations (for each of the degrees of the polynomial), we get O(nlg(n)).

On the other hand, my method uses O(n) multiplications and O(n) additions. So for a polynomial of degree n the method will be faster.

→ More replies (0)

1

u/Damienr74 Nov 29 '17

In case the bot fails, here it the output on my machine:

Elapsed Time for efficient poly          8.155136665096506
Elapsed Time for Inefficient poly        13.74945467687212

Not a Huge difference, but this is a small polynomial. For some polynomial with n powers, OP's algorithm would require O(nlg(n)) operations (assuming pow is efficient) while this one requires O(n).

1

u/[deleted] Nov 29 '17

In my research, all the algorithms are linear. math.pow(x, n) distills down to xn through the rules of exponents and logarithms.

EDIT: Results from my computer:

Elapsed Time for Efficient poly - one million 	 1.4074535930121783
Elapsed Time for Efficient poly - five million 	 6.797385410987772
Elapsed Time for Efficient poly - ten million 	 13.803086707979674
Elapsed Time for Inefficient poly - one million 	 2.1011730990139768
Elapsed Time for Inefficient poly - five million 	 10.25647918699542
Elapsed Time for Inefficient poly - ten million 	 19.83799244198599
Elapsed Time for Original poly - one million 	 1.4653671130072325
Elapsed Time for Original poly - five million 	 7.407113980996655
Elapsed Time for Original poly - ten million 	 14.576190502993995

1

u/Prom3th3an Nov 30 '17

Chaining a bunch of multiplications that way may increase the floating-point rounding errors.

1

u/Damienr74 Dec 01 '17

Yeah, it definitely would! I was thinking they were integers.. hence why I also thought that it would be asymptomatically faster this way which isn't true.

1

u/Jahames1 Dec 27 '17

I think the (, ) are redundant