def cascade(s1, s2):
    """Cascade returns the cascaded sparameters of s1 and s2. s1 and s2 should be in complex list form
    [[f,S11,S12,S21,S22]...] and the returned sparameters will be in the same format. Assumes that s1,s2 have the
    same frequencies. If 1-S2_22*S1_11 is zero we add a small non zero real part or loss."""
    out_sparameters = []
    for row_index, row in enumerate(s1):
        [f1, S1_11, S1_12, S1_21, S1_22] = row
        [f2, S2_11, S2_12, S2_21, S2_22] = s2[row_index]
        if f1 != f2:
            raise TypeError(
                'Frequencies do not match! F lists must be the same')
        denominator = 1 - S1_22 * S2_11
        if denominator == complex(0, 0):
            denominator = complex(10 ** -20, 0)
        S11 = S1_11 + S2_11 * (S1_12 * S1_21) / denominator
        S12 = S1_12 * S2_12 / denominator
        S21 = S1_21 * S2_21 / denominator
        S22 = S2_22 + S1_22 * (S2_12 * S2_21) / denominator
        new_row = [f1, S11, S12, S21, S22]
        out_sparameters.append(new_row)
    return out_sparameters
