def log_1_plus_exp(mat, target = None, exact=False):
    """
    Apply log(1+exp(x)) to each element of the matrix mat. If exact is True, use
    slow and accurate log and exp.
    """

    if not target:
        target = mat

    if exact:
      err_code = _cudamat.apply_log_1_plus_exp_exact(mat.p_mat, target.p_mat)
    else:
      err_code = _cudamat.apply_log_1_plus_exp(mat.p_mat, target.p_mat)
    if err_code:
        raise generate_exception(err_code)

    return target

    Apply log(1+exp(x)) to each element of the matrix mat. If exact is True, use
    slow and accurate log and exp.
    
cudamat/cudamat.py