Enleve[x_,l_]:=Module[{res={},n=Length[l]},Do[If[l[[j]]!=x,AppendTo[res,l[[j]]]],{j,1,n}];res];

Enlevel[s_,l_]:=Module[{res={},n=Length[l]},Do[If[!MemberQ[s,l[[j]]],AppendTo[res,l[[j]]]],{j,1,n}];res];

(*** Conditions aux limites ouvertes***)

MatConOBC[l_]:=Module[{n=Length[l],d,r},r=Table[0,{x,n},{y,n}];Do[{d=Norm[l[[j]]-l[[jj]]];If[d==1 ,r[[j,jj]]=1,r[[j,jj]]=0]},{j,1,n}, {jj,1,n}];r];

(*Construction en partant du code lent, pour un quasicristal exact*)

(*Construction avec code rapide ; deux versions sont codées, la première en utilisant tous les sites de l'approximant, la seconde en partant de la maille des conditions aux limites périodiques*)

MatConOBC1[v_,p_]:=Module[{res,l=Length[p],lv=Length[v]},res=Table[0,{x,l},{y,l}];Do[{res[[v[[j,1]],v[[j,2]]]]=1;res[[v[[j,2]],v[[j,1]]]]=1},{j,1,lv}];res];

MatConOBC2[v_,p_]:=Module[{vrj,rj,jn,jjn,rep,lcl,l,res,id,idd},lcl=ListeClasses2[p];rep=Rep2[lcl];l=Length[rep];
res=Table[0,{x,l},{y,l}];Do[{rj=rep[[j]];vrj=Voisdej2[rj,v];id=Intersection[rep,vrj];idd=Enleve[rj,id];Do[{jjn=(Position [rep,idd[[jj]]])[[1,1]];res[[j,jjn]]=1;res[[jjn,j]]=1},{jj,1,Length[idd]}]},{j,1,l}];res];

Voisdej2[j_,v_]:=Module[{r,l=Length[v],res={},t},Do[{r=Mod[kk+1,2];If[v[[k,kk]]==j,AppendTo[res,v[[k,r]]]]},{kk,1,2},{k,1,l}];res];

ListPlot[eigOBC2ap1]

(*** Conditions aux limites périodiques ***)

(*Classej[j_,l_]:=Module[{res={j},n=Length[l],ll,q},ll=normalise[l];q=ll[[j]];eigphi0=Eigenvalues[MatConFluxOBC[vois3,ppniso3,0,1]];If[j≤4,AppendTo[res,Enleve[j,{1,2,3,4}]],If[(0.999998<q[[1]]≤ 1.0)||(0.999998<q[[2]]≤ 1.0)||(q[[1]]<Exp[-16*Log[10]])||(q[[2]]<Exp[-16*Log[10]]),Do[If[((Abs[q[[1]]-ll[[k,1]]]<5*Exp[-16*Log[10]])&&(0.999998<Abs[q[[2]]-ll[[k,2]]]<1.000001))||((Abs[q[[2]]-ll[[k,2]]]<5*Exp[-16*Log[10]])&&(0.999998<Abs[q[[1]]-ll[[k,1]]]<1.000001)),AppendTo[res,k]],{k,1,n}]]];res];*)

Classej2[j_,l_]:=Module[{res={j},n=Length[l],ll,q},ll=n*normalise[l];q=ll[[j]];If[j≤4,AppendTo[res,Enleve[j,{1,2,3,4}]],If[(n-1<q[[1]]≤n)||(n-1<q[[2]]≤ n)||(q[[1]]<1)||(q[[2]]<1),Do[If[((Abs[q[[1]]-ll[[k,1]]]<1)&&(n-1<Abs[q[[2]]-ll[[k,2]]]≤n))||((Abs[q[[2]]-ll[[k,2]]]<1)&&(n-1<Abs[q[[1]]-ll[[k,1]]]≤n)),AppendTo[res,k]],{k,1,n}]]];res];

MatConPBC[v_,p_]:=Module[{jn,jjn,rep,lcl,l,res,id},lcl=ListeClasses2[p];rep=Rep2[lcl];l=Length[rep];
res=Table[0,{x,l},{y,l}];Do[{id=Voisrep[rep[[j]],lcl,v];Do[{jjn=(Position [rep,id[[jj]]])[[1,1]];res[[j,jjn]]=1;res[[jjn,j]]=1},{jj,1,Length[id]}]},{j,1,l}];res];

(*ListeClasses[l_]:=Module[{res={},n=Length[l]},Do[AppendTo[res,Sort[Flatten[Classej[j,l]]]],{j,1,n}];res];*)

ListeClasses2[l_]:=Module[{res={},n=Length[l]},Do[AppendTo[res,Sort[Flatten[Classej2[j,l]]]],{j,1,n}];res];

(*Rep[l_]:=Module[{res={},dejavu={},k=1},Do[If[!MemberQ[dejavu,l[[j,1]]],{AppendTo[res,{l[[j,1]],k}],k=k+1,AppendTo[dejavu,l[[j,1]]]}],{j,1,Length[l]}];res];*)

Rep2[l_]:=Module[{res={},k=1},Do[If[!MemberQ[res,l[[j,1]]],AppendTo[res,l[[j,1]]]],{j,1,Length[l]}];res];

Voisdej[j_,v_]:=Module[{l=Length[v],res={},t},Do[If[v[[k,1]]==j,AppendTo[res,v[[k,2]]]],{k,1,l}];res];

Tousvois[j_,v_,l_]:=Module[{res={},ll=normalise[l],cl},cl=Flatten[Classej2[j,ll]];Do[AppendTo[res,Voisdej[cl[[k]],v]],{k,1,Length[cl]}];Flatten[res]];

Voisrep[j_,lcl_,vois_]:=Module[{res={},n=Length[lcl],v,cl},cl=lcl[[j]];Do[{v=Voisdej[cl[[m]],vois];Do[If[!MemberQ[res,lcl[[k,1]]],Do[If[MemberQ[v,lcl[[k,jj]]],AppendTo[res,lcl[[k,1]]]],{jj,1,Length[lcl[[k]]]}]],{k,1,n}]},{m,1,Length[cl]}];res];

normalise[l_]:=l/Max[l];

(*Calculs pour approximants k=1,2,3*)

TestE[l_]:=Sort[l]==-Sort[l,Greater];

TestEeps[l_,eps_]:=Module[{res=True,n=Length[l]},Do[res=res&&(Abs[(Sort[l])[[j]]+(Sort[l,Greater])[[j]]]<eps),{j,1,n}];res];