Matrix Manipulation

Many different kinds of matrix manipulation routines are available:

  • tile() to repeat a matrix along dimensions
  • join() to concatenate two matrices along a dimension
  • array() to adjust the dimensions of an array
  • transpose a matrix or vector

tile() allows you to repeat a matrix along specified dimensions, effectively 'tiling' the matrix. Please note that the dimensions passed in indicate the number of times to replicate the matrix in each dimension, not the final dimensions of the matrix.

float h[] = {1, 2, 3, 4};
array small_arr = array(2, 2, h); // 2x2 matrix
af_print(small_arr);
array large_arr = tile(small_arr, 2, 3); // produces 4x6 matrix: (2*2)x(2*3)
af_print(large_arr);

join() allows you to joining two matrices together. Matrix dimensions must match along every dimension except the dimension of joining (dimensions are 0-indexed). For example, a 2x3 matrix can be joined with a 2x4 matrix along dimension 1, but not along dimension 0 since {3,4} don`t match up.

float hA[] = { 1, 2, 3, 4, 5, 6 };
float hB[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
array A = array(3, 2, hA);
array B = array(3, 3, hB);
af_print(join(1, A, B)); // 3x5 matrix
// array result = join(0, A, B); // fail: dimension mismatch

Construct a regular mesh grid from vectors x and y. For example, a mesh grid of the vectors {1,2,3,4} and {5,6} would result in two matrices:

float hx[] = {1, 2, 3, 4};
float hy[] = {5, 6};
array x = array(4, hx);
array y = array(2, hy);
af_print(tile(x, 1, 2));
af_print(tile(y.T(), 4, 1));

array() can be used to create a (shallow) copy of a matrix with different dimensions. The number of elements must remain the same as the original array.

int hA[] = {1, 2, 3, 4, 5, 6};
array A = array(3, 2, hA);
af_print(A); // 2x3 matrix
af_print(moddims(A, 2, 3)); // 2x3 matrix
af_print(moddims(A, 6, 1)); // 6x1 column vector
// moddims(A, 2, 2); // fail: wrong number of elements
// moddims(A, 8, 8); // fail: wrong number of elements

The T() and H() methods can be used to form the matrix or vector transpose .

array x = randu(2, 2, f32);
af_print(x.T()); // transpose (real)
array c = randu(2, 2, c32);
af_print(c.T()); // transpose (complex)
af_print(c.H()); // Hermitian (conjugate) transpose