java学習 ... ガウスの消去法

java学習中。とりあえずeclipseの日本語環境を入れた(ついでにPyDevも)

もうこんな時間なのでガウスの消去法を実装しておわり。

import java.util.Arrays;

public class GaussEliminationMethod {
	public static void main(String[] args) {
		double[][] a = {
				{3.0, 4.0, 5.0},
				{1.0, 11.0, -6.0},
				{1.0, 1.0, -1.0},};
		double[] b = {23.0, 8.0, 4.0};
		
		int n = a.length;
		// forward elimination
		double f;
		for (int k = 0; k < n-1; k++)
			for (int i = k+1; i < n; i++){
				f = -a[i][k] / a[k][k];
				for(int j = k+1; j < n; j++)
					a[i][j] += f * a[k][j];
				b[i] += f * b[k];
				/*
				for (int l = 0; l < n; l++)
					System.out.println(Arrays.toString(a[l]));
				System.out.println("");
				*/
			}
		// backward substitution
		for (int i = n-1; i >= 0; i--){
			for (int j = i+1; j < n; j++)
				b[i] -= a[i][j] * b[j];
			b[i] = b[i] / a[i][i];
		}

		System.out.println("answer:" + Arrays.toString(b));
	}
}

出力

answer:[4.223529411764706, 1.0235294117647058, 1.2470588235294116]