1typeMatrixstruct{ 2arr[]int 3row,colint 4} 5 6funcnewMatrix(a[]int,row,colint)*Matrix{ 7arr:=make([]int,row*col) 8copy(arr,a) 9return&Matrix{arr,row,col}10}1112funcnewIMatrix(nint)*Matrix{13arr:=make([]int,n*n)14fori:=0;i<n;i++{15arr[i+i*n]=116}17return&Matrix{arr,n,n}18}1920// At function
21func(mMatrix)At(i,jint)int{returnm.arr[i*m.col+j]}2223// AddMatrix function
24funcAddMatrix(a,b*Matrix)*Matrix{25ifa.row!=b.row||a.col!=b.col{26return&Matrix{}27}28res:=newMatrix(nil,a.row,a.col)29fori:=0;i<a.row*a.col;i++{30res.arr[i]=a.arr[i]+b.arr[i]31}32returnres33}3435// SubMatrix function
36funcSubMatrix(a,b*Matrix)*Matrix{37ifa.row!=b.row||a.col!=b.col{38return&Matrix{}39}40res:=newMatrix(nil,a.row,a.col)41fori:=0;i<a.row*a.col;i++{42res.arr[i]=a.arr[i]-b.arr[i]43}44returnres45}4647// MulMatrix function
48funcMulMatrix(a,b*Matrix)*Matrix{49ifa.col!=b.row{50return&Matrix{}51}52res:=newMatrix(nil,a.row,b.col)53fori:=0;i<res.row;i++{54forj:=0;j<res.col;j++{55fork:=0;k<a.col;k++{56res.arr[i*res.col+j]+=a.arr[i*a.col+k]*b.arr[k*b.col+j]57}58}59}60returnres61}6263// PowMatrix function
64funcPowMatrix(a*Matrix,nint)*Matrix{65ifa.row!=a.col{66return&Matrix{}67}68t:=newMatrix(a.arr,a.row,a.col)69res:=newIMatrix(a.row)70forn>0{71ifn&1==1{72res=MulMatrix(res,t)73}74t=MulMatrix(t,t)75n>>=176}77returnres78}