This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def greatest_common_divisor(a, b): | |
""" | |
除非 b == 0,否則結果將與 b 具有相同的正負號 | |
(因此,當 b 除以它,結果是正號) | |
""" | |
while b > 0: | |
a, b = b, a % b | |
return a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
a = np.array([ | |
[1, -2, -1], | |
[2, -1, 1], | |
[3, -6, -5] | |
]) | |
a_inv = np.linalg.inv(a) | |
print("A:") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
a = np.array([ | |
[5, 12], | |
[15, 25] | |
]) | |
b = np.array( | |
[20, 10] | |
) |