Instantly share code, notes, and snippets.

View ModularMultiplicativeInverse.py
def greatest_common_divisor_extended(a, b):
'''
擴展歐幾里得算法 (Extended Euclidean algorithm)
'''
if b == 0:
return a, 1, 0
gcd, y, x = greatest_common_divisor_extended(b, a % b)
return gcd, x, y - (a // b) * x
View GreatestCommonDivisor.py
def greatest_common_divisor(a, b):
"""
除非 b == 0,否則結果將與 b 具有相同的正負號
(因此,當 b 除以它,結果是正號)
"""
while b > 0:
a, b = b, a % b
return a
View MatrixInverse.py
import numpy as np
a = np.array([
[1, -2, -1],
[2, -1, 1],
[3, -6, -5]
])
a_inv = np.linalg.inv(a)
print("A:")
View Matrix.py
import numpy as np
a = np.array([
[5, 12],
[15, 25]
])
b = np.array(
[20, 10]
)