def inverse_truncated_modal(M, num_modes, svd=None):
    """Invert `M` with `num_modes` modes.

	Modes are given by a singular value decomposition of the input matrix.
	If `svd` is given, it is used instead. Modes are ordered in descending
	order by their singular values.

	Parameters
	----------
	M : ndarray
		The matrix to invert. This matrix must be two-dimensional.
	num_modes : integer
		The number of modes to use for the inversion. Higher-order modes
		are ignored.
	svd : SVD object
		A precalculated singular value decomposition object. This will
		be used if supplied, to avoid recalculation of an SVD.

	Returns
	-------
	ndarray
		The inverted matrix.
	"""
    if svd is None:
        from .singular_value_decomposition import SVD
        svd = SVD(M, num_modes)
    U, S, Vt = svd.svd
    U = U[:, :num_modes]
    S = S[:num_modes]
    Vt = Vt[:num_modes, :]
    return (Vt.T / S).dot(U.T)
