123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- imp:{(.0#`)`$(x,(1&#x)#"."),/:y}
- (P;L;C;D;N):(=)@/:(Pc;Lc;Cc;Dc;Nc):Cs:"(^). "
- LP:|/"^("=\:
- / parse vector of nodes into a parent vector
- prs:{(!#x)^'*|{(s;p):x;p,:**|s
- $["("=y;s,:,(-1+#p;1)
- "^"=y;s,:,(-1+#p;1+*|*|s)
- ")"=y;[s:(-*|*|s)_s;p:-1_p; p,:**|s]
- ]
- (s;p)}/[(,0N 0;!0);x]}
- / re-sort both the values and parent indices according to a permutation
- srt:{(x@z;(<z)@y@z)}
- / parse and prune unnecessary nodes
- prn:{(-#&i)_/:srt.(x;prs[x];<i:|/" .)"=\:x)}
- / pleasant display of tree
- dsp:{`0:(" ",x)(1+!#x)*/:(,1+&#x),~~((y@)\!#y)-\:|\*/1(y=)\!#y}
- / last child of a given node in a given parent vector
- lc:{(c;r):(-1+#x;|x)
- *|-1_(c-r?)\y}
- rrp:{[n;p]rm@:&(P n)rm:(,/L@Lc^n@g@)_&2>#'g:=p
- p:@[p;w;:;p@(-/1(~^rm?p@)\)/w:(&|/0,rm=\:p)^rm]
- rm,:rm2:(P n@)#2+(L n@)#&2=#'g:=p
- p:@[p;w;:;p@p@w:(&|/0,rm2=\:p)]
- (n;p):(-#rm)_/:srt[n;p;<@[&#p;rm;:;1]]
- $[2=+/~p;1_/:(n;0|p-1);(n;p)]}
- copy:{[n;p;e;l]
- (sn;sp):(*/l)#/:(Pc;0),'(-*l-1)#/:e#/:(n;p)
- sp:@[(#p)+(0|sp-e-*l)+(*l)*(&#/|l);(*l)*!*|l;:;0N]
- (sn;sp)}
- beta0:{[n;p;off]
- (l;e):0 1+lc[p]'off
- idx:(2;1+idx?l)_idx:off+!e-off:*off
- p:@[p;(off=p@)#*idx;:;p[off]]
- idx[0]:(n[off+1]=n@)#*idx
- l:1 0+#'|idx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/(Pc;Lc)=\:
- \d .
- loadNumerical:{ (Pc0;Lc0;Cc0;Dc0;Nc0;Cs0):imp["numerical";$`Pc`Lc`Cc`Dc`Nc`Cs]
- (ALPHA0;alpha0;prn0;P0;L0):imp["numerical";$`ALPHA`alpha`prn`P`L]
- Pc::Pc0
- Lc::Lc0
- Cc::Cc0
- Dc::Dc0
- Nc::Nc0
- Cs::Cs0
- ALPHA::ALPHA0
- alpha::alpha0
- prn::prn0
- P::P0
- L::L0 }
- para:{`0:x}
- tbl:{`0:,/'$/|1(-:1+|/#',/)\$(!#x;x),y}
- tblb:{`0:(x;" ^"@y)}
- tblmb:{`0:(,x),(" -";" ^")@'y}
|