• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonwindowsphpgamebathyscapheguic翻訳omegattwitterframeworkbtronvb.net計画中(planning stage)testdomarduinodirectxpreviewerゲームエンジン

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


Commit MetaInfo

Revision28c4e0ee567abd8d3f930dc94b20729a4c91061e (tree)
Time2020-02-21 07:02:28
AuthorEric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Better docstrings, and make mult_inverses print not return string.

Change Summary

Incremental Difference

diff -r 698f6818f630 -r 28c4e0ee567a numtheory_utils.py
--- a/numtheory_utils.py Wed Feb 19 20:27:56 2020 -0800
+++ b/numtheory_utils.py Thu Feb 20 14:02:28 2020 -0800
@@ -1,4 +1,13 @@
11 def extended_gcd(x, y):
2+ """Return a tuple 't' with three elements such that:
3+ t[0) * x + t[1] * y == t[2]
4+
5+ t[2] will be either 0 or 1. If it is zero, then x and y are not
6+ relatively prime. If it is one, then they are.
7+
8+ This makes use of Euclid's algorithm for finding the GCD, but extends it
9+ to keep track of the extra data returned in t[0] and t[1].
10+ """
211 sx = 1 if x > 0 else -1
312 x = abs(x)
413 sy = 1 if y > 0 else -1
@@ -56,7 +65,10 @@
5665 print()
5766
5867
59-def mult_inverses(a, b):
68+def print_mult_inverses(a, b):
69+ """Prints out the multiplicative inverse of a (mod b) and the multiplicative
70+ inverse of b (mod a).
71+ """
6072 am, bm, g = extended_gcd(a, b)
6173 if g == 0:
6274 raise ValueError(f"{a} and {b} are not relatively prime.")
@@ -64,5 +76,5 @@
6476 am = b + am
6577 if bm < 0:
6678 bm = a + bm
67- return f"{a} * {am} % {b} == {a * am % b}"\
68- f"\n{b} * {bm} % {a} == {b * bm % a}"
79+ print(f"{a} * {am} % {b} == {a * am % b}"\
80+ f"\n{b} * {bm} % {a} == {b * bm % a}")