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).