orderByDerivative function

Matrix orderByDerivative(
  1. Matrix q,
  2. int dim,
  3. int blockSize
)

Implementation

Matrix orderByDerivative(Matrix q, int dim, int blockSize) {
  assert(dim > 1 && dim < 5, 'dim must be 2, 3 or 4');
  assert(dim == q.rowsNum, 'dim must equal the number of rows in q');
  int n = dim * blockSize;
  List<List<double>> d = [];

  for (int i = 0; i < q.rowsNum; i++) {
    for (int j = 0; j < q.columnsNum; j++) {
      Matrix f = Matrix.identity(blockSize) * q[i][j];
      int newX = (i + j) ~/ n;
      int newY = (i + j) % n;

      for (int ix = newX; ix < newX + blockSize; ix++) {
        List<double> row = [];
        for (int iy = newY; iy < newY + blockSize; iy++) {
          row.add(f[ix - newX][iy - newY]);
        }

        d.add(row);
      }
    }
  }

  return Matrix.fromList(d);
}