code: def kinematic_kf(
      dim, order, dt=1.0, dim_z=1, order_by_dim=True, kf=None
  ):
      """
      Returns a KalmanFilter using newtonian kinematics of arbitrary order
      for any number of dimensions. For example, a constant velocity filter
      in 3D space would have order 1 dimension 3.
  
  
      Examples
      --------
  
      A constant velocity filter in 3D space with delta time = .2 seconds
      would be created with
  
      >>> kf = kinematic_kf(dim=3, order=1, dt=.2)
      >>> kf.F
      >>> array([[1. , 0.2, 0. , 0. , 0. , 0. ],
                 [0. , 1. , 0. , 0. , 0. , 0. ],
                 [0. , 0. , 1. , 0.2, 0. , 0. ],
                 [0. , 0. , 0. , 1. , 0. , 0. ],
                 [0. , 0. , 0. , 0. , 1. , 0.2],
                 [0. , 0. , 0. , 0. , 0. , 1. ]])
  
  
      which will set the state `x` to be interpreted as
  
      [x, x', y, y', z, z'].T
  
      If you set `order_by_dim` to False, then `x` is ordered as
  
      [x y z x' y' z'].T
  
      As another example, a 2D constant jerk is created with
  
      >> kinematic_kf(2, 3)
  
  
      Assumes that the measurement z is position in each dimension. If this is not
      true you will have to alter the H matrix by hand.
  
      P, Q, R are all set to the Identity matrix.
  
      H is assigned assuming the measurement is position, one per dimension `dim`.
  
  
      >>> kf = kinematic_kf(2, 1, dt=3.0)
      >>> kf.F
      array([[1., 3., 0., 0.],
             [0., 1., 0., 0.],
             [0., 0., 1., 3.],
             [0., 0., 0., 1.]])
  
      Parameters
      ----------
  
      dim : int, >= 1
          number of dimensions (2D space would be dim=2)
  
      order : int, >= 0
          order of the filter. 2 would be a const acceleration model with
          a stat
  
      dim_z : int, default 1
          size of z vector *per* dimension `dim`. Normally should be 1
  
      dt : float, default 1.0
          Time step. Used to create the state transition matrix
  
      order_by_dim : bool, default=True
          Defines ordering of variables in the state vector. `True` orders
          by keeping all derivatives of each dimensions)
  
          [x x' x'' y y' y'']
  
          whereas `False` interleaves the dimensions
  
          [x y z x' y' z' x'' y'' z'']
  
      kf : kalman filter like object, optional, default None
          Provide your own pre-created filter. This lets you use classes other
          than KalmanFilter.
      """
  
      from filterpy.kalman import KalmanFilter
  
      if dim < 1:
          raise ValueError("dim must be >= 1")
      if order < 0:
          raise ValueError("order must be >= 0")
      if dim_z < 1:
          raise ValueError("dim_z must be >= 1")
  
      dim_x = order + 1
  
      if kf is None:
          kf = KalmanFilter(dim_x=dim * dim_x, dim_z=dim_z)
      assert kf.dim_x == dim * dim_x
      assert kf.dim_z == dim_z
  
      F = kinematic_state_transition(order, dt)
      if order_by_dim:
          diag = [F] * dim
          kf.F = block_diag(*diag)
      else:
          kf.F.fill(0.0)
          for i, x in enumerate(F.ravel()):
              f = np.eye(dim) * x
  
              ix, iy = (i // dim_x) * dim, (i % dim_x) * dim
              kf.F[ix : ix + dim, iy : iy + dim] = f
  
      if order_by_dim:
          for i in range(dim_z):
              for j in range(dim):
                  kf.H[i, j * dim_x] = 1.0
      else:
          for i in range(dim_z):
              for j in range(dim):
                  kf.H[i, j] = 1.0
  
      return kf
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import math
  import numpy
  from scipy.linalg import block_diag
label_desc: Use image node_id 2 for calling a global function with example usage: kinematic_kf(dim, order, dt, dim_z, order_by_dim, kf) and returns: kf