def full_lagrange(xs, fxs): newxs = bytearray('') for item in xs: newxs.append(item) newfxs = bytearray('') for item in fxs: newfxs.append(item) return fastpolymath_c.full_lagrange(str(newxs), str(newfxs))
def _full_lagrange(xs, fxs): assert (len(xs) == len(fxs)) if fastpolymath and SPEEDUP: newxs = bytearray('') for item in xs: newxs.append(item) newfxs = bytearray('') for item in fxs: newfxs.append(item) return fastpolymath.full_lagrange(xs, fxs) returnedcoefficients = [] # we need to compute: # l_0 = (x - x_1) / (x_0 - x_1) * (x - x_2) / (x_0 - x_2) * ... # l_1 = (x - x_0) / (x_1 - x_0) * (x - x_2) / (x_1 - x_2) * ... for i in range(len(fxs)): this_polynomial = [1] # take the terms one at a time. # I'm computing the denominator and using it to compute the polynomial. for j in range(len(fxs)): # skip the i = jth term because that's how Lagrange works... if i == j: continue # I'm computing the denominator and using it to compute the polynomial. denominator = _gf256_sub(xs[i], xs[j]) # denominator = xs[i]-xs[j] # don't need to negate because -x = x in GF256 this_term = [ _gf256_div(xs[j], denominator), _gf256_div(1, denominator) ] # this_term = [-xs[j]/denominator, 1/denominator] # let's build the polynomial... this_polynomial = _multiply_polynomials(this_polynomial, this_term) # okay, now I've gone and computed the polynomial. I need to multiply it # by the result of f(x) this_polynomial = _multiply_polynomials(this_polynomial, [fxs[i]]) # we've solved this polynomial. We should add to the others. returnedcoefficients = _add_polynomials(returnedcoefficients, this_polynomial) return returnedcoefficients
def _full_lagrange(xs, fxs): assert(len(xs) == len(fxs)) if fastpolymath and SPEEDUP: newxs = bytearray('') for item in xs: newxs.append(item) newfxs = bytearray('') for item in fxs: newfxs.append(item) return fastpolymath.full_lagrange(xs, fxs) returnedcoefficients = [] # we need to compute: # l_0 = (x - x_1) / (x_0 - x_1) * (x - x_2) / (x_0 - x_2) * ... # l_1 = (x - x_0) / (x_1 - x_0) * (x - x_2) / (x_1 - x_2) * ... for i in range(len(fxs)): this_polynomial = [1] # take the terms one at a time. # I'm computing the denominator and using it to compute the polynomial. for j in range(len(fxs)): # skip the i = jth term because that's how Lagrange works... if i == j: continue # I'm computing the denominator and using it to compute the polynomial. denominator = _gf256_sub(xs[i], xs[j]) # denominator = xs[i]-xs[j] # don't need to negate because -x = x in GF256 this_term = [_gf256_div(xs[j], denominator), _gf256_div(1, denominator)] # this_term = [-xs[j]/denominator, 1/denominator] # let's build the polynomial... this_polynomial = _multiply_polynomials(this_polynomial, this_term) # okay, now I've gone and computed the polynomial. I need to multiply it # by the result of f(x) this_polynomial = _multiply_polynomials(this_polynomial, [fxs[i]]) # we've solved this polynomial. We should add to the others. returnedcoefficients = _add_polynomials(returnedcoefficients, this_polynomial) return returnedcoefficients
import fastpolymath_c print "[2,4,5] * [14,30,32] -> " x = fastpolymath_c.full_lagrange(chr(2)+chr(4)+chr(5),chr(14)+chr(30)+chr(32)) for c in x: print ord(c), print