Last active
August 29, 2015 13:59
-
-
Save odanado/10467266 to your computer and use it in GitHub Desktop.
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
template <typename T> | |
void exEuclidAlgo2(T a,T b,T& x,T &y) { | |
T x0,y0,u0,v0; | |
T x1,y1,u1,v1; | |
T r0,r1,r2; | |
T k; | |
r0 = a; | |
r1 = b; | |
k = r0 / r1; | |
r2 = r0 % r1; | |
x0 = k; | |
y0 = 1; | |
u0 = 1; | |
v0 = 0; | |
r0 = r1; | |
r1 = r2; | |
//ユークリッドの互除法実行 しつつ行列Aを計算 | |
while(r1 != 0) { | |
k = r0 / r1; | |
r2 = r0 % r1; | |
x1 = x0 * k + y0; | |
y1 = x0; | |
u1 = u0 * k + v0; | |
v1 = u0; | |
r0 = r1; | |
r1 = r2; | |
x0 = x1; | |
y0 = y1; | |
u0 = u1; | |
v0 = v1; | |
} | |
//逆行列の計算 | |
T d = x0*v0 - y0*u0; | |
x = v0/d; | |
y = -y0/d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment