12
1
Write a program that performs Polynomial Interpolation using true arbitrary precision rational numbers. The input looks like this:
f(1) = 2/3 f(2) = 4/5 f(3) = 6/7 ...
You may assume that there's exactly one whitespace before and after the =
sign, all the numbers are either fractions or integers. You may also assume, that all fraction in the input are already irreducible.
No error checking is needed, you may assume, that the input is valid and no x is doubled in the f(x).
The output should be in a LaTeX compatible form, the emitted LaTeX code should yield the same graphical representation as the output given here.
f(x) = 123x^2 + \frac{45}{2}x + \frac{7}{4}
The fraction must be reduced as much as possible, eg. something like \frac{2}{4}
is not allowed. If the number is integer, don't use a fraction.
Special rules:
Your program should...
- work for polynomials up to degree 12
- complete in less then 1 minute for reasonable input
- not use any functions that do the whole calculation for you
- output the polynomial of smallest possible degree
Testcases:
The given testcases are just for clarification. Your program should yield correct result for all correct inputs.
Input
f(1) = 2/3 f(2) = 4/5 f(3) = 6/7
Output
f(x) = - \frac{4}{105}x^2 + \frac{26}{105}x + \frac{16}{35}
Input
f(-12) = 13/2 f(5/3) = 3/5 f(13) = -6 f(1/5) = -3/4
Output
f(x) = - \frac{2186133}{239455744}x^3 + \frac{2741731}{149659840}x^2 + \frac{26720517}{29201920}x - \frac{279464297}{299319680}
Input
f(4/3) = 617/81 f(2) = 20/3 f(-8/3) = 6749/81 f(-5) = 7367/12 f(0) = 23/3
Output
f(x) = \frac{1}{2}x^4 - 2x^3 + \frac{7}{4}x^2 + \frac{23}{3}
Input
f(0) = 5 f(1) = 7 f(2) = 9 f(3) = 11 f(4) = 13
Output
f(x) = 2x + 5
Input
f(1/2) = -1/2 f(-25) = -1/2 f(-54/12) = -1/2
Output
f(x) = -\frac{1}{2}
Why are you talking about real numbers if all you're ever using are rational numbers? – Joey – 2011-03-21T16:17:10.500
Sorry. My english is poor. Yes, only use rational numbers. The results have to be exact. – FUZxxl – 2011-03-21T16:22:29.973
In the first testcase, are dots (
...
) really part of the input? – Eelvex – 2011-03-21T18:01:29.013@Eelvex: Nope. Fixed. – FUZxxl – 2011-03-21T18:03:32.850
The output for the third testcase is wrong. The correct answer is
-\frac{37745}{14592}x^4 - \frac{853249}{43776}x^3 + \frac{57809}{7296}x^2 + \frac{225205}{2736}x + \frac{23}{3}
. I suspect the input was intended to be something different :) – Timwi – 2011-03-21T20:11:14.130The second testcase is also wrong. The correct answer is
f(x) = -\frac{10270401}{1197278720}x^3 + \frac{17890327}{748299200}x^2 + \frac{131825777}{146009600}x - \frac{278804033}{299319680}
. – Timwi – 2011-03-21T20:20:24.587@Timwi: They are not wrong, they are equivalent. – Eelvex – 2011-03-21T20:33:05.433
@Eelvex: They are wrong. They are different polynomials, and they don’t interpolate the input points. In what way did you think they could be equivalent? – Timwi – 2011-03-21T21:26:20.183
@Timwi @Elvex:Hm... That's strange. I'm going to write a reference implementation tomorrow. I obtained these values using Wolfram Alpha and ouble checked them using bc. I don't know, where the mistake is. – FUZxxl – 2011-03-21T21:27:37.650
@Timwi: They correspond to different polynomials that pass through the same given points. eg. if
f(x) = 1/2x^4 - 2x^3 + 7/4x^2 + 23/3
thenf(0)=23/3, f(-5)=7367/12,
etc. – Eelvex – 2011-03-21T22:02:24.860@Eelvex: But they don’t pass through the same points. (That is impossible because there is no degree of freedom.) For the third testcase,
f(4/3) = 617/81
instead of the617/8
given. For the second testcase,f(13) = -6
and not the-12/3
given. (I guess now we know that the test input is probably wrong, rather than the output.) – Timwi – 2011-03-22T00:48:29.073@Timwi: Ah, you're right. – Eelvex – 2011-03-22T01:07:20.957
@Timwi@Eelve: Fied up the testcases – FUZxxl – 2011-03-22T08:52:38.040