rtcode.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
class VERTEXP
class VERTEXP is
attr sep:VERTEXC;
attr companion:INT;
create(s:VERTEXC,c:INT):SAME is
res:SAME:=new; res.sep:=s; res.companion:=c; return res;
end;
create(s:VERTEXC):SAME is
res:SAME:=new; res.sep:=s; res.companion:=0.int; return res;
end;
create(c:INT):SAME is
res:SAME:=new; res.sep:=#; res.companion:=c; return res;
end;
create:SAME is
res:SAME:=new; res.sep:=#; res.companion:=0.int; return res;
end;
clone:SAME is
res:SAME:=#; res.sep:=sep.clone; res.companion:=companion; return res;
end;
end; -- class VERTEXP
class RTCODE
class RTCODE is
-- class about reduced tcode for polynomial invariants
include KCODE{VERTEXP};
include KCODE_CMP{VERTEXP};
create(tcode:TCODE):SAME is
res:SAME:=new; res.k:=#(tcode.size);
loop i::=tcode.k.ind!;
res[i]:=#(tcode[i].sep,tcode[i].companion);
end;
return res;
end;
clone(stp:CARD):SAME is
-- clone after "stp"
res:SAME:=#; res.k:=#(k.size);
loop i::=stp.upto!(length); res[i]:=[i].clone; end;
return res;
end;
support:CARD is
-- not void if 1>=spport.
i:CARD; loop i:=k.ind!; if ~void([i]) then return i; end; end;
return i;
end;
printD(tPtr:CARD) is
su:CARD:=support;
cod:VERTEXC;
#OUT+"tPtr="+tPtr.str+"\n";
#OUT+"i : companion,sep \n";
loop i::=su.upto!(k.size-1);
#OUT+i.str;
if i=tPtr then #OUT+"< "; else #OUT+": "; end;
if void([i]) then #OUT+" void";
else
#OUT+[i].companion.str+" ";
cod:=[i].sep;
if VERTEXC::normal.in(cod) then #OUT+"n"; end;
if VERTEXC::ks.in(cod) then #OUT+"["; end;
if VERTEXC::ke.in(cod) then #OUT+"]"; end;
if VERTEXC::ts.in(cod) then #OUT+"("; end;
if VERTEXC::te.in(cod) then #OUT+")"; end;
if VERTEXC::band.in(cod) then #OUT+"b"; end;
if VERTEXC::crossing.in(cod) then #OUT+"c"; end;
if VERTEXC::under.in(cod) then #OUT+"u"; end;
if VERTEXC::over.in(cod) then #OUT+"o"; end;
if VERTEXC::positive.in(cod) then #OUT+"+"; end;
if VERTEXC::negative.in(cod) then #OUT+"-"; end;
if VERTEXC::endc.in(cod) then #OUT+"/"; end;
end;
#OUT+"\n";
end;
end;
exgCmpTail(s1,s2,s3:CARD) is
-- exchange segment s1..s2-1 s2..s3-1. only set companion after s1
if s1=s2 then return; end;
CodeRev(s1.int,s2.int-1); CodeRev(s2.int,s3.int-1); CodeRev(s1.int,s3.int-1);
loop i::=s1.card.upto!(k.size-2);
[i].companion:=expCmp_n([i].companion,s1.int,s2.int,s3.int);
end;
end;
rotOvCmpTail(stp:CARD):INT is
-- move start points until
-- the first crossing of the specified component cross __over__
-- return moved length
if ~VERTEXC::ks.in([stp].sep) then return 0; end;
p1::=stp+1; enp::=[stp].companion.card;
loop while!(VERTEXC::over.in([p1].sep) or VERTEXC::under.in([p1+1].sep)) ;
p1:=p1+1;
end;
if VERTEXC::under.in([p1].sep) and VERTEXC::over.in([p1+1].sep) then
exgCmpTail(stp+1,p1+1,enp);
end;
return (p1-stp).int;
end;
rotOvCmpTail2(inout stp:CARD):INT is
reply:INT:=0.int;
if ~VERTEXC::ks.in([stp].sep) then return reply; end;
p1:CARD:=stp+1; enp:CARD:=[stp].companion.card;
loop while!(VERTEXC::under.in([p1].sep));
p1:=p1+1;
end;
if VERTEXC::over.in([p1].sep) then
reply:=p1.int-stp.int-1;
exgCmpTail(stp+1,p1,enp);
else
stp:=p1+1; -- go to next component
reply:=rotOvCmpTail2(inout stp).int;
end;
return reply;
end;
DelCTail(t1,t2:CARD,tPtr:CARD) pre (t1<t2)and(t2<k.size) is
--remove codes at t1 t2. assume that t1 < t2
loop ti::=t1.upto!(t2-2);
[ti]:=[ti+1];
if tPtr<=[ti].companion.card then [[ti].companion].companion:=ti.int; end;
end;
loop ti::=(t2-1).upto!(length-2);
[ti]:=[ti+2];
if tPtr<=[ti].companion.card then [[ti].companion].companion:=ti.int; end;
end;
k:=k.resize(k.size-2);
end;
DelSeqTail(t1,t2:CARD,tPtr:CARD) pre (t1<t2)and(t2<k.size) is
-- remove codes from t1 to t2-1
loop i::=t2.upto!(k.size-1); j::=t1.up!;
[j]:=[i];
if tPtr<=[j].companion.card then [[j].companion].companion:=j.int; end;
end;
k:=k.resize(k.size+t1-t2)
end;
------------- trimmer for R1 move ------------
TrimRotOV(inout tPtr:CARD) is
res::=rotOvCmpTail2(inout tPtr);
end;
TrimC(tPtrT, cPtr:CARD) is
-- trim loop near 'ks'code
p1::=cPtr+1; p2:CARD;
loop while!((p1<length) and (([cPtr].companion.card)=[p1].companion.card+1));
p2:=[p1].companion.card;
if p1>p2 then DelCTail(p2,p1,tPtrT);
else DelCTail(p1,p2,tPtrT);
end;
end;
end;
TrimL(tPtrT, lPtr:CARD) is
-- trim at a loop
if length<=(lPtr+5) then return; end;
p1::=lPtr+1; p2::=lPtr;
loop while!((tPtrT<=p2)and(p1<length)and(p1=([p2].companion.card)));
p2:=p2-1; p1:=p1+1;
end;
p2:=p2+1;
if p2<p1 then DelSeqTail(p2,p1,tPtrT); end;
end;
TrimDelPC(tPtrT:CARD) is
-- del crossing code
if true then return; end;
p1::=tPtrT-1;
loop
p1:=p1+1;
until!( (length<=p1)or( VERTEXC::crossing.in([p1].sep)
and ([p1].companion.card<tPtrT)) );
end;
if p1<length then
p2::=p1;
loop p1:=(p1+1).upto!(length-1);
if ([p1].companion.card<tPtrT)
and VERTEXC::crossing.in([p1].sep) then ;
else
[p2]:=[p1];
[[p2].companion].companion:=p2.int;
p2:=p2+1;
end;
end;
[p2]:=[length];
k:=k.resize(p2+1);
end;
end;
end; -- class RTCODE
class RES_MARK
class RES_MARK is
attr p:CARD; -- pos
attr m:CARD; -- length
attr c:INT; -- #component
attr w:INT; -- writhe
create:SAME is
res::=new; res.p:=0; res.m:=0; res.c:=0.int; res.w:=0.int; return res;
end;
clone:SAME is
res::=new; res.p:=p; res.m:=m; res.c:=c; res.w:=w; return res;
end;
end; -- class RES_MARK
class RTCODE_TRIM
class RTCODE_TRIM is
--------Code Sort---------
--Copyright (C) 1989 1996 Kouji KODAMA
-- 1996/10
-- LINUX version
-- 1991/2 : Code Sort / with enumerating weight of tree.
-- 1989/8 : Code Sort / counting over cross .
-- TCodeRes:ARRAY{RTCODE};
-- TraceRes:ARRAY{RES_MARK};
-- ResFlg:ARRAY{BOOL}
enumTree(TCode:RTCODE, tval:FLT):FLT is
-- estimate #resolution
t:FLT;
cod:VERTEXP;
t:=0.0;
loop ptr::=TCode.k.ind!;
cod:=TCode[ptr];
if VERTEXC::crossing.in(cod.sep) then
t:=t*tval;
if ((ptr.int+1)<cod.companion)and VERTEXC::under.in(cod.sep) then
t:=t+1.0;
end;
end;
--if (crossing .in cod.sep) and ((ptr+1) < cod.companion) then
--if (under .in cod.sep)then t:=t+1.0; end;
--t:=t*tval*tval;
--end;
end;
return t;
end;
sMin1(inout TCode:RTCODE, tstep:FLT) is
-- search starting point for minimal resolution tree
TC1,TC2:RTCODE; -- work
val1,val2:FLT;
count, stp1,stp,enp,tp1,tp2:INT;
val::=enumTree(TCode, tstep); -- set dummy as a minimal
TC1:=TCode.clone;
TC1.rotateToOverCmp;
val1:=enumTree(TC1, tstep);
stp1:=0;
loop while!( ~VERTEXC::endc.in(TC1[stp1].sep));
stp:=stp1; stp1:=TC1[stp1].companion+1;
TC2:=TC1.clone;
TC2.sCompoCmp(stp);
enp:=TC2[0].companion;
tp1:=1; tp2:=enp-1; count:=tp2-tp1;
loop while!(count.is_pos) ;
--loop
--rotb(TC2,stp); DEC(count);
--until! (count<=0)or((over .in TC2^[tp1].sep) and(under .in TC2^[tp2].sep));
-- end;
count:=count-TC2.rotOvCmp(0.int);
val2:=enumTree(TC2, tstep);
if (val2<val1) then val1:=val2; TC1:=TC2.clone; end;
end;
end;
if (val1<val) then TCode:=TC1.clone; end;
end;
SCode(inout TCode:RTCODE, tstep, cvRes, cvTime:FLT):FLT is
-- cvRes[res/tval], cvTime[res/sec]
TC1::=TCode.clone; sMin1(inout TC1,tstep);
val1::=enumTree(TC1,tstep);
TC2::=TCode.clone; TC2.flip; sMin1(inout TC2,tstep);
val2::=enumTree(TC2,tstep);
if val1<val2 then TCode:=TC1.clone; else TCode:=TC2.clone; end;
val, res, time:FLT;
val:=val1.min(val2);
res:=val*cvRes; -- estimated resolution size
time:=res/cvTime; -- estimated time
POLY_TIME::SetV(TCode.number_crossing,res,time);
return time;
end;
end; -- class RTCODE_SORT
class POLY_TIME
class POLY_TIME is
out_expected_time(t:FLT) is
-- print out "t" second.
-- 1year = 3.2e+7 sec.
-- 1mon = 2.6e+6 sec.
-- 1week = 6.0e+5 sec.
-- 1day = 8.6e+4 sec.
if t>10.0 then #OUT+"May be needed "+t.str(1)+"sec.\n"; end;
-- #OUT+"May be needed "+t.str(4)+"sec.\n";
end;
rateCl:FLT is
-- #resolution/sec for Ppoly.
r:BOOL;
-- generate sample knot
s:STR:="2 : "; loop 13.times!; s:=s+"1 "; end; s:=s+"endcode ";
knot:KNOT:=#;
r:=BRAID_CNV::BraidWord2Knot(s,inout knot,false,true);
tc:TCODE:=#;
if ~knot.SetTCode(out tc) then #OUT+"SetTCode error\n"; end;
time:TIME_MEASURE:=#; time.mark;
PPOLY::Ppoly_time(tc,PPOLY::Def_xyz);
t1::=time.elapsed; -- time in sec.
c::=PPOLY::count; -- #of resolution for P
rate::=c.flt/t1.flt;
-- #OUT+"POLY_TIME: "+t1.str+"sec "+ c.str +"res "+rate.str+"r/s\n";
return rate;
end;
-- cvRes: val/res
-- cvTime: res/sec
-- tstep: enumerate 'tval' size of resolution tree.
-- 2^(1/3)= 1.25992105: P-polynomial (= 2^(1/3))
-- 1.2:P-poly.
-- (1.5)^(1/4): P-low
-- 3^(1/3)= 1.44224957: F-polynomial (= 3^(1/3))
-- Let tval:=tstep^(#cross*2).
-- cvRes:=tval/res[#stump of res].
-- Convert to size of resolution tree.
-- cvTime:=res[#stump of res]/time[second].
-- Convert size of tree to computing time.
----------Tree Test: check time and size of resolution tree--------------
shared dataV:FLT;
shared dataT:FLT;
shared dataC:INT;
shared timer:TIME_MEASURE;
SetV(cross:INT,res_e,time_e:FLT) is
-- save #crossing, expected #of resolution and time
--if (time_e>10.0) then
-- #OUT+"May be needed "+time_e.round.int.str+"sec.\n";
--end;
dataT:=time_e; dataV:=res_e; dataC:=cross;
timer:=#;
end;
SetR(resolution:FLT, title:STR) is
-- Write #resolution in PPoly,VPoly,FPoly, .....
-- Before Use this, use 'SetV'.(see end of scode)
-- format:
time::=timer.elapsed.flt;
LOGOUT::LogTime;
#LOGOUT+"\nstatistics " +title+"\n" +
"#crossing,#resolution,time,estimated-resolution,etsimated-time\n"
+dataC.str+" " -- crossings
+resolution.str+" " -- resolution
+time.str+" " -- time
+dataV.str+" " -- estimated resolution
+dataT.str+" " -- estimated time
+"\n";
if resolution>0.0 and time>0.0 and dataT>0.0 and dataV>0.0 then
r0::=(resolution/time); -- real
r1::=(dataV/dataT); -- estimated
#LOGOUT+(resolution/dataV).str+"=cvRes\n";
#LOGOUT+(r0/r1).str
+" = rate/estimated_rate= "
+r0.str+"/"+r1.str+"=coeff. of cvTime\n";
#LOGOUT+"\n";
end;
end;
end; -- class POLY_TIME
class POLY_COUNT
class POLY_COUNT is
-- count components at the bottom of resolution tree.
attr compoT:ARRAY{INTI};
create:SAME is
res:SAME:=new; res.compoT:=#; return res;
end;
count(compo:INT) is
if compo>=compoT.size.int then
s0::=compoT.size; compoT:=compoT.resize(compo.card+1);
loop i::=s0.upto!(compo.card); compoT[i]:=0.inti; end;
end;
compoT[compo.card]:=compoT[compo.card]+1.inti;
end;
total:INTI is
c::=0.inti; loop i::=1.upto!(compoT.size-1); c:=c+compoT[i]; end;
return c;
end;
str:STR is
c::=0.inti; s:STR:=" ";
loop i::=1.upto!(compoT.size-1);
s:=s+" "+compoT[i].str; c:=c+compoT[i];
end;
s:=s+" : "+c.str+"\n";
return s;
end;
end;