JPhrases/Specimens

From J Wiki
Jump to: navigation, search

1C. Specimens

Although many aspects of a phrase may be illustrated by applying it to scalar arguments or arrays of successive integers, more thorough explorations require the use of several classes of arrays, including negative, fractional, complex, alphabetic, and boxed. Moreover, the use of only systematic arguments such as i. 6 and i. 2 3 4 might suggest that a phrase possesses properties that it does not when applied to a random array such as ?. 2 3 4$10 .

The phrases of this section provide a variety of specimen arrays. Because they will be used in illustrating the application of phrases in many different sections, they should perhaps be loaded into the special z locale, so as to be generally available, either in the form d0_z_ or d0

d0=: QI=: ?.@$ Shape x of integers from i. y
d1=: QD=: ?.@(,:~)@$ Shape 2,x of integers from i. y
d2=: QN=: -/@QD Random negative and positive integers
d3=: QF=: %/@QD Random fractions
d4=: QC=: j./@QD Random complex numbers
d5=: QA=: {&a.@(a.&i.@]+[QI 26"_) Random literal (26 beginning at y.)
m6=: QI&10 Array of random single-digit integers
m7=: QZ=: QI&1 Array of zeros
m8=: QB=: QI&2 Array of Booleans

Random arrays can be produced by expressions of the form ? 2 3 4$10 ,  but for repeatable experiments it is necessary to use a specific random seed, as provided by the function ?. , and in any other phrase that uses it. For example:

   (QA&'A' ; QA&'a' ; QA&'+') 2 3 4
+----+----+----+
|DTLN|dtln|.>68|
|FBRR|fbrr|0,<<|
|YJNV|yjnv|C48@|
|    |    |    |
|ABNR|abnr|+,8<|
|AJBK|ajbk|+4,5|
|RPYW|rpyw|<:CA|
+----+----+----+

The following phrases may be applied to box an argument, to scale and shift it to a specified range, or to normalize it to the range 0 to 1:

a9=: B=: 1 : '<"x.' Adverb: k B is box of rank k
m10=: B0=: 0 B Box atoms
m11=: B1=: 1 B Box vectors (lists)
m12=: B2=: 2 B Box matrices (tables)
m13=: RG=: (1: + >./ - <./)@, Range of values of y
d14=: SC=: * % RG@] Scale range of y to scalar x
m15=: NR=: 1&SC Normalize range to 1
m16=: SZ=: - <./@, Shift to make smallest value zero
m17=: NM=: SZ@NR Normalize to range 1 and min value 0
   B0 B1 2 3 4 QA 'A'
+--------------------+
|+----+|+----+|+----+|
||DTLN|||FBRR|||YJNV||
|+----+|+----+|+----+|
+------+------+------|
|+----+|+----+|+----+|
||ABNR|||AJBK|||RPYW||
|+----+|+----+|+----+|
+--------------------+

   B2 3 4 5 QA 'a'
+-----+-----+-----+
|dtlnf|rpywn|zstqb|
|brryj|crksx|qxhlt|
|nvabn|tgbti|mghje|
|rajbk|qtzjg|mxxbx|
+-----+-----+-----+

   (] ; NR ; SZ ; NM) 3 4 QN 10
+----------+---------------+-------+---------------+
|_4  2 _1 0|1.6 0.4 0.1   0|1 7 4 5|0.1 0.7 0.4 0.5|
|_3 _5  1 1|0.9 2.5 0.1 0.1|2 0 6 6|0.2   0 0.6 0.6|
| 4 _2  0 3|1.6 0.4   0 0.9|9 3 5 8|0.9 0.3 0.5 0.8|
+----------+---------------+-------+---------------+
	]	Scale range to 1	 Shift to 0 min	Norm pos and range