def METHOD_NAME(path, matrix, key=None):
    """Save matrix to file.
    Parameters
    ----------
    path
        Path to the file (`str` or `pathlib.Path`).
    matrix
        Matrix to save.
    key
        Key of the matrix (only for NPY, NPZ, and MATLAB files).
    Raises
    ------
    IOError
        If saving fails.
    """
    logger = getLogger('pymor.tools.io.save_matrix')
    logger.info('Saving matrix to file %s', path)
    # convert if path is str
    path = Path(path)
    extension = _get_file_extension(path)
    file_format_map = {
        '.mat': ('MATLAB', _savemat),
        '.mtx': ('Matrix Market', _mmwrite),
        '.mtz.gz': ('Matrix Market', _mmwrite),
        '.npy': ('NPY', _save),
        '.npz': ('NPZ', _savez),
        '.txt': ('Text', _savetxt),
    }
    if extension in file_format_map:
        file_type, saver = file_format_map[extension]
        logger.info(file_type + ' file detected.')
        return saver(path, matrix, key)
    raise IOError(f'Unknown extension "{extension}"')