Creating a string using python using loops with logical condition problems versus alternative join function?

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: Lagrange general formulaExpanded formula

0
1

, , . , , . l l:

from sympy import Symbol, prod

def lag_l(xx, j):
    x = Symbol("x")
    parts = ((x - x_i) / (xx[j] - x_i) for i, x_i in enumerate(xx) if i != j)
    return prod(parts)

def lag_L(xx, yy):
    return sum(y*lag_l(xx, j) for j, y in enumerate(yy))

def lag_fn(xx, yy, x):
    return lag_L(xx, yy).subs({'x': x})

:

>>> lag_l([1,2,3], 0)
(-x + 2)*(-x/2 + 3/2)
>>> lag_l([1,2,3], 1)
(-x + 3)*(x - 1)
>>> lag_l([1,2,3], 2)
(x/2 - 1/2)*(x - 2)

:

>>> lag_L([1,2,3],[1,4,9])
(-x + 2)*(-x/2 + 3/2) + 4*(-x + 3)*(x - 1) + 9*(x/2 - 1/2)*(x - 2)

( lag_fn):

>>> lag_fn([1,2,3],[1,4,9], 3)
9

.. , :

>>> from sympy import simplify
>>> simplify(lag_L([1,2,3],[1,4,9]))
x**2
+3

Source: https://habr.com/ru/post/1531024/


All Articles