Basic Linear Algebra in q


James Neill 23/05/2015

As an array processing language q allows us to perform vectorised operations on lists. The presence of vectors and matrices in q means that basic linear algebra techniques are available to us. A matrix in q is represented as a list of lists:

q)A:(1 2f;3 4f)
q)A
1 2
3 4

A vector in q is just a list:

q)v:1 2 3f
q)v
1 2 3f

Addition and Subtraction

If I have two vectors v and w the sum or difference of the two is a new vector whose elements are the sum or difference of the elements of v and w.$$\mathbf{v}=\begin{pmatrix}1 \\ 2\end{pmatrix};\mathbf{w}=\begin{pmatrix}3 \\ 4\end{pmatrix}\to \mathbf{v}+\mathbf{w}=\begin{pmatrix}4 \\ 6\end{pmatrix}$$

q)v:1 2f
q)w:3 4f
q)v+w
4 6f
q)v-w
-2 -2f

Matrices operate similarly:$$\mathbf{A}=\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} ; \mathbf{B}=\begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}\to \mathbf{A}+\mathbf{B}=\begin{pmatrix}6 & 8 \\ 10 & 12 \end{pmatrix}$$

q)B:(5 6f;7 8f)
q)A+B
6  8 
10 12
q)A-B
-4 -4
-4 -4

Dot Product of 2 Vectors

The dot product of 2 vectors is the sum of the product of the individual elements of each vector:$$\mathbf{v}\cdot\mathbf{w}=v_1 w_1+v_2 w_2 = 3+8 = 11$$In q we can do this with the mmu or $ operators:

q)v mmu w
11f
q)v$w
11f

Multiplication of 2 Matrices

Matrix multiplcation can only occur if the number of columns in the left matrix equals the number of rows in the right matrix. The product of two matrices is the matrix whose elements are the dot product of the row in the left matrix and the corresponding column in the right matrix:$$\mathbf{A}\mathbf{B}=\begin{pmatrix}(1)(5)+(2)(7)&(1)(6)+(2)(8) \\(3)(5)+(4)(7)&(3)(6)+(4)(8)\end{pmatrix}=\begin{pmatrix}19&22\\43&50\end{pmatrix}$$

q)A mmu B
19 22
43 50
q)A$B
19 22
43 50

Matrix Transpose

The transpose of a matrix is formed by turning the rows of a matrix into columns and vice versa:$$\mathbf{A}=\begin{pmatrix}1&2\\3&4\end{pmatrix};\mathbf{A}^T =\begin{pmatrix}1&3\\2&4\end{pmatrix}$$ In q we can get the transpose of a matrix by flipping it

q)A
1 2
3 4
q)flip A
1 3
2 4

Identity Matrix

The identity matrix is the n x n square matrix with ones along the main diagonal and zeros elsewhere. It is commonly denoted as In. For an m x n matrix A the identity matrix has the property:$$\mathbf{I}_m \mathbf{A}=\mathbf{AI}_n =\mathbf{A}$$This is how we construct the identity matrix in q:

q)IMatrix:{`float${x=/:x}til x}
q)I3:IMatrix 3
q)I3
1 0 0
0 1 0
0 0 1

Diagonal Matrix

A diagonal matrix is a matrix with 0 for all elements that are not on the main diagonal. We can construct one in q using the identity matrix definition above:

q)DiagMatrix:{`float$x*{x=/:x}til count x}
q)DiagMatrix 4 0 1.3
4 0 0  
0 0 0  
0 0 1.3

Matrix Inversion

An n x n square matrix A is invertible if there exists an n x n matrix A-1 such that the following is true:$$\mathbf{AA}^{-1} =\mathbf{A}^{-1} \mathbf{A}=\mathbf{I}_n$$If a square matrix is singular then it has no inverse. There are many different algorithms for calculating the inverse of a square matrix. q has an inbuilt function for this.

q)A
1 2
3 4
q)inv[A]
-2  1   
1.5 -0.5

Unfortunately this algorithm sacrifices numerical stability for accuracy. To deal with this I have written a C extension that uses lapack's dgetrf and dgetri routines to invert matrices (the source code can be found here):

q)inv[(1 2f;1 3f)]
3  -2
-1 1 
q)qinv[(1 2f;1 3f)]
3  -2
-1 1 
q)inv[(5 10010f;10010 20040030f)]


q)qinv[(5 10010f;10010 20040030f)]
400800.6 -200.2
-200.2   0.1