code: def is_valid_linkage(Z, warning=False, throw=False, name=None):
      """
      Check the validity of a linkage matrix.
  
      A linkage matrix is valid if it is a 2-D array (type double)
      with :math:`n` rows and 4 columns. The first two columns must contain
      indices between 0 and :math:`2n-1`. For a given row ``i``, the following
      two expressions have to hold:
  
      .. math::
  
          0 \\leq \\mathtt{Z[i,0]} \\leq i+n-1
          0 \\leq Z[i,1] \\leq i+n-1
  
      I.e., a cluster cannot join another cluster unless the cluster being joined
      has been generated.
  
      Parameters
      ----------
      Z : array_like
          Linkage matrix.
      warning : bool, optional
          When True, issues a Python warning if the linkage
          matrix passed is invalid.
      throw : bool, optional
          When True, throws a Python exception if the linkage
          matrix passed is invalid.
      name : str, optional
          This string refers to the variable name of the invalid
          linkage matrix.
  
      Returns
      -------
      b : bool
          True if the inconsistency matrix is valid.
  
      See Also
      --------
      linkage: for a description of what a linkage matrix is.
  
      Examples
      --------
      >>> from scipy.cluster.hierarchy import ward, is_valid_linkage
      >>> from scipy.spatial.distance import pdist
  
      All linkage matrices generated by the clustering methods in this module
      will be valid (i.e., they will have the appropriate dimensions and the two
      required expressions will hold for all the rows).
  
      We can check this using `scipy.cluster.hierarchy.is_valid_linkage`:
  
      >>> X = [[0, 0], [0, 1], [1, 0],
      ...      [0, 4], [0, 3], [1, 4],
      ...      [4, 0], [3, 0], [4, 1],
      ...      [4, 4], [3, 4], [4, 3]]
  
      >>> Z = ward(pdist(X))
      >>> Z
      array([[ 0.        ,  1.        ,  1.        ,  2.        ],
             [ 3.        ,  4.        ,  1.        ,  2.        ],
             [ 6.        ,  7.        ,  1.        ,  2.        ],
             [ 9.        , 10.        ,  1.        ,  2.        ],
             [ 2.        , 12.        ,  1.29099445,  3.        ],
             [ 5.        , 13.        ,  1.29099445,  3.        ],
             [ 8.        , 14.        ,  1.29099445,  3.        ],
             [11.        , 15.        ,  1.29099445,  3.        ],
             [16.        , 17.        ,  5.77350269,  6.        ],
             [18.        , 19.        ,  5.77350269,  6.        ],
             [20.        , 21.        ,  8.16496581, 12.        ]])
      >>> is_valid_linkage(Z)
      True
  
      However, if we create a linkage matrix in a wrong way - or if we modify
      a valid one in a way that any of the required expressions don't hold
      anymore, then the check will fail:
  
      >>> Z[3][1] = 20    # the cluster number 20 is not defined at this point
      >>> is_valid_linkage(Z)
      False
  
      """
      xp = array_namespace(Z)
      Z = as_xparray(Z, order="c", xp=xp)
      valid = True
      name_str = "%r " % name if name else ""
      try:
          if Z.dtype != xp.float64:
              raise TypeError(
                  "Linkage matrix %smust contain doubles." % name_str
              )
          if len(Z.shape) != 2:
              raise ValueError(
                  "Linkage matrix %smust have shape=2 (i.e. be "
                  "two-dimensional)." % name_str
              )
          if Z.shape[1] != 4:
              raise ValueError(
                  "Linkage matrix %smust have 4 columns." % name_str
              )
          if Z.shape[0] == 0:
              raise ValueError(
                  "Linkage must be computed on at least two "
                  "observations."
              )
          n = Z.shape[0]
          if n > 1:
              if xp.any(Z[:, 0] < 0) or xp.any(Z[:, 1] < 0):
                  raise ValueError(
                      "Linkage %scontains negative indices." % name_str
                  )
              if xp.any(Z[:, 2] < 0):
                  raise ValueError(
                      "Linkage %scontains negative distances."
                      % name_str
                  )
              if xp.any(Z[:, 3] < 0):
                  raise ValueError(
                      "Linkage %scontains negative counts." % name_str
                  )
          if _check_hierarchy_uses_cluster_before_formed(Z):
              raise ValueError(
                  "Linkage %suses non-singleton cluster before "
                  "it is formed." % name_str
              )
          if _check_hierarchy_uses_cluster_more_than_once(Z):
              raise ValueError(
                  "Linkage %suses the same cluster more than once."
                  % name_str
              )
      except Exception as e:
          if throw:
              raise
          if warning:
              _warning(str(e))
          valid = False
  
      return valid
  
imports: import warnings
  import bisect
  from collections import deque
  import numpy
  from .None import _hierarchy, _optimal_leaf_ordering
  import scipy.spatial.distance
  from scipy._lib._array_api import array_namespace, as_xparray, copy
  from scipy._lib._disjoint_set import DisjointSet
label_desc: Use image node_id 21 for calling a global function with example usage: is_valid_linkage(Z, warning, throw, name) and returns: valid