# # Linear Equation (without pivoting) # def gj_pivot(x) row=x.size col=row+1 pivot=Array.new(row){|i| i} a=Array.new(row){|i| x[i].dup} for k in 0..(row-1) # find absolute-maximal coefficient # among a[k+1][k]..a[row-1][k] max = k for i in k+1..row-1 if a[pivot[i]][k].abs > a[pivot[max]][k].abs max = i end end # swap pivot[k] and pivot[max] tmp=pivot[max] pivot[max]=pivot[k] pivot[k]=tmp # normalize a[k][i] s.t. a[k][k]=1 akk = a[pivot[k]][k] for i in 0..(col-1) a[pivot[k]][i]=a[pivot[k]][i]/akk end # adjust i-th row s.t. a[i][k]=0, skipping k-th row for i in 0..(row-1) if i != k aik = a[pivot[i]][k] for j in k..(col-1) a[pivot[i]][j] = a[pivot[i]][j] - aik * a[pivot[k]][j] end end end end b=Array.new(row) for i in 0..(row-1) b[i]=a[pivot[i]][row] end b end