Exemple #1
0
def Propagate_trunc2(M, p, dt, ddt=1):

    E, EL, ER = mp.eig(M.T, left=True, right=True)
    E, EL, ER = mp.eig_sort(E, EL, ER)
    times = np.arange(0, dt, ddt) + dt
    N = int(dt / ddt)
    if len(p) == 1:
        intermediate_populations = [p for i in range(N)]
        return intermediate_populations
    else:
        R = [[0 for i in range(len(p))] for j in range(N)]
        for i in range(N):
            R[i][-2] = mp.exp(E[-2] * times[i])
            R[i][-1] = 1
        # print(M)
        # print(ER)
        print(E)
        # print(R)
        # print(EL)
        # print(p)
        # print(ER*mp.diag(R[0])*EL*p)
        intermediate_populations = []
        for i in range(N):
            # print(mp.diag(R[i]))
            A = ER * mp.diag(R[i]) * EL * p
            intermediate_populations.append(
                np.array((A.T).apply(mp.re).tolist()[0], dtype=float))
        print(intermediate_populations)
        return intermediate_populations
Exemple #2
0
def Propagate_stationary(M, p, dt, ddt=1):
    syst1 = time.time()

    E, EL, ER = mp.eig(M.T, left=True, right=True)
    E, EL, ER = mp.eig_sort(E, EL, ER)
    times = range(ddt, dt + ddt, ddt)
    UR = ER**-1
    R = [0] * (len(E) - 1)
    R.append(1)
    # print(M.T)
    # print(E)
    intermediate_populations = []
    print(time.time() - syst1)
    # print(np.exp(time*np.diag(e)))
    # E = np.real(np.dot(np.dot(U, np.diag(R)), Uinv))
    for i in range(len(times)):
        # print(mp.diag(R[i]))
        A = ER * mp.diag(R) * UR * p
        intermediate_populations.append(
            np.array((A.T).apply(mp.re).tolist()[0], dtype=float))
    # print(intermediate_populations)
    print(time.time() - syst1)
    # intermediate_populations = [np.array(((ER*mp.diag(R)*EL*p).T).apply(mp.re).tolist()[0], dtype=float) for t in times]
    # print(intermediate_populations)
    return intermediate_populations
Exemple #3
0
def eigenvalues(mat, sort=True):
    if mode == mode_python:
        e, _ = np.linalg.eig(mat)
        if sort:
            idx = np.argsort(e)
            return e[idx]
        else:
            return e
    else:
        e, v = mpmath.eig(mat)
        if sort:
            e, v = mpmath.eig_sort(e, v)
        return mpmath.matrix(e)
Exemple #4
0
def diagonalise(mat, sort=True):
    if mode == mode_python:
        e, v = np.linalg.eig(mat)
        if sort:
            idx = e.argsort()[::-1]
            v = v[:, idx]
        P = np.transpose(np.matrix(v, dtype=np.complex128))
        return np.dot(P, np.dot(mat, np.linalg.inv(P)))
    else:
        e, vl, vr = mpmath.eig(mat, True, True)
        if sort:
            e, vl, vr = mpmath.eig_sort(e, vl, vr)
        return vr**-1 * mat * vr
Exemple #5
0
def eigen(M):
    # print(M)
    # M = mp.matrix(M.tolist())
    # eigenValues, eigenVectors = mp.eig(M)
    # eigenValues = np.diag(np.array(eigenMatrix).astype('float128'))
    E, EL, ER = mp.eig(M, left=True, right=True)
    E, EL, ER = mp.eig_sort(E, EL, ER)
    # print(ER*mp.diag(E)*EL)
    eigenVectors = ER
    eigenValues = E
    # eigenVectors = np.array(ER.apply(mp.re).tolist(), dtype=float)
    # eigenValues = np.array([mp.re(x) for x in E], dtype=float)
    # idx = eigenValues.argsort()[::-1]
    # eigenValues = eigenValues
    if len(eigenVectors.shape) == 1:
        eigenVectors = [eigenVectors]
    # print(eigenValues)
    # print(eigenVectors)
    return eigenValues, eigenVectors
Exemple #6
0
def mc_compute_stationary_mpmath(P,
                                 precision=17,
                                 irreducible=False,
                                 ltol=0,
                                 utol=None):
    """
    Computes the stationary distributions of Markov matrix P.

    Parameters
    ----------
    P : array_like(float, ndim=2)
        A discrete Markov transition matrix.

    precision : scalar(int), optional(default: 17)
        Decimal precision in float-point arithemetic with mpmath.
        mpmath.mp.dps is set to *precision*.

    irreducible : bool, optional(default: False)
        Set True if P is known a priori to be irreducible
        (for any i, j, (P^k)_{ij} > 0 for some k).
        If True, the eigenvector for the maximum eigenvalue is returned.

    ltol, utol: scalar(float), optional(default: ltol=0, utol=None)
        Lower and upper tolerance levels.
        Find eigenvectors for eigenvalues in [1-ltol, 1+utol]
        (where [1-ltol, 1+utol] = [1-ltol, +inf) when utol=None).

    Returns
    -------
    vecs : list of numpy.arrays of mpmath.ctx_mp_python.mpf
        A list of the eigenvectors of whose eigenvalues in [1-ltol, 1+utol].

    Notes
    -----
    mpmath 0.18 or above is required.

    References
    ----------

        http://mpmath.org/doc/current

    """
    LTOL = ltol  # Lower tolerance level
    if utol is None:  # Upper tolerance level
        UTOL = 'inf'
    else:
        UTOL = utol

    with mp.workdps(precision):  # Temporarily change the working precision
        E, EL = mp.eig(mp.matrix(P), left=True, right=False)
        # E  : a list of length n containing the eigenvalues of A
        # EL : a matrix whose rows contain the left eigenvectors of A
        # See: github.com/fredrik-johansson/mpmath/blob/master/mpmath/matrices/eigen.py
        E, EL = mp.eig_sort(E, EL)  # Sorted in a descending order

        if irreducible:
            num_eigval_one = 1
        else:
            num_eigval_one = sum(
                mp.mpf(1) - mp.mpf(LTOL) <= val <= mp.mpf(1) + mp.mpf(UTOL)
                for val in E)

        vecs = [
            np.array((EL[i, :] / sum(EL[i, :])).tolist()[0])
            for i in range(EL.rows - num_eigval_one, EL.rows)
        ]

    return vecs
def mc_compute_stationary_mpmath(P, precision=17, irreducible=False, ltol=0, utol=None):
    """
    Computes the stationary distributions of Markov matrix P.

    Parameters
    ----------
    P : array_like(float, ndim=2)
        A discrete Markov transition matrix.

    precision : scalar(int), optional(default: 17)
        Decimal precision in float-point arithemetic with mpmath.
        mpmath.mp.dps is set to *precision*.

    irreducible : bool, optional(default: False)
        Set True if P is known a priori to be irreducible
        (for any i, j, (P^k)_{ij} > 0 for some k).
        If True, the eigenvector for the maximum eigenvalue is returned.

    ltol, utol: scalar(float), optional(default: ltol=0, utol=None)
        Lower and upper tolerance levels.
        Find eigenvectors for eigenvalues in [1-ltol, 1+utol]
        (where [1-ltol, 1+utol] = [1-ltol, +inf) when utol=None).

    Returns
    -------
    vecs : list of numpy.arrays of mpmath.ctx_mp_python.mpf
        A list of the eigenvectors of whose eigenvalues in [1-ltol, 1+utol].

    Notes
    -----
    mpmath 0.18 or above is required.

    References
    ----------

        http://mpmath.org/doc/current

    """
    LTOL = ltol  # Lower tolerance level
    if utol is None:  # Upper tolerance level
        UTOL = 'inf'
    else:
        UTOL = utol

    with mp.workdps(precision):  # Temporarily change the working precision
        E, EL = mp.eig(mp.matrix(P), left=True, right=False)
        # E  : a list of length n containing the eigenvalues of A
        # EL : a matrix whose rows contain the left eigenvectors of A
        # See: github.com/fredrik-johansson/mpmath/blob/master/mpmath/matrices/eigen.py
        E, EL = mp.eig_sort(E, EL)  # Sorted in a descending order

        if irreducible:
            num_eigval_one = 1
        else:
            num_eigval_one = sum(
                mp.mpf(1) - mp.mpf(LTOL) <= val <= mp.mpf(1) + mp.mpf(UTOL)
                for val in E
                )

        vecs = [np.array((EL[i, :]/sum(EL[i, :])).tolist()[0])
                for i in range(EL.rows-num_eigval_one, EL.rows)]

    return vecs