# Revision history [back]

I am not sure i understand your question. Given a list mydata of pairs (point, value), you want to find a polynomial P with integer coefficients such that P(point) == value for each element of mydata ?

If this is your question, you can do it with polynomials with coefficients on RationalField:

sage: mydata = [(0,0),(2,1),(4,6)]
sage: R = PolynomialRing(QQ, 'x')
sage: P = R.lagrange_polynomial(mydata); P
1/2*x^2 - 1/2*x


This will not work if you replace QQ by ZZ, because Lagrange interpolation needs to make divisions. Note also that it is not sufficient to require that the pairs (point, value) are integers to ensure that P will have integer coefficients. For example, there is no polynomial $P$ with integer coefficients such that $P(0) = 0$ and $P(2) = 1$.

Now, if you want to approximate (not interpolate) you data with polynomials, you need to be more precise about the measure of the quality of the approximation. For example, if you are looking for a leas square approximation, you can hage a look at polyfit (but the coefficients will be floating points).

I am not sure i understand your question. Given a list mydata of pairs (point, value), you want to find a polynomial P with integer coefficients such that P(point) == value for each element of mydata ?

If this is your question, you can do it with polynomials with coefficients on RationalField:

sage: mydata = [(0,0),(2,1),(4,6)]
sage: R = PolynomialRing(QQ, 'x')
sage: P = R.lagrange_polynomial(mydata); P
1/2*x^2 - 1/2*x


This will not work if you replace QQ by ZZ, because Lagrange interpolation needs to make divisions. Note also that it is not sufficient to require that the pairs (point, value) are integers to ensure that P will have integer coefficients. For example, there is no polynomial $P$ with integer coefficients such that $P(0) = 0$ and $P(2) = 1$.1$(the first condition implies that the variable$X$divides$P$, which implies that$P(2)$is even). Now, if you want to approximate (not interpolate) you data with polynomials, you need to be more precise about the measure of the quality of the approximation. For example, if you are looking for a leas square approximation, you can hage a look at polyfit (but the coefficients will be floating points). I am not sure i understand your question. Given a list mydata of pairs (point, value), you want to find a polynomial P with integer coefficients such that P(point) == value for each element of mydata ? If this is your question, you can do it with polynomials with coefficients on RationalField: sage: mydata = [(0,0),(2,1),(4,6)] sage: R = PolynomialRing(QQ, 'x') sage: P = R.lagrange_polynomial(mydata); P 1/2*x^2 - 1/2*x  This will not work if you replace QQ by ZZ, because Lagrange interpolation needs to make divisions. Note also that it is not sufficient to require that the pairs (point, value) are integers to ensure that P will have integer coefficients. For example, there is no polynomial$P$with integer coefficients such that$P(0) = 0$and$P(2) = 1$(the (otherwise the first condition implies that the variable$X$divides$P$, which implies that$P(2)\$ is even).

Now, if you want to approximate (not interpolate) you data with polynomials, you need to be more precise about the measure of the quality of the approximation. For example, if you are looking for a leas square approximation, you can hage a look at polyfit (but the coefficients will be floating points).