def is_valid_linkage(Z, warning=False, throw=False, name=None):
Z = np.asarray(Z, order='<STR_LIT:c>')<EOL>valid = True<EOL>try:<EOL><INDENT>if type(Z) != np.ndarray:<EOL><INDENT>if name:<EOL><INDENT>raise TypeError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT><DEDENT>if Z.dtype != np.double:<EOL><INDENT>if name:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>% name)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT><DEDENT>if len(Z.shape) != <NUM_LIT:2>:<EOL><INDENT>if name:<EOL><INDENT>raise ValueError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>if Z.shape[<NUM_LIT:1>] != <NUM_LIT:4>:<EOL><INDENT>if name:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>if Z.shape[<NUM_LIT:0>] == <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>n = Z.shape[<NUM_LIT:0>]<EOL>if n > <NUM_LIT:1>:<EOL><INDENT>if ((Z[:, <NUM_LIT:0>] < <NUM_LIT:0>).any() or (Z[:, <NUM_LIT:1>] < <NUM_LIT:0>).any()):<EOL><INDENT>if name:<EOL><INDENT>raise ValueError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>if (Z[:, <NUM_LIT:2>] < <NUM_LIT:0>).any():<EOL><INDENT>if name:<EOL><INDENT>raise ValueError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>if (Z[:, <NUM_LIT:3>] < <NUM_LIT:0>).any():<EOL><INDENT>if name:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>% name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>if _check_hierarchy_uses_cluster_before_formed(Z):<EOL><INDENT>if name:<EOL><INDENT>raise ValueError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>if _check_hierarchy_uses_cluster_more_than_once(Z):<EOL><INDENT>if name:<EOL><INDENT>raise ValueError(('<STR_LIT>'<EOL>'<STR_LIT>') % name)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>if throw:<EOL><INDENT>raise<EOL><DEDENT>if warning:<EOL><INDENT>_warning(str(e))<EOL><DEDENT>valid = False<EOL><DEDENT>return valid<EOL>
Checks the validity of a linkage matrix.

A linkage matrix is valid if it is a two dimensional
ndarray (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``,
:math:`0 \\leq \\mathtt{Z[i,0]} \\leq i+n-1`
and :math:`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 iff the inconsistency matrix is valid.