If f(x+y)equals (f x)+(f y) for all vectors x and y in its domain, then f is said to be a linear vector function. In other words, f@:+ -: f@[ + f@] is a test for the linearity of f:
f=: +/\"1 NB. Sum scan (subtotals) is a linear vector function x=: 4 3 2 1 0 y=: 2 3 5 7 11 x ([ , ] , f@[ , f@] , (f@[ + f@]) ,: f@:+) y 4 3 2 1 0 x 2 3 5 7 11 y 4 7 9 10 10 f x 2 5 10 17 28 f y 6 12 19 27 38 (f x)+(f y) 6 12 19 27 38 f (x+y)
For vector arguments of dimension n, any linear vector function f can be expressed as a matrix product, using a matrix obtained by applying f to the identity matrix of order n. Moreover, the inverse of a linear function is a linear function. For example:
g=: f^:_1 NB. Inverse of subtotals is first differences (] ; f ; g ; f@g) y +--------------------------------------------+ |2 3 5 7 11|2 5 10 17 28|2 1 2 2 4|2 3 5 7 11| +--------------------------------------------+ I=: = i. 5 NB. Identity matrix mf=: (f I) [ mg=: (g I) mp=: +/ . * NB. Matrix product mf ; mg ; (y,(y mp mf),:f y) ; (mf mp mg) +----------------------------------------------+ |1 1 1 1 1|1 _1 0 0 0|2 3 5 7 11|1 0 0 0 0| |0 1 1 1 1|0 1 _1 0 0|2 5 10 17 28|0 1 0 0 0| |0 0 1 1 1|0 0 1 _1 0|2 5 10 17 28|0 0 1 0 0| |0 0 0 1 1|0 0 0 1 _1| |0 0 0 1 0| |0 0 0 0 1|0 0 0 0 1| |0 0 0 0 1| +----------------------------------------------+
a0=: MR=: 1 : 'x.@=@i.@#' | Matrix representation of linear function |
d1=: mp=: +/ . * | Matrix product |
a2=: L=: &mp | Linear function represented by matrix |
a3=: inv=: ^:_1 | Inverse adverb |
a4=: MRI=: (^:_1) 'MR' f. | Matrix representation of inverse function |
a5=: MR (%.@) | " |
m=: f MR y [ mi=: f MRI y m ; mi ; y,(y L m),:(mi L m L y) +------------------------------------+ |1 1 1 1 1|1 _1 0 0 0|2 3 5 7 11| |0 1 1 1 1|0 1 _1 0 0|2 5 10 17 28| |0 0 1 1 1|0 0 1 _1 0|2 5 10 17 28| |0 0 0 1 1|0 0 0 1 _1| | |0 0 0 0 1|0 0 0 0 1| | +------------------------------------+