I encoded a function to generate Lagrange interpolation expressions.
So I got:
def polinomioLagrange(Xs, Ys, t):
for k in range(len(Xs)):
if k >0:
expresion = expresion + '+' + str(Ys[k]) + '*'
elif k==0:
expresion = expresion + str(Ys[k]) + '*'
expresion = expresion + '('
for i in range(len(Xs)):
if k==i:
continue
expresion = expresion + '(' + '3' + '-' + str(Xs[i]) + ')'
if k != len(Xs)-1 and i!= len(Xs)-1:
expresion=expresion+'*'
expresion = expresion + '/'
for i in range(len(Xs)):
if k==i:
continue
expresion = expresion + '(' + str(Xs[k]) + '-' + str(Xs[i]) + ')'
if i != len(Xs)-1 and k != len(Xs)-1:
expresion=expresion+'*'
print expresion
I call it, for example:, polinomioLagrange([0,1,2,4],[7,0,-1,63],3)and I get outputs similar to:
7*((3-1)*(3-2)*(3-4)/(0-1)*(0-2)*(0-4))+0*((3-0)*(3-2)*(3-4)/(1-0)*(1-2)*(1-4))+-1*((3-0)*(3-1)*(3-4)/(2-0)*(2-1)*(2-4))+63*((3-0)(3-1)(3-2)/(4-0)(4-1)(4-2))
As you can see, in the last member there are no stars:
63*((3-0)(3-1)(3-2)/(4-0)(4-1)(4-2))
What because
if k != len(Xs)-1 and i!= len(Xs)-1:
expresion=expresion+'*'
But I really played with the indices and got not quite the result that I want:
7*((3-1)*(3-2)*(3-4)/(0-1)*(0-2)*(0-4))+0*((3-0)*(3-2)*(3-4)/(1-0)*(1-2)*(1-4))+-1*((3-0)*(3-1)*(3-4)/(2-0)*(2-1)*(2-4))+63*((3-0)*(3-1)*(3-2)/(4-0)*(4-1)*(4-2))
Which logical condition will do the job and, what else, which is the easiest way using more complex python functions such as jointo do this?
The closest I got is the cyclical and changing logical conditions:
if i != len(Xs)-1:
expresion=expresion+'*'
Receiving:
7*((3-1)*(3-2)*(3-4)/(0-1)*(0-2)*(0-4))+0*((3-0)*(3-2)*(3-4)/(1-0)*(1-2)*(1-4))+-1*((3-0)*(3-1)*(3-4)/(2-0)*(2-1)*(2-4))+63*((3-0)*(3-1)*(3-2)*/(4-0)*(4-1)*(4-2)*)
Almost correct , with the exception of ...)*/(...and...4-2)*)
Just for the record, this is what I encode:

