code: def order_by_derivative(Q, dim, block_size):
      """
      Given a matrix Q, ordered assuming state space
          [x y z x' y' z' x'' y'' z''...]
  
      return a reordered matrix assuming an ordering of
         [ x x' x'' y y' y'' z z' y'']
  
      This works for any covariance matrix or state transition function
  
      Parameters
      ----------
      Q : np.array, square
          The matrix to reorder
  
      dim : int >= 1
  
         number of independent state variables. 3 for x, y, z
  
      block_size : int >= 0
          Size of derivatives. Second derivative would be a block size of 3
          (x, x', x'')
  
  
      """
  
      N = dim * block_size
  
      D = zeros((N, N))
  
      Q = array(Q)
      for i, x in enumerate(Q.ravel()):
          f = eye(block_size) * x
  
          ix, iy = (i // dim) * block_size, (i % dim) * block_size
          D[ix : ix + block_size, iy : iy + block_size] = f
  
      return D
  
file_path: filterpy/filterpy/common/discretization.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  from numpy import zeros, vstack, eye, array
  from numpy.linalg import inv
  from scipy.linalg import expm, block_diag
label_desc: Use image node_id 1 for calling a global function with example usage: order_by_derivative(Q, dim, block_size) and returns: D