Index of phrases¶
Constants and functions referred to in the Q Phrasebook.
DEC:"0123456789"
HEX:"0123456789abcdef"
PI:3.141592653589793238
PUN:",;:.!?"
depth:{$[type[x]<0;
0;
"j"$sum(and)scan 1b,-1_{1=count distinct count each x}each raze scan x]}
fac:{prd 1+til x} / factorial
ly:{mod[;2] sum 0=x mod\:4 100 400} / is x a leap year?
oa:{x xexp/:0 1} / ones and all Xs (float)
oe:{x xexp\:0 1} / 1 and each X (float)
shape:{$[0=d:depth x;
0#0j;
d#{first raze over x}each(d{each[x;]}\count)@\:x]}
tc:til count@
tt:{2 vs til "j"$2 xexp x} / truth table of order x
zm:{(2#count x)#0} / zero matrix (square matrix)
Arithmetic¶
Casting and representation¶
| new | description | phrase |
|---|---|---|
| 200 | binary from integer | 2 vs x, 0b vs x, 0x0 vs x |
| 201 | integer from binary | 2 sv x |
| 202 | hexadecimals from integers | HEX 16 vs x |
| 203 | hexadecimals from decimal characters | HEX 16 vs "i"$x |
| 204 | decimal digits from integer | 10 vs x |
| 205 | integer in base y | y vs x |
| 206 | integer in base 10 100 1000 | 10 100 1000 vs x |
| 207 | integer from hexadecimal | 16 sv HEX?x |
| 208 | vector from date | 100000 100 100 vs x |
| 209 | integer from date | "I"$ string[x] except "." |
| 210 | number from string | parse x |
| 211 | number from string, default y | parse $[x~"";y;x] |
| 212 | numeric codes from string | "i"$x |
| 213 | integer from Roman | d:0,1000 500 100 50 10 5 1 "MDCLXVI"sum floor a*-1 xexp a<1 rotate a:d?x |
| 214 | numbers from text matrix | parse each x |
| 215 | numbers from evaluating rows of text matrix | raze('[eval;parse])each x |
Execution¶
| new | description | phrase |
|---|---|---|
| 300 | case structure | $[c0;t0;c1;t1;c2;t2;f] |
| 301 | choose x or y depending on flag g | $[g;x;y] |
| 302 | reverse on condition y | y reverse/x |
| 303 | conditional change of sign | y*1 -1 x |
| 304 | efficient execution of f x where x has repeated values | u:distinct x(f u)u?x |
| 305 | error to stop execution | '`stop |
| 306 | execute rows of character matrix | ('[eval;parse])each x |
| 307 | sum numbers in character matrix | sum parse " " sv x |
Finance¶
| new | description | phrase |
|---|---|---|
| 400 | present value of cash flows c at times t and discount rate d | sum c*d xexp t |
| 401 | future value of cash flows | sum x* (1+y%100) xexp reverse tc x |
| 402 | compound interest for principals y at percentages g for periods x | y*\:(1+g%100)xexp\:x |
| 403 | annuity coefficient for x periods at interest rate y% | y%/:100*flip 1-(1+y%100)xexp\:neg x |
| 404 | fifo stock y decremented with x units | 1_deltas 0,0|(sums y)-x |
| 405 | payback | C:prds(count W)#1+RC*B-sums W%(count W)#T _ 1,C |
| 406 | round to nearest integer | floor 0.5+x |
| 407 | round to two decimal places | 0.01*floor 0.5+x*100 |
| 408 | round y to x decimals | (10 xexp neg x)*`long$y*10 xexp x |
| 409 | round to nearest even integer | floor x+not 1>x mod 2 |
| 410 | round, but to nearest even integer if fractional part is 0.5 | floor x+0.5*not 0.5=x mod 2 |
| 411 | round x to nearest multiple of y | y*floor 0.5+x%y |
| 412 | round x to zero if magnitude less than y | @[x;where y>abs x;:;0.0] |
| 413 | round summands | i:floor Y:y%xa:{(floor .5+/x)#idesc x}Y-ix*@[i;a;+;1] |
Find¶
| new | description | phrase |
|---|---|---|
| 500 | y in x | x?y |
| 501 | flags | where x |
| 502 | all occurrences of y in x | where x in y |
| 503 | first occurrence of y | x?y |
| 504 | first occurrence of maximum | x?max x |
| 505 | first occurrence of minimum | x?min x |
| 506 | first occurrence of g (circularly) after y | q)mod[;count x] y+(y rotate x)?g |
| 507 | last occurrence of y | count[x]-1+reverse[x]?y |
| 508 | last occurrence of y, counted from the rear | reverse[x]?y |
| 509 | distinct items | value group x |
| 510 | first differing item of x and y | first where x<>y |
| 511 | items not in y | not x in y |
| 512 | consecutive repeated items | where differ x |
| 513 | rows of matrix y in matrix x | x?y |
| 514 | items of y in array x | shape[x] vs where raze[x] in raze y |
| 515 | rows of y in in corresponding rows of x | x?'y |
| 516 | first blank | x?" " |
| 517 | last non-blank | (" "=reverse x)?0b |
| 518 | string x in string y | z:where y=first xz where x~/:y z+\:tc x |
| 519 | blank rows | all flip x=" " |
| 520 | rows starting with item of y | x where x[;0]in y |
Flags¶
Format¶
| new | description | phrase |
|---|---|---|
| 700 | formatted integers | flip DEC 10 vs x |
| 701 | count of format | ('[count;string]) |
| 702 | leading zeros for positive integers in field width y | 1_'string x+10 xexp y |
| 703 | number of decimals | count each 2_'string x mod 1 |
| 704 | number of decimals, maximum y | y&count each 2_'string x mod 1 |
| 705 | number of positions in nonnegative integer | 1+floor log10 x+0=x |
| 706 | number of positions in integer | 1+(x<0)+floor 10 xlog abs x+0=x |
| 707 | time hh |
":"sv 0 2 4_ string x |
| 708 | date yyyy/mm/dd from integer yyyymmdd | "/"sv 0 4 6_ string x |
| 709 | indexing plotting characters with flags | " X" x |
| 710 | histogram | " *"(1+ til max x)<=/:x |
| 711 | barchart of integer list | " X" reverse til[max x]<\:x |
| 712 | horizontal barchart of integers | " X" x>\:til max x |
| 713 | horizontal barchart, normalized to length y | " X" (floor x*y%max x)>\:til y |
Geometry¶
| new | description | phrase |
|---|---|---|
| 800 | degrees from degrees and minutes | sum x%1 60 |
| 801 | degrees from radians | 57.295779513082323*x |
| 802 | radians from degrees | 0.017453292519943295*x |
| 803 | nautical miles from radians | x*180*60%PI |
| 804 | great circle distance | cos distinct(prd sin x)+(prd cos x)*cos(-/)y |
| 805 | area of triangle with sides x (Heron’s rule) | sqrt prd(sum x%2)-0,x |
| 806 | arctangent y÷x | atan y%x |
| 807 | complementary angle (arccos sin x) | acos sin x |
| 808 | rotation matrix for angle x (in radians) counter-clockwise | ((cos x;neg sin x);(sin x;cos x)) |
| 809 | contour levels y at points with altitude x | y -1+sum not y>x |
Indexes¶
| new | description | phrase |
|---|---|---|
| 900 | all indexes of vector x | tc x |
| 901 | y-wide sublists of x | y+til count[x]-y-1 |
| 902 | sublists of length y, starting at x | x+\:til y |
| 903 | all pairs of til[x] and til[y] |
ap:{x vs til prd x}x,y |
| 904 | indexes of an array | ap shape x |
| 905 | scattered indexing | x ./: y |
| 906 | raveled index from general index | shape[x] sv y |
| 907 | index an arbitrary-rank array | x . y |
Mathematics¶
| new | description | phrase |
|---|---|---|
| 1000 | Beta function | gamma:prd"f"$1_til@((gamma x)*gamma y)%gamma[x+y] |
| 1001 | number of combinations of n objects taken k at a time | fac[n]%fac[n-k]*fac[k] |
| 1002 | number of permutations of n objects taken k at a time | prd(n-k-1)+til k |
| 1003 | permutations | (1 0#x){raze(1 rotate)scan'x,'y}/x |
| 1004 | invert permutation | iasc x |
| 1005 | connectivity list from connectivity matrix | rc[x;]where raze x |
| 1006 | connectivity matrix from connectivity list | (2#x)#(til x*x)in x sv/:y |
| 1007 | node matrix from connection matrix | b:flip each x=/:1 -1(mmu\:) . "f"$(b;tc x) |
| 1008 | connection matrix from node matrix | (-/)flip x=/:tc distinct raze x |
| 1009 | first y Fibonacci numbers | y{x,sum -2#x}/0 1 |
| 1010 | maximum separation of items of x | (max x)-min x |
| 1011 | partitions of y with no part less than x | part:{t:x _ til 1+floor y%2;(raze t,''t part'y-t),y} |
| 1012 | Pascal’s triangle | x {0+':x,0}\ 1 |
| 1013 | pointer chasing | ((r*til p)mod p)\[1] |
| 1014 | polygon area | 0.5*sum last[x]{(-/)y*reverse x}':x |
| 1015 | quadraticsolution | q:0.5*y+signum[y]*sqrt(y*y)-4*x*z(q%x),z%q |
| 1016 | saddle-point indexes | rc[x;]where raze minmax x |
| 1017 | flag row minimums | rn:{x=min each x} |
| 1018 | flag column maximums | cx:{x=\:max x} |
| 1019 | flag minmaxs | minmax:{(rn x)&cx x} |
| 1020 | row-column indexes of ravel-indexes y in matrix x | rc:{(div;mod).\:(y;count first x)} |
| 1021 | value of saddle point | raze[x]where raze minmax x |
| 1022 | set union | distinct y,x |
| 1023 | set difference | x except y |
| 1024 | set intersection | x where x in y |
| 1025 | range union | f:{(x b;1 rotate a b:0,where x>1+a:-1 rotate maxs y)}flip f . flip asc x |
| 1026 | Taylor series | sum y*(x xexp a)%prds 1|a:tc y |
| 1027 | value of Taylor series with coefficients y at x | sum y*prds 1.0,x%1+til(count y)-1 |
Matrixes¶
| new | description | phrase |
|---|---|---|
| 1100 | apply to dimension 1 function defined on dimension 0 | f each x |
| 1101 | truth table of order x | 2 vs til "j"$2 xexp x |
| 1102 | upper triangular matrix of order x | {x<=\:x}til x |
| 1103 | lower triangular matrix of order x | {x>=\:x}til x |
| 1104 | identity matrix of order x | {x=/:x}til x |
| 1105 | Hilbert matrix of order x | reciprocal 1+{x+/:x}til x |
| 1106 | empty row to start matrix of x columns | () |
| 1107 | main diagonal | x ./:2#'tc x |
| 1108 | diagonals from columns | (neg til 5)rotate'x |
| 1109 | columns from diagonals | (til 5)rotate'x |
| 1110 | add vector y to main diagonal of x | @'[x;tc x;+;y] |
| 1111 | extend distance table to next leg | x('[min;+])\:x |
| 1112 | extend a transitive binary relation | x('[any;&])\:x |
| 1113 | first column as a matrix | x[;enlist 0] |
| 1114 | value of two-by-two determinant | (-)over(x 0)*reverse x 1 |
| 1115 | Shur product | ((last shape x)#x) * (first shape y)#'y |
| 1116 | Shur sum | ((last shape x)#x) + (first shape y)#'y |
Miscellaneous¶
Parts and items¶
Polynomials¶
| new | description | phrase |
|---|---|---|
| 1400 | polynomial with roots x | {(x,0)-y*0,x} over 1,x |
| 1401 | polynomial product | sum(tc x)rotate'(1_'zm x),'y*/:x |
| 1402 | polynomial derivative | -1 _ x*reverse tc x |
| 1403 | value of ascending polynomial coefficients y at points x | x sv\:y |
| 1404 | coefficients of best linear fit of points (x,y) (least squares) | oa:{x xexp/:0 1}cbf:{first(enlist y)lsq oa x} |
| 1405 | predicted values of best linear fit (least squares) | oe:{x xexp\:0 1}pvbf:{(oe x)mmu cbf[x;y]} |
| 1406 | coefficients of exponential fit of points (x,y) | (*). exp(1;x)*cbf[x;log y] |
| 1407 | predicted values of exponential fit | exp pvbf[x;log y] |
| 1408 | G-degree polynomial fit of points (x,y) | a:x xexp/:til g+1b:first(enlist y)lsq areverse b |
| 1409 | extrapolated value of abscissa x and ordinate y at g | a:x xexp/:reverse tc xb:(enlist y) lsq ag sv raze b |
| 1410 | y-th moment of x | c:count x(sum(x-(sum x)%c)xexp y)%c |
Rank¶
| new | description | phrase |
|---|---|---|
| 1500 | rank of an array (number of dimensions) | depth x |
| 1501 | shape of an array | shape:{$[0=d:depth x; 0#0j; d#{first(raze/)x}each(d{each[x;]}\count)@\:x]} |
| 1502 | atom from 1-item vector | first x |
| 1503 | vector from array | (raze/)x |
| 1504 | vector from column y of matrix | x[;y] |
| 1505 | 1-row matrix from vector | enlist x |
| 1506 | 1-row matrix from atom or vector | {$[0<type x;(1,count x)#x;1 1#x]} |
| 1507 | 2-row matrix from two vectors | (x;y) |
| 1508 | y-row matrix from vector x | y#enlist x |
| 1509 | 1-column matrix from vector | 1#'x |
| 1510 | 2-column matrix from two vectors | x,'y |
| 1511 | 2-column matrix from vector | 2 cut x,(count[x]mod 2)#" " |
| 1512 | x-column matrix from vector y | x#'y |
| 1513 | matrix with diagonal x | x*{x=/:\:x}tc x |
| 1514 | increase rank of y to rank of x | (depth[x]-depth y)enlist/y |
Shape¶
Sort and merge¶
| new | description | phrase |
|---|---|---|
| 1700 | ordinals (ranking, all different) | idesc idesc x |
| 1701 | ordinals (ranking, shareable) | asc[x]?x |
| 1702 | choose grading direction | x iasc x*1 -1[y] |
| 1703 | grade up x according to key y | x iasc y?x |
| 1704 | playing order of x ranked players | f:1+2 sv reverse tt neg floor neg 2 xlog xf*f<=x |
| 1705 | sort x | asc x, desc x |
| 1706 | sort y on x | y iasc x |
| 1707 | sort rows of x on column y | x iasc x[;y] |
| 1708 | sort ascending indexes x according to data y | x iasc y x |
| 1709 | move flagged items to one end | x idesc y |
| 1710 | sort y by value into x classes | value asc y group x xrank y |
| 1711 | which class of y x belongs to | -1+sum x>/:y |
| 1712 | assign x to one of y classes of width h, starting with g | a:x where (x>=g)&x<g+y*h@[;0;1_](g,a){x asc key x}group floor(g,a)%h |
| 1713 | merge x y by g | (x,y)[iasc idesc g] |
| 1714 | mesh: merge x y z under control of g | (x,y,z)rank g |
| 1715 | merge items from x and y alternately | raze x,'y |
| 1716 | ripple shuffle | x iasc idesc count[x]#0 1 |
Statistics¶
| new | description | phrase |
|---|---|---|
| 1800 | maximum | max x |
| 1801 | non-negative maximum | max 0,x |
| 1802 | maximum of x with weights y | max x*y |
| 1803 | minimum | min x |
| 1804 | minimum of x with weights y | min x*y |
| 1805 | average (mean) | avg x |
| 1806 | average (mean) of x weighted by y | y wavg x |
| 1807 | median of x | med x |
| 1808 | standard deviation | dev x |
| 1809 | variance (dispersion) | var x |
| 1810 | normal deviates from interval (0,1) | x?1. |
| 1811 | running sum | sums x |
| 1812 | moving sum | y msum x |
Strings¶
| new | description | phrase |
|---|---|---|
| 1900 | alphabetic comparison | x<y |
| 1901 | remove blanks | x except " " |
| 1902 | remove trailing blanks | (neg reverse[x=" "]?0b)_ x |
| 1903 | remove leading blanks | x where maxs x<>" " |
| 1904 | remove leading and trailing blanks | x{y _ x}/1 -1*(" "=1 reverse\x)?'0b |
| 1905 | collapse multiple blanks | x where 1b,1_(or)prior" "<>x |
| 1906 | move blanks to end | x iasc x=" " |
| 1907 | justify right | neg[(reverse[x]=" ")?0b]rotate x |
| 1908 | center text x in line of width y | neg[floor(y-count x)%2]rotate y#x,y#" " |
| 1909 | insert spaces in text | -1_raze x,'" " |
| 1910 | conditional text | $[x;"true";"false"] |
| 1911 | double quotes | ssr[x;"\"";"\"\""] |
| 1912 | remove leading zeros | ((x="0")?0b) _ x |
| 1913 | remove punctuation characters | x except PUN |
| 1914 | ordinal of word in x pointed at by y | sum not y<1+where x=" " |
| 1915 | find start of string x in string y | y ss x |
| 1916 | find first occurrence of string x in string y | first y ss x |
| 1917 | locate quotes and text between them | (or)prior(<>)scan x="\"" |
| 1918 | locate text between quotes | (and)prior(<>)scan x="\"" |
| 1919 | depth of parentheses | sums({x-0b,-1_y}/)"()"=\:x |
| 1920 | first word in string x | |
| 1921 | insert blank in y after flag in x | raze y,'x#'" " |
| 1922 | insert x[i] blanks after y[g[i]] | raze((0,1+g)_y),'(x,0)#'" " |
| 1923 | spread flagged field heads right | x raze(count each a _ x)#'a:where y |
Temporal¶
| new | description | phrase |
|---|---|---|
| 2000 | is year x a leap year? | ly:{sum[0=x mod\:4 100 400]mod 2} |
| 2001 | days in month x of Gregorian year y | $[x=2;28+ly y;(0,12#7#31 30)x] |
| 2002 | date in ascending format | "/"sv reverse 0 4 6_ x |
| 2003 | current time of 12-hour clock | p:x>11:59:59string[x-43200*p]," ","AP"[p],"M" |
| 2004 | current date, American format | "/"sv string 1 rotate parse ssr[;".";" "] string x |
Tests¶
Text¶
| new | description | phrase |
|---|---|---|
| 2200 | left-justify fields of length y to length g | raze g#/:((sums 0,-1_y)_x),\:g#" " |
| 2201 | right-justify fields of length y to length g | raze(neg g)#/:(g#" "),/:(sums 0,-1_y) _ x |
| 2202 | collapse multiple occurrences | x where 1 rotate(or)prior a:x<>y |
| 2203 | comma-separated list from matrix | ","sv x |
| 2204 | rows starting with y | x where x[;tc y]~\:y |
| 2205 | rows with items different from y | max y<>flip x |
| 2206 | frame character matrix | flip"-",'(flip"|",'x,'"|"),'"-" |
| 2207 | insert y "*" after "=" | g:where x="="raze((0,1+-1_ g)_x),\:y#"*" |
| 2208 | matrix from string at parts flagged by x | a:(where y)_xa,'#'[;" "]max[b]-b:count each a |
| 2209 | remove duplicate rows | distinct x |
| 2210 | variable length lines | (x;y;z) |
| 2211 | collapse whitespace | x where{x|1_x,1b}max" "<>flip x |
| 2212 | remove blank rows | x where max" "<>flip x |
| 2213 | remove leading blank rows | ((min each x=" ")?0b)_ x |
| 2214 | remove trailing blank rows | (neg sum mins reverse(and/)each x=" ")_ x |
| 2215 | remove blank columns | x where max" "<>x |
| 2216 | remove leading blank columns | sum[mins min x=" "]_'x |
| 2217 | remove trailing blank columns | neg[sum mins reverse min x=" "]_'x |
| 2218 | append empty row | flip(flip x),'" " |
| 2219 | insert empty row after row y | a:flip(flip x),'" "a rank@[count[a]#1;y+1;+;1] |