def main():

    n = 4
    seed = None
    prng = randomPairing(n, seed)
    #prng = standardPairing(n)
    #prng = rainbowPairing(n)
    #prng = randomPairing(n, w = [1, 1, 1])
    #prng = randomCrossPairing(n)
    #prng = smallRainbows([2,3],[1,4])
    print(prng.prng)
    print("Size is ", prng.size())
    prng.draw()

    path = prng2path(prng)
    print(path)
    dp.plotDyckPath(path)

    #newPrng = addCrossing(prng, 0, 1)
    #newPrng.draw()
    '''
    prng = Pairing(path = None, prngArray = [2, 3, 0, 1])
    prng.draw(up = False)
    prng2 = Pairing(path = None, prngArray = [1, 0, 3, 2])
    print(prng == prng2)
    print(hash(prng))
    '''

    plt.show()
def drawAsPolygon(mndr):
    ''' draws meander as polygon. The upper pairing is represented
    by a Dyck path above the diagonal y = x, and the lower pairing
    is represented by a Dyck path below the main diagonal. 
    '''
    upath = pr.prng2path(mndr.uPairing)
    ax = dp.plotDyckPath(upath)
    dpath = pr.prng2path(mndr.dPairing)
    ax = dp.plotDyckPath(dpath, ax=ax, method="lowerLatticePath")
    return ax
def areaMeander(mndr):
    '''calculates the area of a meander system defined as the area 
    of the polygone that has the Dyck path for the upper pairing
    as its upper boundary and the reflected Dyck path for the lower
    pairing as its lower boundary. The size of the meander is subtracted
    for normalization, so the area of the standard meander system is 0'''
    area = dp.areaDyckPath(pr.prng2path(mndr.uPairing))
    area += dp.areaDyckPath(pr.prng2path(mndr.dPairing))
    #area += mndr.size()
    return area
def randomPairing(n, w=None, seed=None):
    '''generates a (uniformly) random non-crossing pairing of length 2n
    If w != None, then generates a pairing from a Galton-Watson tree
    with weight sequence w'''
    if w == None:
        path = dp.randomDyckPath(n, seed=seed)
    else:
        tree = ut.randomTree(n + 1, w, seed=seed)
        path = ut.treeToDyckPath(tree)
    prng = Pairing(path)
    return prng
    def draw(self, ax=None, up=True, asPath=False):
        ''' draws the pairing. 
        If up is False, then draws it in the lower half-plane.
        If asPath is true, draws it as the corresponding Dyck path.
        '''

        if ax == None:
            fig = plt.figure()
            ax = fig.add_subplot(1, 1, 1)

        if not asPath:
            width = len(self.prng)
            ax.set_xlim(0, width)
            ax.set_ylim(-width / 2, width / 2)

            if up:
                theta1 = 0.0
                theta2 = 180.0
            else:
                theta1 = 180.0
                theta2 = 360.0
            for i in range(width):
                j = self.prng[i]
                xy = (i + (j - i) / 2, 0)
                w = np.abs((j - i) / 2)
                #print("i = " + str(i) + " j = " + str(j) + " xy = " + str(xy)
                #      + " w = " + str(w))
                arc = mpatches.Arc(xy, 2 * w, 2 * w, 0, theta1, theta2)
                ax.add_patch(arc)
            #ax.grid(True)
            ax.xaxis.set_ticks(np.arange(0, width))
            ax.yaxis.set_ticks(
                np.arange(-width / 2 + 1, width / 2, width / 2 - 1))
        else:
            path = prng2path(self)
            if up:
                dp.plotDyckPath(path, ax)
            else:
                dp.plotDyckPath(path, ax, method="lowerLatticePath")
        pass
def main():
    print('OrderedTree methods are used')
    w = np.array([1] * 3)
    #rw = tu.rescale(w) #This rescaling is done in function that generates Lukasiewicz path
    #print(rw)

    n = 10
    seed = 12345
    #seed = 0 #(random - changing from one execution to another)

    tr = randomTree(n, w, SEED=seed)
    #tr = randomBTree(n, SEED = seed)
    print(tr)
    tr.draw(drawLabels=True)

    heights, sizes = tr.calcHeightsSizes(0)
    print("heights = " + str(heights))
    print("sizes = " + str(sizes))
    '''
    These are some drawings and calculations associated with Slim Trees
    '''

    n = 400
    #n = 20
    seed = 12345
    seed = 0
    k = 10
    tr = randomSlimTree(k, n, SEED=seed)
    #print(tr)
    #fig, ax = tr.draw(drawLabels = True, block = False)
    #fig, ax = tr.draw(drawLabels = False, block = False)
    #fig1, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
    _, ax = tr.draw(drawLabels=False, block=False)
    H, v = tr.height()
    dist = tr.heightVertex(v)
    print("Height of vertex " + str(v) + " is " + str(dist))
    tr.drawPathToVertex(ax, v)
    print("Height of the tree is " + str(H))
    v = 1
    S = tr.sizeVertex(v)
    print("size of vertex " + str(v) + " is " + str(S))

    n = 5
    seed = 3
    path = dp.randomDyckPath(n, seed=seed)
    print("path = ", path)
    tr = fromDyck(path)

    _, ax = tr.draw(drawLabels=True, block=False)

    plt.show()
Esempio n. 7
0
def main():
    '''
    #Generate and prints a random tree (weighted)
    n = 100
    w = [1, 1, 1]
    tree = randomTree(n, w)
    print(tree)
    tree.draw(drawLabels = True)
    
    
    #Creates a random Dyck path and plot it 
    #path = randomDyckPath(n)
    path = treeToDyckPath(tree)
    #print(path)
    S = np.cumsum([0] + path)
    fig1, ax1 = plt.subplots(nrows=1, ncols=1)
    ax1.plot(S)
    ax1.grid(True)
    ax1.xaxis.set_ticks(np.arange(0,S.shape[0]))
    ax1.yaxis.set_ticks(np.arange(0, np.max(S) + 1))
    
    
    #Generates a random meander, finds a random cluster cycle (all 
    #point in the cycle are near each other and prints them. 
    n = 100    
    mndr = mr.randomMeander(n)  
    fig, ax = mndr.draw()
    clusters = findQuasiClusters(mndr)  
    print("clusters = \n", clusters)
    
    #here it looks for the largest quasi cluster cycle -- some gaps are allowed. 
    mgs = 9
    cycle = largestCluster(mndr, max_gap_size = mgs)
    print("Largest quasi cluster cycle is ", cycle)
    print("Its length is ", largestClusterLength(mndr, max_gap_size= mgs))
    
    mndr.drawCycle(cycle, ax = ax)
    '''

    #let us generate all non-crossing pairings of length n
    n = 6
    A = allPairings(n)
    print(len(A))
    '''
    p = [3, 2, 1, 0, 5, 4]
    q = [1, 0, 3, 2, 5, 4]
    mndr = Meander(p, q)
    mndr.draw()
    x = dot(p, q)
    print(x)
    '''

    M = dotMatrix(n)
    print(type(M))
    print(M)
    path = "/Users/vladislavkargin/Dropbox/Programs/forPapers/Meanders/"
    print(os.path.isdir(path))
    np.savetxt(path + "Matrix" + str(n) + ".csv", M.astype(int), delimiter=',')
    w = la.eigvalsh(M)
    print(w)

    q = 0.5
    #q = 7/8
    Mq = q**(n - M)
    print(Mq)
    w = la.eigvalsh(Mq)
    print("Eigenvalues: ", w)
    print("Maximum eigenvalue: ", max(w))
    print("Minimal Eigenvalue: ", min(w))
    print("Minimal Eigenvalue * C_n^2: ", min(w) * (Mq.shape[0]**2))
    print("Number of Negative Eigenvalues: ", (w[w < 0]).shape[0])
    print("Index: ", (w[w > 0]).shape[0] - (w[w < 0]).shape[0])
    plt.plot(w)
    plt.grid()
    ''' I used this for an example in my Math 447 class.
    plotRandomProperMeander(5)
    plt.grid(True)        
    '''
    '''This is for paper. It is now available as a notebook in Colab'''
    '''
    seed = 3
    n = 5
    path1 = randomDyckPath(n, seed = seed)
    plotDyckPath(path1)
    prng1 = pg.Pairing(path = path1)
    prng1.draw()
    
    area = areaDyckPath(path1)
    print("Area of path1 is ", area)   
     
    path2 = randomDyckPath(n, seed = seed + 2)
    #plotDyckPath(path2, method = "lowerLatticePath")
    area = areaDyckPath(path2)
    print("Area of path2 is ", area)  
    
    prng2 = pg.Pairing(path = path2)
    mndr = Meander(prng1, prng2)
    mndr.draw(drawCycles=True)
    mr.drawAsPolygon(mndr)
   '''
    seed = 3
    n = 10
    path1 = dp.randomDyckPath(n, seed=seed)
    dp.plotDyckPath(path1)
    area = dp.areaDyckPath(path1)
    print("Area of path1 is ", area)
    nvalleys = len(dp.valleys(path1))
    print("Number of valleys is ", nvalleys)

    n = 4
    np.random.seed()
    perm = np.random.permutation(n)
    print(perm)
    cycles = cmb.cyclesOfPerm(perm)
    print(cycles)

    plt.show()
Esempio n. 8
0
def random231perm(n):
    ''' generates a random 231 permutation of n objects, like 3 0 2 1 for n = 4'''
    path = dp.randomDyckPath(n)
    p = dp.pathTo231perm(path)
    return p