39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
class Object(dict):
|
|
def __getattr__(self, key):
|
|
return self[key]
|
|
def __setattr__(self, key, value):
|
|
self[key] = value
|
|
|
|
# -- Line calculations
|
|
|
|
from sympy import Line, Point
|
|
def intersection(x1, y1, x2, y2, x3, y3, x4, y4):
|
|
p = Line(Point(x1, y1), Point(x2, y2)).intersection(Line(Point(x3, y3), Point(x4, y4)))[0]
|
|
x = float(p.x)
|
|
y = float(p.y)
|
|
return x, y
|
|
|
|
def x_axis_intersection(x1, y1, x2, y2):
|
|
return intersection(x1, y1, x2, y2, 0, 0, 1, 0)
|
|
|
|
def y_axis_intersection(x1, y1, x2, y2):
|
|
return intersection(x1, y1, x2, y2, 0, 0, 0, 1)
|
|
|
|
def multiplicative_inverse_over_prime_finite_field(a, p):
|
|
"""
|
|
NOTE: GIGO if not is_prime(p)
|
|
"""
|
|
def extended_gcd(a, b):
|
|
if a == 0:
|
|
return b, 0, 1
|
|
else:
|
|
gcd, x, y = extended_gcd(b % a, a)
|
|
return gcd, y - (b // a) * x, x
|
|
gcd, x, _ = extended_gcd(a, p)
|
|
return x % p
|
|
|
|
def print_multiplicative_inverse_over_prime_finite_field_table(p):
|
|
for i in range(1, p):
|
|
inverse = multiplicative_inverse_over_prime_finite_field(i, p)
|
|
print(i, inverse, (i * inverse) % p)
|