Display
An Implementation of J
sprintf =: ": real =: {.@+. imag =: {:@+. minus =: $&'_'@('-'&=@{.) ubar =: >@({&(<;._1 ' _ _ _. _.'))@('iInN'&i.@{.) afte =: minus , (i.&0@(e.&'-+0') }. ]) efmt =: >:@(i.&'e') ({. , afte@}.) ] finite =: ]`efmt@.('e'&e.) massage =: finite`ubar@.(e.&'iInN'@{.) fmtD =: (minus,massage@(e.&'-+'@{.}.])) @ sprintf cleanZ =: (* ] >&| (2^_44)"_ * |.)&.+. fmtZ1 =: fmtD@real , 'j'&,@fmtD@imag`(''"_)@.(0&=@imag) fmtZ =: fmtZ1 @ cleanZ fmtB =: {&'01' fmtI =: sprintf fmt =: (fmtB&.>)`(fmtI&.>)`(fmtD&.>)`(fmtZ&.>) @. (1 4 8&i.@(3!:0)) sh =: (*/@}:,{:@(1&,))@$ ($,) ] width =: (<:@{. 0} ])@:>:@(>./)@sh@:(#&>) th =: (-@width ;@:({.&.>)"1 ]) @ fmtThe model is divided into groups of verbs. The first group are utilities:
Boxed Display
The display of a boxed array b is a literal array d=:":b
such that:
The rank of d is the greater of 2 or the rank of b.
Excluding the last two axes, the shape of d matches
the shape of b.
The frame (formed by ┌ ┬ ┐├ ┼ ┤└ ┴ ┘ │ ─)
is the same in all the planes.
Boxed display can be modelled as follows:
boxed =: 32&= @ (3!:0) mt =: 0&e.@$ boxc =: {. 9!:6 '' tcorn =: 2 0{boxc tint =: 1 10{boxc bcorn =: 8 6{boxc bint =: 7 10{boxc sh =: (*/@}: , {:)@(1&,)@$ $ , rows =: */\.@}:@$ bl =: }.@(,&0)@(+/)@(0&=)@(|/ i.@{.@(,&1)) mask =: 1&,. #&, ,.&0@>:@i.@# mat =: mask@bl@rows { ' '&,@sh edge =: ,@(1&,.)@[ }.@# +:@#@[ $ ] left =: edge&(3 9{boxc)@>@(0&{)@[ , "0 1"2 ] right =: edge&(5 9{boxc)@>@(0&{)@[ ,~"0 1"2 ] top =: 1&|.@(tcorn&,)@(edge&tint)@>@(1&{)@[ ,"2 ] bot =: 1&|.@(bcorn&,)@(edge&bint)@>@(1&{)@[ ,"2~ ] perim =: [ top [ bot [ left [ right ] topleft =: (4{boxc)&((<0 0)}) @ ((_2{boxc)&,.) @ ((_1{boxc)&,) inside =: 1 1&}. @ ; @: (,.&.>/"1) @: (topleft&.>) take =: [ {. ]`(]&' ')@.mt@] frame =: [ perim {@[ inside@:(take&.>)"2 ,:^:(1&=@#@$)@] rc =: (>./@sh&.>) @: (,.@|:"2@:(0&{"1);1&{"1) @: ($&>) thorn1 =: ":`thbox @. boxed thbox =: (rc frame ]) @: (mat@thorn1&.>)The model is divided into groups of definitions (which are verbs unless indicated otherwise). The first group are utilities:
y=: 2 3$(i.2 3);'abc';(i.4 1);(<2 2$'ussr');12;<+&.>i.2 2 3 x=: mat@thorn1&.>y $&.> x rc x { rc x ┌───┬───┬────┐ ┌────┬─────┐ ┌────┬────┬────┐ │2 5│1 3│4 1 │ │4 11│5 3 9│ │4 5 │4 3 │4 9 │ ├───┼───┼────┤ └────┴─────┘ ├────┼────┼────┤ │4 4│1 2│11 9│ │11 5│11 3│11 9│ └───┴───┴────┘ └────┴────┴────┘ a =: 2 3 4 $ 'abcdefghijklmnopqrstuvwx' a mat a $a abcd abcd 2 3 4 efgh efgh ijkl ijkl $ mat a 7 4 mnop mnop qrst qrst uvwx uvwx topleft 3 4$'a' ┼──── │aaaa │aaaa │aaaa (2 3;4 5) perim 6 10$'a' ┌────┬─────┐ │aaaaaaaaaa│ │aaaaaaaaaa│ ├aaaaaaaaaa┤ │aaaaaaaaaa│ │aaaaaaaaaa│ │aaaaaaaaaa│ └────┴─────┘ ] t=: ({rc x) inside@:(take&.>)"2 x 0 1 2│abc│0 3 4 5│ │1 │ │2 │ │3 ─────┼───┼───────── ┌──┐ │12 │┌─┬──┬──┐ │us│ │ ││0│1 │2 │ │sr│ │ │├─┼──┼──┤ └──┘ │ ││3│4 │5 │ │ │└─┴──┴──┘ │ │ │ │┌─┬──┬──┐ │ ││6│7 │8 │ │ │├─┼──┼──┤ │ ││9│10│11│ │ │└─┴──┴──┘ (rc x) perim t ┌─────┬───┬─────────┐ │0 1 2│abc│0 │ │3 4 5│ │1 │ │ │ │2 │ │ │ │3 │ ├─────┼───┼─────────┤ │┌──┐ │12 │┌─┬──┬──┐│ ││us│ │ ││0│1 │2 ││ ││sr│ │ │├─┼──┼──┤│ │└──┘ │ ││3│4 │5 ││ │ │ │└─┴──┴──┘│ │ │ │ │ │ │ │┌─┬──┬──┐│ │ │ ││6│7 │8 ││ │ │ │├─┼──┼──┤│ │ │ ││9│10│11││ │ │ │└─┴──┴──┘│ └─────┴───┴─────────┘
Formatted Display
x":y is a literal representation of y specified
by x. Positive elements of x specify
fixed-point notation, while negative
elements specify exponential notation. The left and right ranks are one;
that is, lists in the arguments are independently formatted.
The computation can be modelled as follows:
fmtexp =: {&'++-'@* , _3&{.@('00'&,)@":@| cexp =: >:@(i.&'e') ({. , fmtexp@".@}.) ] cminus =: '-'&((e.&'_' # i.@#)@]}) larg =: (+_20&*@(0&=))@-@(1&|)@|@".@(-.&' %e') nsprintf =: larg@[ cexp@cminus@":"1 ] psprintf =: ".@(-.&' %f')@[ ($&' '@(0&=)@<.@[ , cminus@":"1) ] sprintf =: nsprintf`psprintf@.('f'&e.@[) wd =: <.@| npstr =: ' %- '&,@(,&'e')@(0.1&":)@(-1&<)@| ppstr =: *@wd }. ' %'&,@(,&'f')@(0.1&":) pstr =: npstr`ppstr@.(0&<:) jexp =: >:@(i.&'e') ({. , ":@".@(-.&' +')@}.) ] jminus =: '_'&((e.&'-' # i.@#)@]}) stars =: ]`{.@.(*@[)`($&'*'@[)@.(*@[*.(<#)) c2j =: stars ]`jexp@.('e'&e.)@jminus lb =: (0&=@wd *. 0&<:)@{. thcell =: (wd@[ <@c2j pstr@[ sprintf ])"0 thorn2 =: (lb@[ }. ;@:thcell) " 1The model is divided into groups of verbs.
embrace ' %0.3f' sprintf ^5 { 148.413} embrace '%9.3f' sprintf ^_5 { 0.007} embrace ' %- 0.3e' sprintf ^_5 { 6.738e-003} embrace ' %- 9.3e' sprintf -^5 { -1.484e+002} embrace ' %- 6.3e' sprintf -^_5 { -6.738e-003}pstr applies to the left argument of ": and produces the necessary left argument to sprintf. For example:
x embrace pstr x _12 { %- 11.0e} _7.3 { %- 6.3e} _0.3 { %- 0.3e} 0 { %0.0f} 0.3 { %0.3f} 7.3 {%7.3f} 12 {%12.0f}c2j and its constituents transform the result of sprintf to follow J conventions, in the treatment of signs (jminus), exponential notation (jexp), and overflow (stars).