Live data from Hacker News

Implementing Advanced Math Functions

spin.atomicobject.com

11–20 of 24 posts

Re: Implementing Advanced Math Functions

#11

Unfortunately, Taylor series are just about the worst choice possible for implementing transcendental math functions, due to the highly non-uniform error distribution. You end up doing far to much work to get good accuracy near the edges of the domain of interest. Much better choices of polynomial approximations for almost any such situation are (in increasing order of goodness and difficulty to work with): Chebyshev…

How do you define "minimax" such that it's not a Chebyshev series?

Re: Implementing Advanced Math Functions

#12

Unfortunately, Taylor series are just about the worst choice possible for implementing transcendental math functions, due to the highly non-uniform error distribution. You end up doing far to much work to get good accuracy near the edges of the domain of interest. Much better choices of polynomial approximations for almost any such situation are (in increasing order of goodness and difficulty to work with): Chebyshev…

Taylor series is fine for implementing arbitrary precision transcendental functions though. I found this Python implementation enlightening.

http://code.google.com/p/mpmath/source/browse/trunk/mpmath/l...

Re: Implementing Advanced Math Functions

#13

Unfortunately, Taylor series are just about the worst choice possible for implementing transcendental math functions, due to the highly non-uniform error distribution. You end up doing far to much work to get good accuracy near the edges of the domain of interest. Much better choices of polynomial approximations for almost any such situation are (in increasing order of goodness and difficulty to work with): Chebyshev…

How do you define "minimax" such that it's not a Chebyshev series?

The minimax polynomial minimizes the maximum error (the L-inf norm). The truncated Chebyshev series of an arbitrary function does not have that property (though it often comes close).

Re: Implementing Advanced Math Functions

#14
post #12

Unfortunately, Taylor series are just about the worst choice possible for implementing transcendental math functions, due to the highly non-uniform error distribution. You end up doing far to much work to get good accuracy near the edges of the domain of interest. Much better choices of polynomial approximations for almost any such situation are (in increasing order of goodness and difficulty to work with): Chebyshev…

Taylor series is fine for implementing arbitrary precision transcendental functions though. I found this Python implementation enlightening. http://code.google.com/p/mpmath/source/browse/trunk/mpmath/l...

Yes, arbitrary-precision approximations are one area in which they are appropriate.

Re: Implementing Advanced Math Functions

#15

Earlier quoted context omitted.

How do you define "minimax" such that it's not a Chebyshev series?

The minimax polynomial minimizes the maximum error (the L-inf norm). The truncated Chebyshev series of an arbitrary function does not have that property (though it often comes close).

I thought the truncated Chebyshev series of an arbitrary function minimizes the L-inf error among all polynomials of the same degree, because Chebyshev polynomials have minimum L-inf norm among all monic polynomials of the same degree.

Let's try this: Tell me how you would construct a minimax approximation according to your definition?

Re: Implementing Advanced Math Functions

#16

Earlier quoted context omitted.

The minimax polynomial minimizes the maximum error (the L-inf norm). The truncated Chebyshev series of an arbitrary function does not have that property (though it often comes close).

I thought the truncated Chebyshev series of an arbitrary function minimizes the L-inf error among all polynomials of the same degree, because Chebyshev polynomials have minimum L-inf norm among all monic polynomials of the same degree. Let's try this: Tell me how you would construct a minimax approximation according to your definition?

The truncated Chebyshev series does not generally minimize the L-inf error among all polynomials of the same degree (unless the function being approximated is a polynomial of degree n+1). Constructing a minimax approximation is typically done via the Remez exchange algorithm, which is iterative, fussy, and prone to convergence failures. However, none of those matter when designing an offline approximation as is the case when you're writing math library functions.

Re: Implementing Advanced Math Functions

#17
post #9

Earlier quoted context omitted.

If your goal is accuracy, you are absolutely correct. But in the instances where I've wanted to implement my own versions of transcendental math functions, accuracy was actually pretty low on my list of priorities. Often just a couple terms of a taylor series was accurate enough, fast, and easy to implement.

It's true regardless of what your goal is (unless your goal is to play with Taylor series approximations). Even if you're going purely for speed, you can often replace a third-order Taylor series with a second-order minimax polynomial, and deliver the same or better accuracy with significantly fewer operations.

Do you know of good resource for how to compute minimax polynomials that approximate transcendental functions? My Google foo appears to be failing me.

Re: Implementing Advanced Math Functions

#18
post #9

Earlier quoted context omitted.

If your goal is accuracy, you are absolutely correct. But in the instances where I've wanted to implement my own versions of transcendental math functions, accuracy was actually pretty low on my list of priorities. Often just a couple terms of a taylor series was accurate enough, fast, and easy to implement.

It's true regardless of what your goal is (unless your goal is to play with Taylor series approximations). Even if you're going purely for speed, you can often replace a third-order Taylor series with a second-order minimax polynomial, and deliver the same or better accuracy with significantly fewer operations.

[deleted]

Re: Implementing Advanced Math Functions

#19

Earlier quoted context omitted.

It's true regardless of what your goal is (unless your goal is to play with Taylor series approximations). Even if you're going purely for speed, you can often replace a third-order Taylor series with a second-order minimax polynomial, and deliver the same or better accuracy with significantly fewer operations.

Do you know of good resource for how to compute minimax polynomials that approximate transcendental functions? My Google foo appears to be failing me.

Look up the remez algorithm

Re: Implementing Advanced Math Functions

#20
I tried to write a library to calculate vectorized trig functions. Minimax or least-squares polynomials are much much better than Taylor series. But here's the thing: The majority of the computation required is not in the calculation of the kernel (i.e. f(x) for -pi/2If you're interested, I have the test code here: https://github.com/jeremysalwen/vectrig
Post reply on HN