Ejemplo n.º 1
0
 def _sherman_morrison_update(self, x):
     ## x should have shape (n, 1)
     ## General idea is this, but this does it in a more efficient way:
     ## Ainv -= np.linalg.multi_dot([Ainv, x, x.T, Ainv]) / (1.0 + np.linalg.multi_dot([x.T, Ainv, x]))
     Ainv_x = np.dot(self.Ainv, x)
     coef = -1. / (1. + np.dot(x.T, Ainv_x))
     blas.dger(alpha=coef, x=Ainv_x, y=Ainv_x, a=self.Ainv.T, overwrite_a=1)
Ejemplo n.º 2
0
 def rank_one_update(self, u, v):
     # https://timvieira.github.io/blog/post/2021/03/25/fast-rank-one-updates-to-matrix-inverse/
     B = self.B
     Bu = B @ u
     s = 1 + float(v.T @ Bu)
     alpha = -1 / s
     # Warning: `overwrite_a=True` silently fails when B is not an order=F array!
     dger(alpha, Bu, v.T @ B, a=B, overwrite_a=1)
     return s
Ejemplo n.º 3
0
Archivo: core.py Proyecto: dwtang/scarf
def _update_A_b_blas(A, b, row, pivot):
    """Subtract a multiple of the row A[row, :] from all other rows.

  Implemented with BLAS routines wrapped by scipy.
  """
    x = np.copy(A[:, pivot])
    x[row] = 0.0  # mask this row
    blas.daxpy(x=x, y=b, a=-b[row])  # y += a * x
    # A += alpha * x * A[row, :]; do it in transpose since A is in C-order.
    blas.dger(alpha=-1.0, x=A[row, :], y=x, a=A.T, overwrite_a=1)
Ejemplo n.º 4
0
def gt_fouriertrans(g_tau, tau, w_n):
    r"""Performs a forward fourier transform for the interacting Green function
    in which only the interval :math:`[0,\beta]` is required and output given
    into positive fermionic matsubara frequencies up to the given cutoff.
    Array sizes need not match between frequencies and times

    .. math:: G(i\omega_n) = \int_0^\beta G(\tau)
       e^{i\omega_n \tau} d\tau

    Parameters
    ----------
    g_tau : real float array
            Imaginary time interacting Green function
    tau : real float array
            Imaginary time points
    w_n : real float array
            fermionic matsubara frequencies. Only use the positive ones
    beta : float
        Inverse temperature of the system

    Returns
    -------
    out : complex ndarray
            Interacting Greens function in matsubara frequencies
    """
    beta = tau[-1]
    power = np.exp(1j * dger(1, w_n, tau))

    g_shape = g_tau.shape
    g_tau = g_tau.reshape(-1, 1, g_shape[-1]) * power

    return np.squeeze(romb(g_tau, dx=beta / (tau.size - 1)))
Ejemplo n.º 5
0
def compute_ATA_blas_dger(N):
    '''
    Compute ATA by using the scipy wrraper for BLAS dger.
    '''
    ATA = np.zeros((N, N))
    for i in range(N):
        ai = compute_v(N)
        ATA = bla.dger(alpha=1, x=ai, y=ai, a=ATA, overwrite_a=1)
    return ATA
Ejemplo n.º 6
0
def gw_invfouriertrans(g_iwn, tau, w_n):
    r"""Performs an inverse fourier transform of the green Function in which
    only the imaginary positive matsubara frequencies
    :math:`\omega_n= \pi(2n+1)/\beta` with :math:`n \in \mathbb{N}` are used.
    Output is the real valued positivite imaginary time green function.
    positive time output :math:`\tau \in [0;\beta]`.
    Array sizes need not match between frequencies and times

    .. math::
       G(\tau) &= \frac{1}{\beta} \sum_{\omega_n}
                   G(i\omega_n)e^{-i\omega_n \tau} \\
       &= \frac{1}{\beta} \sum_{\omega_n}\left( G(i\omega_n)
          -\frac{1}{i\omega_n}\right) e^{-i\omega_n \tau} +
          \frac{1}{\beta} \sum_{\omega_n}\frac{1}{i\omega_n}e^{-i\omega_n \tau} \\
       &= \frac{2}{\beta} \sum_{\omega_n>0}^{\omega_{max}} \left[
           \Re e G_{nt}(i\omega_n) \cos(\omega_n \tau)
            + \Im m G_{nt}(i\omega_n) \sin(\omega_n \tau) \right] - \frac{1}{2}

    where :math:`G_{nt}(i\omega_n)=\left((i\omega_n) -\frac{1}{i\omega_n}\right)`

    Parameters
    ----------
    g_iwn : real float array
            Imaginary time interacting Green function
    tau : real float array
            Imaginary time points
    w_n : real float array
            fermionic matsubara frequencies. Only use the positive ones
    beta : float
        Inverse temperature of the system

    Returns
    -------
    out : complex ndarray
            Interacting Greens function in matsubara frequencies

    See also
    --------
    gt_fouriertrans"""

    beta = tau[-1]
    w_ntau = dger(1, tau, w_n)
    fou_cos = np.cos(w_ntau)
    fou_sin = np.sin(w_ntau)
    g_shape = g_iwn.shape

    g_iwn = (g_iwn + 1.j / w_n).reshape(-1, 1, g_shape[-1])
    g_tau = g_iwn.real * fou_cos + g_iwn.imag * fou_sin
    return np.squeeze(np.sum(g_tau, axis=-1) * 2 / beta - 0.5)
Ejemplo n.º 7
0
Archivo: net.py Proyecto: pdsujnow/nerv
        def backward(self, net, model, gradient):
            children = net.children[self]
            if not children:
                self.message[:] = 0
                return

            # Collect the incoming message from all children.
            incoming_message = zeros((fan_out_, 1))
            for child in children:
                offset = 0
                for other_parent in net.parents[child]:
                    if other_parent is self:
                        break
                    offset += other_parent.fan_out
                incoming_message += child.message[offset:
                        offset + incoming_message.size]

            # Calculate the gradients.
            back = tanh_prime(self.activations) * incoming_message
            dger(1.0, self.input.T, back.T, a=gradient.weight[name_].T,
                    overwrite_a=True)
            gradient.bias[name_] += back

            self.message = dot(transpose(model.weight[name_]), back)
Ejemplo n.º 8
0
Archivo: net.py Proyecto: afcarl/nerv
        def backward(self, net, model, gradient):
            children = net.children[self]
            if not children:
                self.message[:] = 0
                return

            # Collect the incoming message from all children.
            incoming_message = zeros((fan_out_, 1))
            for child in children:
                offset = 0
                for other_parent in net.parents[child]:
                    if other_parent is self:
                        break
                    offset += other_parent.fan_out
                incoming_message += child.message[offset:
                        offset + incoming_message.size]

            # Calculate the gradients.
            back = tanh_prime(self.activations) * incoming_message
            dger(1.0, self.input.T, back.T, a=gradient.weight[name_].T,
                    overwrite_a=True)
            gradient.bias[name_] += back

            self.message = dot(transpose(model.weight[name_]), back)
Ejemplo n.º 9
0
def gnew(g, v, k, sign):
    """Quick update of the interacting Green function matrix after a single
    spin flip of the auxiliary field. It calculates

    .. math:: \\alpha = \\frac{\\exp(2\\sigma v_j) - 1}
                        {1 + (1 - G_{jj})(\\exp(2\\sigma v_j) - 1)}
    .. math:: G'_{ij} = G_{ij} + \\alpha (G_{ik} - \\delta_{ik})G_{kj}

    no sumation in the indexes"""
    dv = sign * v * 2
    ee = np.exp(dv) - 1.
    a = ee / (1. + (1. - g[k, k]) * ee)
    x = g[:, k].copy()
    x[k] -= 1
    y = g[k, :].copy()
    g = dger(a, x, y, 1, 1, g, 1, 1, 1)
Ejemplo n.º 10
0
def gnew(g, v, k, sign):
    """Quick update of the interacting Green function matrix after a single
    spin flip of the auxiliary field. It calculates

    .. math:: \\alpha = \\frac{\\exp(2\\sigma v_j) - 1}
                        {1 + (1 - G_{jj})(\\exp(2\\sigma v_j) - 1)}
    .. math:: G'_{ij} = G_{ij} + \\alpha (G_{ik} - \\delta_{ik})G_{kj}

    no sumation in the indexes"""
    dv = sign*v*2
    ee = np.exp(dv)-1.
    a = ee/(1. + (1.-g[k, k])*ee)
    x = g[:, k].copy()
    x[k] -= 1
    y = g[k, :].copy()
    g = dger(a, x, y, 1, 1, g, 1, 1, 1)
Ejemplo n.º 11
0
def gnew(g, dv, k):
    """Quick update of the interacting Green function matrix after a single
    spin flip of the auxiliary field. It calculates

    .. math:: \\alpha = \\frac{\\exp(v'_j - v_j) - 1}
                        {1 + (1 - G_{jj})(\\exp(v'_j v_j) - 1)}
    .. math:: G'_{ij} = G_{ij} + \\alpha (G_{ik} - \\delta_{ik})G_{kj}

    no sumation in the indexes"""
    ee = exp(dv) - 1.
    a = ee / (1. + (1. - g[k, k]) * ee)
    x = g[:, k].copy()
    x[k] -= 1
    y = g[k, :].copy()
    if np.issubdtype(g.dtype, np.float):
        g = dger(a, x, y, 1, 1, g, 1, 1, 1)
    if np.issubdtype(g.dtype, np.complex128):
        g = zgeru(a, x, y, 1, 1, g, 1, 1, 1)
Ejemplo n.º 12
0
def learn_atoms(X,
                n_atoms,
                n_times_atom,
                n_iter=10,
                max_shift=11,
                random_state=None):
    """Learn atoms using the MoTIF algorithm.

    Parameters
    ----------
    X : array, shape (n_trials, n_times)
        The data on which to apply MoTIF.
    n_atoms : int
        The number of atoms.
    n_times_atom : int
        The support of the atoms
    n_iter : int
        The number of iterations
    max_shift : int
        The maximum allowable shift for the atoms.
    random_state : int | None
        The random initialization.
    """

    rng = check_random_state(random_state)

    n_trials, n_times = X.shape

    atoms = rng.rand(n_atoms, n_times_atom)
    corrs = np.zeros(n_trials)
    match = np.zeros((n_atoms, n_trials), dtype=np.int)

    # loop through atoms
    for k in range(n_atoms):

        aligned_data = np.zeros((n_times_atom, n_trials))

        # compute Bk
        B = np.zeros((n_times_atom, n_times_atom), order='F')
        for l in range(k):
            for p in np.arange(max_shift):
                atom_shifted = np.roll(atoms[l], -p)[np.newaxis, :]
                # B += np.dot(atom_shifted.T, atom_shifted)
                B = blas.dger(1,
                              atom_shifted,
                              atom_shifted,
                              a=B,
                              overwrite_a=1)

        # make B invertible by adding a full-rank matrix
        B += np.eye(B.shape[0]) * np.finfo(np.float32).eps

        for i in range(n_iter):
            print('[seed %s] Atom %d Iteration %d' % (random_state, k, i))
            # loop through training data
            for n in range(n_trials):
                # ### STEP 1: Find out where the data and atom align ####

                # which of these to use for template matching?
                vec1 = (X[n] - np.mean(X[n])) / (np.std(X[n]) * len(X[n]))
                vec2 = (atoms[k] - np.mean(atoms[k])) / np.std(atoms[k])
                tmp = np.abs(correlate(vec1, vec2, 'same'))

                offset = n_times_atom // 2
                match[k, n] = tmp[offset:-offset].argmax() + offset
                corrs[n] = tmp[match[k, n]]

                # aligned_data[:, n] = np.roll(X[n], -shift[n])[:n_times_atom]
                aligned_data[:, n] = X[n, match[k, n] - offset:match[k, n] +
                                       offset].copy()

            # ### STEP 2: Solve the generalized eigenvalue problem ####
            A = np.dot(aligned_data, aligned_data.T).copy()
            if k == 0:
                B = None
            e, U = eigh(A, B)
            # e, U = eigh(A)

            atoms[k, :] = U[:, -1] / np.linalg.norm(U[:, -1])
    return atoms
print(">>> print(m)")
x = Vector('x', [1, 2, 3])
y = Vector('y', [4, 5])
alpha = 1
m = et.dger(alpha, x, y)
print(m)
print("\nScipy version")
print(">>> x = [1,2,3]")
print(">>> y = [4,5]")
print(">>> alpha = 1")
print(">>> m = blas.dger(alpha,x,y)")
print(">>> print(m)")
x = [1, 2, 3]
y = [4, 5]
alpha = 1
m = blas.dger(alpha, x, y)
print(m)

print("\n(2b) - DGER")
print(">>> x = Vector('x',[4,5])")
print(">>> y = Vector('y',[1,2,3])")
print(">>> alpha = 2")
print(">>> m = et.dger(alpha,x,y)")
print(">>> print(m)")
x = Vector('x', [4, 5])
y = Vector('y', [1, 2, 3])
alpha = 2
m = et.dger(alpha, x, y)
print(m)
print("\nScipy version")
print(">>> x = [4,5]")