function c = solver(a)
% SOLVER: Takes the amino acid chain, a, as input and returns the
% corresponding configuration sequence, c. The configuration sequence, c,
% consists of the directions to the next successive amino acid:
% Direction: 1 = east, i = north, -1 = west, -i = south
%%%%%%%%%%%%%% Early termination cases %%%%%%%%%%%%%%%%%%%%%%%%
M=length(a);s=sum(a);
hcl=M-1;
c=ones(hcl,1);
q=find(a);
if s<4
if s==2
brk=floor((q(1)+q(2))/2); c(brk)=-1i; c(brk+1:hcl)=-1;
elseif s==3
if q(2)>q(1)+1
brk=floor((q(1)+q(2))/2);
c(q(1):brk-1)=1i;
c(brk+1:q(2)-1)=-1i;
end
beven=rem(q(2)-q(1),2);
if (q(3)==q(2)+1)
c(q(2))=-1i;
else
if beven
brk=floor((q(2)+q(3)-1)/2);
else
brk=floor((q(2)+q(3))/2);
end
c(brk)=-1i;
c(brk+1:q(3)-1)=-1;
c(q(3):hcl)=-1i;
end
end
return
end
% Permutator, written by Yuval Cohen
aCropL=min(M-q(1)+1,q(end));
if aCropL<13
dl=M-aCropL;
if dl
head = q(1)-1 >= M-q(end);
if head
at=a(q(1):end);
else
at=a(1:q(end));
end
else
at=a;
end
ctl=aCropL-1;
m=1;
t=[ones(ctl,1) ones(ctl,3^(ctl-1)-1)*(1+1i)];
for n=2:ctl;
bendLeft=[ones(n-1,1); 1i+zeros(ctl-n+1,1)]*ones(1,m);
bendRight=[ones(n-1,1); -1i+zeros(ctl-n+1,1)]*ones(1,m);
t(:,m+1:2*m)=t(:,1:m).*bendLeft;
t(:,2*m+1:3*m)=t(:,1:m).*bendRight;
m=3*m;
end
p = zeros(aCropL,m);
p(2:aCropL,:)=cumsum(t,1);
pD=diff(sort(p,1),1,1);
[n,bads]=find(pD==0);
p(:,bads)=[];
t(:,bads)=[];
pE=p(logical(at),:);
id = logical(ones(s));
[It,Jt] = find(triu(id,1));
e=sum(abs(pE(It,:)-pE(Jt,:)));
if ~dl
[m,n]=min(e);
c=t(:,n);
return
end
[m,n]=min(e);
emin=find(e==m);
m=1;
while m<length(emin);
c=t(:,emin(m));
if head
ct=[ones(dl,1)*[1 1i -1i -1]; c*ones(1,4)];
else
ct=[c*ones(1,4); ones(dl,1)*[1 1i -1i -1]];
end
for n=1:4
p=[0; cumsum(ct(:,n))];
pD=diff(sort(p));
if all(pD)
c=ct(:,n);
return;
end
end
m=m+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% End early termination cases %%%%%%%%%%%%%%%%%%%
c=zeros(M-1,1);
global I J iones
global C hv jv K Kh Kj SCM
global aori aoriM aoriN1 aoriP aorif;
aori=logical(a);aoriM=M;aoriN1=sum(a);aoriP=sum(1:aoriN1-1);
aorif=find(aori);iones=aorif;
[I,J] = find(triu(ones(aoriN1),1));Ia=logical(a);
I=aorif(I);J=aorif(J);
hv=zeros(4,2,2,2);jv=zeros(4,2,2,2);
hv([ 2 8 10 16 17 23 25 31])= 1;
hv([ 4 6 12 14 19 21 27 29])=-1;
jv([ 1 5 11 15 18 22 28 32])= 1;
jv([ 3 7 9 13 20 24 26 30])=-1;
card=[2 4 6 8];jd=[0 -1 1 0];hd=[-1 0 0 1];
K=100;%ajusted thanks to the guys which researched the lengths
%"Hydrophilic go away" this is the basic solver
% by Lucio Andrade
if sum(a(1:floor(end/2)))<sum(a(floor(end/2):end))
voltea=1; a=a(end:-1:1); else voltea=0;
end
a(~a)=-1;
di=diff([0;a;0]);locs=find(di);
zb=locs(di(locs)<0);zb(zb>length(a))=[];zb(zb<1)=[];
ze=locs(di(locs)>0)-1;ze(ze>length(a))=[];ze(ze<1)=[];
zee=zb+floor((ze-zb+1)/2);aa=ones(5,1);
todel=zb==zee;zb(todel)=[];zee(todel)=[];
act=zeros(M,1);act(zb)=1;act(zee)=-1;act=cumsum(act);
if ( length(zb)>1 & zb(1)==1 )
h=-floor((zb(2)-ze(1))/3)-ze(1);
act(1:ze(1))=0; else h=0; end
j=0;H=K+h;J=K+j;ss=sum(aa);
if a(1)==1 Kh=H;Kj=J;cont=1;
else Kh=0;Kj=0; cont=0;
end
C=int8(0);C(K+K,K+K)=0;C(J,H)=a(1);
for q=2:M
if act(q) [du,sh]=sort(-abs(1i*(j+jd)+(h+hd)));
else [du,sh]=sort(abs(1i*(j+jd)+(h+hd))); end
B=C(J-1:J+1,H-1:H+1);
opc=sh(find(~B(card(sh))));
if checa(J+jd(opc(1)),H+hd(opc(1)),min(M-q,ss)) sel=opc(1);
elseif checa(J+jd(opc(2)),H+hd(opc(2)),min(M-q,ss)) sel=opc(2);
elseif checa(J+jd(opc(3)),H+hd(opc(3)),min(M-q,ss)) sel=opc(3);
else sel=opc(4);
end
J=J+jd(sel); H=H+hd(sel); j=j+jd(sel); h=h+hd(sel);
if a(q)==1
Kh=(cont*Kh+H)/(cont+1);Kj=(cont*Kj+J)/(cont+1);
cont=cont+1;
end
C(J,H)=a(q);c(q-1)=1i*jd(sel)+hd(sel);
end
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
% OVERALL COMPARISON
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
CC=zeros(M-1,20);%ajust 20 at the end to number of trails
%'Hydrophilic go away' by LAC
CC(:,[1 2])=[c -c(end:-1:1)];CCk=2;
%fillrows by LAC
CCk=CCk+4;
CC(:,[CCk-3:CCk])=[fillrows(aori,-1) fillrows(aori,0) ...
fillrows(aori,1) fillrows(aori,2) ];
%magicball by LAC
CCk=CCk+1;CC(:,CCk)= magicball(aori);
%Other guys ... :) (various)
CCk=CCk+1;CC(:,CCk)= solverpira(aori);
%Mr Keenan contribution
CCk=CCk+1;CC(:,CCk)= pleats(aori,1);
setspecialcases %Various authors
kk=size(SCM,2);
CCk=CCk+kk;CC(:,CCk-kk+1:CCk)= SCM(1:M-1,:);
c=minenergy(CC(:,1:CCk));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out=checa(h,j,k)
global C
C(h,j)=1;
[hh,jj]=direc(h,j);
if ~k out=int8(1);C(h,j)=0;return;end;
if ~C(h+hh(1),j+jj(1)) out=checa(h+hh(1),j+jj(1),k-1);
if out; C(h,j)=0; return; end;
end
if ~C(h+hh(2),j+jj(2)) out=checa(h+hh(2),j+jj(2),k-1);
if out; C(h,j)=0; return; end;
end
if ~C(h+hh(3),j+jj(3)) out=checa(h+hh(3),j+jj(3),k-1);
if out; C(h,j)=0; return; end;
end
if ~C(h+hh(4),j+jj(4)) out=checa(h+hh(4),j+jj(4),k-1);
if out; C(h,j)=0; return; end;
end
C(h,j)=0; out=int8(0);
function [hh,jj]=direc(h,j)
global hv jv Kh Kj
hKh=h-Kh;
jKj=j-Kj;
hKh0=(hKh<0)+1;
jKj0=(jKj<0)+1;
ab=(abs(hKh)>abs(jKj))+1;
hh=hv(:,hKh0,jKj0,ab);
jj=jv(:,hKh0,jKj0,ab);
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
function c=fillrows(a,sw) % by Lucio Andrade
a=a(:);M=length(a);
if sum(a)<2 c=ones(M-1,1); return; end
v=diff(find(diff([0;~a;0])));
vz=v(1:2:end); lvz=length(vz);
v=diff(find(diff([0;a;0])));
vo=v(1:2:end); lvo=length(vo);
mu=ones(lvo,1);vodmu=ceil(vo./mu);
while max(vodmu)>sum(mu)+sw
can=find(vodmu==max(vodmu));
[dum0,h]=max(abs(can-(lvo+1)/2));
mu(can(h))=mu(can(h))+1;
vodmu=ceil(vo./mu);
end
nro=sum(mu); voe=zeros(nro,1);vze=zeros(nro,1);
vodmu=floor(vo./mu);j=1;k=1;
while j<=nro
tem=zeros(mu(k),1)+vodmu(k);
tem(1:rem(vo(k),mu(k)))=vodmu(k)+1;
if (j+j+mu(k)-1)<nro tem=tem(end:-1:1,:); end
voe(j:j+mu(k)-1)=tem;
j=j+mu(k);k=k+1;
end
switch sum(a([1 1 end]))
case 0, vze(cumsum(mu))=vz(2:end);
case 1, vze(cumsum(mu(1:end-1)))=vz(2:end);
case 2, vze(cumsum(mu))=vz;
case 3, vze(cumsum(mu(1:end-1)))=vz;
end
voe2=voe-1;
voe2(2:2:end)=-voe2(2:2:end);
tra=ceil((vze+1)/2);
va=ones(sum(mu),1);
nva=testtra(tra,voe2,vze,1,0);
ava=inf;
while nva<ava
ava=nva;va(:)=inf;
whe1=find(vze+1-tra);
for i=1:length(whe1)
va(i)=testtra(tra,voe2,vze,whe1(i),1);
end
[nva,h]=min(va);
if nva<=ava
tra(whe1(h))=tra(whe1(h))+1;
end
end
nva=testtra(tra,voe2,vze,1,0);
ava=inf;
while nva<ava
ava=nva;va(:)=inf;
whe1=find(tra-1);
for i=1:length(whe1)
va(i)=testtra(tra,voe2,vze,whe1(i),-1);
end
[nva,h]=min(va);
if nva<=ava
tra(whe1(h))=tra(whe1(h))-1;
end
end
ltra=length(tra);
lv=zeros(ltra,1);rv=lv;
for j=1:ltra-1
lv(j)=rv(j)+voe2(j);
rv(j+1)=lv(j)+tra(j)+tra(j)-vze(j)-2;
end
lv(ltra)=rv(ltra)+voe2(ltra);
vze2=vze;
vze2(2:2:end)=-vze2(2:2:end);
pui=[(lv(1:nro-1)+rv(2:nro)+vze2(1:nro-1))/2;lv(nro)];
locs=abs(diff([0;pui]))+1;
if ~a(1) locs(1)=locs(1)+vz(1); end
if ~a(end) locs(end)=locs(end)+vz(end); end
locs=cumsum(locs);
c=ones(M-1,1);
c(locs(1:end-1))=1i;
lp=locs(1:2:end);
rp=locs(2:2:end);
for i=1:length(rp)
c(lp(i)+1:rp(i)-1)=-1;
end
function va=testtra(tra,voe2,vze,w,s)
tra(w)=tra(w)+s;ltra=length(tra);
lv=zeros(ltra,1);rv=lv;
for j=1:ltra-1
lv(j)=rv(j)+voe2(j);
rv(j+1)=lv(j)+tra(j)+tra(j)-vze(j)-2;
end
lv(ltra)=rv(ltra)+voe2(ltra);
va=myvar(lv+rv);
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
% From this point ideas from others the above written
% all by Lucio Andrade
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
function c = solverpira(a)
global I J iones
al=length(a);
s=sum(a);
c=ones((al-1),1);
hcl=al-1;
ind1=1:hcl;
revind=al:-1:1;
revind1=hcl:-1:1;
id = ones(s);
[I,J] = find(triu(id,1));
Ia=logical(a);
iones=find(Ia);
I=iones(I);
J=iones(J);
iones1=iones(1);
iones2=iones(end)-1;
ionesind=iones1:iones2;
a1=a(iones1:iones2+1);
a1l=length(a1);
a1l1=a1l-1;
rev1ind=a1l:-1:1;
rev1ind1=a1l1:-1:1;
csal=ceil(sqrt(al));
bc=Inf;
C=[zeros(hcl,2*al)];
Ck=1;
% zigzag
hc=1i+zeros(hcl,1);
hc1=hc;
for n=max(2,min(a1l1,3)):min([13,csal+2,floor(a1l/3)]);
t=[ones(n-1,1);1i; -ones(n-1,1);1i];
t=t(:,ones(floor(a1l/n),1));
t=t(:);
t=t(1:a1l1);
hc(ionesind)=t;
C(:,Ck)=hc;
Ck=Ck+1;
t=improvezigzagend(t,a1,n-1,1);
if ~isempty(t)
hc1=hc;
hc1(ionesind)=t;
if t(a1l1)==-1i
i=find(real(t));
hc1(iones2+1:hcl)=t(i(end));
end
C(:,Ck)=hc1;
Ck=Ck+1;
end
t=zigzag3(a1,n);
hc1(1:iones1-1)=1;
hc1(ionesind)=t;
i=find(real(t));
hc1(iones2+1:hcl)=t(i(end));
C(:,Ck)=hc1;
Ck=Ck+1;
t=improvezigzagend(t,a1,n,0);
if ~isempty(t)
hc1(ionesind)=t;
i=find(real(t));
hc1(iones2+1:hcl)=t(i(end));
C(:,Ck)=hc1;
Ck=Ck+1;
end
t=zigzag3(a1(rev1ind),n);
i=find(real(t));
hc1(1:iones1-1)=t(i(end));
hc1(ionesind)=t(rev1ind1);
hc1(iones2+1:hcl)=1;
C(:,Ck)=hc1;
Ck=Ck+1;
end
% random walk
for jgh=1:ceil(al/8),
for k1=1:ceil(ceil(al/8)/4),
hc=ones(hcl,1);
hc(k1+1)=1i;
hc(k1+2:2*k1+2)=-1;
hc(2*k1+3)=-1i;
k=2*k1+4; d=-1i; pos=-1; m=(k1+1)+1i;
while k<=hcl,
if randn<=0,
hc(k)=d;
m=m+abs(real(d))+1i*abs(imag(d));
pos=pos+d;
q=sign(real(pos))*sign(imag(pos));
if real(d)==0;
d=1i*d*q;
hc(k+1:hcl)=d;
k=k+real(m)+1;
pos=pos+d*real(m);
else
d=-1i*d*q;
hc(k+1:hcl)=d;
k=k+imag(m)+1;
pos=pos+d*imag(m);
end
else
m=m+1i*abs(real(d))+abs(imag(d));
q=sign(real(pos))*sign(imag(pos));
if q==0;
q=-sign(real(pos));
end
if real(d)==0;
d=-1i*d*q;
pos=pos+d;
hc(k)=d;
d=-1i*d*q;
hc(k+1:hcl)=d;
k=k+imag(m)+1;
pos=pos+d*imag(m);
else
d=1i*d*q;
pos=pos+d;
hc(k)=d;
d=1i*d*q;
hc(k+1:hcl)=d;
k=k+real(m)+1;
pos=pos+d*real(m);
end
end
end
hc=hc(ind1);
C(:,Ck:Ck+1)=[hc hc(revind1)];
Ck=Ck+2;
end
end % random walk
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% snake disc
hc = snakedisc(al);
hc=hc(revind1);
C(:,Ck)=hc;
Ck=Ck+1;
hc = rlesnakeold(a(revind));
hc=hc(revind1);
C(:,Ck)=hc;
Ck=Ck+1;
cc=c;
if s<a1l
% rle snake
hc = rlesnake(a1,s);
cc(:)=1i;
cc(ionesind)=hc;
C(:,Ck)=cc;
Ck=Ck+1;
hc = rlesnake(a1(rev1ind),s);
hc=hc(rev1ind1);
cc(:)=1i;
cc(ionesind)=hc;
C(:,Ck)=cc;
Ck=Ck+1;
end
for kk = 1:12
hc = snakesquare(a);
C(:,Ck)=hc;
Ck=Ck+1;
end
for kk = 1:12
hc = snakesquare(a(revind));
hc = hc(revind1);
C(:,Ck)=hc;
Ck=Ck+1;
end
[c,bc]=minenergy(C(:,1:Ck-1));
[c,bc] = msolver(a,c,bc);
p=[0; cumsum(c)];
C(:,1)=c;
Ck=2;
x=real(p);
y=imag(p);
X1=x(:,ones(1,al));
X2=X1';
Y1=y(:,ones(1,al));
Y2=Y1';
dY=(Y1==Y2);
dX=(X1==X2);
maxX=find(max(X1.*dY)==x');
xI1=maxX(find(diff(maxX)==1));xI1=xI1(~isreal(c(xI1))&xI1>2&xI1<hcl-1);
minX=find(min(X1+(~dY*1000))==x');
xI2=minX(find(diff(minX)==1));xI2=xI2(~isreal(c(xI2))&xI2>2&xI2<hcl-1);
maxY=find(max(Y1.*dX)==y');
yI1=maxY(find(diff(maxY)==1));yI1=yI1(isreal(c(yI1))&yI1>2&yI1<hcl-1);
minY=find(min(Y1+(~dX*1000))==y');
yI2=minY(find(diff(minY)==1));yI2=yI2(isreal(c(yI2))&yI2>2&yI2<hcl-1);
for i=xI1,
C(:,Ck)=[c(2:i-1);1 ;c(i) ;-1;c(i+1:hcl-1)];
Ck=Ck+1;
end
for i=xI2,
C(:,Ck)=[c(2:i-1);-1 ;c(i) ;1;c(i+1:hcl-1)];
Ck=Ck+1;
end
for i=yI1,
C(:,Ck)=[c(2:i-1);1i ;c(i) ;-1i;c(i+1:hcl-1)];
Ck=Ck+1;
end
for i=yI2,
C(:,Ck)=[c(2:i-1);-1i ;c(i) ;1i;c(i+1:hcl-1)];
Ck=Ck+1;
end
c=minenergy(C(:,1:Ck-1));
t=[1 -1 1i -1i];
p=[0; cumsum(c)];
a1=~(sum(p(:,[1 1 1 1])==t(ones(length(p),1),:)));
c=minenergy([c [-t(find(a1));c(1:hcl-1,ones(1,sum(a1)))]]);
a=randn(1,63);
%_____________________________________________
function [c1,me] = msolver(a,c1,me)
global I J
al=length(a);
hc=ones((al-1),1);
r0 = sum(diff(a)>0);
s = ceil(sqrt(r0+sum(a)));
f = find(a);
k = 10;
e = Inf;
while k > 0
k = k-1;
sign = 1;
f1 = f;
if (f1(1) > 1)
c = ones(1,f1(1)-2);
else
c = [];
end
t0 = 0;
while ~isempty(f1)
t = length(c)+1+t0+s;
f1 = f1(f1 > t);
if ~isempty(f1) & f1(1) > t+1
t1 = floor((f1(1)-t)/2+randn/1.5);
else
t1 = 0;
end
c = [c ones(1,1+t0+s+t1)];
t0 = t1;
%sign = -sign;
end
if (length(c) < al-1)
c = [c ones(1,al-1-length(c))];
else
c = c(1:(al-1));
end
%p = [0;cumsum(c(:))];
%pdiff = p(I)-p(J);
%e = sum(abs(p(I)-p(J)));
%e = sum(pdiff.*conj(pdiff));
%e = Inf; %Ha! /PR
%if e<me
% if me-e > 1
% k = min(10,k+4);
% end
% c1 = c(:);
% me = e;
%end
end
c = ones(al-1,1);
n = cumsum([3 10:8:al-1]);
temp = min(find(n>al-1))-1;
if ~isempty(temp)
n = n(1:temp);
end
add = 0;
for kk = 1:length(n)
c(n(kk):n(kk)+kk+add) = -1;
c(n(kk)+kk+add+1:n(kk)+2*kk+2*add+1) = 1i;
add = add + 1;
end
n = cumsum([2 8:8:al-1]);
temp = min(find(n>al-1))-1;
if ~isempty(temp)
n = n(1:temp);
end
add = -1;
for kk = 1:length(n)
c(n(kk):n(kk)+kk+add) = -1i;
add = add + 1;
end
while sum(diff([0;a])>0) > sqrt(sum(a));
a = (a + [0; a(1:end-1)] + [a(2:end); 0]) > 0;
end;
d = diff([0;a;0]);
i1 = find(d>0)-1;
N = find(d<0) - i1 - 1;
C = zeros(al-1,11);
Ck=1;
for k = sqrt(sum(a)) + (-1:.2:1);
m = [];
for i=1:length(i1);
n = max(0, ceil ((N(i)-2.5*k) / k));
r = N(i) - n*k;
if r>1.5*k;
m = [m, i1(i)+[r/4, r/2-k/2+k*(1:n), 3*r/4+k*n]];
else
m = [m, i1(i) + r/2];
end;
end;
cb = zeros(length(a)-1,1);
for i=1:length(m)-1;
d = (m(i)+m(i+1))/2;
b = round(d);
cb(b) = 1;
m(i+1) = m(i+1)-2*(d-b);
end;
c2 = 1-2*rem(cumsum(cb(:)),2);
c2(logical(cb))=1i;
C(:,Ck)=c2;
Ck=Ck+1;
end
[c2,result]=minenergy(C(:,1:Ck-1));
if result<me
c1=c2;
me=result;
end
%%% Snake Disc.
function c = snakedisc(N)
R = sqrt(N/pi);
c = [];
dd = 1;
ll = 0;
for hh = -R:R
ll = max(round(abs(halfchord(R,hh+0.5))),1);
c = [ c;dd(ones(1,ll-1),1);1i;-dd(ones(1,ll),1) ];
dd = -dd;
end
if length(c) < N-1; c(end+1:N-1)=c(end); end;
c = c(1:N-1);
function [c,e]=minenergy(c)
global I J iones
if(size(c,2)<2)
return
end
p = [zeros(1,size(c,2));cumsum(c)];
% Sum of square distances costs only O(iones) operations. /PR
if(size(c,2)>3)
secondmoment=myvar(real(p(iones,:)))+myvar(imag(p(iones,:)));
[scratch,ixlist] = sort(secondmoment);
ixlist=ixlist(1:3);
else
ixlist=1:size(c,2);
end
[e,x]=min(sum(abs(p(I,ixlist)-p(J,ixlist))));
c=c(:,ixlist(x));
function y = myvar(x)
m=size(x,1);
avg = sum(x)/m;
centerx = x - avg(ones(m,1),:);
y = sum((conj(centerx) .* centerx))/m;
function ch = halfchord(r,h)
ch = sqrt(r*r-h*h);
function c = snakesquare(a)
c1 = sum(a);
r0 = sum(diff(a)>0);
s = ceil(sqrt(r0+c1));
f1 = find(a);
al = length(a);
sign = 1;
if (f1(1) > 1)
c = ones(f1(1)-2,1);
else
c = [];
end
t0 = 0;
while ~isempty(f1)
t = length(c)+1+t0+s;
f1 = f1(f1 > t);
if ~isempty(f1) & f1(1) > t+1
t1 = floor((f1(1)-t)/2+randn/1.5);
else
t1 = 0;
end
c = [c;sign(ones(t0+s+t1,1));1i];
t0 = t1;
sign = -sign;
end
if (length(c) < al-1)
c = [c;-sign(ones(al-1-length(c),1))];
else
c = c(1:(al-1));
end
%%% RLE Snake.
function c = rlesnake(a,N1)
% run length encode a
N = length(a);
S = round(sqrt(N1)*1.5);
S1=S/2;
%pminrev=ceil(S/3); % better estimation?(depending on length(edges),N1,....)
pminrev=0;
edges = [0;find(diff(a));N];
n=1;
while n<size(edges,1)-3
if edges(n+3)-edges(n)<S1
% combine rows to one
edges(n+1:n+2)=[];
else
n=n+2;
end
end
edges = diff(edges);
k=edges(1);
c = ones(N-1,1);
if k>S
l=ceil(k/2)-1;
c(l+1)=1i;
c(l+2:k)=-1;
dd=-1;
p=rem(k,2)-l/2; % position (around middle)
else
p=(k-1)/2;
dd = 1;
end
for nn = 2:2:length(edges)
n=edges(nn);
N=edges(nn+1);
Nlong=0;
if N>S
Nlong=floor(N/2); % should be calculated better (depending on p and n)
N=N-Nlong;
end
pd=N/2; % desired position for next row
ap=abs(p);
dpd=pd-ap; % offset to current position
adpd=abs(dpd);
nN=n+N;
if dd*p<0 % next step would be farther from middle
if n<=ap
c(k:k+nN-1)=dd;
p=p+dd*(nN);
k=k+nN;
N=0;
else
n1=ceil(ap);
c(k:k+n1-1)=dd;
k=k+n1;
p=p+dd*n1;
n=n-n1;
nN=n+N;
% recalculate position values
ap=abs(p);
dpd=pd-ap; % offset to current position
adpd=abs(dpd);
end
end
if N % not yet processed above
if adpd>n % higher desired step then possible
if ap>pd % step towards middle
if ap<pminrev % not yet reversing
if ap+nN<pminrev % don't reverse at all
c(k+1:k+nN-1)=dd;
p=p+dd*nN;
else % reverse after zeros
c(k+1:k+n-1)=dd;
c(k+n)=1i;
dd=-dd;
c(k+n+1:k+nN-1)=dd;
p=p+dd*(n-N-1);
end
else % reverse
c(k)=1i;
dd=-dd;
c(k+1:k+n-1+N)=dd;
p=p+dd*(nN-1);
end % reversed
else % step out
p=p+dd*(n-N+1);
c(k:k+n-1)=dd;
c(k+n)=1i;
dd=-dd;
c(k+n+1:k+nN-1)=dd;
end
else
n1=floor(adpd);
n2=floor((n-n1)/2);
if dpd>0
p=p+dd*(n1-rem(n-n1,2)-N+1);
c(k:k+n1+n2-1)=dd;
dd=-dd;
c(k+n1+n2)=1i;
c(k+n1+n2+1:k+nN-1)=dd;
else
p=p-dd*(rem(n-n1,2)-1+n1+N);
c(k:k+n2-1)=dd;
dd=-dd;
c(k+n2)=1i;
c(k+n2+1:k+nN-1)=dd;
end
end % if else
k=k+nN;
if Nlong
c(k)=1i;
dd=-dd;
c(k+1:k+Nlong-1)=dd;
k=k+Nlong;
p=p+dd*(Nlong-1);
end
end
end
function c=zigzag3(a,l)
% zigzag3
al=length(a);
k=1; % index in c
c=zeros(al-1,1);
d=1; % direction
while 1
if k==1
% first line
% this is put in this loop because the end of the loop has to be used
% this hopefully works, but nice code is something completely different....
% sorry about that
m=find(a==0);
pos=0;
if ~isempty(m)&m(1)<=l
m=m(1)-1;
n=find(a(m+2:al));
if ~isempty(n)&n>1
c(1:m-1)=d;
n=floor(n(1)/2);
c(m:m+n-1)=-1i;
c(m+n)=d;
c(m+n+1:m+2*n)=1i;
pos=m;
k=m+1+2*n;
if a(k)==0|pos<l
c(k)=d;
k=k+1;
pos=pos+1;
end
while pos<l
n=find(a(k+1:al)==0);
if isempty(n)|pos+n(1)>=l
break;
end
n=n(1)-1;
c(k:k+n-1)=d;
pos=pos+n;
k=k+n;
n=find(a(k+1:al));
if isempty(n)
break;
elseif n(1)==1
c(k)=d;
pos=pos+1;
k=k+1;
else
n=floor(n(1)/2);
c(k:k+n-1)=-1i;
c(k+n)=1;
c(k+n+1:k+2*n)=1i;
k=k+2*n+1;
pos=pos+1;
if pos<l
c(k)=d;
pos=pos+1;
k=k+1;
end
end
end % while
end % if
end
if pos<l
c(k:k+l-pos-1)=d;
k=k+l-pos;
end
else % not the first line
c(k:k+l-1)=d;
k=k+l;
end
if k==al-1
c(k)=1i;
break;
elseif k>=al-1
break;
end
l1=find(a(k+1:al)); % can't be empty
l1=l1(1);
if l1>=3
l1=floor((l1-1)/2);
c(k:k+l1-1)=d;
k=k+l1;
c(k)=1i;
c(k+1:k+l1)=-d;
k=k+l1+1;
else
c(k)=1i;
k=k+1;
end
d=-d;
end
c=c(1:al-1);
function c = rlesnakeold(a)
N = length(a);
% run length encode a
edges = [1 1+find(a(2:N) ~= a(1:N-1))'];
val = a(edges)';
run = [ edges(2:end)-edges(1:end-1) N-edges(end)+1 ];
N1 = sum(run(val>0));
S = round(sqrt(N1));
c = [];
dd = 1;
for nn = 1:length(val)
V = val(nn);
R = run(nn);
if V
if R < 1.5*S
c = [c;dd(ones(1,R),1)];
else
if R == 1
c = [c;dd];
elseif R == 2
c = [c;dd;1i];
dd = -dd;
else
k = round((R-1)/2);
c = [c;dd(ones(1,k),1);1i;-dd(ones(1,R-k-1),1)];
dd = -dd;
end
end
else
if R == 1
c = [c;1i];
dd = -dd;
elseif R == 2
c = [c;dd;1i];
dd = -dd;
else
k = round((R-1)/2);
c = [c;dd(ones(1,k),1);1i;-dd(ones(1,R-k-1),1)];
dd = -dd;
end
end
end
if length(c) > N-1; c=c(1:N-1); end;
function c=improvezigzagend(c,a,l,godown)
i=find(a==0);
if isempty(i)
c=[]; % no trial
return
end
al=length(a);
cl=al-1;
i=i(end); % last 0
j=find(a(1:i));
j=j(end); % last 1 before last 0
p=sum(c(1:j));
c1=c(j);
n=i-j;
if n<2|imag(p)<1
c=[];
return
end
n=floor(n/2);
if c1==1i % up
c(j:j+n-1)=c(j-1);
c(j+n)=1i;
c(j+n+1:cl)=-c(j-1);
else
c(j:j+n-1)=1i;
c(j+n)=c1;
c(j+n+1:j+2*n)=-1i;
c(j+2*n+1:cl)=c1;
end
wentdown=0;
p=sum(c);
pr=real(p);
if pr<-1&imag(p)+pr>0
j=al+pr+1;
wentdown=1;
elseif pr>l+2
j=al-pr+l+1;
wentdown=1;
end
if wentdown
c0=c;
c(j:al-1)=-1i;
if ~godown
p=cumsum(c);
if any(ismember(p(j:al-1),p(1:j-2)))
c=c0;
end
end
end
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
%++++++++++++++++++++++++++++++++++++++++++++++++++++++
function c=magicball(a) %by Lucio Andrade
M=length(a);
if M<10; c=ones(M-1,1); return; end
ao=find(a);
if isempty(ao) c=ones(M-1,1); return; end
aol=ao(end)-ao(1)+1;
bll=[0 2 2 2 2 2 3 3 3 4 4 4 4 4 5 ...
5 5 5 5 5 5 5 6 6 5 6 6 6 6 7 7 ...
7 7 6 6 6 7 7 7 7 7 7 7 8 8 8 8 ...
7 8 8 8 8 8 8 9 8 9 9 9 9 9 9 9 ...
9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 11 ...
11 11 10 10 10 10 10 10 11 11 11 11 11 11 11 11 ...
11 11 11 11 11 11 12 12 11 11 12 12 12 12 12 12 ...
12 12 12 12 12 12 12 12 13 13 12 13 13 13 13 13 ...
13 13 13 13 13 13 13 13 13 13 13 13 14 14 14 13 ...
13 13 14 14 14 14 14 14 14 14 14 14 14 15 14 15 ...
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 ...
15 15 15 15 15 15 15 15 15 15 15 15 15 16 16 16 ...
16 17 17 17 17 17 17 17 16 16 16 16 16 16 16 17 ...
17 17 16 16 16 16 16 17 17 17 17 17 17 17 17 17 ...
17 17 17 17 17 17 17 17 17 17 17 18 17 18 18 18 ...
18 18 17 18 17 18 18 18 18 18 18];
BL=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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11 11 9 7 6 0 0 0 0 0 0 0
7 8 9 10 11 11 11 10 8 5 3 0 0 0 0 0 0 0
3 5 8 10 11 11 11 11 10 8 6 0 0 0 0 0 0 0
6 7 9 10 11 11 10 10 9 7 6 0 0 0 0 0 0 0
6 7 9 10 11 11 10 10 9 7 6 0 0 0 0 0 0 0
6 7 9 10 11 12 11 10 9 7 6 0 0 0 0 0 0 0
6 7 9 10 11 12 11 10 9 7 6 0 0 0 0 0 0 0
6 7 9 11 12 12 11 10 9 7 5 0 0 0 0 0 0 0
6 7 9 10 11 12 12 11 9 7 6 0 0 0 0 0 0 0
6 8 10 11 12 12 11 10 9 7 5 0 0 0 0 0 0 0
5 7 9 10 11 12 12 11 10 8 5 3 0 0 0 0 0 0
5 7 9 10 11 12 12 11 10 8 5 3 0 0 0 0 0 0
7 8 10 11 11 11 11 11 10 8 7 0 0 0 0 0 0 0
7 8 10 11 11 11 11 11 10 8 7 0 0 0 0 0 0 0
3 5 9 10 11 12 12 12 11 9 7 6 0 0 0 0 0 0
3 5 9 10 11 12 12 12 11 9 7 6 0 0 0 0 0 0
4 6 9 10 11 12 12 12 11 9 7 6 0 0 0 0 0 0
4 6 9 10 11 12 12 12 11 9 7 6 0 0 0 0 0 0
4 6 9 11 12 12 12 12 11 9 7 6 0 0 0 0 0 0
4 6 9 11 12 12 12 12 11 9 7 6 0 0 0 0 0 0
4 6 9 11 12 12 12 12 11 10 8 6 0 0 0 0 0 0
4 6 9 11 12 12 12 12 11 10 8 6 0 0 0 0 0 0
5 7 10 11 11 12 13 12 11 10 7 5 0 0 0 0 0 0
5 7 10 11 11 12 13 12 11 10 8 7 0 0 0 0 0 0
5 7 10 11 11 12 13 12 11 10 8 7 0 0 0 0 0 0
5 7 10 11 11 12 13 12 11 10 8 7 0 0 0 0 0 0
5 7 10 11 12 13 13 12 11 10 8 7 0 0 0 0 0 0
5 7 10 11 12 13 13 12 11 10 8 7 0 0 0 0 0 0
5 6 8 10 12 13 13 13 12 10 8 6 5 0 0 0 0 0
5 6 8 10 12 13 13 13 12 10 8 6 5 0 0 0 0 0
7 8 10 11 12 13 13 12 11 10 8 7 0 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
5 7 10 11 12 13 13 13 12 11 10 7 5 0 0 0 0 0
6 7 9 11 12 13 14 13 12 11 9 7 6 0 0 0 0 0
6 7 9 11 12 13 14 13 12 11 10 8 6 0 0 0 0 0
6 7 9 11 12 13 14 13 12 11 10 8 6 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
7 8 10 12 13 13 13 13 13 12 10 8 7 0 0 0 0 0
2 5 9 11 12 13 14 14 13 12 11 10 8 6 0 0 0 0
5 7 10 12 13 13 13 13 13 13 11 9 6 3 0 0 0 0
5 7 10 12 13 13 13 13 13 13 11 9 6 4 0 0 0 0
7 9 11 12 13 14 14 14 13 12 11 8 7 0 0 0 0 0
7 9 11 12 13 14 14 14 13 12 11 8 7 0 0 0 0 0
7 9 11 12 13 14 14 14 13 12 11 8 7 0 0 0 0 0
6 7 10 12 13 14 14 13 13 13 12 10 6 4 0 0 0 0
6 7 10 12 13 14 14 13 13 13 12 10 6 4 0 0 0 0
6 7 10 12 13 14 14 14 14 13 12 10 6 4 0 0 0 0
6 7 10 12 13 14 14 14 14 13 12 10 6 4 0 0 0 0
6 7 10 12 13 14 14 14 14 13 12 10 7 5 0 0 0 0
6 7 10 12 13 14 14 14 14 13 12 10 7 5 0 0 0 0
7 8 10 12 13 14 14 14 14 13 12 10 7 5 0 0 0 0
7 8 10 12 13 14 14 14 14 13 12 10 7 5 0 0 0 0
5 7 10 12 13 14 15 15 14 13 12 10 8 7 0 0 0 0
5 7 10 12 13 14 15 15 14 13 12 10 8 7 0 0 0 0
7 9 11 12 13 14 14 14 14 13 12 10 7 6 0 0 0 0
3 6 10 12 13 13 14 15 14 13 13 12 10 6 3 0 0 0
7 9 11 12 13 14 15 15 14 13 12 10 7 6 0 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
5 7 10 12 13 14 15 15 15 14 13 12 10 7 5 0 0 0
3 6 9 11 13 14 15 15 15 15 14 13 11 8 7 0 0 0
3 6 9 11 13 14 15 15 15 15 14 13 11 8 7 0 0 0
3 6 10 12 13 14 15 15 15 15 14 13 11 8 7 0 0 0
3 6 10 12 13 14 15 15 15 15 14 13 11 8 7 0 0 0
6 8 11 12 13 14 15 15 14 14 13 12 11 8 6 0 0 0
6 8 11 12 13 14 15 15 14 14 14 13 11 8 6 0 0 0
6 8 11 12 13 14 15 15 14 14 14 13 11 8 6 0 0 0
7 8 10 12 14 15 15 15 15 15 14 12 10 8 7 0 0 0
7 8 10 12 14 15 15 15 15 15 14 12 10 8 7 0 0 0
7 8 10 12 14 15 15 15 15 15 14 12 10 8 7 0 0 0
6 8 11 12 13 14 15 16 16 15 14 13 11 8 6 0 0 0
8 9 11 13 14 15 16 16 15 14 13 12 10 7 6 0 0 0
6 8 11 13 14 14 15 16 16 15 14 13 11 8 6 0 0 0
8 9 11 13 14 15 16 16 15 14 13 12 11 8 6 0 0 0
7 9 11 13 14 14 15 16 16 15 14 13 11 8 7 0 0 0
7 9 11 13 14 14 15 16 16 15 14 13 11 8 7 0 0 0
7 9 12 13 14 15 15 15 15 15 14 13 12 9 7 0 0 0
7 9 12 13 14 15 15 15 15 15 14 13 12 9 7 0 0 0
8 9 11 13 14 15 16 16 16 15 14 13 11 9 8 0 0 0
8 9 11 13 14 15 16 16 16 15 14 13 11 9 8 0 0 0
8 9 11 13 14 15 16 16 16 15 14 13 11 9 8 0 0 0
4 6 9 11 13 15 16 16 16 16 15 14 13 11 8 7 0 0
4 6 9 11 13 15 16 16 16 16 15 14 13 11 8 7 0 0
4 6 9 12 14 15 16 16 16 16 15 14 13 11 8 7 0 0
4 6 9 12 14 15 16 16 16 16 15 14 13 11 8 7 0 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
3 6 10 12 14 15 15 16 17 16 15 15 14 12 10 6 3 0
7 8 10 12 14 15 16 17 17 16 15 14 13 12 9 7 0 0
7 8 10 12 14 15 16 17 17 16 15 14 13 12 9 7 0 0
7 8 10 12 14 15 16 17 17 16 15 14 13 12 9 7 0 0
7 8 11 13 14 15 16 17 17 16 15 14 13 12 9 7 0 0
7 8 11 13 14 15 16 17 17 16 15 14 13 12 9 7 0 0
7 9 12 14 15 16 16 16 16 16 16 15 13 11 8 6 0 0
7 9 12 14 15 16 16 16 16 16 16 15 13 11 8 6 0 0
5 7 10 12 14 15 16 17 17 17 16 15 14 12 10 7 5 0
5 7 10 12 14 15 16 17 17 17 16 15 14 12 10 7 5 0
5 7 10 12 14 15 16 17 17 17 16 15 14 12 10 7 5 0
8 9 12 14 15 16 17 17 16 16 16 15 13 11 9 7 0 0
8 9 12 14 15 16 17 17 16 16 16 15 13 11 9 7 0 0
8 9 12 14 15 16 17 17 17 17 16 15 13 11 9 7 0 0
8 9 12 14 15 16 17 17 17 17 16 15 13 11 9 7 0 0
7 9 12 14 15 16 17 17 17 17 16 15 14 12 9 7 0 0
6 8 11 13 15 16 16 16 16 16 16 15 14 13 11 8 6 0
6 8 11 13 15 16 16 16 16 16 16 15 14 13 11 8 6 0
6 8 11 13 15 16 16 16 17 17 16 15 14 13 11 8 6 0
6 8 11 13 15 16 16 16 17 17 16 15 14 13 11 8 6 0
7 8 11 13 14 16 17 17 17 17 17 16 14 13 11 8 7 0
7 8 11 13 14 16 17 17 17 17 17 16 14 13 11 8 7 0
7 8 11 13 14 16 17 17 17 17 17 16 14 13 11 8 7 0
7 8 11 13 14 16 17 17 17 17 17 16 14 13 11 8 7 0
7 8 11 13 14 16 17 17 17 17 17 16 14 13 11 8 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
7 9 12 14 15 16 17 17 17 17 17 16 15 14 12 9 7 0
8 9 12 14 15 16 17 18 18 17 16 16 15 13 11 9 8 0
8 9 12 14 15 16 17 18 18 18 17 16 15 13 11 9 8 0
8 9 12 14 15 16 17 18 18 18 17 16 15 13 11 9 8 0
6 8 11 13 15 16 17 17 17 17 17 17 16 15 13 10 6 4
8 9 12 14 15 16 17 18 18 18 17 16 15 13 12 10 8 0
6 8 12 14 15 16 17 18 18 17 17 17 16 15 13 10 6 4
6 8 12 14 15 16 17 18 18 17 17 17 16 15 13 10 6 4
6 8 12 14 15 16 17 18 18 17 17 17 16 15 13 10 6 4
6 8 12 14 15 16 17 18 18 17 17 17 16 15 13 10 7 5
6 8 12 14 15 16 17 18 18 17 17 17 16 15 13 10 7 5
8 9 12 14 15 16 17 18 18 18 18 17 16 15 13 10 8 0
6 8 12 14 15 16 17 18 18 18 18 17 16 15 13 10 7 5
8 10 13 14 15 16 17 18 18 18 18 17 16 15 13 10 8 0
7 9 12 14 15 16 17 18 18 18 18 17 16 15 13 10 7 5
7 9 12 14 15 16 17 18 18 18 18 17 16 15 13 11 8 5
7 9 12 14 15 16 17 18 18 18 18 17 16 15 13 11 8 5
4 7 11 13 15 16 16 17 18 18 18 18 17 16 14 12 10 8
4 7 11 13 15 16 17 18 18 18 18 18 17 16 14 12 10 8
4 7 11 13 15 16 17 18 18 18 18 18 17 16 14 12 10 8];
bl=cumsum(BL(aol,1:bll(aol)));
numben=bll(aol)-1;
bl(end+1:end+2)=bl(end)+5;
ax=[a(ao(1):ao(end));zeros(6*bll(aol),1)];
P=[ 0 0 0; 0 0 -1; 0 0 1;-1 -1 0; 0 -1 -1; 0 1 1;
1 1 0;-1 -1 -1;-1 -1 1; 0 -1 -2; 0 1 2; 1 1 -1;
1 1 1;-1 -2 -1; 0 -1 -3; 0 1 3; 1 2 1;-1 -2 -2;
1 2 2;-1 -2 -3; 1 2 3;-1 -3 -3; 1 3 3;-1 -3 -4;
1 3 4;-1 -3 -5; 1 3 5];
hbl=[0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;
0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;
0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2;0 1 2];
if aol<6 P(end,:)=[];hbl(end,:)=[]; end
if aol<5 P(end-1,:)=[];hbl(end-1,:)=[]; end
if aol<4 P(end-2,:)=[];hbl(end-2,:)=[]; end
j=1;
if (bl(1)==1 | bl(2)==3)
pr=find(P(:,1)>=0);
[du,h]=min(sum(ax(bl(hbl(pr,:)+j)+P(pr,:))+ax(bl(hbl(pr,:)+j)+P(pr,:)+1),2));
else
[du,h]=min(sum(ax(bl(hbl+j)+P)+ax(bl(hbl+j)+P+1),2));
end
bl(j)=bl(j)+P(h,1);
bl(j+1:end)=bl(j+1:end)+P(h,1)+P(h,1);
for j=2:numben
[du,h]=min(sum(ax(bl(hbl+j)+P)+ax(bl(hbl+j)+P+1),2));
bl(j)=bl(j)+P(h,1);
bl(j+1:end)=bl(j+1:end)+P(h,1)+P(h,1);
end
c=ones(M-1,1);
bl=bl+ao(1)-1;
bl(bl>=ao(end))=[];
c(bl)=1i;
sign=0;
for i=1:M-1
if c(i)==1i sign=~sign;
elseif sign c(i)=-1; end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function c = pleats(aarg,type) % by MR Keenan ?
al=length(aarg);
s=sum(aarg);
hcl=al-1;
c=ones(hcl,1);
if s<3
if s==2
q=find(aarg);
brk=floor((q(1)+q(2))/2); c(brk)=-1i; c(brk+1:end)=-1;
end
return
end
%remove the a's that have less than one zeros in a row.
a = aarg;
for j=2:length(aarg)-1
if( aarg(j) == 0 )
if( aarg(j-1) == 1 & aarg(j+1) == 1 )
a(j) = 1;
end
end
end
%remove the a's that have less than two zeros in a row.
if( type == 2 )
for j=2:length(aarg)-2
if( aarg(j) == 0 & aarg(j+1) == 0 )
if( aarg(j-1) == 1 & aarg(j+2) == 1 )
a(j) = 1;
a(j+1) = 1;
end
end
end
end
c = [ones(length(a)-1,1)];
a = a(:);
while sum(diff([0;a;0])==1) > sqrt(sum(a));
a = conv([1;1;1],a);
a = a(2:end-1)>0;
end;
k = sqrt(sum(a));
d = diff([0;a;0]);
i1 = find(d==1)-1;
N = find(d==-1) - i1 - 1;
m = [];
for i=1:length(i1);
n = max(0, ceil ((N(i)-2.5*k) / k));
r = N(i) - n*k;
if r>1.5*k;
m = [m, i1(i)+[r/4, r/2-k/2+k*cumsum(ones(1,n)), 3*r/4+k*n]];
else
m = [m, i1(i) + r/2];
end;
end;
cb = zeros(length(a)-1,1);
for i=1:length(m)-1;
d = (m(i)+m(i+1))/2;
b = round(d);
cb(b) = 1;
m(i+1) = m(i+1)-2*(d-b);
end;
c = (-1).^cumsum(cb);
c(logical(cb))=1i;
function nonused
dum=0;
ch=[];
for k=1:99
if dum(k)==-i ch=[ch '-i '];
elseif dum(k)==i ch=[ch 'i '];
elseif dum(k)==-1 ch=[ch '-1 '];
else ch=[ch '1 '];
end
end
ch(end)=';'
function setspecialcases
global SCM
%%%% add strings at your convenience, reverse the strings if you
%%%% want to test both directions
SCM=[i 1 -i -i -1 -1 i i i 1 1 1 -i -i -i -i -1 -i -1 i ...
-1 -1 i i -1 i 1 i i 1 1 i 1 -i 1 1 -i -i 1 -i -1 ...
-i -i -i -1 -i -1 -1 -1 i -1 -1 i i -1 i i i 1 i i ...
1 1 i 1 1 1 -i 1 1 -i -i 1 -i -i -i -1 -i -i -i -1 ...
-i -1 -1 -1 -1 -1 i -1 -1 i i -1 i i i i i 1;
%%% dumbunny1
1 -i -1 -1 i i 1 1 1 -i -i -i -1 -1 -1 -1 i i i i 1 ...
1 1 1 1 -i -i -i -i 1 i i i i i i -1 -i -1 i i -1 ...
-i -i -1 i i -1 -i -i -1 i -1 -i -1 -i 1 -i -i -1 ...
i -1 -i -i 1 1 -i -1 -i 1 -i 1 i 1 -i -i 1 i i 1 ...
-i -i 1 i i 1 -i 1 i 1 i i 1 i -1 i 1 i -1;
%%% dumbunny2
1 1 1 1 1 1 1 i -1 -1 -1 -1 -1 -1 -1 -1 i 1 1 1 1 1 ...
1 1 1 1 i -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 i 1 1 1 1 1 ...
1 1 1 1 1 1 i -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 i 1 ...
1 1 1 1 1 1 1 1 1 i -1 -1 -1 -1 -1 -1 -1 -1 -1 i 1 i ...
1 -i 1 1 1 1 1 1 i -1 -1 -1 -1 -1;
%%% dumbunny3
1 1 1 i -1 -1 -1 -1 -i -i 1 1 1 1 1 i i i -1 -1 -1 -1 ...
-1 -1 -i -i -i -i 1 1 1 1 1 1 1 i i i i i -1 -1 -1 -1 ...
-1 -1 -1 -1 -i -i -i -i -i -i 1 1 1 1 1 1 1 1 1 i i i ...
i i i i -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -i -i -i -i -i ...
-i -i -i 1 1 1 1 1 1 1 1 1 1 1;
%%% this or the next spiral works
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 i i i i i i i i 1 1 1 ...
1 1 1 1 1 1 1 -i -i -i -i -i -i -i -1 -1 -1 -1 -1 -1 ...
-1 -1 -1 i i i i i i 1 1 1 1 1 1 1 1 -i -i -i -i -i ...
-1 -1 -1 -1 -1 -1 -1 i i i i 1 1 1 1 1 1 -i -i -i -1 ...
-1 -1 -1 -1 i i 1 1 1 1 -i -1 -1 -1;
%%% this or the last spiral works
-i 1 -i -1 -i -1 i -1 i 1 i i 1 1 -i 1 -i -i -i -1 -i ...
-1 -1 -1 i -1 i i i 1 i i 1 1 1 1 -i 1 -i -i -i -i ...
-i -1 -i -1 -1 -1 -1 -1 i -1 i i i i i 1 i i 1 1 1 ...
1 1 1 -i 1 -i -i -i -i -i -i -i -1 -i -1 -1 -1 -1 -1 ...
-1 -1 i -1 i i i i i i i 1 i i 1 1 1;
%%% cross pattern
-1 -1 -1 -i -i -1 -i -i -i -i -i -i -i 1 -i 1 1 1 1 1 ...
1 1 i 1 i i i i i i i -1 i -1 -1 -1 -1 -1 -1 -i -i ...
-1 -i -i -i -i -i 1 -i 1 1 1 1 1 i 1 i i i i i -1 i ...
-1 -1 -1 -1 -i -i -1 -i -i -i 1 -i 1 1 1 i 1 i i i ...
-1 i -1 -1 -i -i -1 -i 1 -i 1 i 1 i -1 i;
%%% reversed cross pattern
]';
|