def testDiag2Chain(self):
        # Perform the diagonalization for a 2-site chain. Compare
        # an exact diagonalization against scipy.
        
        # First calculate X,P
        calc = Calculate()
        polygon = sp.array([[0,0],[2,0]]) 
        maple_link = maple.MapleLink("/opt/maple13/bin/maple -tu")
        precision = 25
        
        X,P = calc.correlations(polygon, maple_link, precision)
        
        entropy = calc.entropy(X, P, 1, precision,False)

        # Manual diag.
        # [ a, b,
        #   c, d]
        XP = X*P
        a = XP[0,0]
        b = XP[0,1]
        c = XP[1,0]
        d = XP[1,1]
        
        eig_plus = ((a+d)+sympy.mpmath.sqrt((a+d)**2-4*(a*d-b*c)))/2
        eig_minus = ((a+d)-sympy.mpmath.sqrt((a+d)**2-4*(a*d-b*c)))/2
        
        sqrt_eigs = [sympy.mpmath.sqrt(eig_plus),sympy.mpmath.sqrt(eig_minus)]
        
        S = 0
        for vk in sqrt_eigs:
            S += ((vk + 0.5)*sympy.mpmath.log(vk + 0.5) - (vk - 0.5)*sympy.mpmath.log(vk - 0.5))
            
        # Scipy operates in double, so we check equality up to a tolerance
        bound = 10**(-13)
        eq_(True, abs(S - entropy) < bound)
예제 #2
0
    def testPolygonScramble(self):
        # Here, rearrange polygon randomly and repeatedly get results. See if come out different.

        # Presets.
        precision = 20
        maple_link = maple.MapleLink(
            "/opt/maple13/bin/maple -tu"
        )  #TODO: hopefully remove this maple dependency...too hardwirey.
        L = 3
        calc = Calculate()

        polygon = calc.square_lattice(L)

        X, P = calc.correlations(polygon, maple_link, precision)

        # Get eigenvals
        with sympy.mpmath.extraprec(500):
            XP = X * P
        XPnum = sympy.matrix2numpy(XP)
        sqrt_eigs = sp.sqrt(linalg.eigvals(XPnum))
        sqrt_eigs_orig = sqrt_eigs.real

        # Scramble polygon.
        sp.random.shuffle(polygon)

        # Perform the integration again.
        X, P = calc.correlations(polygon, maple_link, precision)

        # Get eigenvals
        with sympy.mpmath.extraprec(500):
            XP = X * P
        XPnum = sympy.matrix2numpy(XP)
        sqrt_eigs = sp.sqrt(linalg.eigvals(XPnum))
        sqrt_eigs_scrambled = sqrt_eigs.real

        # Compare.
        a = set(tuple([round(float(x), 10) for x in sqrt_eigs_orig]))
        b = set(tuple([round(float(x), 10) for x in sqrt_eigs_scrambled]))
        eq_(a, b)
    def testPolygonScramble(self):
        # Here, rearrange polygon randomly and repeatedly get results. See if come out different.

        # Presets.
        precision = 20
        maple_link = maple.MapleLink("/opt/maple13/bin/maple -tu") #TODO: hopefully remove this maple dependency...too hardwirey.
        L = 3
        calc = Calculate()
        
        polygon = calc.square_lattice(L)
        
        X,P = calc.correlations(polygon, maple_link, precision)

        # Get eigenvals
        with sympy.mpmath.extraprec(500):
            XP = X*P
        XPnum = sympy.matrix2numpy(XP)
        sqrt_eigs = sp.sqrt(linalg.eigvals(XPnum))
        sqrt_eigs_orig = sqrt_eigs.real
        
        # Scramble polygon.
        sp.random.shuffle(polygon)
        
        # Perform the integration again.        
        X,P = calc.correlations(polygon, maple_link, precision)

        # Get eigenvals
        with sympy.mpmath.extraprec(500):
            XP = X*P
        XPnum = sympy.matrix2numpy(XP)
        sqrt_eigs = sp.sqrt(linalg.eigvals(XPnum))
        sqrt_eigs_scrambled = sqrt_eigs.real
        
        # Compare.
        a = set(tuple([round(float(x),10) for x in sqrt_eigs_orig]))
        b = set(tuple([round(float(x),10) for x in sqrt_eigs_scrambled]))
        eq_(a,b)
예제 #4
0
    def testDiag2Chain(self):
        # Perform the diagonalization for a 2-site chain. Compare
        # an exact diagonalization against scipy.

        # First calculate X,P
        calc = Calculate()
        polygon = sp.array([[0, 0], [2, 0]])
        maple_link = maple.MapleLink("/opt/maple13/bin/maple -tu")
        precision = 25

        X, P = calc.correlations(polygon, maple_link, precision)

        entropy = calc.entropy(X, P, 1, precision, False)

        # Manual diag.
        # [ a, b,
        #   c, d]
        XP = X * P
        a = XP[0, 0]
        b = XP[0, 1]
        c = XP[1, 0]
        d = XP[1, 1]

        eig_plus = ((a + d) + sympy.mpmath.sqrt((a + d)**2 - 4 *
                                                (a * d - b * c))) / 2
        eig_minus = ((a + d) - sympy.mpmath.sqrt((a + d)**2 - 4 *
                                                 (a * d - b * c))) / 2

        sqrt_eigs = [sympy.mpmath.sqrt(eig_plus), sympy.mpmath.sqrt(eig_minus)]

        S = 0
        for vk in sqrt_eigs:
            S += ((vk + 0.5) * sympy.mpmath.log(vk + 0.5) -
                  (vk - 0.5) * sympy.mpmath.log(vk - 0.5))

        # Scipy operates in double, so we check equality up to a tolerance
        bound = 10**(-13)
        eq_(True, abs(S - entropy) < bound)
class CorrelationsTest(unittest.TestCase):
    #TODO: Currently limited to a small case.  In future, when program optimized, expand this!
    
    maple_link = None
    # For L = 3...
    X = None
    P = None
    XP = None
    precision = None
    
    @classmethod
    def setUpClass(cls):
        cls.calc = Calculate()
        if cls.X is None and cls.P is None:
            # Startup Maple. #TODO: Replace with sympy when they fix bug (see src.main). This is ugly since relying on a hardcoded dir.
            cls.maple_link = maple.MapleLink("/opt/maple13/bin/maple -tu")
            cls.precision = 25
            
            lattice = cls.calc.square_lattice(5)
            cls.X,cls.P = cls.calc.correlations(lattice,cls.maple_link,cls.precision)
        else:
            # TODO: rremove this if this doesn't show up
            print "Bug: This function has been called twice"
    
    def setUp(self):
        self.calc = Calculate()
        
    def testSymmetric(self):
        eq_(True,self.X.is_symmetric())
        eq_(True,self.P.is_symmetric())
        
    def testReal(self):
        eq_(True, self.X == self.X.conjugate())
        eq_(True, self.P == self.P.conjugate())
        
    def testPrecision(self):
        """
        See if the precision increases with the variable.
        """
        polygon = self.calc.square_lattice(1)
        precisions = [20,30,40,50]
         
        # Find X,P for each precision
        corrs_collection = []
        for precision in precisions:
            X,P = self.calc.correlations(polygon, self.maple_link, precision)
            corrs_collection.append([X,P])
             
        for i in range(0, len(precisions)-1):
            # get X and P pairs for precisions[i] and precisions[i+1]
            pairX = sp.array([corrs_collection[i][0],corrs_collection[i+1][0]])
            pairP = sp.array([corrs_collection[i][1],corrs_collection[i+1][1]])
             
            # Check if increasing the precision variable actually does this.
            bound = 10**(-precisions[i])
             
            eq_(True, ( abs(pairX[0] - pairX[1]) < bound ).all())
            eq_(True, ( abs(pairP[0] - pairP[1]) < bound ).all())
        
    def testPrecomputed(self):
        """
        Test the precomputed correlations feature.
        """
        precision = 25

        lattice3 = self.calc.square_lattice(3)
        lattice4 = self.calc.square_lattice(4)
        
        X3,P3 = self.calc.correlations(lattice3, self.maple_link, precision, verbose=True)
        X4,P4 = self.calc.correlations(lattice4, self.maple_link, precision, verbose=True)
        
        # With precomputed.
        precomputed_correlations = {}
        X3_quick, P3_quick, precomputed_correlations = self.calc.correlations(lattice3, self.maple_link, precision, precomputed_correlations, verbose=True)
        X4_quick, P4_quick, precomputed_correlations = self.calc.correlations(lattice4, self.maple_link, precision, precomputed_correlations, verbose=True)

        eq_(True, X3 == X3_quick)
        eq_(True, P3 == P3_quick)
        eq_(True, X4 == X4_quick)
        eq_(True, P4 == P4_quick)
        
    def testMatrixSymmetries(self):
        """
        X and P should have the same pattern as the distance matrix defined by
        the lattice.
        """
        precision = 20
        polygon = self.calc.square_lattice(5)
        X,P = self.calc.correlations(polygon, self.maple_link, precision)
        
        # Round X and P down so we can see if elements are distinct or not.
        X = sympy.matrix2numpy(X)
        P = sympy.matrix2numpy(P)
        X = X.astype('float')
        P = P.astype('float')
        
        # Get the pattern of the distance matrix.
        D = spatial.distance.cdist(polygon,polygon)
        
        # The pattern of the distance matrix
        D_pat = sp.zeros(D.shape)
        getSignatureMatrix(D_pat,sp.nditer(D),D.shape)
        
        # Get the pattern of X and P.
        X_pat = sp.zeros(X.shape)
        P_pat = sp.zeros(P.shape)
        getSignatureMatrix(X_pat,sp.nditer(X),X.shape)
        getSignatureMatrix(P_pat,sp.nditer(P),P.shape)
        
        # Check if patterns match.
        eq_(False,(D_pat - X_pat).all())
        eq_(False,(D_pat - P_pat).all())
예제 #6
0
class CorrelationsTest(unittest.TestCase):
    #TODO: Currently limited to a small case.  In future, when program optimized, expand this!

    maple_link = None
    # For L = 3...
    X = None
    P = None
    XP = None
    precision = None

    @classmethod
    def setUpClass(cls):
        cls.calc = Calculate()
        if cls.X is None and cls.P is None:
            # Startup Maple. #TODO: Replace with sympy when they fix bug (see src.main). This is ugly since relying on a hardcoded dir.
            cls.maple_link = maple.MapleLink("/opt/maple13/bin/maple -tu")
            cls.precision = 25

            lattice = cls.calc.square_lattice(5)
            cls.X, cls.P = cls.calc.correlations(lattice, cls.maple_link,
                                                 cls.precision)
        else:
            # TODO: rremove this if this doesn't show up
            print "Bug: This function has been called twice"

    def setUp(self):
        self.calc = Calculate()

    def testSymmetric(self):
        eq_(True, self.X.is_symmetric())
        eq_(True, self.P.is_symmetric())

    def testReal(self):
        eq_(True, self.X == self.X.conjugate())
        eq_(True, self.P == self.P.conjugate())

    def testPrecision(self):
        """
        See if the precision increases with the variable.
        """
        polygon = self.calc.square_lattice(1)
        precisions = [20, 30, 40, 50]

        # Find X,P for each precision
        corrs_collection = []
        for precision in precisions:
            X, P = self.calc.correlations(polygon, self.maple_link, precision)
            corrs_collection.append([X, P])

        for i in range(0, len(precisions) - 1):
            # get X and P pairs for precisions[i] and precisions[i+1]
            pairX = sp.array(
                [corrs_collection[i][0], corrs_collection[i + 1][0]])
            pairP = sp.array(
                [corrs_collection[i][1], corrs_collection[i + 1][1]])

            # Check if increasing the precision variable actually does this.
            bound = 10**(-precisions[i])

            eq_(True, (abs(pairX[0] - pairX[1]) < bound).all())
            eq_(True, (abs(pairP[0] - pairP[1]) < bound).all())

    def testPrecomputed(self):
        """
        Test the precomputed correlations feature.
        """
        precision = 25

        lattice3 = self.calc.square_lattice(3)
        lattice4 = self.calc.square_lattice(4)

        X3, P3 = self.calc.correlations(lattice3,
                                        self.maple_link,
                                        precision,
                                        verbose=True)
        X4, P4 = self.calc.correlations(lattice4,
                                        self.maple_link,
                                        precision,
                                        verbose=True)

        # With precomputed.
        precomputed_correlations = {}
        X3_quick, P3_quick, precomputed_correlations = self.calc.correlations(
            lattice3,
            self.maple_link,
            precision,
            precomputed_correlations,
            verbose=True)
        X4_quick, P4_quick, precomputed_correlations = self.calc.correlations(
            lattice4,
            self.maple_link,
            precision,
            precomputed_correlations,
            verbose=True)

        eq_(True, X3 == X3_quick)
        eq_(True, P3 == P3_quick)
        eq_(True, X4 == X4_quick)
        eq_(True, P4 == P4_quick)

    def testMatrixSymmetries(self):
        """
        X and P should have the same pattern as the distance matrix defined by
        the lattice.
        """
        precision = 20
        polygon = self.calc.square_lattice(5)
        X, P = self.calc.correlations(polygon, self.maple_link, precision)

        # Round X and P down so we can see if elements are distinct or not.
        X = sympy.matrix2numpy(X)
        P = sympy.matrix2numpy(P)
        X = X.astype('float')
        P = P.astype('float')

        # Get the pattern of the distance matrix.
        D = spatial.distance.cdist(polygon, polygon)

        # The pattern of the distance matrix
        D_pat = sp.zeros(D.shape)
        getSignatureMatrix(D_pat, sp.nditer(D), D.shape)

        # Get the pattern of X and P.
        X_pat = sp.zeros(X.shape)
        P_pat = sp.zeros(P.shape)
        getSignatureMatrix(X_pat, sp.nditer(X), X.shape)
        getSignatureMatrix(P_pat, sp.nditer(P), P.shape)

        # Check if patterns match.
        eq_(False, (D_pat - X_pat).all())
        eq_(False, (D_pat - P_pat).all())