I am now posting the original matlab code. I am really disappointed if julia is significantly slower than matlab when it comes large sparse matrix diagonalization, which is common problem in computational physics.
clear all;close all; clc;tic
global dim
global table
N=12; % number of atoms
M=N; % number of sites
% number of basis vectors calculated analytically
dim=prod(1:max(1,N+M-1))/prod(1:max(1,N))/prod(1:max(1,M-1))
% basis is the table to store the information of the basis vectors
basis=zeros(M,dim);
basis(1,1)=N;
s=1;
table=zeros(1,dim);
weight=sqrt(100*(1:M)+3);
table(1)=weight*basis(:,1);
while (basis(M,s)<N)
s1=M-1;
while (basis(s1,s)==0)
s1=s1-1;
end
for s2=1:s1-1
basis(s2,s+1)=basis(s2,s);
end
basis(s1,s+1)=basis(s1,s)-1;
basis(s1+1,s+1)=N-sum(basis(1:s1,s+1));
table(s+1)=weight*basis(:,s+1);
s=s+1;
end
[table,ind]=sort(table);
% to generate the table storing information of the hamiltonian
COO1=zeros(3,dim);
COO2=zeros(3,dim*M*2);
for s1=1:dim
COO1(1,s1)=s1;
COO1(2,s1)=s1;
COO1(3,s1)=0.5*sum(basis(:,s1).^2-basis(:,s1));
end
Int=sparse(COO1(1,:),COO1(2,:),COO1(3,:),dim,dim);
s=0;
target=[(2:M),1];
for s1=1:dim
if (mod(s1,10000)==0)
s1
end
% s1
for s2=1:M
if (basis(s2,s1)>0)
s=s+1;
final=basis(:,s1);
final(s2)=final(s2)-1;
s3=target(s2);
final(s3)=final(s3)+1;
value=weight*final;
index=search(value);
COO2(1,s)=s1;
COO2(2,s)=ind(index);
COO2(3,s)=-sqrt(final(s3)*(final(s2)+1));
end
end
end
COO2=COO2(:,1:s);
Kin=sparse(COO2(1,:),COO2(2,:),COO2(3,:),dim,dim);
Kin=Kin+Kin';
% save Kin_Int_N10.mat Kin Int
toc
Vlist=0:0.5:20;
Cmax=zeros(1,length(Vlist));
tic
for s1=1:length(Vlist)
s1
H=Kin+Vlist(s1)*Int;
tic
[V,D]=eigs(H,1,'sa'); % to solve the smallest algebraic eigenvalues
toc
d=diag(D);
[d,ind]=sort(d);
gstate=V(:,ind(1));
gstate2=abs(gstate').^2;
Cmax(s1)=max(gstate2);
end
plot(Vlist,Cmax)
ylim([0 1])
xlim([0 max(Vlist)])
xlabel('V/J','fontsize',14)
ylabel('C^2_{max}','fontsize',14)
str=strcat('N=',num2str(N));
title(str,'fontsize',14)
% legend(str,'fontsize',14)
toc
this script calls a function which is put in another file
function index=search(value)
global dim
global table
L=1;
R=dim;
M=floor((L+R)/2);
while (L<=R)
if (value>table(M))
L=M+1;
M=floor((L+R)/2);
elseif (value<table(M))
R=M-1;
M=floor((L+R)/2);
elseif (value==table(M))
index=M;
return
end
end