import math


def decay(A_t, B_tminus1, alpha, threshold):
    """
    Apply decay to the tie-strength matrix.

    Input A(t) the adjacency matrix at time t, B(t-1) the
    matrix of tie strengths at the previous time step, the decay rate
    alpha, and the threshold for eliminating small values for computational
    efficiency. Any values in the tie strength matrix that are below the value
    given by threshold will be set to 0.

    Returns the matrix of tie-strengths at time t.

    """

    # Calculate and apply the decay on B_tminus1
    H_t = B_tminus1.multiply(math.exp(-alpha))

    # eliminate small values from the matrix by setting them to 0
    H_t = H_t.multiply(H_t >= threshold)

    # add on new values
    B_t = H_t + A_t

    return B_t