using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace vcsGJMAT { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //------------------------------------------------------------------- private void toolStripButton1_Click(object sender, EventArgs e) { int i; int n = 2; double[,] a = new double[3, 4]; a[0, 0] = 2.0; a[0, 1] = 3.0; a[0, 2] = 1.0; a[0, 3] = 4.0; a[1, 0] = 4.0; a[1, 1] = 1.0; a[1, 2] = -3.0; a[1, 3] = -2.0; a[2, 0] = -1.0; a[2, 1] = 2.0; a[2, 2] = 2.0; a[2, 3] = 2.0; MATGJ(n, a); for (i = 0; i <= n; i++) { Console.WriteLine(i.ToString("0") + ":" + a[i, n + 1].ToString("0.000")); } //連立一次方程式 [a]{x}={y}の解{x}を求める.a(i,j)=[a|y]を入力する //入力 出力結果 //[a]={ 2, 3, 1} {y}={ 4} {x}={ 2} // { 4, 1,-3} {-2} {-1} // {-1, 2, 2} { 2] { 3} } //------------------------------------------------------------------- private void MATGJ(int n,double[,] a) { //Gauss-Jordan法による連立一次方程式の解法 int i,j, k, s; double p, d, max, dumy; for (k = 0; k <= n; k++) { max = 0.0; s = k; for (j = k; j <= n; j++) { if (Math.Abs(a[j, k]) > max) { max = Math.Abs(a[j, k]); s = j; } } for (j = 0; j <= n + 1; j++) { dumy = a[k, j]; a[k, j] = a[s, j]; a[s, j] = dumy; } p = a[k, k]; for (j = k; j <= n + 1; j++) { a[k, j] = a[k, j] / p; } for (i = 0; i <= n; i++) { if (i != k) { d = a[i, k]; for (j = k; j <= n + 1; j++) { a[i, j] = a[i, j] - d * a[k, j]; } } } } } //------------------------------------------------------------------- } }