예제 #1
0
def display(n=10):
    s1 = [n-i for i in range(n)]+[0 for i in range(n-1)]
    # build causal right circulant matrix
    # np.matrix rather than np.ndarray could perform matrix mulitplication
    s1 = np.matrix(np.flipud(np.fliplr(circulant(s1)[n-1:,:])))
    #s2 = np.matrix(s1).T
    # using a diffrent and random matrix for generality
    s2 = [np.random.randint(1,20) for i in range(n)]+[0 for i in range(n-1)]
    s2 = np.matrix(np.flipud(np.fliplr(circulant(s2)[n-1:,:]))).T

    print s1
    print "\n", s2
    print "\n", s1*s2
예제 #2
0
def getUpwindMatrix(N, dx):
     
  #stencil    = [-1.0, 1.0]
  #zero_pos = 2
  #coeff      = 1.0
  
  #stencil    = [1.0, -4.0, 3.0]
  #coeff      = 1.0/2.0
  #zero_pos   = 3
  
  #stencil    = [1.0, -6.0, 3.0, 2.0]
  #coeff      = 1.0/6.0
  #zero_pos   = 3
  
  #stencil  = [-5.0, 30.0, -90.0, 50.0, 15.0]
  #coeff    = 1.0/60.0
  #zero_pos = 4
  
  stencil = [3.0, -20.0, 60.0, -120.0, 65.0, 12.0]
  coeff   = 1.0/60.0
  zero_pos = 5
  
  first_col = np.zeros(N)
  
  # Because we need to specific first column (not row) in circulant, flip stencil array
  first_col[0:np.size(stencil)] = np.flipud(stencil)

  # Circulant shift of coefficient column so that entry number zero_pos becomes first entry
  first_col = np.roll(first_col, -np.size(stencil)+zero_pos, axis=0)

  return sp.csc_matrix( coeff*(1.0/dx)*la.circulant(first_col) )
예제 #3
0
파일: test_basic.py 프로젝트: 7924102/scipy
 def test_basic3(self):
     # b is a 3-d matrix.
     c = np.array([1, 2, -3, -5])
     b = np.arange(24).reshape(4, 3, 2)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
예제 #4
0
파일: test_basic.py 프로젝트: 7924102/scipy
 def test_complex(self):
     # Complex b and c
     c = np.array([1+2j, -3, 4j, 5])
     b = np.arange(8).reshape(4, 2) + 0.5j
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
예제 #5
0
파일: utils.py 프로젝트: mac389/jass
def _distance_matrix(L):
    Dmax = L//2
 
    D  = range(Dmax+1)
    D += D[-2+(L%2):0:-1]
 
    return circulant(D)/Dmax
예제 #6
0
def calculation(data, m, n, activation, w):
    nrow, ncol = sorted((m, n))
    r = np.random.rand(nrow, 1)
    circul_matrix = circulant(r)
    circul_matrix = np.hstack((circul_matrix, circul_matrix[:, :ncol-nrow]))

    fft_r = np.fft.fft(r)
    fft_x = np.fft.fft(data)
    Rx = np.fft.ifft(fft_r * fft_x)
    if activation == 1:
        hx = sigmoid(Rx)
    elif activation == 2:
        hx = np.tanh(Rx)

    FP = hx
    rev_x = np.flipud(data)
    s_rev_x = np.roll(rev_x, 1, axis=0)
    if activation == 1:
        dhx = hx * (1 - hx)
    elif activation == 2:
        dhx = 1 - hx**2

    fft_s_rev_x = np.fft.fft(s_rev_x.transpose())
    fft_wT_rox = np.fft.fft((w * dhx).transpose())
    BP = np.fft.ifft((fft_s_rev_x * fft_wT_rox).transpose())

    return circul_matrix, FP, BP
예제 #7
0
def spectral_diff_matrix(N,dt,diff):
  ''' 
  generates a periodic sinc differentation matrix. This is equivalent 
  
  Parameters
  ----------
    N : number of observations 
    dt : sample spacing
    diff : derivative order (max=2)

  '''
  scale = dt*N/(2*np.pi)
  dt = 2*np.pi/N
  t,h = sympy.symbols('t,h')
  sinc = sympy.sin(sympy.pi*t/h)/((2*sympy.pi/h)*sympy.tan(t/2))
  if diff == 0:
    sinc_diff = sinc
  else:
    sinc_diff = sinc.diff(*(t,)*diff)

  func = sympy.lambdify((t,h),sinc_diff,'numpy')
  times = dt*np.arange(N)
  val = func(times,dt)
  if diff == 0:
    val[0] = 1.0
  elif diff == 1:
    val[0] = 0.0
  elif diff == 2:
    val[0] = -(np.pi**2/(3*dt**2)) - 1.0/6.0

  D = circulant(val)/scale**diff
  return D
예제 #8
0
파일: tests.py 프로젝트: dfm/ski
def test_toeplitz(N=50):
    print("Testing circulant linear algebra...")
    x = np.linspace(0, 10, N)
    y = np.vstack((np.sin(x), np.cos(x), x, x**2)).T
    c_row = np.exp(-0.5 * x ** 2)
    c_row[0] += 0.1
    cnum = circulant(c_row)
    cmat = CirculantMatrix(c_row)

    # Test dot products.
    assert np.allclose(np.dot(cnum, y[:, 0]), cmat.dot(y[:, 0]))
    assert np.allclose(np.dot(cnum, y), cmat.dot(y))

    # Test solves.
    assert np.allclose(np.linalg.solve(cnum, y[:, 0]), cmat.solve(y[:, 0]))
    assert np.allclose(np.linalg.solve(cnum, y), cmat.solve(y))

    # Test eigenvalues.
    ev = np.linalg.eigvals(cnum)
    ev = ev[np.argsort(np.abs(ev))[::-1]]
    assert np.allclose(np.abs(cmat.eigvals()), np.abs(ev))

    print("Testing Toeplitz linear algebra...")
    tnum = toeplitz(c_row)
    tmat = ToeplitzMatrix(c_row)

    # Test dot products.
    assert np.allclose(np.dot(tnum, y[:, 0]), tmat.dot(y[:, 0]))
    assert np.allclose(np.dot(tnum, y), tmat.dot(y))

    # Test solves.
    assert np.allclose(np.linalg.solve(tnum, y[:, 0]),
                       tmat.solve(y[:, 0], tol=1e-12, verbose=True))
    assert np.allclose(np.linalg.solve(tnum, y),
                       tmat.solve(y, tol=1e-12, verbose=True))
예제 #9
0
def C(size):
    
    firstrow = np.zeros(size)
    firstrow[-1] = -1
    firstrow[0] = 2
    firstrow[1] = -1
    
    return linalg.circulant(firstrow)
예제 #10
0
def Dc(size): #periodic first derivative
    
    firstrow = np.zeros(size)
    firstrow[-1] = -1
    firstrow[0] = 1

    
    return linalg.circulant(firstrow)
def generate_all_shifts(atom, signal_atom_diff):
    """ Shifted version of a matrix with the number
    of shifts being signal_atom_diff
    the atom is a vector and the shifted versions 
    are the rows
    extra zeros are tacked on
    """
    return circulant(np.hstack((atom, np.zeros(signal_atom_diff)))).T[:signal_atom_diff+1]
예제 #12
0
파일: test_basic.py 프로젝트: 7924102/scipy
 def test_random_b_and_c(self):
     # Random b and c
     np.random.seed(54321)
     c = np.random.randn(50)
     b = np.random.randn(50)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
예제 #13
0
파일: test_basic.py 프로젝트: 7924102/scipy
 def test_singular(self):
     # c gives a singular circulant matrix.
     c = np.array([1, 1, 0, 0])
     b = np.array([1, 2, 3, 4])
     x = solve_circulant(c, b, singular='lstsq')
     y, res, rnk, s = lstsq(circulant(c), b)
     assert_allclose(x, y)
     assert_raises(LinAlgError, solve_circulant, x, y)
예제 #14
0
파일: gibbs.py 프로젝트: hallliu/s2014
def est_covs(samples, epsilon):
    c1 = samples[:,:1000]
    cf = samples[:,-1000:]
    
    r1 = np.array([2+epsilon, -1, 0, -1])
    Q = sp.circulant(r1)

    return 4-np.trace(np.dot(Q,np.cov(c1))), 4-np.trace(np.dot(Q, np.cov(cf))), 4-np.trace(np.dot(Q,np.cov(samples)))
예제 #15
0
파일: gibbs.py 프로젝트: hallliu/s2014
def make_gibbs_hist(epsilon):
    total_rhos = np.zeros(100)

    r1 = np.array([2+epsilon, -1, 0, -1])
    Q = sp.circulant(r1)
    for i in range(100):
        sample = run_gibbs_normal(10000, epsilon)
        total_rhos[i] = 4-np.trace(np.dot(Q, np.cov(sample)))
    plt.hist(total_rhos)
예제 #16
0
파일: gibbs.py 프로젝트: hallliu/s2014
def make_direct_hist(epsilon):
    total_rhos = np.zeros(100)

    r1 = np.array([2+epsilon, -1, 0, -1])
    Q = sp.circulant(r1)
    tm = sp.inv(sp.cholesky(Q))
    for i in range(100):
        std_sample = np.random.randn(4,10000)
        sample = np.dot(tm, std_sample)
        total_rhos[i] = 4 - np.trace(np.dot(Q, np.cov(sample)))
    plt.hist(total_rhos)
예제 #17
0
 def prepare_autoregression_coefficients(self,CF):
     '''Calculate autoregression coefficients (cVec) and variance of 
     additional noise term (sigma**2).
     Is it possible to simplify the matrix inverse in case of a circulant
     matrix?'''
     B = circulant(CF[:-1]) # up to k-1
     Binv = np.linalg.inv(B) # invert matrix
     cVec = np.dot(Binv,CF[1:]) # from 1 to k
     sigma = np.sqrt(CF[0] - np.dot(cVec,CF[1:]))
     
     return sigma, cVec
예제 #18
0
def gradient(f, x=None, dx=1, axis=-1):
    """
    Return the gradient of 1 or 2-dimensional array.
    The gradient is computed using central differences in the interior
    and first differences at the boundaries.

    Irregular sampling is supported (it isn't supported by np.gradient)

    Parameters
    ----------
    f : 1d or 2d numpy array
        Input array.
    x : array_like, optional
       Points where the function f is evaluated. It must be of the same
       length as ``f.shape[axis]``.
       If None, regular sampling is assumed (see dx)
    dx : float, optional
       If `x` is None, spacing given by `dx` is assumed. Default is 1.
    axis : int, optional
       The axis along which the difference is taken.

    Returns
    -------
    out : array_like
        Returns the gradient along the given axis.

    Notes
    -----
    To-Do: implement smooth noise-robust differentiators for use on experimental data.
    http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
    """
    
    if x is None:
        x = np.arange(f.shape[axis]) * dx
    else:
        assert x.shape[0] == f.shape[axis]
    I = np.zeros(f.shape[axis])
    I[:2] = np.array([0, -1])
    I[-1] = 1
    I = circulant(I)
    I[0, 0] = -1
    I[-1, -1] = 1
    I[0, -1] = 0
    I[-1, 0] = 0
    H = np.zeros((f.shape[axis], 1))
    H[1:-1, 0] = x[2:] - x[:-2]
    H[0] = x[1] - x[0]
    H[-1] = x[-1] - x[-2]
    if axis == 0:
        return np.dot(I / H, f)
    else:
        return np.dot(I / H, f.T).T
예제 #19
0
 def __init__(self, a, nu, alpha, v0, xaxis):
   self.a     = a
   self.nu    = nu
   self.alpha = alpha
   self.v0    = v0
   self.xaxis = xaxis
   self.dim   = np.size(xaxis)
   self.dx    = xaxis[1] - xaxis[0]
   e     = np.zeros(self.dim)
   e[0]  = -2.0
   e[1]  = 1.0
   e[-1] = 1.0
   self.S  = sp.csc_matrix(spla.circulant(e))
   self.S *= (self.nu/self.dx**2)
예제 #20
0
    def observation(self, obs):
        new_obs = {}
        for k, v in obs.items():
            if 'mask' in k:
                new_obs[k] = self._process_masks(obs[k], self_mask=(k in self.keys_self))
            elif k in self.keys_self:
                new_obs[k + '_self'] = obs[k]
                new_obs[k] = obs[k][circulant(np.arange(self.n_agents))]
                new_obs[k] = new_obs[k][:, 1:, :]  # Remove self observation
            elif k in self.keys_copy:
                new_obs[k] = obs[k]
            else:
                new_obs[k] = np.tile(v, self.n_agents).reshape([v.shape[0], self.n_agents, v.shape[1]]).transpose((1, 0, 2))

        return new_obs
예제 #21
0
파일: core.py 프로젝트: zhucer2003/DAPPER
def typical_init_params(m):
  """
  Approximate (3 degrees of acf of) climatology.
  Obtained for F=8, m=40.

  NB: Should not be used for X0 because it's like
  starting the filter from a state of divergence,
  which might be too challenging to particle filters.
  The code has been left here for legacy reasons.
  """
  mu0 = 2.34*np.ones(m)
  # Auto-cov-function
  acf = lambda i: 0.0 + 14*(i==0) + 0.9*(i==1) - 4.7*(i==2) - 1.2*(i==3)
  P0  = circulant(acf(periodic_distance_range(m)))
  return mu0, P0
예제 #22
0
파일: test_basic.py 프로젝트: 7924102/scipy
    def test_axis_args(self):
        # Test use of caxis, baxis and outaxis.

        # c has shape (2, 1, 4)
        c = np.array([[[-1, 2.5, 3, 3.5]], [[1, 6, 6, 6.5]]])

        # b has shape (3, 4)
        b = np.array([[0, 0, 1, 1], [1, 1, 0, 0], [1, -1, 0, 0]])

        x = solve_circulant(c, b, baxis=1)
        assert_equal(x.shape, (4, 2, 3))
        expected = np.empty_like(x)
        expected[:, 0, :] = solve(circulant(c[0]), b.T)
        expected[:, 1, :] = solve(circulant(c[1]), b.T)
        assert_allclose(x, expected)

        x = solve_circulant(c, b, baxis=1, outaxis=-1)
        assert_equal(x.shape, (2, 3, 4))
        assert_allclose(np.rollaxis(x, -1), expected)

        # np.swapaxes(c, 1, 2) has shape (2, 4, 1); b.T has shape (4, 3).
        x = solve_circulant(np.swapaxes(c, 1, 2), b.T, caxis=1)
        assert_equal(x.shape, (4, 2, 3))
        assert_allclose(x, expected)
예제 #23
0
파일: test_basic.py 프로젝트: tosh1ki/scipy
    def test_axis_args(self):
        # Test use of caxis, baxis and outaxis.

        # c has shape (2, 1, 4)
        c = np.array([[[-1, 2.5, 3, 3.5]], [[1, 6, 6, 6.5]]])

        # b has shape (3, 4)
        b = np.array([[0, 0, 1, 1], [1, 1, 0, 0], [1, -1, 0, 0]])

        x = solve_circulant(c, b, baxis=1)
        assert_equal(x.shape, (4, 2, 3))
        expected = np.empty_like(x)
        expected[:, 0, :] = solve(circulant(c[0]), b.T)
        expected[:, 1, :] = solve(circulant(c[1]), b.T)
        assert_allclose(x, expected)

        x = solve_circulant(c, b, baxis=1, outaxis=-1)
        assert_equal(x.shape, (2, 3, 4))
        assert_allclose(np.rollaxis(x, -1), expected)

        # np.swapaxes(c, 1, 2) has shape (2, 4, 1); b.T has shape (4, 3).
        x = solve_circulant(np.swapaxes(c, 1, 2), b.T, caxis=1)
        assert_equal(x.shape, (4, 2, 3))
        assert_allclose(x, expected)
예제 #24
0
def LNLNP(
    sigma=1.0,  # stimulus standard deviation
    NL=sin,  # subunit nonlinearity
    N=10,  # number of cones, subunits & RGCs
    Sigma=1.5,  # subunit & RGC connection width
    firing_rate=0.1,  # RGC firing rate
    T=1000000,
):  # number of time samples
    """Simulate spiking data for a Linear-Nonlinear-Linear-Nonlinear-Poisson model.
    Then use this data to calculate the STAs and STCs.
    """

    spatial = fromfunction(lambda x: exp(-0.5 * ((x - N / 2) / Sigma) ** 2), (N,))
    U = L.circulant(spatial)  # connections btw cones & subunits
    V = U  # connections btw subunits & RGCs

    U = U[0:N:3, :]
    V = V[0:N:4, 0:N:3]
    NRGC = V.shape[0]

    U = U / sqrt(sum(U * U, axis=1))[:, newaxis]
    V = V / sqrt(sum(V * V, axis=1))[:, newaxis]

    X = sigma * R.randn(N, T)  # cone activations
    bb = NL(dot(U, X))  # subunit activations
    Y = exp(dot(V, bb))  # RGC activations
    Y = Y * NRGC * T * firing_rate / sum(Y)

    print "std( X ) = ", std(X)
    print "std( U X ) = ", std(dot(U, X))
    print "std( V b ) = ", std(dot(V, bb))

    spikes = R.poisson(Y)
    N_spikes = sum(spikes, 1)

    STA = [sum(X * spikes[i, :], 1) / N_spikes[i] for i in arange(NRGC)]

    STC = [
        dot(X - STA[i][:, newaxis], transpose((X - STA[i][:, newaxis]) * Y[i, :])) / N_spikes[i] for i in arange(NRGC)
    ]

    bbar = sum(bb, axis=1) / bb.shape[1]

    Cb = dot((bb - bbar[:, newaxis]), (bb - bbar[:, newaxis]).T) / bb.shape[1]

    STAB = [sum(bb * spikes[i, :], 1) / N_spikes[i] for i in arange(NRGC)]

    return ((N_spikes, STA, STC), U, V, bbar, Cb, STAB)
def Order(u, dz):
    Phis = np.arctan(u[:, :, 1] / u[:, :, 0])
    dummy = np.zeros(N)
    index = np.arange(N)
    dummy[index <= dz] = 1
    dummy[len(index) - index <= dz] = 1
    Rangetrix = sp.circulant(dummy)
    del dummy, index
    if Multiplex == True:
        Rangetrix = np.bmat([[Rangetrix, np.zeros((N, N))],
                             [np.zeros((N, N)), Rangetrix]]).A
    Z = np.exp(1j * Phis)
    Z = np.array([np.dot(Rangetrix, Vector) for Vector in Z])
    Z /= (2 * dz + 1)
    Z = np.abs(Z)
    return Z
예제 #26
0
 def _process_masks(self, mask_obs, self_mask=False):
     '''
         mask_obs will be a (n_agent, n_object) boolean matrix. If the mask is over non-agent
             objects then we do nothing. If the mask is over other agents (self_mask is True),
             then we permute each row such that the mask is consistent with the circulant
             permutation used for self observations
     '''
     new_mask = mask_obs.copy()
     if self_mask:
         assert np.all(new_mask.shape == np.array((self.n_agents, self.n_agents)))
         # Permute each row to the right by one more than the previous
         # E.g., [[1,2],[3,4]] -> [[1,2],[4,3]]
         idx = circulant(np.arange(self.n_agents))
         new_mask = new_mask[np.arange(self.n_agents)[:, None], idx]
         new_mask = new_mask[:, 1:]  # Remove self observation
     return new_mask
예제 #27
0
def circulant_ACF(C, do_abs=False):
    """Compute the auto-covariance-function corresponding to `C`.

    This assumes it is the cov/corr matrix of a 1D periodic domain.
    """
    M = len(C)
    # cols = np.flipud(sla.circulant(np.arange(M)[::-1]))
    cols = sla.circulant(np.arange(M))
    ACF = np.zeros(M)
    for i in range(M):
        row = C[i, cols[i]]
        if do_abs:
            row = abs(row)
        ACF += row
        # Note: this actually also accesses masked values in C.
    return ACF / M
예제 #28
0
 def generic_block_toep(self, beam_j):
     pad_zero = np.zeros(beam_j.shape[0], dtype=np.complex128)
     nb = beam_j.shape[0]
     finshape = 2 * nb - 1
     block_matrix = []
     circ_ndx = linalg.circulant(np.arange(finshape))[:, 0:nb]
     for i in range(finshape):
         if i < nb:
             block_matrix.append(self.padded_circulant(beam_j[i]))
         else:
             block_matrix.append(self.padded_circulant(pad_zero))
     blocked = np.array(block_matrix)
     unshape_circ = blocked[circ_ndx]
     shaped_circ = unshape_circ.transpose(0, 2, 1,
                                          3).reshape(finshape**2, nb**2)
     return shaped_circ
예제 #29
0
def adj_matrix_directed_ring(N, c=0):
    """Returns the adjacency matrix of a ring graph.

	Parameters
	----------
	N: int
		Number of graph nodes.
	c: array
		First column of the adjacency matrix. It carries the edge weights.

	"""
    if c == 0:  # case in which the edge weights were not entered. Then, they are made equal to 1.
        c = np.zeros(N)
        c[1] = 1
    A = linalg.circulant(c)
    return A
예제 #30
0
    def get_force_matrices(self):
        ''' Produce matrices to interpolate the force from segments and 
		collocation points to grid points '''

        M, K = self.M, self.K
        Iseg = np.zeros((K, M))
        Icoll = np.zeros((K, M))

        Iseg[:M, :] = np.eye(M)
        #Icoll
        wvcoll = np.zeros((K, ))
        wvcoll[0] = 1. - self.perc_interp
        wvcoll[1] = self.perc_interp
        Icoll = scalg.circulant(wvcoll)[:, :M]

        return Iseg, Icoll
예제 #31
0
def get_binary_drop_mask(indexes, max_x, max_y, percent_taking_ind=0.25):
    num_to_select = int(len(indexes) * percent_taking_ind)
    string = ['0'] * max_x * max_y
    list_of_random_items = random.sample(indexes, num_to_select)
    for i in list_of_random_items:
        string[i] = '1'

    string = "".join(string)

    x = max_x / 2
    y = max_y / 2

    center_index = get_index(y, x, max_x)
    ind_string = string[center_index:] + string[:center_index]
    ind_for_matrix = [int(i) for i in ind_string]
    return circulant(ind_for_matrix)
예제 #32
0
def jl_transform(dataset_in, objective_dim, type_transform="basic"):
    """
    This function takes the dataset_in and returns the reduced dataset. The
    output dimension is objective_dim.

    dataset_in -- original dataset, list of Numpy ndarray
    objective_dim -- objective dimension of the reduction
    type_transform -- type of the transformation matrix used.
    If "basic" (default), each component of the transformation matrix
    is taken at random in N(0,1).
    If "discrete", each component of the transformation matrix
    is taken at random in {-1,1}.
    If "circulant", he first row of the transformation matrix
    is taken at random in N(0,1), and each row is obtainedfrom the
    previous one by a one-left shift.
    If "toeplitz", the first row and column of the transformation
    matrix is taken at random in N(0,1), and each diagonal has a
    constant value taken from these first vector.
    """
    if type_transform.lower() == "basic":
        jlt = (1 / math.sqrt(objective_dim)) * np.random.normal(
            0, 1, size=(objective_dim, len(dataset_in[0])))
    elif type_transform.lower() == "discrete":
        jlt = (1 / math.sqrt(objective_dim)) * np.random.choice(
            [-1, 1], size=(objective_dim, len(dataset_in[0])))
    elif type_transform.lower() == "circulant":
        from scipy.linalg import circulant
        first_row = np.random.normal(0, 1, size=(1, len(dataset_in[0])))
        jlt = ((1 / math.sqrt(objective_dim)) *
               circulant(first_row))[:objective_dim]
    elif type_transform.lower() == "toeplitz":
        from scipy.linalg import toeplitz
        first_row = np.random.normal(0, 1, size=(1, len(dataset_in[0])))
        first_column = np.random.normal(0, 1, size=(1, objective_dim))
        jlt = ((1 / math.sqrt(objective_dim)) *
               toeplitz(first_column, first_row))
    else:
        print('Wrong transformation type')
        return None

    trans_dataset = []
    [
        trans_dataset.append(np.dot(jlt, np.transpose(dataset_in[i])))
        for i in range(len(dataset_in))
    ]

    return np.asarray(trans_dataset)
예제 #33
0
 def __init__(self, T, lam, input_size, code_size, kernel_size, kernels):
     """Model from Constrained Recurrent Sparse Auto-Encoders paper"""
     super(CRsAE1d, self).__init__()
     self.T = T
     self.input_size = input_size
     self.code_size = code_size
     self.kernel_size = kernel_size
     self.kernels = kernels
     self.local_dictionary = nn.Parameter(torch.randn(kernels, kernel_size))    # self.H is the decoder, torch.transpose(self.H) is the encoder   #TODO: maybe 2d convolution?
     filters = self.local_dictionary.tolist()
     self.H = torch.tensor(np.concatenate([circulant(filter + [0]*(self.input_size - self.kernel_size)) for filter in filters], axis=1))
     print(self.H)
     print("H's shape on creation: {}".format(self.H.shape))
     eigenvalues, _ = torch.eig(torch.mm(torch.t(self.H), self.H))
     max_eigenvalue = torch.max(eigenvalues)
     self.L = max_eigenvalue.item() + 1       # choice of value is arbitrary, might wanna change that later
     self.lam = lam
예제 #34
0
def _MakeBlockCirculantMatrix(h):
    # Constructs block circulant matrix
    # h - convolution kernel (linear degradation model)

    N1, N2 = h.shape
    H_row = np.zeros((N1 * N2, N2))
    Hbc = np.zeros((N1 * N2, N1 * N2))

    for ii in range(N1):
        temp = circulant(h[ii]).T
        H_row[ii * N2:ii * N2 + N2, :] = temp

    for ii in range(N1):
        temp = np.roll(H_row, axis=0, shift=ii * N2)
        Hbc[:, ii * N2:ii * N2 + N2] = temp

    return Hbc
예제 #35
0
    def compute_grad(self, ret_comps=False):
        mc, ac_os, tau_os = .0, .0, .0

        # Compute for all rollouts.
        # All are lists with length = number of rollouts.
        # qs_env, vs = self._ae.qfns(self._ro, ret_vfns=True)
        # vs = [v[:-1] for v in vs]  # leave out the last one

        # Do it for each rollout, in order to avoid memory overflow,
        # and the actual reason is to make coding easier.
        # pdb.set_trace()
        if self.cv_onestep_weighting:
            onestep_ws = self._ae.weights(self._ro, policy=self.policy)
        else:
            onestep_ws = np.ones(len(self._ro))
        for i, r in enumerate(self._ro.rollouts):
            decay = self.ae._pe.gamma * self.delta
            ws = decay**np.arange(len(r))
            Ws = np.triu(la.circulant(ws).T,
                         k=0)  # XXX WITHOUT the diagonal terms!!!!
            qs = np.ravel(np.matmul(Ws, r.rws[:, None]))
            gd = self.prepare_grad_data(r)
            mc += self.policy.nabla_logp_f(r.obs_short, r.acs, qs)
            # CV for the first action, state (action) dependent CV.
            ac_os += self.policy.nabla_logp_f(
                r.obs_short, r.acs, gd.qs * onestep_ws[i]) - gd.grad_exp_qs
            # CV for the future trajectory (for each t: \delta Q_{t+1} + ... + \delta^{step} Q_{t+step})
            # Note that it starts from t+1.
            if len(np.array(gd.Ws).shape) == 0:
                tau_cvs = gd.Ws * (gd.qs * onestep_ws[i] - gd.exp_qs)
            else:
                tau_cvs = np.ravel(
                    np.matmul(gd.Ws,
                              (gd.qs * onestep_ws[i] - gd.exp_qs)[:, None]))
            tau_os += self.policy.nabla_logp_f(r.obs_short, r.acs, tau_cvs)

        # Average.
        mc /= len(self._ro)
        ac_os /= len(self._ro)
        tau_os /= len(self._ro)

        g = -(mc - (ac_os + tau_os))  # gradient ascent
        if ret_comps:
            return g, mc, ac_os, tau_os
        else:
            return g
예제 #36
0
def make_L(N,p,q):
    while True:
        try:
            f, Fp, Fq, g, h = generate(N,p,q)
            L = np.zeros((2*N,2*N), dtype=float)
            L[:N,:N] = np.eye(N)
            L[N:,N:] = q * np.eye(N)
            # print('h = ')
            # print(h)
            L[:N,N:] = circulant(h)
            break
        except ValueError:
            pass
        except Exception as e:
            raise e

    return L
예제 #37
0
def _tilt_slice_matrix(matrix, slice_shift, slice_width, tune=0):
    """
    Tilts and slices the ``matrix``
    Tilting means shifting each column upwards one step more than the previous columnns, i.e.

    a a a a a       a b c d
    b b b b b       b c d e
    c c c c c  -->  c d e f
    ...             ...
    y y y y y       y z a b
    z z z z z       z a b c
    """
    invrange = matrix.shape[0] - 1 - np.arange(matrix.shape[0])
    matrix[matrix.shape[0] - slice_shift:, :slice_shift] += tune
    matrix[:slice_shift, matrix.shape[1] - slice_shift:] -= tune
    return np.roll(matrix[np.arange(matrix.shape[0]), circulant(invrange)[invrange]],
                   slice_shift, axis=0)[:slice_width]
예제 #38
0
def tilt_slice_matrix(matrix, slice_shift, slice_width, tune=0):
    """Tilts and slices the ``matrix``

    Tilting means shifting each column upwards one step more than the previous columnns, i.e.

    a a a a a       a b c d
    b b b b b       b c d e
    c c c c c  -->  c d e f
    ...             ...
    y y y y y       y z a b
    z z z z z       z a b c

    """
    invrange = matrix.shape[0] - 1 - np.arange(matrix.shape[0])
    matrix[matrix.shape[0] - slice_shift:, :slice_shift] += tune
    matrix[:slice_shift, matrix.shape[1] - slice_shift:] -= tune
    return np.roll(matrix[np.arange(matrix.shape[0]), circulant(invrange)[invrange]],
                   slice_shift, axis=0)[:slice_width]
예제 #39
0
def get_effective_medium_eigenvalue_gap_from_matrix(N, k_over_2, beta):

    assert_parameters(N, k_over_2, beta)

    pS, pL = get_connection_probabilities(N, k_over_2, beta)

    N = int(N)
    k_over_2 = int(k_over_2)
    k = int(2 * k_over_2)

    P = np.array([0.0] + [pS] * k_over_2 + [pL] *
                 (N - 1 - k) + [pS] * k_over_2) / k
    P = circulant(P)

    omega = np.sort(np.linalg.eigvalsh(P))
    omega_N_m_1 = omega[-2]

    return 1 - omega_N_m_1
예제 #40
0
 def __init__(self,
              qs,
              exp_qs,
              grad_exp_qs,
              decay=None,
              stop_cv_step=None):
     self.qs = qs  # T
     self.exp_qs = exp_qs  # T
     self.grad_exp_qs = grad_exp_qs  # d (already sum over the trajectory)
     if decay is not None:
         ws = decay**np.arange(len(r))
         if stop_cv_step is not None:
             ws[min(stop_cv_step, len(r)):] = 0
         Ws = np.triu(la.circulant(ws).T,
                      k=1)  # XXX WITHOUT the diagonal terms!!!!
     else:
         Ws = 1.0
     self.Ws = Ws  # T * T
예제 #41
0
def deconv_nonparam_alg_tikhonov_svd(array_ct_value_aif, array_ct_value_tissue, lamdaa):
    """Deconvolution-based / Non-parametric / Tikhonov SVD
    """
    a = array_ct_value_aif
    c = array_ct_value_tissue

    a_pad = np.pad(a, (0, len(a)))
    c_pad = np.pad(c, (0, len(a)))

    I = np.identity(2 * len(a))

    A = circulant(a_pad)
    A_T = A.T

    block_cirMat = np.dot(A_T, A) + (lamdaa ** 2) * I
    b = solve(block_cirMat, A_T @ c_pad)

    b = b[0:len(a)]
    return b
예제 #42
0
파일: gibbs.py 프로젝트: hallliu/s2014
def run_gibbs_normal(n_samp, epsilon):
    r1 = np.array([2+epsilon, -1, 0, -1])
    prec = sp.circulant(r1)
    samples = np.zeros((4, n_samp), dtype='float64')
    running_sample = np.zeros(4, dtype='float64')

    # Precompute 40k std normal samples
    std_normal_samples = np.random.randn(4*n_samp)
    
    # Run 10000 iterations over all 4 coordinates
    for i in range(n_samp):
        for j in range(4):
            mean = np.dot(prec[j], running_sample) - prec[j, j]*running_sample[j]
            mean /= -prec[j, j]
            std = np.sqrt(1/prec[j, j])
            running_sample[j] = std_normal_samples[i*4+j] * std + mean
        samples[:, i] = running_sample

    return samples
예제 #43
0
def svd_denoising(array_1d):
    """
    """
    matrix_cir = circulant(array_1d)
    matrix_cir_sparse = sparse_matrix(matrix_cir)
    u, sigma, vt = np.linalg.svd(matrix_cir_sparse)

    threshold = sigma[2] * 0.10

    sigma_denoised = np.zeros([len(array_1d), len(array_1d)])
    for i in range(len(array_1d)):
        if sigma[i] > threshold:
            sigma_denoised[i][i] = sigma[i]
        else:
            sigma_denoised[i][i] = 0

    matrix_cir_sparse_new = np.dot(np.dot(u, sigma_denoised), vt)
    array_1d_new = matrix_cir_sparse_new[:, 0]
    return array_1d_new
예제 #44
0
    def get_force_matrices(self):
        ''' Produce matrices to interpolate the force from segments and 
		collocation points to grid points.

		@warning: method not used in this implementation of the exact sol. but
		used for linearised sol.
		'''

        M, K = self.M, self.K
        Iseg = np.zeros((K, M))
        Icoll = np.zeros((K, M))

        Iseg[:M, :] = np.eye(M)
        #Icoll
        wvcoll = np.zeros((K, ))
        wvcoll[0] = 1. - self.perc_interp
        wvcoll[1] = self.perc_interp
        Icoll = scalg.circulant(wvcoll)[:, :M]

        return Iseg, Icoll
예제 #45
0
    def adv(self, c, V, done, w=1., lambd=None, gamma=None):
        """
            A_t = TD_t + decay TD_{t+1} + decay^2 TD_{t+2} + ...

            where decay = lambd*gamma, and TD_t = c_t + delta*v_{t+1} - v_t
        """

        assert c.shape == V.shape
        assert type(done) is bool
        if isinstance(w, np.ndarray):
            assert (len(w) == len(c) - 1) and len(w.shape) <= 1
        lambd = self.lambd if lambd is None else lambd
        gamma = self.gamma if gamma is None else gamma

        delta_lambd = self.delta * lambd
        td = self.reshape_cost(c, V, done, w=1.0)
        decay = delta_lambd**np.arange(len(td))
        decay = np.triu(la.circulant(decay).T, k=0)
        adv = np.ravel(np.matmul(decay, td[:, None]))
        return (gamma**np.arange(len(adv))) * adv
예제 #46
0
def Generate_Spatial_Operators(x_mesh, scheme, derivation_order):

    N = x_mesh.size
    # you should pre-allocate a sparse matrix predicting already the number of non-zero entries
    circulating_row = np.zeros(N, )

    if scheme == "2nd-order-central":

        # generating computational molecule
        x_stencil = x_mesh[:3]  # first three points
        x_eval = x_mesh[1]
        weights = Generate_Weights(x_stencil, x_eval, derivation_order)
        circulating_row[-1] = weights[0]
        circulating_row[0] = weights[1]
        circulating_row[1] = weights[2]

    if scheme == "2nd-order-upwind":  # also called QUICK

        # generating computational molecule
        x_stencil = x_mesh[:3]  # first three points
        x_eval = x_mesh[2]
        weights = Generate_Weights(x_stencil, x_eval, derivation_order)
        circulating_row[-2] = weights[0]
        circulating_row[-1] = weights[1]
        circulating_row[0] = weights[2]

    if scheme == "1st-order-upwind":

        # assuming advection velocity is positive, c>0
        # generating computational molecule
        x_stencil = x_mesh[:2]  # first two points
        x_eval = x_mesh[1]
        weights = Generate_Weights(x_stencil, x_eval, derivation_order)
        circulating_row[-1] = weights[0]
        circulating_row[0] = weights[1]

    A_circulant = scylinalg.circulant(circulating_row)
    A = A_circulant.transpose()

    # convering to csr format, which appears to be more efficient for matrix operations
    return scysparse.csr_matrix(A)
예제 #47
0
def unitary_determinant_test(dim=2, n_samples=1000):
    for i in range(n_samples):
        if True:
            vec = make_good_unitary(dim=dim).v
            print(vec)
        else:
            sp = spa.SemanticPointer(dim)
            sp.make_unitary()
            vec = sp.v
        if not np.allclose(np.dot(circulant(vec)[0], circulant(vec)[1]), 0):
            print(np.dot(circulant(vec)[0], circulant(vec)[1]))
            print(np.linalg.det(circulant(vec)))
        # assert np.allclose(np.dot(circulant(vec)[0], circulant(vec)[1]), 0)
        assert np.allclose(np.abs(np.linalg.det(circulant(vec))), 1)
    return True
예제 #48
0
    def test_estimator_reconstruction(self):
        """
        Test the output of the optimization step

        This test estimates the matrices Gk (assuming no noise) and creates the estimator Cx.
        This test verifies Cx is a circulant matrix, and that its first column is a vector of elements
        in the complex unit circle (i.e complex numbers with magnitude 1).
        """
        seed: int = 1995
        data_type = np.float64
        tol = 1e-12
        signal_length: int = 3
        approximation_rank: int = 1
        _, _, exact_power_spectrum, g_mats, exact_cov_fourier = _test_optimization_template(
            data_type, signal_length, approximation_rank, seed)
        g_mats = np.asfortranarray(g_mats[1:])
        exact_power_spectrum = np.ascontiguousarray(exact_power_spectrum)

        diagonals: Matrix = extract_diagonals(g_mats, signal_length)
        estimated_covariance: Matrix = construct_estimator(
            diagonals, exact_power_spectrum, signal_length, 0)

        # The Hadamard product of this matrix with the exact covariance (in Fourier basis)
        # should be close to the estimator. This matrix should be circulant.
        circulant_coefficient: Matrix = np.divide(estimated_covariance,
                                                  exact_cov_fourier)
        possible_circulant_mat: Matrix = circulant(circulant_coefficient[:, 0])

        self.assertTrue(
            np.allclose(possible_circulant_mat,
                        circulant_coefficient,
                        atol=tol,
                        rtol=0),
            msg=f'The phases matrix is NOT circulant='
            f'{np.max(np.abs(possible_circulant_mat - circulant_coefficient))}'
        )
        self.assertTrue(
            np.allclose(np.abs(circulant_coefficient[:, 0]),
                        np.ones(signal_length),
                        atol=tol,
                        rtol=0))
def get_shifted_X(X, num_signals, signal_length, atom_length, num_shifts):
    """
    We need to match each shift of each atom to each signal, this means that we need
    a lot of copies of different signals

    This function takes our matrix of signals where each signal is a row
    for each atom we make a matrix with number of rows equal to the number of shifts
    
    number of shifts = signal_atom_diff + 1

    of a given signal copied, and we do that for each signal so that array has dimension
    num_signals \times num_shifts \times atom_length

    we do all the shifting and matching to the original signal since
    that way we don't have to do any shifting in our dictionary as
    that means everything is more compact when we are doing matching
    """
    signal_shift_mat = np.zeros((num_signals, num_shifts, atom_length))
    for i in xrange(num_signals):
        signal_shift_mat[i] = circulant(X[i]).T[:num_shifts, -atom_length:]
    return signal_shift_mat
예제 #50
0
def _create_prcm_gaussian(n_measurements, n_features):
    """ Method to create a partial random circulant matrix from a gaussian
    sequence.

    Parameters
    ---------------
    n_measurements : python Integer
        Number of measurements

    n_features : python Integer
        Number of features

    Returns
    -----------------
    Partial random circulant matrix as np.array of size (n_measurements, n_features).
    """
    gaussian = np.random.normal(size=(n_features))
    A = circulant(gaussian)  # This is an n_features x n_features mat
    selected_rows = np.random.randint(0, n_features, n_measurements)
    A = A[selected_rows, :]
    return A
예제 #51
0
def matrix():
    eigenvalues = []
    determinant = []
    circulant_matrix = []
    for i in range(2, 21):
        array = []
        for j in range(1, i + 1):
            l = sigma(j)
            array.append(l)

        circulant_matrix.append(circulant(array))
        eigenvalues.append(eigvals(circulant_matrix[-1]))
        determinant.append(det(np.transpose(circulant_matrix[-1])))
    i = range(2, 21)
    df = DataFrame({'n': i, 'determininant': determinant, 'eigenvalues': eigenvalues})
    df.to_excel('rough.xlsx', index=False)
    for i in range(len(determinant)):
        print(np.transpose(circulant_matrix[i]))
        print("the eigenvalues of circulant matrix  for n= " + str(i + 2) + " is " + str(eigenvalues[i]))
        print("the determinant of circulant matrix  for n= " + str(i + 2) + " is " + str(determinant[i]))
        print()
예제 #52
0
 def _neighborhood_generation(self, token_string, num_samples):
     neighborhood_data = []
     num_indexes = len(token_string)
     if self.neighbour_version == 'random_uniform':
         active_indexes = np.random.binomial(1, self.neighbour_parameter, size=(num_samples, num_indexes))
     elif self.neighbour_version == 'consecutive':
         tmp = np.zeros(num_indexes)
         tmp[0:self.neighbour_parameter] = 1
         active_indexes = circulant(tmp)[:num_samples]
     elif self.neighbour_version == 'one_on':
         active_indexes = np.eye(num_indexes)[:num_samples]
     elif self.neighbour_version == 'one_off':
         active_indexes = (np.ones(num_indexes)-np.eye(num_indexes))[:num_samples]
     elif self.neighbour_version == 'random_normal':
         active_indexes = self._normal_tokens(num_samples, num_indexes, self.neighbour_parameter)
     for act_idx in active_indexes:
         inactive = np.argwhere(act_idx == 0)
         sample = copy.deepcopy(token_string)
         for inact in inactive:
             sample[inact[0]] = self.inactive_string
         neighborhood_data.append(sample)
     return neighborhood_data, active_indexes
예제 #53
0
 def get_gaussian_wb(self):
     self.w = np.zeros((self.n_feat, self.n_input_feat))
     if self.n_feat < self.n_input_feat:
         raise Exception(
             "the dimension of projected features should be large than or equal to dimension of the raw features"
         )
     np.random.seed(self.rand_seed)
     for i in range(int(math.ceil(self.n_feat / float(self.n_input_feat)))):
         generator = np.random.normal(scale=1.0 / float(self.kernel.sigma),
                                      size=(self.n_input_feat, ))
         cir = circulant(generator)
         flip = np.diag(2 *
                        np.random.randint(0, 2, size=(self.n_input_feat)) -
                        1).astype(np.float64)
         row_start = i * self.n_input_feat
         row_end = min((i + 1) * self.n_input_feat, self.n_feat)
         self.w[row_start:row_end, :] = np.dot(cir, flip)[:row_end -
                                                          row_start, :]
     np.random.seed(self.rand_seed)
     self.b = np.random.uniform(low=0.0,
                                high=2.0 * np.pi,
                                size=(self.n_feat, 1))
예제 #54
0
def Fmat(m, c, dx, dt):
    """
  m  - System size
  c  - Velocity of wave. Wave travels to the rigth for c>0.
  dx - Grid spacing
  dt - Time step
  
  Note that the 1st-order upwind scheme used here is exact
  (vis-a-vis the analytic solution) only for dt = abs(dx/c),
  in which case it corresponds to
  np.roll(x,1,axis=x.ndim-1), i.e. circshift in Matlab.
  """
    assert abs(c * dt / dx) <= 1, "Must satisfy CFL condition"
    # 1st order explicit upwind scheme
    row1 = np.zeros(m)
    row1[-1] = +(sign(c) + 1) / 2
    row1[+1] = -(sign(c) - 1) / 2
    row1[0] = -1
    L = circulant(row1)
    F = eye(m) + (dt / dx * abs(c)) * L
    F = sparse.dia_matrix(F)
    return F
예제 #55
0
def make_controllable(T, alpha=0.01, shift_flip='shift'):
    """ Takes a transition graph and turns the `action->next state` transitions into probabilities

    Arguments:

        T : Transition tensor of type `ndarray((nactions, nstates, nstates))`
        alpha : The sharpness with which actions pick outcomes (`float > 0`)
        shift_flip : How to make differences in action-outcome contingencies between contexts. Options include `shift` and `flip`. Shifting takes the transition matrix from the prior action and shifts it, whereas the `flip` option does as its name suggests.

    """
    # TODO: Explain the `shift_flip` parameter further

    n = np.max(T.shape)
    m = T.shape[2]
    x = np.array([alpha**(i / (n - 1)) for i in range(n)])
    x = circulant(x)
    x = x @ x.T
    x = np.flip(x, axis=0)
    x = np.tile(x, [m, m])
    for k in range(T.shape[2]):
        if shift_flip == 'shift':
            y = x[k:T.shape[0] + k, -T.shape[1]:]
        elif shift_flip == 'flip':
            y = x[:T.shape[0], -T.shape[1]:]
            if k % 2 == 0:
                y = np.flip(y, axis=0)
        elif shift_flip == 'shiftandflip':
            y = x[k:T.shape[0] + k, -T.shape[1]:]
            if k % 2 == 0:
                y = np.flip(y, axis=0)
        else:
            y = x[k:T.shape[0] + k, -(T.shape[1] + k):x.shape[1] - k]
        T[:, :, k] = y * T[:, :, k]
        Tsum = np.tile(
            np.sum(T[:, :, k], axis=1).reshape(-1, 1),
            [1, T[:, :, k].shape[1]])
        T[:, :, k] = np.ma.divide(T[:, :, k], Tsum, where=Tsum != 0)
    return T
예제 #56
0
def getUpwindMatrix(N, dx, order):
  
  if order==1:
    stencil    = [-1.0, 1.0]
    zero_pos   = 2
    coeff      = 1.0
  
  elif order==2:
    stencil    = [1.0, -4.0, 3.0]
    coeff      = 1.0/2.0
    zero_pos   = 3

  elif order==3:
    stencil    = [1.0, -6.0, 3.0, 2.0]
    coeff      = 1.0/6.0
    zero_pos   = 3

  elif order==4:
    stencil  = [-5.0, 30.0, -90.0, 50.0, 15.0]
    coeff    = 1.0/60.0
    zero_pos = 4
  
  elif order==5:
    stencil  = [3.0, -20.0, 60.0, -120.0, 65.0, 12.0]
    coeff    = 1.0/60.0
    zero_pos = 5
  else:
    sys.exit('Order '+str(order)+' not implemented')

  first_col = np.zeros(N)
  
  # Because we need to specific first column (not row) in circulant, flip stencil array
  first_col[0:np.size(stencil)] = np.flipud(stencil)

  # Circulant shift of coefficient column so that entry number zero_pos becomes first entry
  first_col = np.roll(first_col, -np.size(stencil)+zero_pos, axis=0)

  return sp.csc_matrix( coeff*(1.0/dx)*la.circulant(first_col) )
예제 #57
0
파일: assembly.py 프로젝트: tbetcke/PyBEM2D
def nodalProjector(meshToBasis):
    """Create the projector onto a nodal basis"""

    segments=meshToBasis.mesh.segments # List of segments in each domain
    nsegs=[len(s) for s in segments] # Number of segments in each domain
    ne=meshToBasis.nelements
    nb=meshToBasis.nbasis

    # Create local matrices
    P1l,P2l=[],[]
    for n in nsegs:
        P2=numpy.eye(n,n,dtype=numpy.complex128)
        P1=circulant(P2[:,1])
        P1l.append(P1)
        P2l.append(P2)
    P=numpy.zeros((ne,nb),dtype=numpy.complex128)
    ind1,ind2=0,ne
    for i,n in enumerate(nsegs):
        P[ind1:ind1+n,ind1:ind1+n]=P1l[i]
        P[ind1:ind1+n,ind2:ind2+n]=P2l[i]
        ind1+=n
        ind2+=n
    return P
def generate_all_shifts(atom, signal_atom_diff):
    return circulant(np.hstack((atom, np.zeros(signal_atom_diff)))).T[:signal_atom_diff+1]
def generate_shifts(atom, num_shifts):
    if num_shifts == 1:
        return np.array([atom])
    else:
        return circulant(np.hstack((atom, np.zeros(num_shifts-1)))).T[:num_shifts][:,num_shifts-1:1-num_shifts]
예제 #60
0
 def test_basic(self):
     y = circulant([1,2,3])
     assert_array_equal(y, [[1,3,2], [2,1,3], [3,2,1]])