Gauss-Jordan method ガウス-ジョルダン法による逆行列の計算(ピボットなし’)
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
// サイズ NxN の行列 A の逆行列を計算する | |
// ピボット選択なし | |
// 実際にはnumeric javascript の numeric.inverse() や numeric.solve() を使うべき | |
function inverseGaussJordan(A, N) { | |
var U=numeric.clone(A); | |
var Ainv=identityMatrix(N); | |
var alpha; | |
var diagValue; | |
for(var i=0; i<N; ++i) { | |
diagValue=U[i][i]; | |
for(var j=0; j<N; ++j) { | |
U[i][j]/=diagValue; | |
Ainv[i][j]/=diagValue; | |
} | |
for(var j=0; j<N; ++j) { | |
if(i==j) { | |
continue; | |
} | |
alpha=U[j][i]/U[i][i]; | |
for(var k=0; k<N; ++k) { | |
U[j][k]-=alpha*U[i][k]; | |
Ainv[j][k]-=alpha*Ainv[i][k]; | |
} | |
} | |
} | |
return Ainv; | |
} | |
function identityMatrix(size) { | |
var tmp=new Array(size); | |
for(var i=0; i<size; ++i) { | |
tmp[i]=new Array(size); | |
for(var j=0; j<size; ++j) { | |
if(i==j) { | |
tmp[i][j]=1; | |
} else { | |
tmp[i][j]=0; | |
} | |
} | |
} | |
return tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment