def sparse_orth(d):
    """ Constructs a sparse orthogonal matrix.
    
    The method is described in:
    Gi-Sang Cheon et al., Constructions for the sparsest orthogonal matrices,
    Bull. Korean Math. Soc 36 (1999) No.1 pp.199-129
    """
    from scipy.sparse import eye
    from scipy import r_, pi, sin, cos
    if d%2 == 0:
        seq = r_[0:d:2,1:d-1:2]
    else:
        seq = r_[0:d-1:2,1:d:2]
    Q = eye(d,d).tocsc()
    for i in seq:
        theta = random() * 2 * pi
        flip = (random() - 0.5)>0;
        Qi = eye(d,d).tocsc()
        Qi[i,i] = cos(theta)
        Qi[(i+1),i] = sin(theta)
        if flip > 0:
            Qi[i,(i+1)] = -sin(theta)
            Qi[(i+1),(i+1)] = cos(theta)
        else:
            Qi[i,(i+1)] = sin(theta)
            Qi[(i+1),(i+1)] = -cos(theta)            
        Q = Q*Qi;
    return Q
dataset/ETHPy150Open pybrain/pybrain/pybrain/utilities.py/sparse_orth/original