pxdBy=intval(zeros(2*N+1,4*N+1)); %(Phi)_x(\triangle B)_y
pydBx=intval(zeros(2*N+1,4*N+1)); %(Phi)_y(\triangle B)_x
Bxdpy=intval(zeros(2*N+1,4*N+1)); %(u)_x(\triangle Phi)_y
Bydpx=intval(zeros(2*N+1,4*N+1)); %(u)_y(\triangle Phi)_x
g1=0.5*ialpha*(-(iPhi).*L1*(Bt.*L2.*(L1.^2*ialpha^2+L2.^2))'); %pxdBy
 g2=0.5*ialpha*(-(iPhi).*L2*(Bt.*L1.*(L1.^2*ialpha^2+L2.^2))'); %pydBx
 g3=0.5*ialpha*(-Bt.*L1*((iPhi).*L2.*(L1.^2*ialpha^2+L2.^2))'); %Bxdpy
  g4=0.5*ialpha*(-Bt.*L2*((iPhi).*L1.*(L1.^2*ialpha^2+L2.^2))'); %Bydpx
for i=1:Ms 
    i1=list2(i,1);
    i2=list2(i,2);
    ti=ss(i);
    for j=1:Ms
        j1=list2(j,1);
        j2=list2(j,2); 
        m1=i1+j1+1;
        n1=i2+j2+2*N+1;
        n2=i2-j2+2*N+1;
        n3=-i2+j2+2*N+1;
        n4=-i2-j2+2*N+1;
        tj=-ss(j); 
        pxdBy(m1,n1)= pxdBy(m1,n1)-g1(i,j);
         pxdBy(m1,n2)= pxdBy(m1,n2)-g1(i,j)*tj;
       pxdBy(m1,n3)= pxdBy(m1,n3)-g1(i,j)*ti;
        pxdBy(m1,n4)= pxdBy(m1,n4)-g1(i,j)*ti*tj;
         
        pydBx(m1,n1)= pydBx(m1,n1)-g2(i,j); 
         pydBx(m1,n2)=pydBx(m1,n2)-g2(i,j)*(-1)*tj;
         pydBx(m1,n3)=pydBx(m1,n3)-g2(i,j)*ti*(-1);
         pydBx(m1,n4)= pydBx(m1,n4)-g2(i,j)*ti*tj;  
          
        Bxdpy(m1,n1)= Bxdpy(m1,n1)-g3(i,j);
         Bxdpy(m1,n2)= Bxdpy(m1,n2)-g3(i,j)*tj;
       Bxdpy(m1,n3)= Bxdpy(m1,n3)-g3(i,j)*ti;
        Bxdpy(m1,n4)= Bxdpy(m1,n4)-g3(i,j)*ti*tj;
         
        Bydpx(m1,n1)= Bydpx(m1,n1)-g4(i,j); 
         Bydpx(m1,n2)=Bydpx(m1,n2)-g4(i,j)*(-1)*tj;
         Bydpx(m1,n3)=Bydpx(m1,n3)-g4(i,j)*(-1)*ti;
         Bydpx(m1,n4)= Bydpx(m1,n4)-g4(i,j)*ti*tj; 
         
        
       if i1-j1~=0
           signij=sign(i1-j1);
           m2=abs(i1-j1)+1;
           s1=(i2-j2)*signij+2*N+1;
           s2=(i2+j2)*signij+2*N+1;
           s3=(-i2-j2)*signij+2*N+1;
           s4=(-i2+j2)*signij+2*N+1;
        pxdBy(m2,s1)= pxdBy(m2,s1)+g1(i,j);
        pxdBy(m2,s2)= pxdBy(m2,s2)+g1(i,j)*tj;
        pxdBy(m2,s3)= pxdBy(m2,s3)+g1(i,j)*ti;
        pxdBy(m2,s4)= pxdBy(m2,s4) +g1(i,j)*tj*ti;
        pydBx(m2,s1)= pydBx(m2,s1)+g2(i,j);
        pydBx(m2,s2)= pydBx(m2,s2)+g2(i,j)*(-1)*tj;
        pydBx(m2,s3)= pydBx(m2,s3)+g2(i,j)*(-1)*ti;
        pydBx(m2,s4)= pydBx(m2,s4)+g2(i,j) *ti*tj;
        Bxdpy(m2,s1)= Bxdpy(m2,s1)+g3(i,j);
        Bxdpy(m2,s2)= Bxdpy(m2,s2)+g3(i,j)*tj;
        Bxdpy(m2,s3)= Bxdpy(m2,s3)+g3(i,j)*ti;
        Bxdpy(m2,s4)= Bxdpy(m2,s4)+g3(i,j)*tj*ti;
        Bydpx(m2,s1)= Bydpx(m2,s1)+g4(i,j);
        Bydpx(m2,s2)= Bydpx(m2,s2)+g4(i,j)*(-1)*tj;
        Bydpx(m2,s3)= Bydpx(m2,s3)+g4(i,j)*(-1)*ti;
        Bydpx(m2,s4)= Bydpx(m2,s4) +g4(i,j) *ti*tj;
         
       else
           t1=abs(i2-j2)+2*N+1;
           t2=(i2+j2)+2*N+1;
           t3=abs(-i2-j2)+2*N+1;
           t4=abs(-i2+j2)+2*N+1;
        pxdBy(1,t1)= pxdBy(1,t1)+g1(i,j);
        pxdBy(1,t2)= pxdBy(1,t2)+g1(i,j)*tj;
        pxdBy(1,t3)= pxdBy(1,t3)+g1(i,j)*ti;
        pxdBy(1,t4)= pxdBy(1,t4)+g1(i,j)*ti*tj;
        pydBx(1,t1)= pydBx(1,t1)+g2(i,j);
        pydBx(1,t2)= pydBx(1,t2)+g2(i,j)*(-1)*tj;
        pydBx(1,t3)= pydBx(1,t3)+g2(i,j)*(-1)*ti;
        pydBx(1,t4)= pydBx(1,t4)+g2(i,j)*ti*tj;  
        Bxdpy(1,t1)= Bxdpy(1,t1)+g3(i,j);
        Bxdpy(1,t2)= Bxdpy(1,t2)+g3(i,j)*tj;
        Bxdpy(1,t3)= Bxdpy(1,t3)+g3(i,j)*ti;
        Bxdpy(1,t4)= Bxdpy(1,t4)+g3(i,j)*ti*tj;
        Bydpx(1,t1)= Bydpx(1,t1)+g4(i,j);
        Bydpx(1,t2)= Bydpx(1,t2)+g4(i,j)*(-1)*tj;
        Bydpx(1,t3)= Bydpx(1,t3)+g4(i,j)*(-1)*ti;
        Bydpx(1,t4)= Bydpx(1,t4)+g4(i,j)*ti*tj;   
         
        end
    end
end

Bp=(Bxdpy-Bydpx+pxdBy-pydBx);%RJ(\Phi_N,\triangle B)+R_NJ(B,\triangle \Phi_N)
BpL2=sum(sum(Bp(1:2*N+1,1:N).^2))*2*PI^2/ialpha;%L2-norm of (I-P_N)Bp
BpL2=BpL2+sum(sum(Bp(1:2*N+1,3*N+2:4*N+1).^2))*2*PI^2/ialpha;
BpL2=BpL2+sum(sum(Bp(N+2:2*N+1,N+1:3*N+1).^2))*2*PI^2/ialpha;
BpL2=sqrt(BpL2);
Bx=intval('0'); %  B_x
dBx=intval('0'); % triangle B_x
By=intval('0'); %  B_y
dBy=intval('0'); % triangle B_y
px=intval('0'); %  Phi_x
dpx=intval('0'); % triangle Phi_x
py=intval('0'); %  Phi_y
dpy=intval('0'); % triangle Phi_y
for i=1:3/2*N
  i1=list2(i,1);
  i2=list2(i,2);
  Bx=Bx+i1*ialpha*(-sin(i1*ialpha*x+i2*y))*Bt(i);
  By=By+i2*(-sin(i1*ialpha*x+i2*y))*Bt(i);
  dBx=dBx+i1*ialpha*(-sin(i1*ialpha*x+i2*y))*Bt(i)*(i1^2*ialpha^2+i2^2);
  dBy=dBy+i2*(-sin(i1*ialpha*x+i2*y))*Bt(i)*(i1^2*ialpha^2+i2^2);
   px=px+i1*ialpha*(-sin(i1*ialpha*x+i2*y))*iPhi(i);
  py=py+i2*(-sin(i1*ialpha*x+i2*y))*iPhi(i);
  dpx=dpx+i1*ialpha*(-sin(i1*ialpha*x+i2*y))*iPhi(i)*(i1^2*ialpha^2+i2^2);
  dpy=dpy+i2*(-sin(i1*ialpha*x+i2*y))*iPhi(i)*(i1^2*ialpha^2+i2^2);
end
for i=1:3/2*N+1:Ms
    i1=list2(i,1);
    i2=list2(i,2);
    Bx=Bx-Bt(i)*(i1*ialpha*(sin(i1*ialpha*x+i2*y)+sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)));
    By=By-Bt(i)*(i2*(sin(i1*ialpha*x+i2*y)-sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)));
    dBx=dBx-Bt(i)*(i1*ialpha*(sin(i1*ialpha*x+i2*y)+sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)))*(i1^2*ialpha^2+i2^2);
    dBy=dBy-Bt(i)*(i2*(sin(i1*ialpha*x+i2*y)-sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)))*(i1^2*ialpha^2+i2^2);
     px=px-iPhi(i)*(i1*ialpha*(sin(i1*ialpha*x+i2*y)+sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)));
    py=py-iPhi(i)*(i2*(sin(i1*ialpha*x+i2*y)-sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)));
    dpx=dpx-iPhi(i)*(i1*ialpha*(sin(i1*ialpha*x+i2*y)+sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)))*(i1^2*ialpha^2+i2^2);
    dpy=dpy-iPhi(i)*(i2*(sin(i1*ialpha*x+i2*y)-sin(i1*ialpha*x-i2*y)*(-1)^(floor(i1/2)+i2+1)))*(i1^2*ialpha^2+i2^2);
end
Bxinf=sup(abs(Bx)); %Linf norm of Phi_x
Byinf=sup(abs(By)); %Linf norm of Phi_y
dBxinf=sup(abs(dBx)); %Linf norm of triangle Phi_x
dByinf=sup(abs(dBy)); % Linf norm of triangle Phi_y
pxinf=sup(abs(px)); %Linf norm of Phi_x
pyinf=sup(abs(py)); %Linf norm of Phi_y
dpxinf=sup(abs(dpx)); %Linf norm of triangle Phi_x
dpyinf=sup(abs(dpy)); % Linf norm of triangle Phi_y
