(new_mu, new_sigma, new_traj_distr, prev_traj_distr, tot=True)
return np.sum(kl_div) if tot else kl_div
This function computes the same quantity as the function above.
    However, it is easier to modify and understand this function, i.e.,
    passing in a different mu and sigma to this function will behave properly.
This function computes the same quantity as the function above.
    However, it is easier to modify and understand this function, i.e.,
    passing in a different mu and sigma to this function will behave properly.
This function computes the same quantity as the function above . However it is easier to modify and understand this function i . e . passing in a different mu and sigma to this function will behave properly .
def traj_distr_kl_alt(new_mu, new_sigma, new_traj_distr, prev_traj_distr, tot=True):
    """
    This function computes the same quantity as the function above.
    However, it is easier to modify and understand this function, i.e.,
    passing in a different mu and sigma to this function will behave properly.
    """
    T, dX, dU = new_mu.shape[0], new_traj_distr.dX, new_traj_distr.dU
    kl_div = np.zeros(T)

    for t in range(T):
        K_prev = prev_traj_distr.K[t, :, :]
        K_new = new_traj_distr.K[t, :, :]

        k_prev = prev_traj_distr.k[t, :]
        k_new = new_traj_distr.k[t, :]

        sig_prev = prev_traj_distr.pol_covar[t, :, :]
        sig_new = new_traj_distr.pol_covar[t, :, :]

        chol_prev = prev_traj_distr.chol_pol_covar[t, :, :]
        chol_new = new_traj_distr.chol_pol_covar[t, :, :]

        inv_prev = prev_traj_distr.inv_pol_covar[t, :, :]
        inv_new = new_traj_distr.inv_pol_covar[t, :, :]

        logdet_prev = 2 * sum(np.log(np.diag(chol_prev)))
        logdet_new = 2 * sum(np.log(np.diag(chol_new)))

        K_diff, k_diff = K_prev - K_new, k_prev - k_new
        mu, sigma = new_mu[t, :dX], new_sigma[t, :dX, :dX]

        kl_div[t] = max(
                0,
                0.5 * (logdet_prev - logdet_new - new_traj_distr.dU +
                       np.sum(np.diag(inv_prev.dot(sig_new))) +
                       k_diff.T.dot(inv_prev).dot(k_diff) +
                       mu.T.dot(K_diff.T).dot(inv_prev).dot(K_diff).dot(mu) +
                       np.sum(np.diag(K_diff.T.dot(inv_prev).dot(K_diff).dot(sigma))) +
                       2 * k_diff.T.dot(inv_prev).dot(K_diff).dot(mu))
        )

    return np.sum(kl_div) if tot else kl_div
def traj_distr_kl_alt ( new_mu , new_sigma , new_traj_distr , prev_traj_distr , tot = True ) : T , dX , dU = new_mu . shape [ 0 ] , new_traj_distr . dX , new_traj_distr . dU kl_div = np . zeros ( T ) for t in range ( T ) : K_prev = prev_traj_distr . K [ t , : , : ] K_new = new_traj_distr . K [ t , : , : ] k_prev = prev_traj_distr . k [ t , : ] k_new = new_traj_distr . k [ t , : ] sig_prev = prev_traj_distr . pol_covar [ t , : , : ] sig_new = new_traj_distr . pol_covar [ t , : , : ] chol_prev = prev_traj_distr . chol_pol_covar [ t , : , : ] chol_new = new_traj_distr . chol_pol_covar [ t , : , : ] inv_prev = prev_traj_distr . inv_pol_covar [ t , : , : ] inv_new = new_traj_distr . inv_pol_covar [ t , : , : ] logdet_prev = 2 * sum ( np . log ( np . diag ( chol_prev ) ) ) logdet_new = 2 * sum ( np . log ( np . diag ( chol_new ) ) ) K_diff , k_diff = K_prev - K_new , k_prev - k_new mu , sigma = new_mu [ t , : dX ] , new_sigma [ t , : dX , : dX ] kl_div [ t ] = max ( 0 , 0.5 * ( logdet_prev - logdet_new - new_traj_distr . dU + np . sum ( np . diag ( inv_prev . dot ( sig_new ) ) ) + k_diff . T . dot ( inv_prev ) . dot ( k_diff ) + mu . T . dot ( K_diff . T ) . dot ( inv_prev ) . dot ( K_diff ) . dot ( mu ) + np . sum ( np . diag ( K_diff . T . dot ( inv_prev ) . dot ( K_diff ) . dot ( sigma ) ) ) + 2 * k_diff . T . dot ( inv_prev ) . dot ( K_diff ) . dot ( mu ) ) ) return np . sum ( kl_div ) if tot else kl_div