サブ inv.js   Last modified: Mar 25, 2004
// 逆行列を求める関数 inv.js

function inv(r, ip, epsinv)
{
  var i, j, k, b, t, m, l
  m = new Array(r)
  l = new Array(r)
  for (k = 0; k < ip; k++) {
    m[k] = l[k] = k; b = r[k][k]
    for (j = k; j < ip; j++) {
      for (i = k; i < ip; i++) {
        if (Math.abs(r[j][i]) > Math.abs(b)) {
          b = r[j][i]
          l[k] = i
          m[k] = j
        }
      }
    }
    if (Math.abs(b) < epsinv) {
      return 1
    }
    j = l[k]
    if (j > k) {
      for (i = 0; i < ip; i++) {
        t = -r[i][k]
        r[i][k] = r[i][j]
        r[i][j] = t
      }
    }
    i = m[k]
    if (i > k) {
      for (j = 0; j < ip; j++) {
        t = -r[k][j]
        r[k][j] = r[i][j]
        r[i][j] = t
      }
    }
    for (i = 0; i < ip; i++) {
      if (i != k) {
        r[k][i] /= -b
      }
    }
    for (i = 0; i < ip; i++) {
      for (j = 0; j < ip; j++) {
        if (i != k && j != k) {
          r[j][i] += r[k][i]*r[j][k]
        }
      }
    }
    for (j = 0; j < ip; j++) {
      if (j != k) {
        r[j][k] /= b
      }
    }
    r[k][k] = 1.0/b
   }
   for (k = ip-2; k >= 0; k--) {
    i = l[k]
    if (i > k) {
      for (j = 0; j < ip; j++) {
        t = r[k][j]
        r[k][j] = -r[i][j]
        r[i][j] = t
      }
    }
    j = m[k]
    if (j > k) {
      for (i = 0; i < ip; i++) {
        t = r[i][k]
        r[i][k] = -r[i][j]
        r[i][j] = t
      }
    }
  }
  return 0
}


・ 直前のページへ戻る  ・ E-mail to Shigenobu AOKI

Made with Macintosh