code: def compute_velocity(swarm, clamp, vh, bounds=None):
      """Update the velocity matrix
  
      This method updates the velocity matrix using the best and current
      positions of the swarm. The velocity matrix is computed using the
      cognitive and social terms of the swarm. The velocity is handled
      by a :code:`VelocityHandler`.
  
      A sample usage can be seen with the following:
  
      .. code-block :: python
  
          import pyswarms.backend as P
          from pyswarms.swarms.backend import Swarm, VelocityHandler
  
          my_swarm = P.create_swarm(n_particles, dimensions)
          my_vh = VelocityHandler(strategy="invert")
  
          for i in range(iters):
              # Inside the for-loop
              my_swarm.velocity = compute_velocity(my_swarm, clamp, my_vh, bounds)
  
      Parameters
      ----------
      swarm : pyswarms.backend.swarms.Swarm
          a Swarm instance
      clamp : tuple of floats, optional
          a tuple of size 2 where the first entry is the minimum velocity
          and the second entry is the maximum velocity. It
          sets the limits for velocity clamping.
      vh : pyswarms.backend.handlers.VelocityHandler
          a VelocityHandler object with a specified handling strategy.
          For further information see :mod:`pyswarms.backend.handlers`.
      bounds : tuple of numpy.ndarray or list, optional
          a tuple of size 2 where the first entry is the minimum bound while
          the second entry is the maximum bound. Each array must be of shape
          :code:`(dimensions,)`.
  
      Returns
      -------
      numpy.ndarray
          Updated velocity matrix
      """
      try:
          # Prepare parameters
          swarm_size = swarm.position.shape
          c1 = swarm.options["c1"]
          c2 = swarm.options["c2"]
          w = swarm.options["w"]
          # Compute for cognitive and social terms
          cognitive = (
              c1
              * np.random.uniform(0, 1, swarm_size)
              * (swarm.pbest_pos - swarm.position)
          )
          social = (
              c2
              * np.random.uniform(0, 1, swarm_size)
              * (swarm.best_pos - swarm.position)
          )
          # Compute temp velocity (subject to clamping if possible)
          temp_velocity = (w * swarm.velocity) + cognitive + social
          updated_velocity = vh(
              temp_velocity,
              clamp,
              position=swarm.position,
              bounds=bounds,
          )
  
      except AttributeError:
          rep.logger.exception(
              "Please pass a Swarm class. You passed {}".format(
                  type(swarm)
              )
          )
          raise
      except KeyError:
          rep.logger.exception("Missing keyword in swarm.options")
          raise
      else:
          return updated_velocity
  
file_path: pyswarms/pyswarms/backend/operators.py
imports: import logging
  import numpy
  from ..utils.reporter import Reporter
  from .handlers import BoundaryHandler, VelocityHandler
  from functools import partial
label_desc: Use image node_id 2 for calling a global function with example usage: compute_velocity(swarm, clamp, vh, bounds) and returns: updated_velocity