Display
An Implementation of J

Numeric Display
Boxed Display
Formatted Display



If the last operation in a line of user input is not assignment, the result of the line is displayed. More specifically, if the global variable asgn is zero at the end of executing an input line, and the line had no errors, jpr is invoked to display the result. jpr first applies thorn1 (the monad ":) to compute the display of y, then writes the lines to the screen.

In all cases, the display of an object is a literal array. The display of a literal array is itself. The display of a verb, adverb, or conjunction is that of the currently selected representation(s). The display of a numeric array, that of a boxed array, and format are now discussed.

Display is implemented by functions and variables in the files f.c and f2.c.


Numeric Display

The display of a numeric array y is a literal array having the same rank as y (but at least one), such that the shape of ":y matches the shape of y in all but the last axis. Columns are right-justified and are separated by one space. The conversion from numeric to literal can be modelled as follows:
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 ]) @ fmt
The model is divided into groups of verbs. The first group are utilities:

sprintf   a function in the C library
type      type
real      the real part of a complex number
imag      the imaginary part of a complex number

fmtD formats a real number. Its constituents transform the result of sprintf to follow J conventions in the treatment of negative signs (minus), exponential notation (efmt and afte), and infinities and indeterminates (ubar).

fmt formats a numeric array into an array of boxed strings. It invokes formatters specialized for the different types: fmtB (Boolean), fmtI (integer), fmtD (floating point), and fmtZ (complex).

sh shapes an array into a table having the same number of rows. width computes the maximum width in each column of an array of boxed strings. th is a model of ": on numeric arrays.


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:

boxed     1 if boxed
mt        1 if empty
boxc      (noun) box drawing characters
tcorn     (noun) the characters ┐ ┌
tint      (noun) the characters ┬ ─
bcorn     (noun) the characters ┘ └
bint      (noun) the characters ┴ ─

mat is the main verb of the next group of definitions. The argument is a literal array; the result is a literal matrix image of the array — a literal table that "looks like" the argument array.

perim draws a perimeter around each plane of the right argument: According to the information in the left argument (a result of rc), perim puts ┐ ┌ ┬ ─ (top), ┘ └ ┴ ─ (bot), ├ │ (left), or ┤ │ (right) at appropriate positions on the perimeter of each plane.

topleft catenates the characters ┼ │ ─ on the top and left edges of a literal table. inside produces the inside (excluding perimeter) of a plane of the display. take is {. if the right argument is non-empty, and is an array of blanks otherwise. frame applies to an array of boxed tabular displays, and computes the overall display. rc computes the number of rows and columns in the display of atoms in a plane.

thorn1 models ":; thbox models ": on a boxed array.

The following examples illustrate the inner workings of the model:
   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) " 1
The model is divided into groups of verbs.

sprintf is a limited model of sprintf in the C library, applying to a string containing a single %e or %f conversion specification and to a single number. Thus, if embrace=:('{'&,)@(,&'}'), then:
   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).

thorn2 is a model of the dyad ":. It works by applying thcell to corresponding atoms of the arguments, producing a list of boxes; the leading blank of the razed result is then dropped or not, according to the value of lb on the left argument.



NextPreviousIndexTable of Contents