├── .gitignore ├── bigint └── bigint.ijs ├── lisp ├── lisp.ijs └── test.ijs ├── parallel └── p.ijs └── render └── render.ijs /.gitignore: -------------------------------------------------------------------------------- 1 | .*.swp 2 | -------------------------------------------------------------------------------- /bigint/bigint.ijs: -------------------------------------------------------------------------------- 1 | r=: <:10000x^30103 2 | NB.t=. <:2x^400000 3 | NB.r and t have similar magnitude. 2 is the machine's base; 10000 is j's base 4 | 5 | t=: <. 48 (| (([: <: 2 ^ [) , (<:2^48) #~ ]) <.@%~) 400000 NB. need <.; why is int^int not an int? 6 | M=: <.2^48 7 | bp=: {{ 8 | {{ c=. y>:M 9 | y=. y-c*M 10 | y=. y+1|.c_: }}^:_ x+y }} 11 | bp2=: {{ (] (1&|.@] + [ - M * ]) >:&M)^:_ x+y }} 12 | bp3=: (] (1&|.@] + [ - M * ]) >:&M)^:_ &: + 13 | 14 | NB.'+~r' %&timex 'bp~t' 15 | NB. 16 | NB.tt=: ([ + (<.2^48) * ])/ x: |. bp~t 17 | NB.tt2=: +~<:2x^400000 18 | NB.tt = tt2 19 | 20 | NB.u=. t-0,|.i.<:#t NB. pathological case 21 | u=: 1,~0#~<:#t 22 | -------------------------------------------------------------------------------- /lisp/lisp.ijs: -------------------------------------------------------------------------------- 1 | NB. lisp: abstract, verbose, deduplicated 2 | NB. apl: concrete, terse, repetitive 3 | 4 | NB. nj0 is number n 5 | NB. nj1 is symbol n. 0j1 is nil 6 | NB. nj2 is cons n I do not like this 7 | NB. nj3 is builtin function n 8 | NB. nj4 is user-defined function n. (env . (parm . body)) 9 | NB. todo reader, better error messages 10 | NB. todo macros? 11 | 12 | CFRAG =: 0.8 NB.compact if fragmentation above... 13 | 14 | t =: {:@+. : [: NB.type/tag 15 | c =: {.@+. : [: NB.content (~unbox) 16 | 17 | pick =: [. @. (#. @: (].`:0)) 18 | 19 | syms =: 'nil';,'t' 20 | intern =: {{ i =. syms i. y=.<,y 21 | if. i = #syms do. syms =: syms , y end. 22 | i j.1 }} 23 | sym_name =: {{>syms{~c y}} 24 | 25 | conses =: i.0 2 NB. first col is car, second cdr 26 | fl =: _1 27 | cons =: {{ 28 | if. _1 = fl do. (conses =: conses , x,y) ] 2 j.~#conses 29 | else. fl=: ca r=. fl 30 | r ra x 31 | r rd y 32 | r j. 2 end. }} 33 | uncons =: {{ fl =: y ra fl }} 34 | (ca =: {{conses{~<0,~c y}}) (ra =: {{x[conses=:y(<0,~c x)}conses}}) NB. car; rplaca 35 | (cd =: {{conses{~<1,~c y}}) (rd =: {{x[conses=:y(<1,~c x)}conses}}) NB. will the latter happen in place? 36 | (caa =: ca@ca) (cad =: ca@cd) (cda =: cd@ca) (cadd =: cad@cd) (caddd =: cadd@cd) 37 | er =: {{'lisp'13!:8]255}}@] 38 | la =: ''"_`(ca,$:@cd) @. (0j1&~:) 39 | 40 | bf =: i.0 2 NB. content; arity, which is 41 | cb =: {{ bf {~ c y }} NB. a function that, when applied to the argument list 42 | nbf =: {{ (bf=:bf,y) ] 3 j.~#bf }} NB. returns its length iff the function is willing to accept those args 43 | 44 | fm =: ":`sym_name`('(',fm@ca,' . ',')',~fm@cd)`('',~":@c)`('',~":@c) @. t 45 | 46 | en =: 0j1 NB.alist 47 | de =: {{en =: en cons~ y cons~ intern x }} 48 | (de intern)'nil' 49 | (de intern)'t' 50 | 'cons' de nbf cons/`2: 51 | 'car' de nbf ca@{.`1: 52 | 'cdr' de nbf cd@{.`1: 53 | 'symbolp'de nbf (1 j.~1=t@{.)`1: 54 | 'consp' de nbf (1 j.~2=t@{.)`1: 55 | 'functionp'de nbf (1 j.~3 4 e.~t@{.)`1: 56 | 'numberp'de nbf (1 j.~0=t@{.)`1: 57 | 'eq' de nbf (1 j.~=/)`2: 58 | '+' de nbf +/ `# 59 | '*' de nbf */ `# 60 | '/' de nbf %~/@|.`(1&>.) 61 | '-' de nbf -~/@|.`(1&>.) 62 | 'rplaca' de nbf ra/`2: 63 | 'rplacd' de nbf rd/`2: 64 | 65 | as =: ($:cd)`(ca@]) pick ((0j1=])`(= caa)) 66 | lu =: cd@as 67 | sn =: intern@>"0'lambda';'if';'quote';'set';'def' 68 | sv =: (0j2+(cons cd))`((ev cadd)`(ev caddd)@.(0j1=(ev cad)))`(cad@])`([:cd(as~ cad)rd(ev cadd))`([: cda sym_name@cad@] de (ev cadd))`ap 69 | al =: {{ b ev~e cons~ cons/"1 y,.~la p['e p b' =. la x }} 70 | ap =: (ev ca) (er`er`er`{{(cb x)@.0 y}}`al @. (t@[)) (ev"0 la@cd) 71 | ev =: ]`(lu~)`(sv@.(sn&i.@ca@]))`]`] @. (t@]) 72 | 73 | GC =: {{ 74 | b =. 0 #~ # h =. (c conses) * p =. 2 4 e.~t conses 75 | b =. 1 (c y)} b 76 | b =. h {{ 1 (,y#x)} y}}^:_ b 77 | if. CFRAG < (+/%#)b do. uncons"0 I.-.b [ fl =: _1 78 | else. d =. +/\-.b 79 | conses =: tc - d{~c (tc=.b#conses)*.b#p 80 | en =: en - d{~c en end. }} 81 | -------------------------------------------------------------------------------- /lisp/test.ijs: -------------------------------------------------------------------------------- 1 | 0!:0<'lisp.ijs' 2 | assert 6 = en ev (intern'+') cons ((intern'-') cons 8 cons 5 cons 0j1) cons 3 cons 0j1 3 | assert 1 = en ev (intern'if') cons ((intern'symbolp') cons ((intern'quote') cons (intern'blep') cons 0j1) cons 0j1) cons 1 cons 2 cons 0j1 4 | assert 6 = en ev ((intern'lambda') cons (0j1 cons~ intern'x') cons ((intern'+') cons 1 cons (intern'x') cons 0j1) cons 0j1) cons 5 cons 0j1 5 | 'f' de en ev ((intern'lambda') cons (0j1 cons~ intern'x') cons (((intern'lambda') cons (0j1 cons~ intern'y') cons ((intern'+') cons (intern'x') cons (intern'y') cons 0j1) cons 0j1) cons 0j1)) cons 5 cons 0j1 6 | 'g' de en ev ((intern'lambda') cons (0j1 cons~ intern'x') cons (intern'x') cons 0j1) 7 | z =. #conses 8 | GC en 9 | assert z > #conses 10 | 'ff' de en ev intern'f' 11 | assert (intern't') = en ev (intern'eq') cons (intern'f') cons (intern'ff') cons 0j1 12 | assert 7 = en ev (intern'if') cons ((intern'functionp') cons (intern'f') cons 0j1) cons 7 cons 2 cons 0j1 13 | assert 11 = en ev (en lu~ intern'f') cons 6 cons 0j1 14 | assert 333 = en ev (en lu~ intern'g') cons 333 cons 0j1 15 | en ev (intern'def') cons (intern'x') cons ((intern'cons') cons 5 cons 6 cons 0j1) cons 0j1 16 | assert 6 = en ev (intern'cdr') cons (intern'x') cons 0j1 17 | en ev (intern'rplacd') cons (intern'x') cons 7 cons 0j1 18 | assert 7 = en ev (intern'cdr') cons (intern'x') cons 0j1 19 | en ev (intern'set') cons (intern'x') cons 14 cons 0j1 20 | assert 14 = en ev intern'x' 21 | GC en 22 | assert 5 = ca 5 cons 7 23 | GC en 24 | GC en 25 | x =. 5 cons 7 26 | y =. 6 cons 9 27 | assert 5 = ca x 28 | 29 | echo fm en 30 | -------------------------------------------------------------------------------- /parallel/p.ijs: -------------------------------------------------------------------------------- 1 | 0&T.@'' ::] ^:_'' NB.eat all the cores 2 | NPAR=. 1 T.'' 3 | 4 | a0=: i.4 1e8 5 | a1=: i.1e8 4 6 | a2=. i.1e9 7 | 8 | peach0=: >@: ((t.'')"_1) 9 | peach1=: {{ > ([: u {&y) t.''"_1 i.#y }} 10 | prankb=: {{ par=. NPAR <. #y 11 | c=. (#y) <.@% par NB.chunk size 12 | d=. c*i.par NB.drops, see also +/\par#c 13 | t=. (c#~<:par) , c+par|#y NB.takes, last of which may be oversized 14 | ([: u"n {&t {. {&d }. y"_) t.''"_1 i.par }} 15 | NB. todo: 16 | NB. when 0{$y is short and n is deeper than _1, parallelise along further axes 17 | NB. also, niceties: 18 | NB. - should be ambivalent 19 | NB. - should do the right thing for "_ 20 | NB. - should take advantage of more parallelism than n permits by looking at u b.0 21 | prank=: ;@:prankb 22 | peach2=: prank _1 23 | 24 | bslash=: {{ 25 | if. x<0 do. NB.nonoverlapping 26 | x=. |x 27 | rs=. (#y) >.@% x NB.result size 28 | par=. NPAR <. rs 29 | rc=. rs <.@% par NB.result chunk 30 | c=. rc * x 31 | d=. c*i.par 32 | t=. (c#~<:par) , c+par|#y 33 | ; ((-x) u\ {&t {. {&d }. y"_) t.''"_1 i.par 34 | else. NB.overlapping; left as an exercise to the reader 35 | 'nyi' assert 0 36 | end. }} 37 | 38 | monoid=: 2 :'x u y NB.MONOID' 39 | 40 | slash0=: {{ par=. NPAR <. #y 41 | NB. annoying; no late binding for non-verbs 42 | if. -. u`'' -:&(0&{@,@>) + monoid 0 `'' do. 43 | u/y return. end. 44 | id=. 1{::^:3,>u 45 | u=. (0{1{::,>u`'') `:6 NB. pull out actual verb to take advantage of SC 46 | if. 0-:#y do. id return. end. 47 | c=. (#y) <.@% par 48 | d=. c*i.par 49 | t=. (c#~<:par) , c+par|#y 50 | > u&.>/ ([: u/ {&t {. {&d }. y"_) t.''"_1 i.par }} 51 | 52 | slash1=: {{ par=. NPAR <. 2 <.@%~ #y 53 | NB. annoying; no late binding for non-verbs 54 | if. -. u`'' -:&(0&{@,@>) + monoid 0 `'' do. 55 | u/y return. end. 56 | id=. 1{::^:3,>u 57 | u=. (0{1{::,>u`'') `:6 NB. pull out actual verb to take advantage of SC 58 | if. 0-:#y do. id return. end. 59 | c=. (#y) <.@% par 60 | d=. c*i.par 61 | t=. (c#~<:par) , c+par|#y 62 | y=. >([: u/ {&t {. {&d }. y"_) t.''"_1 i.par 63 | u monoid id slash2^:(1~:#) y }} 64 | -------------------------------------------------------------------------------- /render/render.ijs: -------------------------------------------------------------------------------- 1 | NB. not entirely sure about the transposed representation? 2 | NB. should be able to apply wp to the entire screenful of coordinates (possibly transposed), not just "1. Haven't thought about the ideal way to do that yet 3 | vecs=: 4 3 2$ 0 0.5 0.3 0.3 0.5 0 0.5 0 0.7 0.3 1 0.5 1 0.5 0.7 0.7 0.5 1 0.5 1 0.3 0.7 0 0.5 4 | vecs=: 0.25+0.5*vecs 5 | 'vx vy'=: |:vecs 6 | abc=. (0&{ + 2&{ - 2 * 1&{) , (2 * 1&{ - 0&{) ,: 0&{ 7 | 'xa xb xc'=: abc vx NB. todo handle case where p0/1/2 are evenly spaced, so a is 0 8 | 'ya yb yc'=: abc vy NB. need to arrange for result -c/b. Should also do the optimization that drops a mul 9 | sample_y=: {{ yc + y * yb + y * ya }} 10 | sample_x=: {{ xc + y * xb + y * xa }} 11 | slug=: |.#:16b2e74 12 | 13 | wp=. {{)m 14 | 'x y'=. y 15 | d=. (*:xb) - 4 * xa * xc - x 16 | s=. (2*xa) %~ (-xb) (+,.-) %:|d NB. | prevents complex results; they will be filtered regardless 17 | r=. (d>:0) *. y > sample_y s 18 | r=. r *. (2 {. }.&slug)"1 0 +/2 4 8 * vx < x 19 | 0 ~: -/+/r }} 20 | 21 | NB. try: ' #' {~ wp"1 |."1]100 %~ 100 100 $ 100 100 #: i. 1e4 22 | --------------------------------------------------------------------------------