• R/O
  • SSH

コミット

タグ

よく使われているワード(クリックで追加)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A small kernel of code for playing with Galois fields of arbitrary characteristic


コミットメタ情報

リビジョンa6fa33805b0aa13effbb5bd9202d50fb4e5d0035 (tree)
日時2017-06-17 02:12:59
作者Eric Hopper <hopper@omni...>
コミッターEric Hopper

ログメッセージ

Add a function to do Lagrange interpolation.

変更サマリ

差分

diff -r 70dafd911f2d -r a6fa33805b0a polyops.py
--- a/polyops.py Fri Jun 16 10:12:45 2017 -0700
+++ b/polyops.py Fri Jun 16 10:12:59 2017 -0700
@@ -168,3 +168,32 @@
168168 result += a_coef * xpower
169169 xpower *= x
170170 return result
171+
172+def lagrange_interp(points):
173+ from functools import reduce
174+ there_is_no_one = object()
175+
176+ def determine_mult_ident_and_numpts(points):
177+ numpts = 0
178+ one = there_is_no_one
179+ for x, y in points:
180+ if (one is there_is_no_one) and (type(x)() != x):
181+ one = x / x
182+ numpts += 1
183+ return (one, numpts)
184+
185+ (xone, numpts) = determine_mult_ident_and_numpts(points)
186+ if xone is there_is_no_one:
187+ if numpts > 1:
188+ raise ValueError("Impossible to interpolate.")
189+ else:
190+ return (next(iter(points))[1],)
191+ xzero = type(next(iter(points))[0])()
192+ master_poly = reduce(polymul,
193+ ((xzero - x, 1) for x, y in points),
194+ (xone,))
195+ xpolys = ((polydiv(master_poly, (xzero - x, xone))[0], x, y)
196+ for x, y in points)
197+ xpolys = (polyscalarmul(xp, y / evalpoly(xp, x))
198+ for xp, x, y in xpolys)
199+ return normalized(reduce(polyadd, xpolys, ()))