%pylab inline
from sympy import *
init_printing()
def krav(N):
K=np.zeros((N+1,N+1),dtype=int)
for i in range(N+1):
K[i,N]=int(binomial(N,i)*(-1)**i)
K[0,i]=1
for i in range(1,N+1):
for j in range(N,0,-1):
K[i,j-1]=K[i-1,j-1]+K[i-1,j]+K[i,j]
return K
for N in range(1,13):
K =krav(N)
y="K["+str(N)+"] = "
print(y)
print(K)
N=5
K=krav(N)
B=np.diag([int(binomial(N,i)) for i in range(N+1)])
S=dot(K,B)
print(not numpy.any(S-S.T))
e=eig(S)
e[0]
N=8
X=krav(N)
Y=np.multiply(X,X)
X,sum(Y,axis=0),sum(Y,axis=1)
rr=range(N+1)
[binomial(2*N-2*j,N-j)*binomial(2*j,j)/binomial(N,j) for j in rr]
mm=range(int(N/2)+1)
[sum(binomial(N+1,2*k+1)*binomial(2*k,k)*binomial(N-2*k,m-k) for k in range(m+1))for m in mm]
binomial(8,4)
krav(4)