D:\SixCocos2d-xVC2012\Cocos2d-x\XWH\cstest>R4
R4_4:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
R4_5:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 1 1 1
1 1 2 2
1 1 2 2
R4_6:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
R4_7:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 2 3 4
1 2 3 4
1 1 1 1
R4_8:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 2 2 1
1 3 3 1
1 4 4 1
R4_9:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 4 3 2
1 3 1 3
1 2 3 4
R4_10:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 2 1 2
1 1 3 3
1 2 3 4
R4_11:
[R4Add]
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
[R4Mul]
1 1 1 1
1 3 4 2
1 4 2 3
1 2 3 4
using System;
using System.Collections.Generic;
namespace gap
{
class R4
{
public int m_ID;// 不同的編號代表不同的乘法
List<VElement> m_Set;
public R4(int ID=10)
{
m_ID=ID;
m_Set=new List<VElement>();
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
VElement a=new VElement(ID,i,j);
m_Set.Add(a);
}
}
public int getidx(VElement a)
{
for(int i=0;i<m_Set.Count;i++)
if(m_Set[i]==a)
return i;
return -1;
}
public void printTable()
{
Console.Write("R{0}_{1}:\n",size(),m_ID);
int n=size();
Console.Write("[R{0}Add]\n",n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int ij=add(i,j);
Console.Write("{0} ",ij+1);
}
Console.Write("\n");
}
Console.Write("[R{0}Mul]\n",n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int ij=mul(i,j);
Console.Write("{0} ",ij+1);
}
Console.Write("\n");
}
}
public int add(int a,int b)
{
VElement C=m_Set[a]+m_Set[b];
int c=getidx(C);
return c;
}
public int mul(int a,int b)
{
VElement C=m_Set[a]*m_Set[b];
int c=getidx(C);
return c;
}
public int size()
{
return m_Set.Count;
}
}
class VElement// 如果這裏的class換成struct,程序運行時會崩潰
{
public int m_ID;// 不同的編號代表不同的乘法
public int[] m_bVal;
public VElement(int ID=10,int b1=0,int b2=0)
{
m_ID=ID;
m_bVal=new int[2]{b1,b2};
}
public static bool operator==(VElement a, VElement b)
{
return (b.m_bVal[0]==a.m_bVal[0] && b.m_bVal[1]==a.m_bVal[1]);
}
public static bool operator!=(VElement a, VElement b)
{
return (b.m_bVal[0]!=a.m_bVal[0] || b.m_bVal[1]!=a.m_bVal[1]);
}
public static VElement operator+(VElement a,VElement b)
{
VElement ret=new VElement(a.m_ID,a.m_bVal[0]^b.m_bVal[0],a.m_bVal[1]^b.m_bVal[1]);
//0^0=0, 1^0=1, 0^1=1, 1^1=0<=>0+0=0, 1+0=1, 0+1=1, 1+1=0
//VElement ret=new VElement(a.m_ID,(a.m_bVal[0]+b.m_bVal[0])%2,(a.m_bVal[1]+b.m_bVal[1])%2);
//0+0=0, 1+0=1, 0+1=1, 1+1=0
return ret;
}
public static VElement operator-(VElement a)
{
VElement ret=new VElement(a.m_ID,(2-a.m_bVal[0])%2,(2-a.m_bVal[1])%2);
return ret;
}
public static VElement mul(int ID,VElement a,VElement b)
{
if(ID==4){
VElement ret=new VElement(ID,0,0);
return ret;
}else if(ID==5){
/*
x^3=0,在Z/(2)中有1根:x_1=0
設a是Z/(2)[x]中可約多項式x^3的另外一個根,證明Z/(2)(a)={0,1,a,a^2,1+a,1+a^2,a+a^2,1+a+a^2}是一個8階環,{0,a,a^2,a+a^2}={O,B,A,I}是一個4階環:
R2_2/([[0,1]],[0,0,0,1])=R4_5的乘法運算:
a^3=0
(a+a^2)(a+a^2)=a^2<=>II=A
(a+a^2)a^2=0<=>IA=O
(a+a^2)a=a^2<=>IB=A
(a^2)^2=0<=>A^2=O
(a)^2=a^2<=>B^2=A
(a^2)a=0<=>AB=O
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,1,1);
VElement A=new VElement(ID,0,1);
VElement B=new VElement(ID,1,0);
if(O==a || O==b)
return O;
if(A==a || A==b)
return O;
return A;
}else if(ID==6){
/*
x^3+x^2=0,在Z/(2)中有2根:x_1=0、x_2=1
設a是Z/(2)[x]中可約多項式x^3+x^2的另外一個根,證明Z/(2)(a)={0,1,a,a^2,1+a,1+a^2,a+a^2,1+a+a^2}是一個8階環,{0,a,a^2,a+a^2}={O,B,A,I}是一個4階環:
R2_2/([[0,1]],[0,0,1,1])=R4_6的乘法運算:
a^2=a^3=a^4
(a+a^2)(a+a^2)=a^2+a^4=0<=>II=O
(a+a^2)a^2=0<=>IA=O
(a+a^2)a=0<=>IB=O
(a^2)^2=a^2<=>A^2=A
(a)^2=a^2<=>B^2=A
(a^2)a=a^2<=>AB=A
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,1,1);
VElement A=new VElement(ID,0,1);
VElement B=new VElement(ID,1,0);
if(O==a || O==b)
return O;
if(I==a || I==b)
return O;
return A;
}else if(ID==7){
/*
R4_7={{{0,0},{0,0}},{{1,0},{0,0}},{{1,1},{0,0}},{{0,1},{0,0}}}={O,I,A,B}
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,0,1);
VElement A=new VElement(ID,1,0);
VElement B=new VElement(ID,1,1);
VElement[,] m_mul=new VElement[,]{
{O,O,O,O},
{O,I,A,B},
{O,I,A,B},
{O,O,O,O}
};
int ia=a.m_bVal[0]*2+a.m_bVal[1];
int ib=b.m_bVal[0]*2+b.m_bVal[1];
VElement ret=m_mul[ia,ib];
return ret;
}else if(ID==8){
/*
R4_8={{{0,0},{0,0}},{{1,0},{0,0}},{{1,0},{1,0}},{{0,0},{1,0}}}={O,I,A,B}
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,0,1);
VElement A=new VElement(ID,1,0);
VElement B=new VElement(ID,1,1);
VElement[,] m_mul=new VElement[,]{
{O,O,O,O},
{O,I,I,O},
{O,A,A,O},
{O,B,B,O}
};
int ia=a.m_bVal[0]*2+a.m_bVal[1];
int ib=b.m_bVal[0]*2+b.m_bVal[1];
VElement ret=m_mul[ia,ib];
return ret;
}else if(ID==9){
/*
x^2+1=0,在Z/(2)中有一根:x_1=1
設a是Z/(2)[x]中可約多項式x^2+1的另外一個根,證明Z/(2)(a)={0,1,a,1+a}是一個含4個元素的環:
R4_9的乘法運算:
a(1+a)=a+1<=>ab=b
a^2=-1=1<=>a^2=I
(1+a)^2=0<=>b^2=0
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,1,1);
VElement A=new VElement(ID,0,1);
VElement B=new VElement(ID,1,0);
if(O==a || O==b)
return O;
if(I==a)
return b;
if(I==b)
return a;
if(A==a && A==b)
return I;
if(B==a && B==b)
return O;
return B;
}else if(ID==10){
VElement ret=new VElement(ID,a.m_bVal[0]*b.m_bVal[0],a.m_bVal[1]*b.m_bVal[1]);
return ret;
}else if(ID==11){
/*
x^2+x+1=0,在Z/(2)中無根
設a是Z/(2)[x]中不可約多項式x^2+x+1的一個根,證明Z/(2)(a)={0,1,a,1+a}是一個含4個元素的域:
R4_11的乘法運算:
a(1+a)=-1=1<=>ab=I
a^2=-a-1=a+1<=>a^2=b
*/
VElement O=new VElement(ID,0,0);
VElement I=new VElement(ID,1,1);
if(O==a || O==b)
return O;
if(I==a)
return b;
if(I==b)
return a;
if(a==b)
return new VElement(ID,a.m_bVal[1],a.m_bVal[0]);
if(a!=b)
return I;
return new VElement();
}else{
return mul(10,a,b);
}
}
public static VElement operator*(VElement a,VElement b)
{
return mul(a.m_ID,a,b);
}
};
/// <summary>
/// Summary description for TestR4
/// </summary>
class TestR4
{
static void Main(string[] args)
{
R4 r4_4=new R4(4);
r4_4.printTable();
R4 r4_5=new R4(5);
r4_5.printTable();
R4 r4_6=new R4(6);
r4_6.printTable();
R4 r4_7=new R4(7);
r4_7.printTable();
R4 r4_8=new R4(8);
r4_8.printTable();
R4 r4_9=new R4(9);
r4_9.printTable();
R4 r4_10=new R4(10);
r4_10.printTable();
R4 r4_11=new R4(11);
r4_11.printTable();
}
}
}