Test whether a given function is polynomial. You have a black box function to which you can give real number inputs and from which you can receive real number outputs. How would you test whether it is likely to be a polynomial?

Vincent Norman

Vincent Norman

Answered question

2022-10-29

Test whether a given function is polynomial
You have a black box function to which you can give real number inputs and from which you can receive real number outputs. How would you test whether it is likely to be a polynomial?
One expensive idea is to use finite differences:
1. Choose a maximum degree n of the "polynomial" you are testing.
2. Choose a consecutive sequence with random step size, and evaluate the function there to get an output sequence. E.g.,
[ 2 , 2.3 , 2.6 , 2.9 , ] [ 4.81 , 5.02 , 5.05 , 4.90 , ]
3. Using the output sequence as S[0], define S[n] so that its k-th entry S [ n ] [ k ] = S [ n 1 ] [ k + 1 ] S [ n 1 ] [ k ] . E . g . S [ 1 ] = [ 5.02 4.81 , 5.05 5.02 , 4.90 5.05 , . . . ] = [ 0.21 , 0.03 , 0.15 , . . . ]
4. If the function is a polynomial (of degree at most n), then the sequence S [ n + 1 ] should be all zeros.
Some issues about programming this method:
Would be expensive for large n
If S[0] has large values, computer arithmetic will produce bad results for S[1] and beyond.

Answer & Explanation

cesantedz

cesantedz

Beginner2022-10-30Added 12 answers

Step 1
I don't know much about "likely". But N + 1 distinct points uniquely determine a degree N polynomial by Lagrange interpolation. If the obtained points are ( x i , y i ), for i = 0 , N, then first define:
L N , j ( x ) = k j N x x k x j x k
(which is clearly a polynomial of degree N). The unique polynomial passing through those points is
p ( x ) = j = 0 n y j L N , j ( x ) ,
which is a polynomial of degree at most N. If you compute one more point from your black box, then you can compare it with this polynomial to see whether it fits.
By the same argument, there is no way to deterministically rule out polynomials given a finite set of input values, since the polynomial described above can always be constructed.
Step 2
If you choose a constant step size (like 1), your p ( x N + 1 ) may be easy to calculate. In that case, you would need to only compute
j = 0 n y j j k ( x N + 1 x j ) = j = 0 n y j j k ( N + 1 j )
which doesn't sound over-complex.
cimithe4c

cimithe4c

Beginner2022-10-31Added 3 answers

Step 1
I think the largest problem here is that given things like Taylor series, every function can eventually be closely approximated as a polynomial. As such, even a finite range of measured outputs of a blackbox representing a sine function will might yield a polynomial. Measuring a large range is computationally difficult as higher powers tend to yield very large values for increasing inputs. Anyway, this is what I would do: Suppose you an input set x R n + 1 × 1 and the corresponding output set S R n + 1 × 1 , where n is the estimated polynomial order (or better, larger than). Then the polynomial itself can be estimated as follows:
1. create a square matrix X = [ x 0 x 1 x 2 . . . x n ] R n × n containing numerical powers of the input vector.
2. Suppose we construct A = [ a 0 a 1 . . . a n ] T representing the black box polynomial parameters such that f ( x ) = a 0 + a 1 x + a 2 x 2 + . . . + a n x n . Then f ( x ) = X A.
3. use the output vector S to compute A as follows: A = X 1 S, provided that X is invertible (which should be the case if all entries of x are unique).
Step 2
If the black box does represent a perfect polynomial, the resulting A vector should be unique and every entry with a higher order than this polynomial should be a flat zero (or approaching floating precision). The uniqueness can be verified by plugging in various different input sets.
This method can become just as troublesome for very large orders of n and can yield just an estimated Taylor series of the function (at least I tested it with a sine function and it did return the Taylor series expansion of it, where the even powers had a parameters smaller than 1 e 7 ).

Do you have a similar question?

Recalculate according to your conditions!

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?