tri.fun 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #import std
  2. #import nat
  3. #import sol
  4. #comment -[
  5. This module contains some functions for counting with trees according
  6. to one possible enumeration (the fastest I can think of). There is a
  7. one to one correspondence between trees and natural numbers, but
  8. sometimes smallish trees can have extremely large ordinals. I'm not
  9. convinced dendriform always works (the inverse of the ordinal
  10. function) but have never found a counterexample.
  11. Copyright (C) 2007 Dennis Furey]-
  12. #library+
  13. #optimize+
  14. # tsuc = ~&a^?\~&aaX ~&ar?\~&NfalPRX ^/~&falPR ~&farPJ; ~&aal2fafalPJPRafarPRPXaar2fafarPJPRNXBq
  15. #fix function_fixer
  16. tsuc = ~&?\&! ~&r?/^(tsuc+~&l,tpred+~&r) ~&/0+ tsuc+ ~&l
  17. tpred = &?=/0! ~&l?/^(tpred+~&l,tsuc+~&r) ~&\0+ tpred+ ~&r
  18. ordinal = # returns the position any tree would have in the above series
  19. ~&a^& ~&W; ^(sum,~&l); successor+ sum^\~&r ~&l; half+ product^/~& successor
  20. dendriform = # returns the tree given the ordinal; uses newton's method to solve a quadratic recurrence
  21. ~&a^?\0! ^W/~&f predecessor+~&a; -+
  22. ^(~&r,nat-difference)+ ^/~&rl nat-difference+~&lrrPX,
  23. ^/~& ^(~&,half+ product^/~& successor)+ {0,1}?</~& ^H\(skip^\~& predecessor+ half+ length) -+
  24. predecessor++ ^=+ nat-difference^/~&+ quotient+,
  25. ^\successor+double+ nat-difference^/(product^/successor ~&)+ !+ double+-+-