gaptool的C#版本(四)

D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>csc FiniteGroup.cs Un.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer th
e latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=53324
0


D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>FiniteGroup D4.txt
0=>0
1=>1
2=>2
3=>3
4=>4
5=>6
6=>5
7=>7
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
3 4 1 2 7 8 5 6
4 3 2 1 8 7 6 5
5 7 6 8 1 3 2 4
6 8 5 7 2 4 1 3
7 5 8 6 3 1 4 2
8 6 7 5 4 2 3 1
中心:0 3
1 2
2 1
換位子羣:0 3
1 2
2 1

D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>csc /d:TESTUN Un.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer th
e latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=53324
0


D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>Un 8
0->ZmodnZobj(1,8)=>0->ZmodnZobj(1,8)
1->ZmodnZobj(3,8)=>1->ZmodnZobj(3,8)
2->ZmodnZobj(5,8)=>2->ZmodnZobj(5,8)
3->ZmodnZobj(7,8)=>3->ZmodnZobj(7,8)
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1

D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>Un 15
0->ZmodnZobj(1,15)=>0->ZmodnZobj(1,15)
1->ZmodnZobj(2,15)=>4->ZmodnZobj(8,15)
2->ZmodnZobj(4,15)=>2->ZmodnZobj(4,15)
3->ZmodnZobj(7,15)=>6->ZmodnZobj(13,15)
4->ZmodnZobj(8,15)=>1->ZmodnZobj(2,15)
5->ZmodnZobj(11,15)=>5->ZmodnZobj(11,15)
6->ZmodnZobj(13,15)=>3->ZmodnZobj(7,15)
7->ZmodnZobj(14,15)=>7->ZmodnZobj(14,15)
1 2 3 4 5 6 7 8
2 3 5 8 1 4 6 7
3 5 1 7 2 8 4 6
4 8 7 3 6 2 1 5
5 1 2 6 3 7 8 4
6 4 8 2 7 1 5 3
7 6 4 1 8 5 3 2
8 7 6 5 4 3 2 1

using System;
using System.Collections.Generic;

namespace gap
{    
    interface IGroup
    {
        void printSet(); 
        void printTable();
        int mul(int a,int b);
        int size();
        int inv(int a);
    }
    
    // (Z/nZ)^*
    class Un:IGroup
    {
        // 靜態函數
        public static List<int> Order(IGroup g,int m){
            List<int> ret=new List<int>();
            int mi=m;
            int m0=0;
            ret.Add(m0);
            while(mi!=m0){
                ret.Add(mi);
                mi=g.mul(mi,m);
            }
            return ret;
        }    

        public static List<Tuple<int,int>> Order(Tuple<int,int> m){
            List<Tuple<int,int>> ret=new List<Tuple<int,int>>();
            Tuple<int,int> mi=new Tuple<int,int>(m.Item1,m.Item2);
            Tuple<int,int> m0=new Tuple<int,int>(1,m.Item2);
            ret.Add(m0);
            while(mi.Item1!=m0.Item1){
                ret.Add(mi);
                mi=new Tuple<int,int>((mi.Item1*m.Item1)%m.Item2,m.Item2);
            }
            return ret;
        }            
        
        public static int gcd(int a, int b)
        {
            //int temp;
            if(b == 0) return a;
            if(a == 0) return b;
            if(a%b == 0) return b;
            else return gcd(b, a%b);
        }
        
        public static List<int> totient(int num)
        {
            List<int> L=new List<int>();
            if(num==1)
            {
                L.Add(1);
                return L;
            }
            for(int i=1;i<=num-1;i++)
            {
                if(gcd(num,i)==1)
                {
                    L.Add(i);
                }
            }
            return L;
        }

        public static List<List<int>> UnMulTable(int n,List<int> v)
        {
            List<List<int>> ret=new List<List<int>>();
            for(int i=0;i<v.Count;i++)
            {
                List<int> I=new List<int>();
                for(int j=0;j<v.Count;j++)
                {
                    int ij=(v[i]*v[j])%n;
                    int index=-1;
                    for(int k=0;k<v.Count;k++)
                    {
                        if(v[k]==ij)
                        {
                            index=k;
                            break;
                        }
                    }
                    I.Add(index);
                }
                ret.Add(I);
            }            
            return ret;
        }    

        public static void PrintUnMulTable(List<List<int>> vv)
        {
           int n=vv.Count;
           for(int i=0;i<n;i++){
               for(int j=0;j<n;j++){
                  int ij=vv[i][j];
                  Console.Write("{0} ",ij+1); 
               } 
               Console.Write("\n");       
           }
        }        
        
        // 實現接口的成員
        public void printSet()
        {
            for(int i=0;i<m_Set.Count;i++){
                int i1=inv(i);
                Tuple<int,int> I=m_Set[i];
                Tuple<int,int> I1=m_Set[i1];                
                Console.Write("{0}->ZmodnZobj({1},{2})=>{3}->ZmodnZobj({4},{5})\n",i,I.Item1,I.Item2,i1,I1.Item1,I1.Item2); 
            }            
        }
        public void printTable()
        {
            PrintUnMulTable(m_Mul);
        }
        public int mul(int a,int b)
        {
            return m_Mul[a][b];
        }
        public int size()
        {
            return m_Set.Count;
        }
        public int inv(int a)
        {
#if DEF1
            //Console.WriteLine("DEF1");    
            List<Tuple<int,int>> S1=Order(m_Set[a]);
            int ord=S1.Count;
            Tuple<int,int> A1=S1[ord-1];
            int a1=-1;
            for(int i=0;i<m_Set.Count;i++)
            {
                if(m_Set[i].Item1==A1.Item1)
                {
                    a1=i;
                    break;
                }
            }
            return a1;            
#else
            //Console.WriteLine("DEF2");        
            List<int> S1=Order(this,a);
            int ord=S1.Count;                
            return S1[ord-1];
#endif
        }
        // 構造函數
        public Un(int n)
        {
            m_n=n;
            List<int> v=totient(n);
            m_Mul=UnMulTable(n,v);
            m_Set=new List<Tuple<int,int>>();
            for(int i=0;i<v.Count;i++){
                Tuple<int,int> I=new Tuple<int,int>(v[i],n);
                m_Set.Add(I);
            }
        }
        // 成員函數    
        // 成員變量
        List<List<int>> m_Mul;        
        List<Tuple<int,int>> m_Set;
        int m_n;    
    }
    
#if TESTUN    
    /// <summary>
    /// Summary description for TestUn.
    /// </summary>
    class TestUn
    {
        static void Main(string[] args)
        {
            int n=0;
            if(args.Length<1){
                Console.Write("Enter n:");
                n = Convert.ToInt32(Console.ReadLine());
                if(n<1)
                    return;
            }else{
                n=Convert.ToInt32(args[0]);
            }
            Un un=new Un(n);
            un.printSet();
            un.printTable();
        }
    }
#endif    
}

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

namespace gap
{
    class gutil
    {
        // 靜態函數
        public static List<int> Order(IGroup g,int m){
            List<int> ret=new List<int>();
            int mi=m;
            int m0=0;
            ret.Add(m0);
            while(mi!=m0){
                ret.Add(mi);
                mi=g.mul(mi,m);
            }
            return ret;
        }
        
        public static int inv(IGroup g,int a){        
            List<int> S1=Order(g,a);
            int ord=S1.Count;                
            return S1[ord-1];
        }
        
        public static void printGroup(IGroup g){
           int n=g.size();
           for(int i=0;i<n;i++){
               for(int j=0;j<n;j++){
                  int ij=g.mul(i,j);
                  Console.Write("{0} ",ij+1); 
               } 
               Console.Write("\n");       
           }
        }
        
        public static int ToNormal(List<int> v,int a){
            for(int i=0;i<v.Count;i++){
                if(v[i]==a)
                    return i;
            }
            return -1;
        }
        
        public static void printSubgroup(IGroup g,List<int> ZA,bool IsNormal){
            for(int i=0;i<ZA.Count;i++){
                for(int j=0;j<ZA.Count;j++){
                    if(IsNormal){
                        int ij=g.mul(ZA[i],ZA[j]);
                        int ij2=ToNormal(ZA,ij)+1;
                        Console.Write("{0} ",ij2);
                    }
                    else
                    {
                        int ij=g.mul(ZA[i],ZA[j]);
                        Console.Write("{0} ",ij);
                    }
                }
                Console.Write("\n");
            }
        }        

        public static bool IsInCenterOfG(IGroup g,int j){
           int n=g.size();
           for(int i=0;i<n;i++){
                int ij=g.mul(i,j);
                int ji=g.mul(j,i);
                if(ij==ji)
                    continue;
                else
                    return false;
            }
            return true;
        }
        
        public static List<int> CenterOfG(IGroup g){
            List<int> ret=new List<int>();
            int n=g.size();
            for(int i=0;i<n;i++){
                if(IsInCenterOfG(g,i))
                    ret.Add(i);
                else
                    continue;
            }
            return ret;
        }
        
        public static List<int> commutatorOfG(IGroup g){
            List<int> ret=new List<int>();
            int n=g.size();
            ret.Add(0);
            for(int i=0;i<n;i++){
                int I=inv(g,i);
                for(int j=i+1;j<n;j++){
                    int ij=g.mul(i,j);
                    int J=inv(g,j);
                    int IJ=g.mul(I,J);
                    int ijIJ=g.mul(ij,IJ);
                    ret.Add(ijIJ);
                }
            }
            //ret.Sort();
            ret.Sort((x,y)=>x.CompareTo(y));
            // 去除重複元素
            for(int i=0;i<ret.Count;i++){
                for(int j=ret.Count-1;j>i;j--){
                    if(ret[i]==ret[j])
                        ret.RemoveAt(j);
                }
            }
            return ret;
        }            
    }
    
    class FiniteGroup:IGroup
    {
        // 實現接口的成員
        public void printSet()
        {
            for(int i=0;i<size();i++){
                int i1=inv(i);
                Console.Write("{0}=>{1}\n",i,i1); 
            }            
        }
        public void printTable()
        {
            gutil.printGroup(this);
        }
        public int mul(int a,int b)
        {
            return m_Mul[a][b];
        }
        public int size()
        {
            return m_Mul.Count;
        }
        public int inv(int a)
        {                
            return gutil.inv(this,a);
        }
        // 構造函數
        public FiniteGroup(string path)
        {
           m_Mul=new List<List<int>>();
           StreamReader sr;
           sr = File.OpenText(path);
           string line;
           line = sr.ReadLine();
           while(line != null){
            List<int> v=new List<int>();
            line=line.Replace(" ", ",");
            string[] vLine=line.Split(new char[]{','});
            foreach (string s in vLine){
                int i=Convert.ToInt32(s);
                v.Add(i-1);
            }                
            m_Mul.Add(v);
            line = sr.ReadLine();
           }
           sr.Close();  
        }
        // 成員函數    
        // 成員變量
        List<List<int>> m_Mul;            
    }
    
    /// <summary>
    /// Summary description for TestFiniteGroup.
    /// </summary>
    class TestFiniteGroup
    {        
        static void Main(string[] args)
        {
            string path="";
            if(args.Length<1){
                Console.Write("請輸入羣A凱萊表文件名:");
                path=Console.ReadLine();
            }else{
                path=args[0];
            }
            FiniteGroup g=new FiniteGroup(path);
            g.printSet();
            g.printTable();
            List<int> Z=gutil.CenterOfG(g);
            List<int> D=gutil.commutatorOfG(g);
            Console.Write("中心:");
            Z.ForEach(x=>Console.Write("{0} ",x));
            Console.Write("\n");
            gutil.printSubgroup(g,Z,true);
            Console.Write("換位子羣:");    
            D.ForEach(x=>Console.Write("{0} ",x));
            Console.Write("\n");
            gutil.printSubgroup(g,D,true);            
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章