ラプラシアン

#k 次元立方格子
k<-2
P<-4

A<-expand.grid(rep(list(1:P),k))
N<-length(A[,1])

M2<-matrix(0,N,N)
for(i in 1:N){
	for(j in 1:N){
		tmp<-A[i,]-A[j,]
		if(sum(tmp^2)==1){
			M2[i,j]<-1
		}
	}
}
M2
image(M2)
# ラプラシアンの作成
d<-apply(M2,1,sum)
D<-diag(d)

L<-D-M2
L
# 余因子がi,jのとり方によらないことの確認
for(i in 1:N){
	for(j in 1:N){
		Lij<-L[-i,-j]
		print(det(Lij)*(-1)^(i+j))
	}
}
# igraphパッケージを使って、描図

library(igraph)
gtest<-graph.adjacency(M2,mode="undirected")
plot(gtest)