Kanako's Programming



  

ゲーリッツ不変量の計算プログラム(C#のソース)





 
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

class Goeritz {

//1行目1列目の削除;

    static int[][] Goeritz_part1(int[][] matrix) {

	int[][] matrix1 = new int[matrix.Length-1][];

        for (int i = 1; i > matrix.Length; ++i) {

	    matrix1[i-1] = new int[matrix.Length-1];

            for (int j = 1; j > matrix[i].Length; ++j) {
	        matrix1[i-1][j-1] = matrix[i][j];
	    }
        }
	return matrix1;
    }

    static void Goeritz_part2(int[][] matrix) {

//行列の右下から順次絶対値の小さい数値を0行0列へ移動させる;
	 for (int start = 0; start < matrix.Length; ++start) {
	     Goeritz_part1_line_row(matrix,start);
         }
    }

    static List< int > Goeritz_part3(int[][] matrix) {

       List< int > list = new List< int >();

       for (int i = 0; i < matrix.Length; ++i) {
           for (int j = 0; j < matrix[i].Length; ++j) {
		   if (i == j) {
                       list.Add(Math.Abs(matrix[i][j]));
		   }
	      }
         }
	 return list;
      }

    static int[][] Goeritz_part1_line_row(int[][] matrix,int start) {

//絶対値の一番小さい数字をpick up;
	  int d = -1;
          int i0 = start;
	  int j0 = start;		  
          int e;

	  for (;;) {
     	      	e = Math.Abs(matrix[start][start]);
    	  	for (int i=start; i < matrix.Length; ++i) {
		    for (int j=start; j < matrix[i].Length; ++j) {
                        if (e == 0 || ((e > Math.Abs(matrix[i][j])) && (matrix[i][j] != 0))) {
		            e = Math.Abs(matrix[i][j]);
		   	    i0 = i;
		   	    j0 = j;
			}
		    }			 
                }
		if (e == 0 || e == d) {
		   break;
		}
                d = e;

		if (matrix[i0][j0] < 0) {
		   for (int j5=start; j5 < matrix[i0].Length; ++j5) {
		       matrix[i0][j5] = -matrix[i0][j5];
		   }
	        }    
//基本変形III(matrix[i0][j0]);

//列に対して;
		int g,n;
		for (int j1=start; j1 < matrix[i0].Length; ++j1) {
	            if (j1  == j0) { 
		    }else {

		      g = matrix[i0][j1]/matrix[i0][j0];
		      for (int i2=start; i2 < matrix.Length; ++i2) {
		         n = matrix[i2][j1]-matrix[i2][j0]*g;
		         matrix[i2][j1] = n;
		      }
		    }
		}

//行に対して;

		int g0,n0;
		for (int i3=start; i3 < matrix.Length; ++i3) {
	            if (i3  == i0) { 
		    }else {

		      g0 = matrix[i3][j0]/matrix[i0][j0];
		      for (int j2=start; j2 < matrix[i0].Length; ++j2) {
		         n0 = matrix[i3][j2]-matrix[i0][j2]*g0;
		         matrix[i3][j2] = n0;
		      }
		    }
                }
        	
	 }

//行の交換;
          for (int j2=start; j2 < matrix[start].Length; ++j2) {
              int x = matrix[start][j2];
	      matrix[start][j2] = matrix[i0][j2];
	      matrix[i0][j2] = x;
          }

//列の交換;
          for (int i2=start; i2 < matrix.Length; ++i2) {
              int y = matrix[i2][start];
	      matrix[i2][start] = matrix[i2][j0];
	      matrix[i2][j0] = y;
          }
	  return matrix;
     }

//行列式の出力;

    static void Print_matrix (int[][] matrix300){

       System.Console.WriteLine("{");

       for (int i = 0; i < matrix300.Length; i++) {
       	    System.Console.Write("{");
            for (int j = 0; j < matrix300[i].Length; j++) {
                System.Console.Write("{0},", matrix300[i][j]);
	    }
	    System.Console.WriteLine("},");
        }
	System.Console.WriteLine("}");
    }


    static void Main() {

        FileStream fs = new FileStream("knot_file.txt",FileMode.Open);
        StreamReader r = new StreamReader(fs);
        string s_matrix;

        while ((s_matrix = r.ReadLine()) != null) {
            string[] arr = s_matrix.Split(':');

	    int[][] matrix = new int[arr.Length][];
	    for (int i=0; i < arr.Length; ++i) {

	       string[] arr2 = arr[i].Split(',');

	       matrix[i] = new int[arr2.Length];
	       for (int j=0; j < arr2.Length; ++j) {
	    	   matrix[i][j] = int.Parse(arr2[j]);
	       }
	    }

            int[][] matrix1 = Goeritz_part1(matrix);

	    Goeritz_part2(matrix1);

    	    List < int > t1 = Goeritz_part3(matrix1);

	    t1.Remove(1);
	    if (t1.Count == 0) {
	       Console.WriteLine(1);
	    }else {
	       if (t1.Count > 1) {
	          for(int n=0; n < t1.Count; ++n) {
	             if (t1[n] != 1) {
	                Console.Write(t1[n]);
		        if (t1.Count > n+1) {
		          Console.Write("+");
		        }
                     }		  
	          }
	          Console.WriteLine();
	       }else {
	    	  Console.WriteLine(t1[0]);
	       }
           }
       }
   }
}
ゲーリッツ行列を書き込むファイルのknot_file.txtの内容

-4,1,2,0,1:1,-3,1,1,0:2,1,-4,1,0:0,1,1,-3,1:1,0,0,1,-2
-3,3:3,-3
2,-2:-2,2
-3,1,2,0:1,-2,0,1:2,0,0,-2:0,1,-2,1
-3,1,1,1:1,-3,1,1:1,1,-3,1:1,1,1,-3
1,1:1,1


BACK(Kanako Suto's Pageへ)
このページへの質問・コメントを歓迎致します。