User:Ewart Shaw/Utils

From J Wiki
Jump to navigation Jump to search

Ewart Shaw's utilities

Here are my basic general-purpose utilities for J9. Some are ancient but kept for compatibility with old scripts.

NB. Ewart Shaw's programming utilities library
NB. ------------------------------------------
NB. Created 3-Nov-1995
NB. Last modified 24-Jan-2023
NB. =============================================================================

cocurrent 'z'

NB. =============================================================================
NB.* LN2PI      n  log(2 pi)
NB.* MAXINT32   n  Maximum integer (32 bit)
NB.* allaxes    a  Apply u to all axes of y (or of x and y)
NB.* basecount  v  Matrix with rows = 0 to x^y in base x
NB.* bincount   v  Matrix with rows = 0 to 2^y in binary
NB.* border     v  Border array y with x (default 0)
NB.* box2d      v  convert character string to boxed matrix of strings
NB.* boxpos     v  Omit all nonpositive elements from boxes
NB.* boxrest    v  Append boxed atoms of (y not in x) to x
NB.* cart       a  Cartesian product adverb
NB.* comb       v  All combinations of x from y (Hui/Chakahwata)
NB.* copyshape  v  Fill shape of x with elements of y
NB.* countint   v  Frequency of 0,1,...
NB.* cutbeg     a  Cut y using Boolean x & apply verb to each part
NB.* cutend     a  as cutbeg, but 1s in x denote ends of cut
NB.* dec        v  Approximate deciles of ,y (see invcdf)
NB.* diffs      v  Successive differences
NB.* distsq     v  Squared Euclidean distances between row vectors of y
NB.* donoun     v  Execute noun (script) y & return the J dialogue
NB.* execnoun   v  Execute noun y (result displayed but NOT returned, cf donoun)
NB.* expand     v  Expand y according to x (cf APL's \)
NB.* expprod    v  'Expanded product' of binary matrix x with matrix y
NB.* genlev     v  Generate levels (all combs of factor levels y)
NB.* indexreflex v  i.~ but often faster (e.g. if y is a large real array)
NB.* initrl     v  Initialize random link based on date/time
NB.* invcdef    v  Approximate inverse ecdf of y evaluated at x (default: deciles)
NB.* iotav      v  Ravel of i.each y (pardon my APL) (Roger Hui's version)
NB.* kronprod   v  Kronecker product of matrices x and y
NB.* lmvn01     v  log of standard multivariate Normal density
NB.* mean       v  mean of y
NB.* median     v  median of y
NB.* mkdir      v  Create directory y
NB.* nint       v  Round y to nearest integer (= 1&precision) for compatibility
NB.* nub        v  ~. (faster for real arrays)
NB.* nubfreq    v  Frequency of each item in nub
NB.* nubkey     v  Return integer vector, identical iff items of y are identical
NB.* nubrank    v  Replace min.value by 0, 2nd smallest by 1, etc.
NB.* ordercols  v  Reorder columns of matrix y (put cols. x first)
NB.* out        v  Output y to screen
NB.* pnx        v  path;name;extension of filename y
NB.* precision  v  Round y to nearest x
NB.* prompt     v  Prompt for input
NB.* rank       v  (Statistical) ranks of y, averaging tied ranks
NB.* rescale    v  Scale y from range {.x to range {:x [default: (0,1) -> (_1,1).]
NB.* rowprod    v  All products of rows of matrix x with matrix y
NB.* seginit    v  Box y-sized initial segments of x (default i. _)
NB.* shape2     v  Matrix containing elements of array y
NB.* showboxes  v  Show boxed array omitting horizontal lines
NB.* slice      v  Slice&box y wherever x occurs [slice1 omits x]
NB.* tfn        v  Text from numeric y to x s.f., replacing '_' by '-'
NB.* tidyreals  v  Tidy values y within x of an integer or each other
NB.* ts         v  (Time, space) to run expression
NB.* tts        v  (Actual time, CPU time, space) to run expression
NB.* underravel a  Apply u. to (,y) then reshape
NB.* values     v  2-col matrix: value (sorted), frequency
NB.* v2         v  Conditional invocation (can speed up agenda with 2 options)
NB.* withdflt   v  x with defaults appended from y
NB.

LN2PI=:    ^. o. 2
MAXINT32=: <. <: 2 ^ 31

allaxes=: {{ for_k. >: i. # $ y do. y=. u"k y end. y
:
for_k. >: i. # $ y do. y=. x u"k y end. y 
}}

basecount=: $~ #: [: i. ^
bincount=:  2 & basecount

border=: (0&$:) : (, , [) allaxes

box2d=: {{ (LF,'|+') box2d y
:
'r c j'=. x   NB. row, column separators, continuation character
deb&.> c&cut&> deb&.> r cut ((r,j);'')&stringreplace y 
}}

boxpos=: (>&0 # ]) &. >

boxrest=: [ , [: ;/ ] -. [: ; [
NB. or, for non-train enthusiasts,
NB.    boxrest=: [ , ;/ @ (-. ;)~

cart=:  {{ (#~ #) u (*&# $ ]) 
}}

comb=: {{ k=. i.>:d=.y-x
 z=. (d$<i.0 0),<i.1 0
 for. i.x do. z=. k ,.&.> ,&.>/\. >:&.> z end.
 ; z 
}}   NB. by Roger Hui & Ken Chakahwata

copyshape =: $@[ $ ,@]

NB. old version from J6:
NB.   cutc=: 2 : ';@(<@x ;. y)'
NB. cutbeg=: cutc 1
NB. cutend=: cutc 2
NB.
cutbeg=: {{ x ([: ; ([: < u);.1) y }}
cutend=: {{ x ([: ; ([: < u);.2) y }}

diffs=: }. - }:
NB. currently near-identical time & space to   diff=: 2&(-~/\)

distsq=: +/"1 @: *: @: (-"1/~)

donoun=: {{ out_jehs_ =. ''
for_ybjehs. <;._2 (, ({: ~: LF"_) # LF"_) y do.
  y_jehs_ =. > ybjehs
  addlf_jehs_=. -. 'NB.' -: 3 {. (#~ [: +./\ ~:&' ') y_jehs_
  out_jehs_ =. out_jehs_ , '   ' , y_jehs_ , addlf_jehs_ # LF
  try. temp_jehs_ =. ": ". y_jehs_
    if. -. (1 { (;: y_jehs_), 2#a:) e. '=.';'=:' do. 
      out_jehs_ =. out_jehs_ , , temp_jehs_ ,&.|: LF
    end.
  catch. 
    k_jehs_=. I. ': donoun' E. err_jehs_ =: (13!:12'')
    if. # k_jehs_ do.
      err_jehs_=. k_jehs_ ({. , (8 + [) }. ]) err_jehs_
    end.
    out_jehs_ =. out_jehs_ , err_jehs_
  end.
end.
}: out_jehs_ 
}}

execnoun=: [: 0!:111 ,&(10{a.)

expand=: #^:_1
NB. expand=: /:@\:@[ { #@[ {. ]   NB. Old version (from Ken Iverson)

expprod=:  (#~#) (($$(*+/\)@,)@[ { 0:,+/@,@[$]) ,@]

genlev=: #: [: i. */

initrl=: 9!:1 @ <. @ ([: (0 60 60 24 31 12"_ #. |.) 6!:0)

invcdf=: (10 %~ i. 11)&$: : ((0 >. <.@(* <:@#)) { /:~@,@])
dec=: invcdf   NB. (monadic) for backward compatibility

iotav=: [: ; i.&.>   NB. (from Roger Hui)

isnewitem=: 1: , }. -.@-:"_1 }:
nubkey=: /:^:2 { (+/\ @ isnewitem @ /:~)
indexreflex=: i.~ @ nubkey
nub=: nubkey {./. ]   NB. was faster than ~. for real arrays

kronprod=: *&$ $ ,@: (*"0 1 cart) f.

lmvn01=: - @ -: @ (LN2PI&*@{:@$ + +/"1@:*:)

mean=: +/ % #

median=: mean@((<. , >.)@-:@<:@# { /:~)

mkdir=: {{ d=. {. yb=. (([: +/\ e.&'\/') </. ]) y
while. 0 < #yb=. }. yb do.
 try. 1!:5 d=. d ,&.> {. yb catch. end.
end. 
}}

nubfreq=: diffs @ (# ,~ (~:@/:~) # i.@#)
nubrank=: i.~ (/:~@~.@,)
countint=: <: @ (nubfreq @ (/:~ @ ((i.@>:@(>./)),])))

ordercols=: (([ , i.@#@] -. [) { ])"1

out=: 1!:2&2
NB. out=: [ smoutput   NB. for compatibility; replace out by smoutput from stdlib
prompt=: [: 1!:1 [: 1: [: out '<Enter> to continue'"_ @ out

pnx=: {{ k=. - <./ (|. y) i. '\/'
p=. k }. y [ nx=. k {. y
k=. nx i: '.'
p ; (k{.nx) ; (>:k)}.nx 
}}

precision=: {{ precision y
:
x * <. 0.5 + y%x
}}
nint=: 1&precision   NB. for backward compatibility

rank=: /:^:2 -:@:+ /:^:2&.|.

rescale=: {{ (0 1,: _1 1) rescale y
:
'omin omax'=. old [ 'nmin nmax'=. new [ 'old new'=. x
nmin + (nmax - nmin) * (y - omin)%(omax - omin)
}}

rowprod=: ,"2/ @ (*"1/)

seginit=: (+/\@:(0 , }:) <@(+ i.)"0 ]) : ({L:0 _~ $:)

shape2=: ((*/@}: , {:) @ $ @ ,:) $ ,

showboxes=: (#~ (25 124{a.) e.~ {."1)@":

slice1=: #@[ }.&.> [ (E. <;.1 ]) ,

slice=: {{
bx=. boxopen x [ by=. boxopen y
NB. m=. shape2 bx E.&> by
xey=. shape2 bx E.&> by
k=. +/ xey * >:i.#bx      NB. key
b=. (+/\ * k) </. >by   NB. boxed bits
kw=. (k-.0) { a:,bx     NB. keyword
((-#b) {. kw) ([ ,. (#@[ }. ])&.>) b
}}

tfn=: {{ 5 tfn y
:
pp=. 9!:10''
9!:11 x
y=. ": y
9!:11 pp
('-',a.) {~ ('_',a.) i. y
}}

tidyreals=: {{ 1e_13 tidyreals y
:
isint=. x > | y - k=. <. x + y=. , y [ s=. $y
y=. (k*isint) + y*-.isint
key=. +/\ 1 , x < | diffs nub=. /:~ ~. y
s $ (nub i. y) { ; key ([: < # # +/ % #)/. nub
}}

NB. ts=: 6!:2 , 7!:2@]   NB. now in standard library

tts=: 3 : 0
1 tts y
:
t0=. 6!:1''
((6!:1'')-t0), x ts y
)

underravel=: {{ $@] $ u@, }}

values=: (~. ,"0 nubfreq) @ /:~

v2=: (v2false * -.@v2test) + v2true * v2test

withdflt=: [ , #@[ }. ]

NB. =============================================================================
NB. For examples/exercises/execution
NB.
NB. multiex  v Display results of several one-liners
NB. runex    v Run example script(s) (e.g. in ESutils_ex.ijs)
NB. TODO     n Banner-size TODO reminder
NB.

multiex=: (,: ".&.>)@:boxopen

runex=: {{ '' runex y
:
out '   NB. ', 70#'='
execnoun 'NB. ### ', y, x, ' ###', LF, 'NB.', LF, ". y, '_EX'
}}

TODO=: {{)n
NB.      TTTTT   OOO   DDDD    OOO     !!  !!
NB.        T    O   O   D  D  O   O    !!  !!
NB.        T    O   O   D  D  O   O    !!  !!
NB.        T    O   O   D  D  O   O
NB.        T     OOO   DDDD    OOO     !!  !!
}}

NB. =============================================================================
NB. Current contents of ~User/config/folders.cfg:
0 : 0
NB. folders.cfg
NB.
NB. each non-empty line is a pair: name path, e.g.
NB. Van /home/guest/van/source
NB.
NB. name begins with lowercase - added to SystemFolders_j_
NB. name begins with uppercase - defines UserFolders_j_

Demos ~addons/demos
Projects ~user/projects
User ~user

NB. Ewart Shaw's additions:

ESlib ~User/projects/es_lib
ESmisc ~User/projects/es_misc

ESutils ~User/projects/es_lib/esutils.ijs

ESdata ~User/projects/es_lib/data
ESdm ~User/projects/es_lib/discmath
ESfun ~User/projects/es_lib/functions
ESgraph ~User/projects/es_lib/graphics
ESjtex ~User/projects/es_lib/jtex
ESmedia ~User/projects/es_misc/media
ESother ~User/projects/es_lib/other
ESquad ~User/projects/es_lib/quad
EStext ~User/projects/es_lib/text

ESgo ~User/projects/es_misc/go
ESriemann ~User/projects/es_misc/riemann
)

Script to produce examples of ES utilities

NB. Ewart Shaw programming utilities examples
NB. -----------------------------------------
NB. Created Nov-1995
NB. Last modified 24-Jan-2023
NB. ========================================================================

require '~ESutils'

NB. ========================================================================

DESCRIBE=: 0 : 0
NB. Run examples of Ewart Shaw's utilities
NB. Type:-
   utils_ex''
NB. to run all examples.
)

utils_ex=: {{
runex 'allaxes'
', bincount' runex 'basecount'
runex&> 'border'; 'boxpos'; 'boxrest'
runex&> 'cart'; 'comb'; 'copyshape'
' (cutbeg, cutend)' runex 'cuts'
runex 'diffs'
', execnoun' runex 'donoun'
runex&> 'expand'; 'expprod'
runex 'genlev'
runex 'ident'
', nub' runex 'indexreflex'
runex&> 'initrl'; 'iotav'
runex 'kronprod'
runex 'lmvn01'
runex&> 'mean'; 'median'; 'mkdir'
' (nubfreq, nubrank, countint, values)' runex 'nubs'
runex&> 'ordercols'; 'out'
runex 'pnx'
', nint' runex'precision'
runex 'rank'
runex 'rescale'
runex 'rowprod'
runex 'seginit'
runex&> 'shape2'; 'showboxes'
', slice1' runex 'slice'
runex 'tfn'
runex 'tidyreals'   NB. cf precision
NB. runex 'ts'   NB.. now in standard library
runex 'tts'
runex 'underravel'
runex 'v2'
runex 'withdflt'
'done'
}}

NB. ========================================================================

allaxes_EX=: 0 : 0
+/\ allaxes i. 2 3 4   NB. higher-dimensional cumulative sums 
multiex '_1 , allaxes i. 2 3 4'; '_1 , allaxes i. 2 3'; '_1 , allaxes i. 2'
)

NB. ========================================================================

basecount_EX=: 0 : 0
multiex '3 basecount 2'; '2 basecount 3'; 'bincount 3'
)

NB. ========================================================================

border_EX=: 0 : 0
multiex 'border i. 3 4'; '_1 border i. 3 4'; '_1 border^:2 i. 3 4'
)

NB. ========================================================================

BOX2D_EX=: _1 }. 0 : 0
| col 1 | col 2 | col 3 | ...
| ... | ... | ... | ...
| ... |
+ continuation
+ lines
| etc. 
)
box2d_EX=: 0 : 0
BOX2D_EX   NB. show contents of already-created noun BOX2D_EX
box2d BOX2D_EX
)

NB. ========================================================================

boxpos_EX=: 0 : 0
1 2 ; 3 4 5 ; 6
v=. 1 2 ; 3 4 5 ; 6
]w=.>v
<"1 w
boxpos <"1 w
)

NB. ========================================================================

boxrest_EX=: 0 : 0
(1 2 7 ; 4 5) boxrest i.10
)

NB. ========================================================================

cart_EX=: 0 : 0
multiex '(i.2 3) ^cart i.3 3'; '(i.3 3) ,.cart 10+i.2 2'
NB.
NB. ### cart: Alternatives ###
NB.
cartbox=: , @ { @ (, & (<@:(<"1)))
(i.3 3) (>@(;&.>) @ cartbox) 10+i.2 2
(i.20 3) ((>@(;&.>) @ cartbox) -: (,"1 cart)) i.20 3
20 ts '(i.20 3) (>@(;&.>) @ cartbox) i.20 3'
20 ts '(i.20 3) (,"1 cart) i.20 3'
c1=. ,"1 cart f.
20 ts '(i.20 3) c1 i.20 3'
)

NB. ========================================================================

comb_EX=: 0 : 0
3 comb 5

comb2=: {{   NB. version with ^: rather than for. 
 k=. i.>:d=.y-x
 ; k&(,.&.> ([: ,&.>/\. >:&.>))^:x (d$<i.0 0),<i.1 0
}}
ts '10 comb 20'
ts '10 comb2 20'
)

NB. ========================================================================

copyshape_EX=: 0 : 0
(i. 15) copyshape i. 3 4
(i. 15) copyshape~ i. 3 4
)

NB. ========================================================================

cuts_EX=: 0 : 0
NB. (cf J Phrases section 5B; phrases c0,a1,a2)
NB.
x=. 1 1 0 0 0 1 0 0 1 1
y=. 3 4 8 2 5 6 9 4 5 4
x +/\cutbeg y
x +/\cutend y
)

NB. ========================================================================

diffs_EX=: 0 : 0
diffs 1 3 6 10
xy ; diffs xy=. 6 2$0 0 1 2 3 3 6 3 8 2 10 1
diff2=: 2&(-~/\)
diff2 1 3 6 10
10000 ts 'diffs xy'
10000 ts 'diff2 xy'
)

NB. ========================================================================

DONOUN_EX=: 0 : 0
NB. --- start of DONOUN
c=. o. 1 [ b=: o. 2
] b=. o. 1
". 'c=. o. 2'
NB. --- end of DONOUN
)

donoun_EX=: 0 : 0
DONOUN_EX   NB. show already-created noun script DONOUN_EX
a=. donoun DONOUN_EX
b
a   NB. donoun returns but doesn't display execution

a=. execnoun DONOUN_EX
b
a   NB. execnoun displays but doesn't return execution
)

NB. ========================================================================

expand_EX=: 0 : 0
1 0 0 1 0 1 expand 2 3 4
)

NB. ========================================================================

expprod_EX=: 0 : 0
expprod2=: shape2 @ (expand"1/)
x=. #: 12 10 9 6 5 3 [ y=. 5 + i. 2 2
x;y;(x expprod y);x expprod2 y
20 ts 'x expprod y'
20 ts 'x expprod2 y'
)

NB. ========================================================================

genlev_EX=: 0 : 0
multiex 'genlev 3 4';'genlev 4 3';'genlev 2 3 2'
)

NB. ========================================================================

indexreflex_EX=: 0 : 0
a=. o. ?. 2000 8 $ 3
(indexreflex -: i.~) a
ts 'indexreflex a'
ts 'i.~ a'
(nub -: ~.) a
ts 'nub a'
ts '~. a'
)

NB. ========================================================================

initrl_EX=: 0 : 0
initrl''
9!:0''
? 10#100
6!:3 ] 0.1   NB. delay 0.1 seconds before repeating
initrl''
9!:0''
? 10#100
)

NB. ========================================================================

invcdf_EX=: 0 : 0
0.025 0.16 0.5 0.84 0.975 invcdf -/ +/ ?. 6 2 100000$0
7j4 ": invcdf ?. 1e6#0
7j4 ": dec ?. 1e6#0   NB. for backward compatibility
)

NB. ========================================================================

iotav_EX=: 0 : 0
iotav 3 1 4 1 5
10 ts '([: ; i.&.>) i.1000'   NB. fast!
)

NB. ========================================================================

kronprod_EX=: 0 : 0
a=. 1 2,:3 _1 [ b=. i. 3 3
multiex 'a'; 'b'; 'a kronprod b'; 'b kronprod a'
)

NB. ========================================================================

lmvn01_EX=: 0 : 0
multiex 'lmvn01 ,0'; 'lmvn01 ,.0 2'; 'lmvn01 0 2'; 'lmvn01 0.1 0.5 0.5 0.7'
)

NB. ========================================================================

mean_EX=: 0 : 0
NB. arithmetic mean, geometric mean, harmonic mean, root mean square:
(mean, mean&.:^., mean&.:%, mean&.:*:) 1 2 2 4
NB. means in a table
(mean ; mean"1 ; mean@,) i. 2 3 4
)

NB. ========================================================================

median_EX=: 0 : 0
multiex 'median *: i. 9'; 'median *: i. 10'; '*: median i. 10'
)

NB. ========================================================================

mkdir_EX=: 0 : 0
dir '~temp/blah'
mkdir jpath '~temp/blah/blah/blah'
dir '~temp/blah'
NB. delete jpath '~temp/blah/blah/blah'  afterwards!!
)

NB. ========================================================================

nubs_EX=: 0 : 0
(/:~@~. ; nubfreq) 3 1 4 1 5 9 2 6 5 3 5
(/:~@~. ; nubfreq) 'abracadabra'
(; nubrank) %/~ 1 2 3 4
countint 3 1 4 1 5 9 2 6 5 3 5
values 3 1 4 1 5 9 2 6 5 3 5
)

NB. ========================================================================

ordercols_EX=: 0 : 0
1 2 ordercols i.7 5
)

NB. ========================================================================

out_EX=: 0 : 0
outfn_EX=: {{   NB. example function temporarily using out for debugging
'a b c'=. out y
a + b - c
}}
outfn_EX i.4   NB. error
outfn_EX i.3   NB. OK
)

NB. ========================================================================

pnx_EX=: 0 : 0
pnx '~user\projects\ES_lib\ESutils.ijs'   NB. MS
pnx '~user/projects/ES_lib/ESutils.ijs'   NB. Unix
pnx 'nopath.name'
pnx 'c:\Program Files\Silly path\Silly name'
)

NB. ========================================================================

precision_EX=: 0 : 0
1 1e_4 0.04 precision"0 _ (2 %~ i. 5), 1p1 4p1
nint  (2 %~ i. 5), 1p1 4p1   NB. for backward compatibility
nint ((-: >: %: 5)^i.20) % %: 5
)

NB. ========================================================================

rank_EX=: 0 : 0
multiex 'rank 3 1 4 1 5 9 2 6 5 3 5'; 'rank ''abracadabra'''
)

NB. ========================================================================

rescale_EX=: 0 : 0
multiex 'bincount 3'; 'rescale bincount 3'; '(0 1,:2 2.1) rescale 3 1 4 1 6'
)

NB. ========================================================================

rowprod_EX=: 0 : 0
multiex '(i.3 4)'; '10+i.2 4'; '(i.3 4) rowprod 10+i.2 4'
)

NB. ========================================================================

seginit_EX=: 0 : 0
seginit 3 0 4 1
'abcdefghijk' seginit 3 0 4 1
(seginit~ [: ?. +/ # 0:) 3 1 4   NB. reproducible pseudorandom lists
'a b c d e'=. 'antidisestablishmentarianism' seginit 3 2 2 9 3
d
si1=: (+/\@:(0 , }:) <@(+ i.)"0 ]) : ({L:0 _~ si1)
NB. Note (slight nevertheless existing) speed-up of using  $:
1000 ts 'a. seginit 3 14 159 26 5 3 5'
1000 ts 'a. si1 3 14 159 26 5 3 5'
NB. previous R.E.Boss / Ewart Shaw version based on
NB. http://jsoftware.com/pipermail/programming/2006-November/003852.html
NB. http://jsoftware.com/pipermail/programming/2006-November/003854.html
si2=: (* #^:_1 I. </. [: i. +/) : ({L:0 _~ $:)
1000 ts 'a. si2 3 14 159 26 5 3 5'
NB. related but worse idea
si3=: (<@}:/. (-~ i.@#))@I.@:>: : ({L:0 _~ $:)
1000 ts 'a. si3 3 14 159 26 5 3 5'
C=: ?. *: 100#20
multiex '1000 ts ''seginit C''';'1000 ts ''si2 C''';'1000 ts ''si3 C'''
)

NB. ========================================================================

shape2_EX=: 0 : 0
($;]) shape2 4
($;]) shape2 i. 4
($;]) shape2 i. 3 4
($;]) shape2 i. 2 3 4
(shape2 -: ,.&.(|:"_1@|:)) i. 2 3 4 5 6 7
100 ts 'shape2 i. 2 3 4 5 6 7'
100 ts ',.&.(|:"_1@|:) i. 2 3 4 5 6 7'
)

NB. ========================================================================

showboxes_EX=: 0 : 0
showboxes ('fee';'fie';'fo';'fum'), <"0 i. 3 4
)

NB. ========================================================================

slice_EX=: 0 : 0
'br' slice 'abracadabra'
'a' slice 'abracadabra'
('br';'c') slice 'abracadabra'
'br' slice1 'abracadabra'   NB. slice1 omits x
)

NB. ========================================================================

tfn_EX=: 0 : 0
tfn 123456 _123456789 1p1 _1x1
8 tfn 2 2$123456 _123456789 1p1 _1x1
)

NB. ========================================================================

tidyreals_EX=: 0 : 0
y=. 0 0 1 1 1 1 1.3 1.3 + 0 1e_14 0 1e_14 9e_14 1e_12 5e_14 _5e_14
~: y
~: tidyreals y   NB. default 1e_13 
~: 0.9e_14 tidyreals y
)

NB. ========================================================================

ts_EX=: 0 : 0
100 ts 'i.1000'
)

NB. ========================================================================

tts_EX=: 0 : 0
tts '%: i.1000000'
10 tts '%: i.1000000'
)

NB. ========================================================================

underravel_EX=: 0 : 0
] a=. ?. 5 10$100
rank underravel a   NB. useful for nonparametric stats
/:~ a
/:~ underravel a
)

NB. ========================================================================

v2_EX=: 0 : 0
v2test =. >
v2true =. ]
v2false =. [
min =: v2 f.
3 1 4 1 5 9 min 2 7 1 8 2 8
NB.
NB. ### v2: Example 2 ###
NB.
v2test  =. >&9
v2true  =. -:
v2false =. +:
dorh1   =: v2 f.
dorh =: +: ` -: @. (]>9:)
x =. ?5000$20
ts 'dorh"0 x'
ts 'dorh1 x'
(dorh"0 -: dorh1) x
ts '(+:`-: @. (>&9))"0 x'
)

NB. ========================================================================

withdflt_EX=: 0 : 0
(<1 2 3) withdflt 2#a:   NB. 2nd argument defaults to a:
1 2 3 withdflt 2#a:      NB. 3 items overwrite defaults
(1;2;3) withdflt 2#a:
)

NB. ========================================================================
NB. describe on loading

1!:2&2 ] DESCRIBE

-- Ewart Shaw (talk) 10:07, 24 January 2023 (UTC)