import torch
import math


def cos_sin(X, S):
    """
       Computes cosine-sine Fourier features
       
       Parameters:
           X (torch.Tensor): N-by-D inputs
           S (torch.Tensor): m-by-D frequencies
       Returns:
           torch.Tensor: M-by-N matrix of features for the inputs, M=2m.
    """
    m = S.shape[-2]
    dot_products = torch.matmul(S, X.t())
    return torch.cat((torch.cos(dot_products), torch.sin(dot_products)), dim=-2) / math.sqrt(m)