Exemple #1
0
    def log_z(self, beta=1., ensembles=None):
        """
        Use trapezoidal rule to evaluate the partition function.
        """
        from numpy import array, multiply, reshape

        is_float = False

        if type(beta) == float:
            beta = reshape(array(beta), (-1,))
            is_float = True

        x = self._ex[0, 1:] - self._ex[0, :-1]
        y = self._ex[0]

        for i in range(1, self._ex.shape[0]):
            x = multiply.outer(x, self._ex[i, 1:] - self._ex[i, :-1])
            y = multiply.outer(y, self._ex[i])

        y = -multiply.outer(beta, y) + self._log_g
        y = reshape(array([y.T[1:], y.T[:-1]]), (2, -1))
        y = log_sum_exp(y, 0) - log(2)
        y = reshape(y, (-1, len(beta))).T + log(x)

        log_z = log_sum_exp(y.T, 0)

        if is_float:
            return float(log_z)
        else:
            return log_z
Exemple #2
0
    def log_z(self, beta=1., ensembles=None):
        """
        Use trapezoidal rule to evaluate the partition function.
        """
        from numpy import array, multiply, reshape

        is_float = False

        if type(beta) == float:
            beta = reshape(array(beta), (-1, ))
            is_float = True

        x = self._ex[0, 1:] - self._ex[0, :-1]
        y = self._ex[0]

        for i in range(1, self._ex.shape[0]):
            x = multiply.outer(x, self._ex[i, 1:] - self._ex[i, :-1])
            y = multiply.outer(y, self._ex[i])

        y = -multiply.outer(beta, y) + self._log_g
        y = reshape(array([y.T[1:], y.T[:-1]]), (2, -1))
        y = log_sum_exp(y, 0) - log(2)
        y = reshape(y, (-1, len(beta))).T + log(x)

        log_z = log_sum_exp(y.T, 0)

        if is_float:
            return float(log_z)
        else:
            return log_z
Exemple #3
0
def rmmq_2site_rankN(R20A=None,
                     R20B=None,
                     dw=None,
                     k_AB=None,
                     k_BA=None,
                     tcp=None):
    """The Bloch-McConnell matrix for 2-site exchange, for rank [NE][NS][NM][NO][ND][2][2].

    @keyword R20A:          The transverse, spin-spin relaxation rate for state A.
    @type R20A:             numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R20B:          The transverse, spin-spin relaxation rate for state B.
    @type R20B:             numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:            The combined chemical exchange difference parameters between states A and B in rad/s.  This can be any combination of dw and dwH.
    @type dw:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The rate of exchange from site A to B (rad/s).
    @type k_AB:             float
    @keyword k_BA:          The rate of exchange from site B to A (rad/s).
    @type k_BA:             float
    @keyword tcp:           The tau_CPMG times (1 / 4.nu1).
    @type tcp:              numpy float array of rank [NE][NS][NM][NO][ND]
    @return:                The relaxation matrix.
    @rtype:                 numpy float array of rank [NE][NS][NM][NO][ND][2][2]
    """

    # Pre-multiply with tcp.
    r20a_tcp = R20A * tcp
    r20b_tcp = R20B * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    # Complex dw.
    dw_tcp_C = dw * tcp * 1j

    # Fill in the elements.
    #matrix[0, 0] = -k_AB - R20A
    #matrix[0, 1] = k_BA
    #matrix[1, 0] = k_AB
    #matrix[1, 1] = -k_BA + 1.j*dw - R20B

    # Multiply and expand.
    m_r20a_tcp = multiply.outer(r20a_tcp, m_r20a)
    m_r20b_tcp = multiply.outer(r20b_tcp, m_r20b)

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer(k_AB_tcp, m_k_AB)
    m_k_BA_tcp = multiply.outer(k_BA_tcp, m_k_BA)

    # Multiply and expand.
    m_dw_tcp_C = multiply.outer(dw_tcp_C, m_dw)

    # Collect matrix.
    matrix = (m_r20a_tcp + m_r20b_tcp + m_k_AB_tcp + m_k_BA_tcp + m_dw_tcp_C)

    return matrix
Exemple #4
0
    def calculate_statistics(self, data):
        """
        Calculate the sufficient statistics for the data.
        """
        from numpy import cos, sin, dot, multiply

        k = self._periodicities()
        cx = cos(multiply.outer(k, data[:, 0]))
        sx = sin(multiply.outer(k, data[:, 0]))
        cy = cos(multiply.outer(k, data[:, 1]))
        sy = sin(multiply.outer(k, data[:, 1]))

        return dot(cx, cy.T), dot(sx, sy.T), dot(cx, sy.T), dot(sx, cy.T)
Exemple #5
0
    def calculate_statistics(self, data):
        """
        Calculate the sufficient statistics for the data.
        """
        from numpy import cos, sin, dot, multiply

        k = self._periodicities()
        cx = cos(multiply.outer(k, data[:, 0]))
        sx = sin(multiply.outer(k, data[:, 0]))
        cy = cos(multiply.outer(k, data[:, 1]))
        sy = sin(multiply.outer(k, data[:, 1]))

        return dot(cx, cy.T), dot(sx, sy.T), dot(cx, sy.T), dot(sx, cy.T)
Exemple #6
0
def rmmq_2site_rankN(R20A=None, R20B=None, dw=None, k_AB=None, k_BA=None, tcp=None):
    """The Bloch-McConnell matrix for 2-site exchange, for rank [NE][NS][NM][NO][ND][2][2].

    @keyword R20A:          The transverse, spin-spin relaxation rate for state A.
    @type R20A:             numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R20B:          The transverse, spin-spin relaxation rate for state B.
    @type R20B:             numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:            The combined chemical exchange difference parameters between states A and B in rad/s.  This can be any combination of dw and dwH.
    @type dw:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The rate of exchange from site A to B (rad/s).
    @type k_AB:             float
    @keyword k_BA:          The rate of exchange from site B to A (rad/s).
    @type k_BA:             float
    @keyword tcp:           The tau_CPMG times (1 / 4.nu1).
    @type tcp:              numpy float array of rank [NE][NS][NM][NO][ND]
    @return:                The relaxation matrix.
    @rtype:                 numpy float array of rank [NE][NS][NM][NO][ND][2][2]
    """

    # Pre-multiply with tcp.
    r20a_tcp = R20A * tcp
    r20b_tcp = R20B * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    # Complex dw.
    dw_tcp_C = dw * tcp * 1j

    # Fill in the elements.
    #matrix[0, 0] = -k_AB - R20A
    #matrix[0, 1] = k_BA
    #matrix[1, 0] = k_AB
    #matrix[1, 1] = -k_BA + 1.j*dw - R20B

    # Multiply and expand.
    m_r20a_tcp = multiply.outer( r20a_tcp, m_r20a )
    m_r20b_tcp = multiply.outer( r20b_tcp, m_r20b )

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer( k_AB_tcp, m_k_AB )
    m_k_BA_tcp = multiply.outer( k_BA_tcp, m_k_BA )

    # Multiply and expand.
    m_dw_tcp_C = multiply.outer( dw_tcp_C, m_dw )

    # Collect matrix.
    matrix = (m_r20a_tcp + m_r20b_tcp + m_k_AB_tcp + m_k_BA_tcp + m_dw_tcp_C)

    return matrix
Exemple #7
0
 def __init__(self, d1, d2):
     super(MulRV, self).__init__([d1, d2],
                                 sym=d1.getSymname() * d2.getSymname())
     breaks = unique(multiply.outer(d1.range(), d2.range()))
     self.a, self.b = min(breaks), max(breaks)
     self.d1 = d1
     self.d2 = d2
def TEME_to_ITRF(jd_ut1, rTEME, vTEME, xp=0.0, yp=0.0, fraction_ut1=0.0):
    """Convert TEME position and velocity into standard ITRS coordinates.

    This converts a position and velocity vector in the idiosyncratic
    True Equator Mean Equinox (TEME) frame of reference used by the SGP4
    theory into vectors into the more standard ITRS frame of reference.
    The velocity should be provided in units per day, not per second.

    From AIAA 2006-6753 Appendix C.

    """
    # TODO: are xp and yp the values from the IERS?  Or from general
    # nutation theory?

    theta, theta_dot = theta_GMST1982(jd_ut1, fraction_ut1)
    angular_velocity = multiply.outer(_zero_zero_minus_one, theta_dot)

    R = rot_z(-theta)

    if len(rTEME.shape) == 1:
        rPEF = (R).dot(rTEME)
        vPEF = (R).dot(vTEME) + _cross(angular_velocity, rPEF)
    else:
        rPEF = mxv(R, rTEME)
        vPEF = mxv(R, vTEME) + _cross(angular_velocity, rPEF)

    if xp == 0.0 and yp == 0.0:
        rITRF = rPEF
        vITRF = vPEF
    else:
        W = (rot_x(yp)).dot(rot_y(xp))
        rITRF = (W).dot(rPEF)
        vITRF = (W).dot(vPEF)
    return rITRF, vITRF
Exemple #9
0
def __generate_scales(ndim, filter_bank, nlevels=2, detail_magnitude=True):
    """
    Generates tuples of the scales that occur for a series of SWT samples. The tuples have the
    negative and positive scales caused by the transforms. It is assumed that the filters are
    integral.
    """
    from numpy import multiply
    from .__compaction import scale_from_filter
    filter_bank = tuple(asarray(f, int) for f in filter_bank)
    level_scale = 1  # the scaling caused just be the current level
    for level in range(nlevels):
        has_detail = False  # first iteration below never has a detail filter
        for filters in product(filter_bank, repeat=ndim):
            # Generate the "full" filter from the 1D filters
            full = 1
            for fltr in filters:
                full = multiply.outer(full, fltr)
            # Generate the scales for the negative and positive parts of the current filter
            neg, pos = scale_from_filter(full)
            if has_detail and detail_magnitude: neg, pos = 0, max(pos, neg)
            yield (level_scale * neg, level_scale * pos)
            # The all-approximate filter is used for determining the scale of the next level
            if not has_detail: next_level_scale = pos - neg
            has_detail = True  # remaining samples in this level has at least one detail filter
        if level + 1 != nlevels:
            filter_bank = __upsample_filters(filter_bank)
            level_scale = next_level_scale
Exemple #10
0
def mutual_information(joint):
    """
    Mutual Information

    MI(x,y) = KL( p(x,y) || p(x) p(y) )

    We can compute this easily from the joint distribution

      joint = p(X=x,Y=y)

    because
      p(X=x) = sum_y p(X=x, Y=y)
      p(Y=y) = sum_x p(X=x, Y=y)

    relationships:
      MI(x,y) is the expected PMI(x,y) wrt p(x,y)
      MI(x,y) = KL(p(x,y) || p(x) p(y))

    properties:
      MI(X,Y) = MI(Y,X) is symmetric
    """
    # we can compute px and py from joint by applying the law of total probability
    px = joint.sum(axis=1)
    py = joint.sum(axis=0)
    independent = multiply.outer(px, py)
    assert joint.shape == independent.shape
    return kl_divergence(array(joint.flat), array(independent.flat))
Exemple #11
0
def mutual_information(joint):
    """
    Mutual Information

    MI(x,y) = KL( p(x,y) || p(x) p(y) )

    We can compute this easily from the joint distribution

      joint = p(X=x,Y=y)

    because
      p(X=x) = sum_y p(X=x, Y=y)
      p(Y=y) = sum_x p(X=x, Y=y)

    relationships:
      MI(x,y) is the expected PMI(x,y) wrt p(x,y)
      MI(x,y) = KL(p(x,y) || p(x) p(y))

    properties:
      MI(X,Y) = MI(Y,X) is symmetric
    """
    # we can compute px and py from joint by applying the law of total probability
    px = joint.sum(axis=1)
    py = joint.sum(axis=0)
    independent = multiply.outer(px, py)
    assert joint.shape == independent.shape
    return kl_divergence(array(joint.flat), array(independent.flat))
Exemple #12
0
def Bias(x, y, alpha, epsilon, kernel=InnerProduct):
    """ Compute regression bias for epsilon insensitive loss regression """
    N = len(alpha)//2
    ap, am = alpha[:N],  alpha[N:]
    sv = SupportVectors(alpha)[0]
    # functionally: b = epsilon + y[sv] + sum( (ap-am) * map(lambda xx: kernel(xx, x[sv]), x) )
    b = epsilon + y[sv] + sum( (ap-am) * multiply.outer(x, x[sv]) )
    return b
Exemple #13
0
def Bias(x, y, alpha, eps, kernel=InnerProduct):
    """ Compute regression bias for epsilon insensitive loss regression """
    N = len(alpha)/2
    ap, am = alpha[:N],  alpha[N:]
    sv = SupportVectors(alpha)[0]
    # functionally: b = eps + y[sv] + sum( (ap-am) * map(lambda xx: kernel(xx, x[sv]), x) )
    b = eps + y[sv] + sum( (ap-am) * multiply.outer(x, x[sv]) )
    return b
Exemple #14
0
 def test_outer_product(self, # {{{
     n=irange(1,10),
     m=irange(1,10),
 ):
     A = crand(n)
     B = crand(m)
     C1 = formAbsorber((),(),(FromLeft(0),FromRight(0)))(A,B)
     C2 = multiply.outer(A,B)
     self.assertAllClose(C1,C2)
Exemple #15
0
def FT(k, exafs, rmin, rmax, dr):

    con = sqrt(2 / pi)

    nn = len(k)

    rx = zeros(int((rmax - rmin) / dr), float)
    exafs_re = zeros(nn, float)
    #    exafs_im = zeros(nn, float)
    #    transform_re = zeros(nn, float)
    #    transform_im = zeros(nn, float)
    #    fourier_re = zeros(int((rmax - rmin) / dr), float)
    #    fourier_im = zeros(int((rmax - rmin) / dr), float)
    #    sn = zeros(nn, float)
    #    cs = zeros(nn, float)

    exafs_re = asarray(exafs, float)

    rx = arange(rmin, rmax, dr)
    r = rx * 2

    #    dx1 = delete(k,0)
    #    dx2 = delete(k, len(k)-1)
    #    dx = dx1 - dx2
    dx = k[1:] - k[:-1]

    v1 = multiply.outer(r, asarray(k, float))

    #    eix = exp(1j*v1)
    #    cos1, sin1 = eix.real, eix.imag

    sin1 = sin(v1)
    cos1 = cos(v1)
    sin1 = sin1 * (-1)

    #    transform_re1 = exafs_re * cos1 - exafs_im * sin1
    #    transform_im1 = exafs_re * sin1 + exafs_im * cos1
    transform_re1 = exafs_re * cos1
    transform_im1 = exafs_re * sin1

    tre11 = delete(transform_re1, 0, 1)
    tre22 = delete(transform_re1, len(transform_re1[0]) - 1, 1)

    tim11 = delete(transform_im1, 0, 1)
    tim22 = delete(transform_im1, len(transform_im1[0]) - 1, 1)

    tre111 = (tre11 + tre22) / 2
    tim111 = (tim11 + tim22) / 2

    r11 = sum(dx * tre111, axis=1)
    r22 = sum(dx * tim111, axis=1)

    fourier_re = r11 * con
    fourier_im = r22 * con

    return rx, fourier_re, fourier_im
Exemple #16
0
 def testOuterProduct(self):
     arr = rand(3,4,5)
     self.assertTrue(allclose(arr*5,make_contractor([[0,1,2],[]],[],range(3))(arr,5),rtol=1e-10))
     self.assertTrue(allclose(arr*5*6,make_contractor([[0,1,2],[],[]],[],range(3))(arr,5,6),rtol=1e-10))
     self.assertTrue(allclose(multiply.outer(arr,arr),make_contractor([[0,1,2],[3,4,5]],[],range(6))(arr,arr),rtol=1e-10))
     self.assertTrue(allclose(reduce(multiply.outer,[arr,]*3),make_contractor([[0,1,2],[3,4,5],[6,7,8]],[],range(9))(arr,arr,arr),rtol=1e-10))
     A = rand(3,4,5)
     B = rand(3,4,5)
     C = rand(3,4,5)
     self.assertTrue(allclose(reduce(multiply.outer,[A,B,C]),make_contractor([[0,1,2],[3,4,5],[6,7,8]],[],[[i] for i in xrange(9)])(A,B,C),rtol=1e-10))
Exemple #17
0
 def test_r2_outer_product_with_interleave_and_merge(self, # {{{
     l=irange(1,10),
     m=irange(1,10),
     n=irange(1,10),
     o=irange(1,10),
 ):
     A = crand(l,m)
     B = crand(n,o)
     C1 = formAbsorber((),(),(FromBoth(1,0),FromBoth(0,1)))(A,B)
     C2 = multiply.outer(A,B).transpose(1,2,0,3).reshape(m*n,l*o)
     self.assertAllClose(C1,C2)
Exemple #18
0
 def test_r2_outer_product_with_interleave(self, # {{{
     l=irange(1,10),
     m=irange(1,10),
     n=irange(1,10),
     o=irange(1,10),
 ):
     A = crand(l,m)
     B = crand(n,o)
     C1 = formAbsorber((),(),(FromLeft(0),FromRight(0),FromLeft(1),FromRight(1)))(A,B)
     C2 = multiply.outer(A,B).transpose(0,2,1,3)
     self.assertAllClose(C1,C2)
Exemple #19
0
 def test_multiple_tensors_outer_product(self,number_of_tensors=irange(1,5)): # {{{
     tensors = [NDArrayData.newRandom(*randomShape(randint(1,3),3)) for _ in range(number_of_tensors)]
     contraction = formDataContractor([],[[(tensor_number,i)] for tensor_number, tensor in enumerate(tensors) for i in range(tensor.ndim)])(*tensors)
     self.assertDataAlmostEqual(
         reduce(lambda x,y: x.contractWith(y,[],[]),tensors),
         contraction
     )
     self.assertAllClose(
         reduce(lambda x,y: multiply.outer(x,y),[tensor.toArray() for tensor in tensors]),
         contraction.toArray()
     )
Exemple #20
0
    def log_z(self, beta=1., ensembles=None):

        from numpy import multiply

        if ensembles is not None:
            e_ij_prime = numpy.array(
                [ensemble.energy(self._e) for ensemble in ensembles])
        else:
            e_ij_prime = multiply.outer(beta, self._e)

        log_z = log_sum_exp((-e_ij_prime + self.log_g()).T, 0)

        return log_z
Exemple #21
0
    def energy(self, x, y=None):
        """
        Return the energy at positions (x,y). 

        @param x: x-coordinates for evaluation
        @type x: array-like

        @param y: y-coordinates for evaluation
        @type y: array-like
        """
        from numpy import sin, cos, dot, multiply

        k = self._periodicities()
        cx, sx = cos(multiply.outer(k, x)), sin(multiply.outer(k, x))
        if y is not None:
            cy, sy = cos(multiply.outer(k, y)), sin(multiply.outer(k, y))
        else:
            cy, sy = cx, sx

        return dot(dot(cx.T, self._cc), cy) + \
               dot(dot(cx.T, self._cs), sy) + \
               dot(dot(sx.T, self._sc), cy) + \
               dot(dot(sx.T, self._ss), sy)
Exemple #22
0
    def energy(self, x, y=None):
        """
        Return the energy at positions (x,y). 

        @param x: x-coordinates for evaluation
        @type x: array-like

        @param y: y-coordinates for evaluation
        @type y: array-like
        """
        from numpy import sin, cos, dot, multiply

        k = self._periodicities()
        cx, sx = cos(multiply.outer(k, x)), sin(multiply.outer(k, x))
        if y is not None:
            cy, sy = cos(multiply.outer(k, y)), sin(multiply.outer(k, y))
        else:
            cy, sy = cx, sx

        return dot(dot(cx.T, self._cc), cy) + \
               dot(dot(cx.T, self._cs), sy) + \
               dot(dot(sx.T, self._sc), cy) + \
               dot(dot(sx.T, self._ss), sy)
def outer(a, b):
    """ 
        Outer product of two vectors.
    
        This is a wrapper around :py:func:`np.multiply.outer`.
        
        :param a: First vector.
        :param b: Second vector.
        :return: outer: Outer product of the two vectors. 
    """
    a = array(a)
    b = array(b)
    res = multiply.outer(a, b)
    return res
Exemple #24
0
def outer(a, b):
    ''' 
        Outer product of two vectors.
    
        This is a wrapper around :py:func:`np.multiply.outer`.
        
        :param a: First vector.
        :param b: Second vector.
        :return: outer: Outer product of the two vectors. 
    '''
    a = array(a)
    b = array(b)
    res = multiply.outer(a, b)
    return res
Exemple #25
0
    def log_z(self, beta=1., ensembles=None):

        from numpy import multiply

        if ensembles is not None:
            e_ij_prime = numpy.array([ensemble.energy(self._e)
                                      for ensemble in ensembles])
        else:
            e_ij_prime = multiply.outer(beta, self._e)
        
        
        log_z = log_sum_exp((-e_ij_prime + self.log_g()).T, 0)

        return log_z
Exemple #26
0
def BFT(r, fre, fim, kmin, kmax, dk):

    print(kmin, kmax, dk)

    con = sqrt(2 / pi)

    n = int((kmax - kmin) / dk + 1.0)
    dk = (kmax - kmin) / (n - 1.0)
    bftk = []

    nn = len(r)

    bftr = zeros(n, float)
    bfti = zeros(n, float)
    #transform_re = zeros(nn, float)
    #transform_im = zeros(nn, float)
    #sn = zeros(nn, float)
    #cs = zeros(nn, float)

    bftk = arange(kmin, kmax + dk, dk)

    dx1 = delete(r, 0)
    dx2 = delete(r, len(r) - 1)
    dx = dx1 - dx2
    r5 = bftk * 2

    v1 = multiply.outer(r5, asarray(r, float))
    sin1 = sin(v1)
    cos1 = cos(v1)
    transform_re1 = fre * cos1 - fim * sin1
    transform_im1 = fre * sin1 + fim * cos1

    tre11 = delete(transform_re1, 0, 1)
    tre22 = delete(transform_re1, len(transform_re1[0]) - 1, 1)

    tim11 = delete(transform_im1, 0, 1)
    tim22 = delete(transform_im1, len(transform_im1[0]) - 1, 1)

    tre111 = (tre11 + tre22) / 2
    tim111 = (tim11 + tim22) / 2

    r11 = sum(dx * tre111, axis=1)
    r22 = sum(dx * tim111, axis=1)

    bftr = r11 * con
    bfti = r22 * con

    return bftk, bftr, bfti
def Update(som, vec, hrad, bmu):
    # compute the vectors inside of a radius
    V, H, n = som.shape
    bmuv, bmuh = bmu
    ndx = indices((V, H))
    ndx[0] -= bmuv
    ndx[1] -= bmuh
    dist = sqrt(ndx[0]**2 + ndx[1]**2)
    mask = less(dist, hrad).astype(float)
    # weight these vectors
    mask *= 0.05
    # update
    #for i in range( n ):
    #    som[:,:,i] = som[:,:,i] + mask*(vec-som)[:,:,i]
    mask = multiply.outer(mask, ones(n, int))
    som += mask * (vec - som)
Exemple #28
0
def Update( som, vec, hrad, bmu ):
    # compute the vectors inside of a radius
    V,H, n = som.shape
    bmuv, bmuh = bmu
    ndx = indices( (V,H) )
    ndx[0] -= bmuv
    ndx[1] -= bmuh
    dist = sqrt( ndx[0]**2 + ndx[1]**2 )
    mask = less( dist, hrad ).astype(float)
    # weight these vectors
    mask *= 0.05
    # update
    #for i in range( n ):
    #    som[:,:,i] = som[:,:,i] + mask*(vec-som)[:,:,i]
    mask = multiply.outer( mask,ones(n,int) )
    som += mask*(vec-som)
Exemple #29
0
    def update(self):
#        pc = (self.input.pc * 10).astype('int16')
        pc = numpy.round(self.input.pc).astype('uint8')
        K, N = pc.shape 
        if self.joint is None:
            self.joint = numpy.zeros(shape=(N, N, K, K), dtype='uint16')
            self.single = numpy.zeros(shape=(N, K), dtype='uint16')
        
        self.single += pc.T
        
        # x = K x N x K x N
        x = multiply.outer(pc, pc)
        y = numpy.transpose(x, (1, 3, 0, 2))
        
        self.joint += y
        
        print self.joint.max(), self.single.max()
        
        self.output.single = self.single
        self.output.joint = self.joint
Exemple #30
0
def TEME_to_ITRF(jd_ut1, rTEME, vTEME, xp=0.0, yp=0.0, fraction_ut1=0.0):
    """Deprecated: use the TEME and ITRS frame objects instead."""
    theta, theta_dot = theta_GMST1982(jd_ut1, fraction_ut1)
    angular_velocity = multiply.outer(_zero_zero_minus_one, theta_dot)

    R = rot_z(-theta)

    if len(rTEME.shape) == 1:
        rPEF = (R).dot(rTEME)
        vPEF = (R).dot(vTEME) + _cross(angular_velocity, rPEF)
    else:
        rPEF = mxv(R, rTEME)
        vPEF = mxv(R, vTEME) + _cross(angular_velocity, rPEF)

    if xp == 0.0 and yp == 0.0:
        rITRF = rPEF
        vITRF = vPEF
    else:
        W = (rot_x(yp)).dot(rot_y(xp))
        rITRF = (W).dot(rPEF)
        vITRF = (W).dot(vPEF)
    return rITRF, vITRF
Exemple #31
0
        def testImplicitJoins(self):
            arr = rand(3,4,5)
            self.assertTrue(allclose(arr,make_contractor_from_implicit_joins([[0,1,2]],[[0],[1],[2]])(arr),rtol=1e-10))
            self.assertTrue(allclose(arr.ravel(),make_contractor_from_implicit_joins([[0,1,2]],[[0,1,2]])(arr),rtol=1e-10))
            self.assertTrue(allclose(arr.transpose(2,0,1).ravel(),make_contractor_from_implicit_joins([[0,1,2]],[[2,0,1]])(arr),rtol=1e-10))
            self.assertTrue(allclose(arr.transpose(2,0,1).reshape(15,4),make_contractor_from_implicit_joins([[0,1,2]],[[2,0],[1]])(arr),rtol=1e-10))

            A = rand(3,5)
            B = rand(5,3)
            MP = make_contractor_from_implicit_joins([[0,1],[1,3]],[[0],[3]])
            self.assertTrue(allclose(dot(A,B),MP(A,B),rtol=1e-10))
            self.assertTrue(allclose(dot(B,A),MP(B,A),rtol=1e-10))
            TP = make_contractor_from_implicit_joins([[0,1],[1,0]],[])
            self.assertTrue(allclose(inner(A.transpose().ravel(),B.ravel()),TP(A,B),rtol=1e-10))

            arr = rand(3,4,5)
            self.assertTrue(allclose(arr*5,make_contractor_from_implicit_joins([[0,1,2],[]],[[0],[1],[2]])(arr,5),rtol=1e-10))
            self.assertTrue(allclose(arr*5*6,make_contractor_from_implicit_joins([[0,1,2],[],[]],[[0],[1],[2]])(arr,5,6),rtol=1e-10))
            self.assertTrue(allclose(multiply.outer(arr,arr),make_contractor_from_implicit_joins([[0,1,2],[3,4,5]],[[i] for i in xrange(6)])(arr,arr),rtol=1e-10))
            self.assertTrue(allclose(reduce(multiply.outer,[arr,]*3),make_contractor_from_implicit_joins([[0,1,2],[3,4,5],[6,7,8]],[[i] for i in xrange(9)])(arr,arr,arr),rtol=1e-10))
            A = rand(3,4,5)
            B = rand(3,4,5)
            C = rand(3,4,5)
            self.assertTrue(allclose(reduce(multiply.outer,[A,B,C]),make_contractor_from_implicit_joins([[0,1,2],[3,4,5],[6,7,8]],[[i] for i in xrange(9)])(A,B,C),rtol=1e-10))
Exemple #32
0
def rr1rho_3d_2site_rankN(R1=None, r1rho_prime=None, dw=None, omega=None, offset=None, w1=None, k_AB=None, k_BA=None, relax_time=None):
    """Definition of the multidimensional 3D exchange matrix, of rank [NE][NS][NM][NO][ND][6][6].

    This code originates from the funNumrho.m file from the Skrynikov & Tollinger code (the sim_all.tar file https://gna.org/support/download.php?file_id=18404 attached to https://gna.org/task/?7712#comment5).


    @keyword R1:            The longitudinal, spin-lattice relaxation rate.
    @type R1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword r1rho_prime:   The R1rho transverse, spin-spin relaxation rate in the absence of exchange.
    @type r1rho_prime:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:            The chemical exchange difference between states A and B in rad/s.
    @type dw:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword omega:         The chemical shift for the spin in rad/s.
    @type omega:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword offset:        The spin-lock offsets for the data.
    @type offset:           numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword w1:            The spin-lock field strength in rad/s.
    @type w1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The forward exchange rate from state A to state B.
    @type k_AB:             float
    @keyword k_BA:          The reverse exchange rate from state B to state A.
    @type k_BA:             float
    @keyword relax_time:    The total relaxation time period for each spin-lock field strength (in seconds).
    @type relax_time:       numpy float array of rank [NE][NS][NM][NO][ND]
    @return:                The relaxation matrix.
    @rtype:                 numpy float array of rank [NE][NS][NM][NO][ND][6][6]
    """

    # Wa: The chemical shift offset of state A from the spin-lock. Larmor frequency [s^-1].
    Wa = omega
    # Wb: The chemical shift offset of state A from the spin-lock. Larmor frequency [s^-1].
    Wb = omega + dw

    # Population-averaged Larmor frequency [s^-1].
    #W = pA*Wa + pB*Wb

    # Offset of spin-lock from A.
    dA = Wa - offset

    # Offset of spin-lock from B.
    dB = Wb - offset

    # Offset of spin-lock from population-average.
    #d = W - offset

    # Alias to original parameter name.
    wA = dA
    wB = dB

    # Multiply and expand.
    mat_r1rho_prime = multiply.outer( r1rho_prime * relax_time, m_r1rho_prime )

    mat_wA = multiply.outer( wA * relax_time, m_wA )
    mat_wB = multiply.outer( wB * relax_time, m_wB )

    mat_w1 = multiply.outer( w1 * relax_time, m_w1 )

    mat_k_AB = multiply.outer( k_AB * relax_time, m_k_AB )
    mat_k_BA = multiply.outer( k_BA * relax_time, m_k_BA )

    mat_R1 = multiply.outer( R1 * relax_time, m_R1 )

    # Collect matrix.
    matrix = (mat_r1rho_prime + mat_wA + mat_wB
        + mat_w1 + mat_k_AB + mat_k_BA
        + mat_R1)

    # Return the matrix.
    return matrix
Exemple #33
0
 def mi_independent_is_zero(px, py):
     assert_equal(mutual_information(multiply.outer(px, py)), 0.0)
Exemple #34
0
 def mi_independent_is_zero(px, py):
     assert mutual_information(multiply.outer(px, py)) <= 1e-10
Exemple #35
0
def corrcoef(x, y=None):
    c = cov(x, y)
    d = diag(c)
    return c/sqrt(multiply.outer(d,d))
Exemple #36
0
def corrcoef(x, y=None):
    c = cov(x, y)
    d = diag(c)
    return c / sqrt(multiply.outer(d, d))
Exemple #37
0
def rcpmg_3d_rankN(R1A=None, R1B=None, R2A=None, R2B=None, pA=None, pB=None, dw=None, k_AB=None, k_BA=None, tcp=None):
    """Definition of the 3D exchange matrix, for rank [NE][NS][NM][NO][ND][7][7].

    @keyword R1A:   The longitudinal, spin-lattice relaxation rate for state A.
    @type R1A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R1B:   The longitudinal, spin-lattice relaxation rate for state B.
    @type R1B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2A:   The transverse, spin-spin relaxation rate for state A.
    @type R2A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2B:   The transverse, spin-spin relaxation rate for state B.
    @type R2B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword pA:    The population of state A.
    @type pA:       float
    @keyword pB:    The population of state B.
    @type pB:       float
    @keyword dw:    The chemical exchange difference between states A and B in rad/s.
    @type dw:       numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:  The forward exchange rate from state A to state B.
    @type k_AB:     float
    @keyword k_BA:  The reverse exchange rate from state B to state A.
    @type k_BA:     float
    @keyword tcp:   The tau_CPMG times (1 / 4.nu1).
    @type tcp:      numpy float array of rank [NE][NS][NM][NO][ND]
    @return:        The relaxation matrix.
    @rtype:         numpy float array of rank [NE][NS][NM][NO][ND][7][7]
    """

    # The omega frequencies for states A and B (state A is assumed to be at zero frequency).
    wA = 0.0
    wB = dw

    r10a_tcp = R1A * tcp
    r10b_tcp = R1B * tcp
    r20a_tcp = R2A * tcp
    r20b_tcp = R2B * tcp
    pA_tcp = pA * tcp
    pB_tcp = pB * tcp
    dw_tcp = dw * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    wA_tcp = wA * tcp
    wB_tcp = wB * tcp

    # Create the matrix.
    # Multiply and expand.
    m_r10a_tcp = multiply.outer( r10a_tcp, m_r10a )
    m_pA_tcp = multiply.outer( pA_tcp, m_pA )

    m_r10b_tcp = multiply.outer( r10b_tcp, m_r10b )
    m_pB_tcp = multiply.outer( pB_tcp, m_pB )

    m_r20a_tcp = multiply.outer( r20a_tcp, m_r20a )
    m_r20b_tcp = multiply.outer( r20b_tcp, m_r20b )

    m_k_AB_tcp = multiply.outer( k_AB_tcp, m_k_AB )
    m_k_BA_tcp = multiply.outer( k_BA_tcp, m_k_BA )

    m_wA_tcp = multiply.outer( wA_tcp, m_wA )
    m_wB_tcp = multiply.outer( wB_tcp, m_wB )

    # Collect matrix.
    c_mat = (m_r10a_tcp * m_pA_tcp + m_r10b_tcp * m_pB_tcp
        + m_r20a_tcp + m_r20b_tcp
        + m_k_AB_tcp + m_k_BA_tcp
        + m_wA_tcp + m_wB_tcp )

    # Return the matrix.
    return c_mat
Exemple #38
0
def rmmq_3site_rankN(R20A=None,
                     R20B=None,
                     R20C=None,
                     dw_AB=None,
                     dw_AC=None,
                     k_AB=None,
                     k_BA=None,
                     k_BC=None,
                     k_CB=None,
                     k_AC=None,
                     k_CA=None,
                     tcp=None):
    """The Bloch-McConnell matrix for 3-site exchange.

    @keyword R20A:          The transverse, spin-spin relaxation rate for state A.
    @type R20A:             numpy float array of rank [NS][NM][NO][ND]
    @keyword R20B:          The transverse, spin-spin relaxation rate for state B.
    @type R20B:             numpy float array of rank [NS][NM][NO][ND]
    @keyword R20C:          The transverse, spin-spin relaxation rate for state C.
    @type R20C:             numpy float array of rank [NS][NM][NO][ND]
    @keyword dw_AB:         The combined chemical exchange difference parameters between states A and B in rad/s.  This can be any combination of dw and dwH.
    @type dw_AB:            numpy float array of rank [NS][NM][NO][ND]
    @keyword dw_AC:         The combined chemical exchange difference parameters between states A and C in rad/s.  This can be any combination of dw and dwH.
    @type dw_AC:            numpy float array of rank [NS][NM][NO][ND]
    @keyword k_AB:          The rate of exchange from site A to B (rad/s).
    @type k_AB:             float
    @keyword k_BA:          The rate of exchange from site B to A (rad/s).
    @type k_BA:             float
    @keyword k_BC:          The rate of exchange from site B to C (rad/s).
    @type k_BC:             float
    @keyword k_CB:          The rate of exchange from site C to B (rad/s).
    @type k_CB:             float
    @keyword k_AC:          The rate of exchange from site A to C (rad/s).
    @type k_AC:             float
    @keyword k_CA:          The rate of exchange from site C to A (rad/s).
    @type k_CA:             float
    @keyword tcp:           The tau_CPMG times (1 / 4.nu1).
    @type tcp:              numpy float array of rank [NE][NS][NM][NO][ND]
    """

    # The first row.
    #matrix[0, 0] = -k_AB - k_AC - R20A
    #matrix[0, 1] = k_BA
    #matrix[0, 2] = k_CA

    # The second row.
    #matrix[1, 0] = k_AB
    #matrix[1, 1] = -k_BA - k_BC + 1.j*dw_AB - R20B
    #matrix[1, 2] = k_CB

    # The third row.
    #matrix[2, 0] = k_AC
    #matrix[2, 1] = k_BC
    #matrix[2, 2] = -k_CB - k_CA + 1.j*dw_AC - R20C

    # Pre-multiply with tcp.
    r20a_tcp = R20A * tcp
    r20b_tcp = R20B * tcp
    r20c_tcp = R20C * tcp

    # Complex dw.
    dw_AB_C_tcp = dw_AB * tcp * 1j
    dw_AC_C_tcp = dw_AC * tcp * 1j

    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    k_BC_tcp = k_BC * tcp
    k_CB_tcp = k_CB * tcp
    k_AC_tcp = k_AC * tcp
    k_CA_tcp = k_CA * tcp

    # Multiply and expand.
    m_r20a_tcp = multiply.outer(r20a_tcp, m_r20a)
    m_r20b_tcp = multiply.outer(r20b_tcp, m_r20b)
    m_r20c_tcp = multiply.outer(r20c_tcp, m_r20c)

    # Multiply and expand.
    m_dw_AB_C_tcp = multiply.outer(dw_AB_C_tcp, m_dw_AB)
    m_dw_AC_C_tcp = multiply.outer(dw_AC_C_tcp, m_dw_AC)

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer(k_AB_tcp, m_k_AB)
    m_k_BA_tcp = multiply.outer(k_BA_tcp, m_k_BA)
    m_k_BC_tcp = multiply.outer(k_BC_tcp, m_k_BC)
    m_k_CB_tcp = multiply.outer(k_CB_tcp, m_k_CB)
    m_k_AC_tcp = multiply.outer(k_AC_tcp, m_k_AC)
    m_k_CA_tcp = multiply.outer(k_CA_tcp, m_k_CA)

    # Collect matrix.
    matrix = (m_r20a_tcp + m_r20b_tcp + m_r20c_tcp + m_dw_AB_C_tcp +
              m_dw_AC_C_tcp + m_k_AB_tcp + m_k_BA_tcp + m_k_BC_tcp +
              m_k_CB_tcp + m_k_AC_tcp + m_k_CA_tcp)

    return matrix
Exemple #39
0
def rcpmg_3d_rankN(R1A=None,
                   R1B=None,
                   R2A=None,
                   R2B=None,
                   pA=None,
                   pB=None,
                   dw=None,
                   k_AB=None,
                   k_BA=None,
                   tcp=None):
    """Definition of the 3D exchange matrix, for rank [NE][NS][NM][NO][ND][7][7].

    @keyword R1A:   The longitudinal, spin-lattice relaxation rate for state A.
    @type R1A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R1B:   The longitudinal, spin-lattice relaxation rate for state B.
    @type R1B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2A:   The transverse, spin-spin relaxation rate for state A.
    @type R2A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2B:   The transverse, spin-spin relaxation rate for state B.
    @type R2B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword pA:    The population of state A.
    @type pA:       float
    @keyword pB:    The population of state B.
    @type pB:       float
    @keyword dw:    The chemical exchange difference between states A and B in rad/s.
    @type dw:       numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:  The forward exchange rate from state A to state B.
    @type k_AB:     float
    @keyword k_BA:  The reverse exchange rate from state B to state A.
    @type k_BA:     float
    @keyword tcp:   The tau_CPMG times (1 / 4.nu1).
    @type tcp:      numpy float array of rank [NE][NS][NM][NO][ND]
    @return:        The relaxation matrix.
    @rtype:         numpy float array of rank [NE][NS][NM][NO][ND][7][7]
    """

    # The omega frequencies for states A and B (state A is assumed to be at zero frequency).
    wA = 0.0
    wB = dw

    r10a_tcp = R1A * tcp
    r10b_tcp = R1B * tcp
    r20a_tcp = R2A * tcp
    r20b_tcp = R2B * tcp
    pA_tcp = pA * tcp
    pB_tcp = pB * tcp
    dw_tcp = dw * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    wA_tcp = wA * tcp
    wB_tcp = wB * tcp

    # Create the matrix.
    # Multiply and expand.
    m_r10a_tcp = multiply.outer(r10a_tcp, m_r10a)
    m_pA_tcp = multiply.outer(pA_tcp, m_pA)

    m_r10b_tcp = multiply.outer(r10b_tcp, m_r10b)
    m_pB_tcp = multiply.outer(pB_tcp, m_pB)

    m_r20a_tcp = multiply.outer(r20a_tcp, m_r20a)
    m_r20b_tcp = multiply.outer(r20b_tcp, m_r20b)

    m_k_AB_tcp = multiply.outer(k_AB_tcp, m_k_AB)
    m_k_BA_tcp = multiply.outer(k_BA_tcp, m_k_BA)

    m_wA_tcp = multiply.outer(wA_tcp, m_wA)
    m_wB_tcp = multiply.outer(wB_tcp, m_wB)

    # Collect matrix.
    c_mat = (m_r10a_tcp * m_pA_tcp + m_r10b_tcp * m_pB_tcp + m_r20a_tcp +
             m_r20b_tcp + m_k_AB_tcp + m_k_BA_tcp + m_wA_tcp + m_wB_tcp)

    # Return the matrix.
    return c_mat
Exemple #40
0
def _autocorr_inner2(x, xc, N): # fastest python
    products = multiply.outer(x, x)
    v = [products.trace(offset=m) for m in range(-len(x)+1, len(x))]
    xc.put(xrange(xc.shape[0]), v)
Exemple #41
0
def rr1rho_3d_3site_rankN(R1=None, r1rho_prime=None, dw_AB=None, dw_AC=None, omega=None, offset=None, w1=None, k_AB=None, k_BA=None, k_BC=None, k_CB=None, k_AC=None, k_CA=None, relax_time=None):
    """Definition of the 3D exchange matrix.

    @keyword R1:            The longitudinal, spin-lattice relaxation rate.
    @type R1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword r1rho_prime:   The R1rho transverse, spin-spin relaxation rate in the absence of exchange.
    @type r1rho_prime:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword omega:         The chemical shift for the spin in rad/s.
    @type omega:            numpy float array of rank [NS][NM][NO][ND]
    @keyword offset:        The spin-lock offsets for the data.
    @type offset:           numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw_AB:         The chemical exchange difference between states A and B in rad/s.
    @type dw_AB:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw_AC:         The chemical exchange difference between states A and C in rad/s.
    @type dw_AC:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword w1:            The spin-lock field strength in rad/s.
    @type w1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The forward exchange rate from state A to state B.
    @type k_AB:             float
    @keyword k_BA:          The reverse exchange rate from state B to state A.
    @type k_BA:             float
    @keyword k_BC:          The forward exchange rate from state B to state C.
    @type k_BC:             float
    @keyword k_CB:          The reverse exchange rate from state C to state B.
    @type k_CB:             float
    @keyword k_AC:          The forward exchange rate from state A to state C.
    @type k_AC:             float
    @keyword k_CA:          The reverse exchange rate from state C to state A.
    @type k_CA:             float
    @keyword relax_time:    The total relaxation time period for each spin-lock field strength (in seconds).
    @type relax_time:       numpy float array of rank [NE][NS][NM][NO][ND]
    """

    # Repetitive calculations (to speed up calculations).
    # The chemical shift offset of state A from the spin-lock. Larmor frequency for state A [s^-1].
    Wa = omega

    # The chemical shift offset of state B from the spin-lock. Larmor frequency for state B [s^-1].
    Wb = omega + dw_AB

    # The chemical shift offset of state C from the spin-lock. Larmor frequency for state C [s^-1].
    Wc = omega + dw_AC

    # Population-averaged Larmor frequency [s^-1].
    #W = pA*Wa + pB*Wb + pC*Wc

    # Offset of spin-lock from A.
    dA = Wa - offset

    # Offset of spin-lock from B.
    dB = Wb - offset

    # Offset of spin-lock from C.
    dC = Wc - offset

    # Offset of spin-lock from population-average.
    #d = W - offset_i

    # Parameter alias.
    wA = dA
    wB = dB
    wC = dC

    # Multiply and expand.
    mat_R1 = multiply.outer( R1 * relax_time, m_R1 )
    mat_r1rho_prime = multiply.outer( r1rho_prime * relax_time, m_r1rho_prime )

    mat_wA = multiply.outer( wA * relax_time, m_wA )
    mat_wB = multiply.outer( wB * relax_time, m_wB )
    mat_wC = multiply.outer( wC * relax_time, m_wC )
    mat_w1 = multiply.outer( w1 * relax_time, m_w1 )

    mat_k_AB = multiply.outer( k_AB * relax_time, m_k_AB )
    mat_k_BA = multiply.outer( k_BA * relax_time, m_k_BA )
    mat_k_BC = multiply.outer( k_BC * relax_time, m_k_BC )

    mat_k_CB = multiply.outer( k_CB * relax_time, m_k_CB )
    mat_k_AC = multiply.outer( k_AC * relax_time, m_k_AC )
    mat_k_CA = multiply.outer( k_CA * relax_time, m_k_CA )

    # Collect matrix.
    matrix = (mat_R1 + mat_r1rho_prime
            + mat_wA + mat_wB + mat_wC + mat_w1
            + mat_k_AB + mat_k_BA + mat_k_BC
            + mat_k_CB + mat_k_AC + mat_k_CA )

    # Return the matrix.
    return matrix
Exemple #42
0
 def __init__(self, d1, d2):
     super(MulRV, self).__init__([d1, d2], sym = d1.getSymname() * d2.getSymname())
     breaks = unique(multiply.outer(d1.range(), d2.range()))
     self.a, self.b = min(breaks), max(breaks)
     self.d1 = d1
     self.d2 = d2
Exemple #43
0
 def dger(alpha,x,y,incx=1,incy=1,a=0.0,overwrite_x=1,overwrite_y=1,overwrite_a=0):
     """Return alpha*multiply.outer(x,y)+a, ignoring all other paramters."""
     if a == 0.0:
         return alpha*multiply.outer(x,y)
     return alpha*multiply.outer(x,y)+a
Exemple #44
0
def rmmq_3site_rankN(R20A=None, R20B=None, R20C=None, dw_AB=None, dw_AC=None, k_AB=None, k_BA=None, k_BC=None, k_CB=None, k_AC=None, k_CA=None, tcp=None):
    """The Bloch-McConnell matrix for 3-site exchange.

    @keyword R20A:          The transverse, spin-spin relaxation rate for state A.
    @type R20A:             numpy float array of rank [NS][NM][NO][ND]
    @keyword R20B:          The transverse, spin-spin relaxation rate for state B.
    @type R20B:             numpy float array of rank [NS][NM][NO][ND]
    @keyword R20C:          The transverse, spin-spin relaxation rate for state C.
    @type R20C:             numpy float array of rank [NS][NM][NO][ND]
    @keyword dw_AB:         The combined chemical exchange difference parameters between states A and B in rad/s.  This can be any combination of dw and dwH.
    @type dw_AB:            numpy float array of rank [NS][NM][NO][ND]
    @keyword dw_AC:         The combined chemical exchange difference parameters between states A and C in rad/s.  This can be any combination of dw and dwH.
    @type dw_AC:            numpy float array of rank [NS][NM][NO][ND]
    @keyword k_AB:          The rate of exchange from site A to B (rad/s).
    @type k_AB:             float
    @keyword k_BA:          The rate of exchange from site B to A (rad/s).
    @type k_BA:             float
    @keyword k_BC:          The rate of exchange from site B to C (rad/s).
    @type k_BC:             float
    @keyword k_CB:          The rate of exchange from site C to B (rad/s).
    @type k_CB:             float
    @keyword k_AC:          The rate of exchange from site A to C (rad/s).
    @type k_AC:             float
    @keyword k_CA:          The rate of exchange from site C to A (rad/s).
    @type k_CA:             float
    @keyword tcp:           The tau_CPMG times (1 / 4.nu1).
    @type tcp:              numpy float array of rank [NE][NS][NM][NO][ND]
    """

    # The first row.
    #matrix[0, 0] = -k_AB - k_AC - R20A
    #matrix[0, 1] = k_BA
    #matrix[0, 2] = k_CA

    # The second row.
    #matrix[1, 0] = k_AB
    #matrix[1, 1] = -k_BA - k_BC + 1.j*dw_AB - R20B
    #matrix[1, 2] = k_CB

    # The third row.
    #matrix[2, 0] = k_AC
    #matrix[2, 1] = k_BC
    #matrix[2, 2] = -k_CB - k_CA + 1.j*dw_AC - R20C

    # Pre-multiply with tcp.
    r20a_tcp = R20A * tcp
    r20b_tcp = R20B * tcp
    r20c_tcp = R20C * tcp

    # Complex dw.
    dw_AB_C_tcp = dw_AB * tcp * 1j
    dw_AC_C_tcp = dw_AC * tcp * 1j

    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    k_BC_tcp = k_BC * tcp
    k_CB_tcp = k_CB * tcp
    k_AC_tcp = k_AC * tcp
    k_CA_tcp = k_CA * tcp

    # Multiply and expand.
    m_r20a_tcp = multiply.outer( r20a_tcp, m_r20a )
    m_r20b_tcp = multiply.outer( r20b_tcp, m_r20b )
    m_r20c_tcp = multiply.outer( r20c_tcp, m_r20c )

    # Multiply and expand.
    m_dw_AB_C_tcp = multiply.outer( dw_AB_C_tcp, m_dw_AB )
    m_dw_AC_C_tcp = multiply.outer( dw_AC_C_tcp, m_dw_AC )

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer( k_AB_tcp, m_k_AB )
    m_k_BA_tcp = multiply.outer( k_BA_tcp, m_k_BA )
    m_k_BC_tcp = multiply.outer( k_BC_tcp, m_k_BC )
    m_k_CB_tcp = multiply.outer( k_CB_tcp, m_k_CB )
    m_k_AC_tcp = multiply.outer( k_AC_tcp, m_k_AC )
    m_k_CA_tcp = multiply.outer( k_CA_tcp, m_k_CA )

    # Collect matrix.
    matrix = (m_r20a_tcp + m_r20b_tcp + m_r20c_tcp
            + m_dw_AB_C_tcp + m_dw_AC_C_tcp
            + m_k_AB_tcp + m_k_BA_tcp + m_k_BC_tcp
            + m_k_CB_tcp + m_k_AC_tcp + m_k_CA_tcp)

    return matrix
Exemple #45
0
 def mi_independent_is_zero(px, py):
     assert_equal(mutual_information(multiply.outer(px, py)), 0.0)
Exemple #46
0
def rr1rho_3d_3site_rankN(R1=None, r1rho_prime=None, dw_AB=None, dw_AC=None, omega=None, offset=None, w1=None, k_AB=None, k_BA=None, k_BC=None, k_CB=None, k_AC=None, k_CA=None, relax_time=None):
    """Definition of the 3D exchange matrix.

    @keyword R1:            The longitudinal, spin-lattice relaxation rate.
    @type R1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword r1rho_prime:   The R1rho transverse, spin-spin relaxation rate in the absence of exchange.
    @type r1rho_prime:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword omega:         The chemical shift for the spin in rad/s.
    @type omega:            numpy float array of rank [NS][NM][NO][ND]
    @keyword offset:        The spin-lock offsets for the data.
    @type offset:           numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw_AB:         The chemical exchange difference between states A and B in rad/s.
    @type dw_AB:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw_AC:         The chemical exchange difference between states A and C in rad/s.
    @type dw_AC:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword w1:            The spin-lock field strength in rad/s.
    @type w1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The forward exchange rate from state A to state B.
    @type k_AB:             float
    @keyword k_BA:          The reverse exchange rate from state B to state A.
    @type k_BA:             float
    @keyword k_BC:          The forward exchange rate from state B to state C.
    @type k_BC:             float
    @keyword k_CB:          The reverse exchange rate from state C to state B.
    @type k_CB:             float
    @keyword k_AC:          The forward exchange rate from state A to state C.
    @type k_AC:             float
    @keyword k_CA:          The reverse exchange rate from state C to state A.
    @type k_CA:             float
    @keyword relax_time:    The total relaxation time period for each spin-lock field strength (in seconds).
    @type relax_time:       numpy float array of rank [NE][NS][NM][NO][ND]
    """

    # Repetitive calculations (to speed up calculations).
    # The chemical shift offset of state A from the spin-lock. Larmor frequency for state A [s^-1].
    Wa = omega

    # The chemical shift offset of state B from the spin-lock. Larmor frequency for state B [s^-1].
    Wb = omega + dw_AB

    # The chemical shift offset of state C from the spin-lock. Larmor frequency for state C [s^-1].
    Wc = omega + dw_AC

    # Population-averaged Larmor frequency [s^-1].
    #W = pA*Wa + pB*Wb + pC*Wc

    # Offset of spin-lock from A.
    dA = Wa - offset

    # Offset of spin-lock from B.
    dB = Wb - offset

    # Offset of spin-lock from C.
    dC = Wc - offset

    # Offset of spin-lock from population-average.
    #d = W - offset_i

    # Parameter alias.
    wA = dA
    wB = dB
    wC = dC

    # Multiply and expand.
    mat_R1 = multiply.outer( R1 * relax_time, m_R1 )
    mat_r1rho_prime = multiply.outer( r1rho_prime * relax_time, m_r1rho_prime )

    mat_wA = multiply.outer( wA * relax_time, m_wA )
    mat_wB = multiply.outer( wB * relax_time, m_wB )
    mat_wC = multiply.outer( wC * relax_time, m_wC )
    mat_w1 = multiply.outer( w1 * relax_time, m_w1 )

    mat_k_AB = multiply.outer( k_AB * relax_time, m_k_AB )
    mat_k_BA = multiply.outer( k_BA * relax_time, m_k_BA )
    mat_k_BC = multiply.outer( k_BC * relax_time, m_k_BC )

    mat_k_CB = multiply.outer( k_CB * relax_time, m_k_CB )
    mat_k_AC = multiply.outer( k_AC * relax_time, m_k_AC )
    mat_k_CA = multiply.outer( k_CA * relax_time, m_k_CA )

    # Collect matrix.
    matrix = (mat_R1 + mat_r1rho_prime
            + mat_wA + mat_wB + mat_wC + mat_w1
            + mat_k_AB + mat_k_BA + mat_k_BC
            + mat_k_CB + mat_k_AC + mat_k_CA )

    # Return the matrix.
    return matrix
Exemple #47
0
def rr1rho_3d_2site_rankN(R1=None,
                          r1rho_prime=None,
                          dw=None,
                          omega=None,
                          offset=None,
                          w1=None,
                          k_AB=None,
                          k_BA=None,
                          relax_time=None):
    """Definition of the multidimensional 3D exchange matrix, of rank [NE][NS][NM][NO][ND][6][6].

    This code originates from the funNumrho.m file from the Skrynikov & Tollinger code (the sim_all.tar file https://web.archive.org/web/https://gna.org/support/download.php?file_id=18404 attached to https://web.archive.org/web/https://gna.org/task/?7712#comment5).


    @keyword R1:            The longitudinal, spin-lattice relaxation rate.
    @type R1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword r1rho_prime:   The R1rho transverse, spin-spin relaxation rate in the absence of exchange.
    @type r1rho_prime:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:            The chemical exchange difference between states A and B in rad/s.
    @type dw:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword omega:         The chemical shift for the spin in rad/s.
    @type omega:            numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword offset:        The spin-lock offsets for the data.
    @type offset:           numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword w1:            The spin-lock field strength in rad/s.
    @type w1:               numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:          The forward exchange rate from state A to state B.
    @type k_AB:             float
    @keyword k_BA:          The reverse exchange rate from state B to state A.
    @type k_BA:             float
    @keyword relax_time:    The total relaxation time period for each spin-lock field strength (in seconds).
    @type relax_time:       numpy float array of rank [NE][NS][NM][NO][ND]
    @return:                The relaxation matrix.
    @rtype:                 numpy float array of rank [NE][NS][NM][NO][ND][6][6]
    """

    # Wa: The chemical shift offset of state A from the spin-lock. Larmor frequency [s^-1].
    Wa = omega
    # Wb: The chemical shift offset of state A from the spin-lock. Larmor frequency [s^-1].
    Wb = omega + dw

    # Population-averaged Larmor frequency [s^-1].
    #W = pA*Wa + pB*Wb

    # Offset of spin-lock from A.
    dA = Wa - offset

    # Offset of spin-lock from B.
    dB = Wb - offset

    # Offset of spin-lock from population-average.
    #d = W - offset

    # Alias to original parameter name.
    wA = dA
    wB = dB

    # Multiply and expand.
    mat_r1rho_prime = multiply.outer(r1rho_prime * relax_time, m_r1rho_prime)

    mat_wA = multiply.outer(wA * relax_time, m_wA)
    mat_wB = multiply.outer(wB * relax_time, m_wB)

    mat_w1 = multiply.outer(w1 * relax_time, m_w1)

    mat_k_AB = multiply.outer(k_AB * relax_time, m_k_AB)
    mat_k_BA = multiply.outer(k_BA * relax_time, m_k_BA)

    mat_R1 = multiply.outer(R1 * relax_time, m_R1)

    # Collect matrix.
    matrix = (mat_r1rho_prime + mat_wA + mat_wB + mat_w1 + mat_k_AB +
              mat_k_BA + mat_R1)

    # Return the matrix.
    return matrix
def rcpmg_star_rankN(R2A=None, R2B=None, dw=None, k_AB=None, k_BA=None, tcp=None):
    """Definition of the exchange matrix, for rank [NE][NS][NM][NO][ND][2][2].

    @keyword R2A:   The transverse, spin-spin relaxation rate for state A.
    @type R2A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2B:   The transverse, spin-spin relaxation rate for state B.
    @type R2B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:    The chemical exchange difference between states A and B in rad/s.
    @type dw:       numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:  The forward exchange rate from state A to state B.
    @type k_AB:     float
    @keyword k_BA:  The reverse exchange rate from state B to state A.
    @type k_BA:     float
    @keyword tcp:   The tau_CPMG times (1 / 4.nu1).
    @type tcp:      numpy float array of rank [NE][NS][NM][NO][ND]
    @return:        The relaxation matrix R and complex conjugate cR2.
    @rtype:         numpy float array of rank [NE][NS][NM][NO][ND][2][2]
    """

    # Pre-multiply with tcp.
    r20a_tcp = R2A * tcp
    r20b_tcp = R2B * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    # Complex dw.
    dw_tcp_C = dw * tcp * -1j

    # Create matrix for collection of Rr matrix.
    # The matrix that contains only the R2 relaxation terms ("Redfield relaxation", i.e. non-exchange broadening).
    #Rr[0, 0] = -R2A_si_mi
    #Rr[1, 1] = -R2B_si_mi

    # Multiply and expand.
    m_r20a_tcp = multiply.outer( r20a_tcp, m_r20a )
    m_r20b_tcp = multiply.outer( r20b_tcp, m_r20b )

    # Collect Rr matrix.
    Rr_mat = (m_r20a_tcp + m_r20b_tcp)

    # Create matrix for collection of Rex.
    # Set up the matrix that contains the exchange terms between the two states A and B.
    #Rex[0, 0] = -k_AB
    #Rex[0, 1] = k_BA
    #Rex[1, 0] = k_AB
    #Rex[1, 1] = -k_BA

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer( k_AB_tcp, m_k_AB )
    m_k_BA_tcp = multiply.outer( k_BA_tcp, m_k_BA )

    # Collect Rex matrix.
    Rex_mat = (m_k_AB_tcp + m_k_BA_tcp)

    # Create the matrix for RCS.
    # The matrix that contains the chemical shift evolution.  It works here only with X magnetization, and the complex notation allows to evolve in the transverse plane (x, y).  The chemical shift for state A is assumed to be zero.
    #RCS[1, 1] = complex(0.0, -dw_si_mi)

    # Multiply and expand.
    m_dw_tcp_C = multiply.outer( dw_tcp_C, m_dw )

    # Collect RCS matrix.
    RCS_mat = m_dw_tcp_C

    # The matrix R that contains all the contributions to the evolution, i.e. relaxation, exchange and chemical shift evolution.
    R_mat = add(Rr_mat, Rex_mat)
    R_mat = add(R_mat, RCS_mat)

    # This is the complex conjugate of the above.  It allows the chemical shift to run in the other direction, i.e. it is used to evolve the shift after a 180 deg pulse.  The factor of 2 is to minimise the number of multiplications for the prop_2 matrix calculation.
    cR2_mat = conj(R_mat) * 2.0

    # Return the matrixes.
    return R_mat, cR2_mat, Rr_mat, Rex_mat, RCS_mat
def _autocorr_inner2(x, xc, N):  # fastest python
    products = multiply.outer(x, x)
    v = [products.trace(offset=m) for m in range(-len(x) + 1, len(x))]
    xc.put(xrange(xc.shape[0]), v)
Exemple #50
0
def rcpmg_star_rankN(R2A=None, R2B=None, dw=None, k_AB=None, k_BA=None, tcp=None):
    """Definition of the exchange matrix, for rank [NE][NS][NM][NO][ND][2][2].

    @keyword R2A:   The transverse, spin-spin relaxation rate for state A.
    @type R2A:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword R2B:   The transverse, spin-spin relaxation rate for state B.
    @type R2B:      numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword dw:    The chemical exchange difference between states A and B in rad/s.
    @type dw:       numpy float array of rank [NE][NS][NM][NO][ND]
    @keyword k_AB:  The forward exchange rate from state A to state B.
    @type k_AB:     float
    @keyword k_BA:  The reverse exchange rate from state B to state A.
    @type k_BA:     float
    @keyword tcp:   The tau_CPMG times (1 / 4.nu1).
    @type tcp:      numpy float array of rank [NE][NS][NM][NO][ND]
    @return:        The relaxation matrix R and complex conjugate cR2.
    @rtype:         numpy float array of rank [NE][NS][NM][NO][ND][2][2]
    """

    # Pre-multiply with tcp.
    r20a_tcp = R2A * tcp
    r20b_tcp = R2B * tcp
    k_AB_tcp = k_AB * tcp
    k_BA_tcp = k_BA * tcp
    # Complex dw.
    dw_tcp_C = dw * tcp * -1j

    # Create matrix for collection of Rr matrix.
    # The matrix that contains only the R2 relaxation terms ("Redfield relaxation", i.e. non-exchange broadening).
    #Rr[0, 0] = -R2A_si_mi
    #Rr[1, 1] = -R2B_si_mi

    # Multiply and expand.
    m_r20a_tcp = multiply.outer( r20a_tcp, m_r20a )
    m_r20b_tcp = multiply.outer( r20b_tcp, m_r20b )

    # Collect Rr matrix.
    Rr_mat = (m_r20a_tcp + m_r20b_tcp)

    # Create matrix for collection of Rex.
    # Set up the matrix that contains the exchange terms between the two states A and B.
    #Rex[0, 0] = -k_AB
    #Rex[0, 1] = k_BA
    #Rex[1, 0] = k_AB
    #Rex[1, 1] = -k_BA

    # Multiply and expand.
    m_k_AB_tcp = multiply.outer( k_AB_tcp, m_k_AB )
    m_k_BA_tcp = multiply.outer( k_BA_tcp, m_k_BA )

    # Collect Rex matrix.
    Rex_mat = (m_k_AB_tcp + m_k_BA_tcp)

    # Create the matrix for RCS.
    # The matrix that contains the chemical shift evolution.  It works here only with X magnetization, and the complex notation allows to evolve in the transverse plane (x, y).  The chemical shift for state A is assumed to be zero.
    #RCS[1, 1] = complex(0.0, -dw_si_mi)

    # Multiply and expand.
    m_dw_tcp_C = multiply.outer( dw_tcp_C, m_dw )

    # Collect RCS matrix.
    RCS_mat = m_dw_tcp_C

    # The matrix R that contains all the contributions to the evolution, i.e. relaxation, exchange and chemical shift evolution.
    R_mat = add(Rr_mat, Rex_mat)
    R_mat = add(R_mat, RCS_mat)

    # This is the complex conjugate of the above.  It allows the chemical shift to run in the other direction, i.e. it is used to evolve the shift after a 180 deg pulse.  The factor of 2 is to minimise the number of multiplications for the prop_2 matrix calculation.
    cR2_mat = conj(R_mat) * 2.0

    # Return the matrixes.
    return R_mat, cR2_mat, Rr_mat, Rex_mat, RCS_mat