Esempio n. 1
0
def gauss_legendre(N, lo=0, up=1, composite=1):
    """
Generate the quadrature nodes and weights in Gauss-Legendre
quadrature
    """

    N,lo,up = [np.array(_).flatten() for _ in [N,lo,up]]
    dim = max(lo.size, up.size, N.size)
    N,lo,up = [np.ones(dim)*_ for _ in [N,lo,up]]
    N = np.array(N, dtype=int)

    if isinstance(composite, int):
        composite = [np.linspace(0,1,composite+1)]*dim
    else:
        composite = np.array(composite)
        if not composite.shape:
            composite = composite.flatten()
        if len(composite.shape)==1:
            composite = np.array([composite]).T
        composite = ((composite.T-lo)/(up-lo)).T


    q = [_gauss_legendre(N[i], composite[i]) for i in xrange(dim)]
    x = np.array([_[0] for _ in q])
    w = np.array([_[1] for _ in q])

    x = combine(x)
    w = combine(w)

    x = (up-lo)*x + lo
    w = np.prod(w*(up-lo), 1)

    return x.T, w
Esempio n. 2
0
def leja(order, dist):
    """
After paper by Narayan and Jakeman
    """

    if len(dist) > 1:
        if isinstance(order, int):
            xw = [leja(order, d) for d in dist]
        else:
            xw = [leja(order[i], dist[i]) for i in xrange(len(dist))]

        x = [_[0][0] for _ in xw]
        w = [_[1] for _ in xw]
        x = combine(x).T
        w = combine(w)
        w = np.prod(w, -1)

        return x, w

    lo, up = dist.range()
    X = [lo, dist.mom(1), up]
    for o in xrange(order):

        X_ = np.array(X[1:-1])
        obj = lambda x:-np.sqrt(dist.pdf(x))*np.prod(np.abs(X_-x))
        opts, vals = zip(*[fminbound(obj, X[i], X[i+1],
            full_output=1)[:2] for i in xrange(len(X)-1)])
        index = np.argmin(vals)
        X.insert(index+1, opts[index])

    X = np.asfarray(X).flatten()[1:-1]
    W = weightgen(X, dist)
    X = X.reshape(1, X.size)

    return np.array(X), np.array(W)
Esempio n. 3
0
def gauss_legendre(N, lo=0, up=1, composite=1):
    """
Generate the quadrature nodes and weights in Gauss-Legendre
quadrature
    """

    N, lo, up = [np.array(_).flatten() for _ in [N, lo, up]]
    dim = max(lo.size, up.size, N.size)
    N, lo, up = [np.ones(dim) * _ for _ in [N, lo, up]]
    N = np.array(N, dtype=int)

    if isinstance(composite, int):
        composite = [np.linspace(0, 1, composite + 1)] * dim
    else:
        composite = np.array(composite)
        if not composite.shape:
            composite = composite.flatten()
        if len(composite.shape) == 1:
            composite = np.array([composite]).T
        composite = ((composite.T - lo) / (up - lo)).T

    q = [_gauss_legendre(N[i], composite[i]) for i in xrange(dim)]
    x = np.array([_[0] for _ in q])
    w = np.array([_[1] for _ in q])

    x = combine(x)
    w = combine(w)

    x = (up - lo) * x + lo
    w = np.prod(w * (up - lo), 1)

    return x.T, w
Esempio n. 4
0
def leja(order, dist):
    """
After paper by Narayan and Jakeman
    """

    if len(dist) > 1:
        if isinstance(order, int):
            xw = [leja(order, d) for d in dist]
        else:
            xw = [leja(order[i], dist[i]) for i in xrange(len(dist))]

        x = [_[0][0] for _ in xw]
        w = [_[1] for _ in xw]
        x = combine(x).T
        w = combine(w)
        w = np.prod(w, -1)

        return x, w

    lo, up = dist.range()
    X = [lo, dist.mom(1), up]
    for o in xrange(order):

        X_ = np.array(X[1:-1])
        obj = lambda x:-np.sqrt(dist.pdf(x))*np.prod(np.abs(X_-x))
        opts, vals = zip(*[fminbound(obj, X[i], X[i+1],
            full_output=1)[:2] for i in xrange(len(X)-1)])
        index = np.argmin(vals)
        X.insert(index+1, opts[index])

    X = np.asfarray(X).flatten()[1:-1]
    W = weightgen(X, dist)
    X = X.reshape(1, X.size)

    return np.array(X), np.array(W)
Esempio n. 5
0
    def tensprod_rule(N, part=None):

        N = N * np.ones(dim, int)
        q = [np.array(funcs[i](N[i])) \
                for i in xrange(dim)]

        x = [_[0] for _ in q]
        x = combine(x, part=part).T

        w = [_[1] for _ in q]
        w = np.prod(combine(w, part=part), -1)

        return x, w
Esempio n. 6
0
    def tensprod_rule(N, part=None):

        N = N*np.ones(dim, int)
        q = [np.array(funcs[i](N[i])) \
                for i in xrange(dim)]

        x = [_[0] for _ in q]
        x = combine(x, part=part).T

        w = [_[1] for _ in q]
        w = np.prod(combine(w, part=part), -1)

        return x, w
Esempio n. 7
0
 def getMenu(self) -> menu.Menu:
     return menu.Menu(utils.combine(
         {
             "Exit": lambda arg: self.__exit(arg),
             "Map": lambda arg: self.__map(arg)
         }, self.__options),
                      title=self.__name,
                      titleColor=self.__color,
                      description=self.__description)
Esempio n. 8
0
def clenshaw_curtis(N, lo=0, up=1, growth=False, composite=1, part=None):
    """
Generate the quadrature nodes and weights in Clenshaw-Curtis
quadrature
    """
    N, lo, up = [np.array(_).flatten() for _ in [N, lo, up]]
    dim = max(lo.size, up.size, N.size)
    N, lo, up = [np.ones(dim) * _ for _ in [N, lo, up]]
    N = np.array(N, dtype=int)

    if isinstance(composite, int):
        composite = [np.linspace(0, 1, composite + 1)] * dim
    else:
        composite = np.array(composite)
        if not composite.shape:
            composite = composite.flatten()
        if len(composite.shape) == 1:
            composite = np.array([composite])
        composite = ((composite.T - lo) / (up - lo)).T

    if growth:
        q = [_clenshaw_curtis(2**N[i]-1*(N[i]==0), composite[i]) \
                for i in xrange(dim)]
    else:
        q = [_clenshaw_curtis(N[i], composite[i]) for i in xrange(dim)]

    x = [_[0] for _ in q]
    w = [_[1] for _ in q]

    x = combine(x, part=part).T
    w = combine(w, part=part)

    x = ((up - lo) * x.T + lo).T
    w = np.prod(w * (up - lo), -1)

    assert len(x) == dim
    assert len(w) == len(x.T)

    return x, w
Esempio n. 9
0
def clenshaw_curtis(N, lo=0, up=1, growth=False, composite=1, part=None):
    """
Generate the quadrature nodes and weights in Clenshaw-Curtis
quadrature
    """
    N,lo,up = [np.array(_).flatten() for _ in [N,lo,up]]
    dim = max(lo.size, up.size, N.size)
    N,lo,up = [np.ones(dim)*_ for _ in [N,lo,up]]
    N = np.array(N, dtype=int)

    if isinstance(composite, int):
        composite = [np.linspace(0,1,composite+1)]*dim
    else:
        composite = np.array(composite)
        if not composite.shape:
            composite = composite.flatten()
        if len(composite.shape)==1:
            composite = np.array([composite])
        composite = ((composite.T-lo)/(up-lo)).T

    if growth:
        q = [_clenshaw_curtis(2**N[i]-1*(N[i]==0), composite[i]) \
                for i in xrange(dim)]
    else:
        q = [_clenshaw_curtis(N[i], composite[i]) for i in xrange(dim)]

    x = [_[0] for _ in q]
    w = [_[1] for _ in q]

    x = combine(x, part=part).T
    w = combine(w, part=part)

    x = ((up-lo)*x.T + lo).T
    w = np.prod(w*(up-lo), -1)

    assert len(x)==dim
    assert len(w)==len(x.T)

    return x, w
Esempio n. 10
0
def gk(order, dist, rule=24):

    assert isinstance(rule, int)

    if len(dist) > 1:

        if isinstance(order, int):
            xw = [gk(order, d, rule) for d in dist]
        else:
            xw = [gk(order[i], dist[i], rule) for i in xrange(len(dist))]

        x = [_[0][0] for _ in xw]
        x = combine(x).T
        w = [_[1] for _ in xw]
        w = np.prod(combine(w), -1)

        return x, w

    foo = eval("gk" + str(rule))
    x, w = foo(order)
    x = dist.inv(ndtr(x))
    x = x.reshape(1, x.size)

    return x, w
Esempio n. 11
0
def gk(order, dist, rule=24):

    assert isinstance(rule, int)

    if len(dist) > 1:

        if isinstance(order, int):
            xw = [gk(order, d, rule) for d in dist]
        else:
            xw = [gk(order[i], dist[i], rule) for i in xrange(len(dist))]

        x = [_[0][0] for _ in xw]
        x = combine(x).T
        w = [_[1] for _ in xw]
        w = np.prod(combine(w), -1)

        return x, w

    foo = eval("gk" + str(rule))
    x, w = foo(order)
    x = dist.inv(ndtr(x))
    x = x.reshape(1, x.size)

    return x, w
Esempio n. 12
0
 def do_TJ(self, seq):
     if self.textstate.font is None:
         if STRICT:
             raise PDFInterpreterError('No font specified!')
         return
     #print "[-" + str(seq) + "-]"
     self.device.resetCharbboxes()
     self.device.render_string(self.textstate, seq) # ta metoda obliczy nam bounding boxy znakow i calego tekstu seq
     #fontik = self.textstate.font
     #print type(fontik)
     #exit()
     #textik = TagInterpreter.toUni(self.textstate.font, seq)
     self.__bbox = combine(self.__bbox, self.__bboxof(self.device)) # pobieramy bounding box calego tekstu
     self.__resetbbox(self.device)
     #print "TJ", self.bbox
     #print "[" + str(seq) + "]"
     if self.__mc:
         #lena = 0
         #lenb = 0
         for el in seq:
             assert(not isinstance(el, unicode))
             # dodajemy tekst do aktualnie przetwarzanej zawartosci oznaczonej z MCIDem
             if isinstance(el, str):
                 #print "[" + el + "]"
                 for bdc in self.__bdcs:
                     bdc.els.append(el)
                     #lena += len(el)
         for bdc in self.__bdcs:
             # pobieramy bouding boxy znakow
             for c in self.device.getCharbboxes():
                 bdc.charbboxes.append(c)
                 #lenb += 1
         #bdc.control.append(fontik)
         #bdc.control.append(seq)
         #for c in textik:
         #    #bdc.charbboxes.append(c)
         #    bdc.control.append(c)
         #    #lenb += 1
         #lena = len(textik)
         #if lena > 1000:
         #    print lena, lenb, len(seq), seq, self.device.getCharbboxes()
         #assert(lena == lenb)
     return
Esempio n. 13
0
def calculate_distance_mode(pairs, df, ref, mode, measure, ref2=[]):
    """
    From a set of pairs, calculate the distance of the offset of the 
    pair in relation to the global offset.

    Parameters:
    -----------
    pairs: list
        list containing tuples of IDs of norms [(id1,id2),(id3,id4)...]
    df: pandas.dataframe
        dataframe containing ids and embeddings of sentences
    ref: np.array
        vector containing the reference to measure the distance
    mode: string
        how vectors are combined (offset, concat or mean)
    measure: string
        measure to calculate the distance (euc or cos)
    ref2: np.array
        calculate the distance to a second reference

    Return:
    -------
        list containing the distance and ids in the form 
        [(dist, id1, id2), (dist, id2, id3),...]
    """
    vdist = []
    pb = progressbar.ProgressBar(len(pairs))
    for i, pair in enumerate(pairs):
        id1, id2 = pair
        combined = utils.combine(pair, df, mode=mode)
        dist = utils.calculate_distance(combined, ref, measure=measure)
        if len(ref2) > 0:
            dist2 = utils.calculate_distance(combined, ref2, measure=measure)
            vdist.append((dist, dist2, id1, id2))
        else:
            vdist.append((dist, id1, id2))
        pb.update()
        #if i == 1000: break
    return vdist
Esempio n. 14
0
def merge_strings_handler(line):
    lexer = VBLexer()
    tokens = lexer.tokenize(line)
    
    expr = exprFactory.get('string.concatenate')
    
    m = expr.search(tokens)
    
    # Sanity check (should be checked in the merge_strings_matcher)
    if m is not None:
        start,end = m.span
    
        s1 = tokens[start]      # This is the 1st string
        s2 = tokens[end - 1]    # This is the 2nd string
    
        return combine(' ', *[
            lexer.untokenize(tokens[:start]),
            '"{0}{1}"'.format(s1.data, s2.data),
            lexer.untokenize(tokens[end:])
        ])
    
    # Something went wrong.. just return the line
    return line
        mismatch_cost = DEFAULT_MISMATCH_COST
    else:
        print("* Defining own costs *")
        ins_cost = get_input_float('Please type the insertion penalty/cost')
        del_cost = get_input_float('Please type the deletion penalty/cost')
        match_cost = get_input_float('Please type the matching penalty/cost')
        mismatch_cost = get_input_float(
            'Please type the mismatching penalty/cost')

    #%% Algorithm Timing
    start_time = timer()
    V, paths = needleman_wunsch(S1, S2, ins_cost, del_cost, match_cost,
                                mismatch_cost)
    st1, st2, score = reconstruct(S1, S2, V, paths)

    combined = combine(V, paths, PATH_CHARACTERS)

    end_time = timer()
    duration_sec = end_time - start_time

    #%% Results
    print("** Results **")
    print(
        f"\tFor: Insertion Cost={ins_cost}, Deletion Cost={del_cost}, Match Cost={match_cost}, Mismatch Cost={mismatch_cost}"
    )

    print(f"Calculation took {duration_sec:.4f} seconds")
    print(f"V=\n{V}")
    print(f"Paths=\n{paths}")
    print(f"Combined=\n{combined}")
Esempio n. 16
0
# Delta labels
dAll = [
    "dCurrentFood", "dFoodUsed", "dFoodSold", "dFoodBought", "dFoodGathered",
    "dCurrentWood", "dWoodUsed", "dWoodSold", "dWoodBought", "dWoodGathered",
    "dCurrentMetal", "dMetalUsed", "dMetalSold", "dMetalBought",
    "dMetalGathered", "dCurrentStone", "dStoneUsed", "dStoneSold",
    "dStoneBought", "dStoneGathered", "dInfantryGained", "dInfantryLost",
    "dInfantryKilled", "dCavalryGained", "dCavalryLost", "dCavalryKilled",
    "dSupportGained", "dSupportLost", "dSupportKilled", "dSiegeGained",
    "dSiegeLost", "dSiegeKilled", "dStructuresGained", "dStructuresLost",
    "dStructuresDestroyed", "MilitaryMovementsOccurred",
    "SupportMovementsOccurred", "dDistanceEnemyBase"
]

print "Gathering and initializing data..."
utils.combine()
X = utils.getData()
X[All] = machine_learning.norm(X[All])
print "Done."

print "Hierarchial clustering..."
hierarchy = machine_learning.recursiveCluster(X[dAll], size=500)
Y = machine_learning.flatten(hierarchy, min=40)
X = X[Y >= 0]  # Eliminating outliers
Y = Y[Y >= 0]
y_values = np.unique(Y)
for i in range(0, len(y_values)):
    Y[Y == y_values[i]] = i
print "Done."

print "Visualizing..."
Esempio n. 17
0
class DummyConverter(PDFLayoutAnalyzer):
    
    def __init__(self, rsrcmgr, pageno=1, laparams=None):
        PDFLayoutAnalyzer.__init__(self, rsrcmgr, pageno=pageno, laparams=laparams)
        self.__bbox = None # bounding box przetwarzanego napisu
        self.__itembbox = None # bounding box aktualnie przetwarzanego znaku
        self.__charbboxes = [] # bounding boxy znakow przetwarzanego napisu
        return
    
    def getBbox(self):
        return self.__bbox
    
    def getCharbboxes(self):
        return self.__charbboxes
    
    # resetuje bounding boxy znakow przed przetworzeniem nastepnego napisu
    def resetCharbboxes(self):
        self.__charbboxes = []

    def setBbox(self, bbox):
        self.__bbox = bbox

    # zaslepka
    def write(self, text):
        return

    # zaslepka
    def receive_layout(self, ltpage):
        return
    
    # metoda ktora w pdfminerze zajmuje sie czym innym, ale my wykorzystujemy
    # ja do obliczania bounding boxow napisu seq i jego znakow
    def render_string(self, textstate, seq):
        matrix = mult_matrix(textstate.matrix, self.ctm)
        font = textstate.font
        fontsize = textstate.fontsize
        scaling = textstate.scaling * .01
        charspace = textstate.charspace * scaling
        wordspace = textstate.wordspace * scaling
        rise = textstate.rise
        if font.is_multibyte():
            wordspace = 0
        dxscale = .001 * fontsize * scaling
        if font.is_vertical():
            textstate.linematrix = self.render_string_vertical(
                seq, matrix, textstate.linematrix, font, fontsize,
                scaling, charspace, wordspace, rise, dxscale)
        else:
            textstate.linematrix = self.render_string_horizontal(
                seq, matrix, textstate.linematrix, font, fontsize,
                scaling, charspace, wordspace, rise, dxscale)
        return
    
    # metoda ktora w pdfminerze zajmuje sie czym innym, ale my wykorzystujemy
    # ja do obliczania bounding boxow napisu seq i jego znakow
    def render_string_horizontal(self, seq, matrix, (x,y), 
                                 font, fontsize, scaling, charspace, wordspace, rise, dxscale):
        needcharspace = False
        #print len(self.__charbboxes)
        #print "render: " + str(seq)
        for obj in seq:
            #print "<" + obj + ">"
            if isinstance(obj, int) or isinstance(obj, float):
                x -= obj*dxscale
                needcharspace = True
            else:
                for cid in font.decode(obj):
                    if needcharspace:
                        x += charspace
                    x += self.render_char(translate_matrix(matrix, (x,y)),
                                          font, fontsize, scaling, rise, cid)
                    self.__bbox = combine(self.__bbox, self.__itembbox) # dodajemy bounding box
                        # przetworzonego znaku do sumy bounding boxow juz przetworzonych znakow
                    if cid == 32 and wordspace:
                        x += wordspace
                    needcharspace = True
        #print len(self.__charbboxes)
        return (x, y)
Esempio n. 18
0
def golub_welsch(order, dist, acc=100, **kws):
    """
Golub-Welsch algorithm for creating quadrature nodes and weights

Parameters
----------
order : int
    Quadrature order
dist : Dist
    Distribution nodes and weights are found for with
    `dim=len(dist)`
acc : int
    Accuracy used in discretized Stieltjes procedure.
    Will be increased by one for each itteration.

Returns
-------
x : numpy.array
    Optimal collocation nodes with `x.shape=(dim, order+1)`
w : numpy.array
    Optimal collocation weights with `w.shape=(order+1,)`

Examples
--------
>>> Z = cp.Normal()
>>> x, w = cp.golub_welsch(3, Z)
>>> print x
[[-2.33441422 -0.74196378  0.74196378  2.33441422]]
>>> print w
[ 0.04587585  0.45412415  0.45412415  0.04587585]

Multivariate
>>> Z = cp.J(cp.Uniform(), cp.Uniform())
>>> x, w = cp. golub_welsch(1, Z)
>>> print x
[[ 0.21132487  0.21132487  0.78867513  0.78867513]
 [ 0.21132487  0.78867513  0.21132487  0.78867513]]
>>> print w
[ 0.25  0.25  0.25  0.25]
    """

    o = np.array(order)*np.ones(len(dist), dtype=int)+1
    P, g, a, b = stieltjes(dist, np.max(o), acc=acc, retall=True, **kws)

    X, W = [], []
    dim = len(dist)

    for d in xrange(dim):
        if o[d]:
            A = np.empty((2, o[d]))
            A[0] = a[d, :o[d]]
            A[1, :-1] = np.sqrt(b[d, 1:o[d]])
            vals, vecs = eig_banded(A, lower=True)

            x, w = vals.real, vecs[0, :]**2
            indices = np.argsort(x)
            x, w = x[indices], w[indices]

        else:
            x, w = np.array([a[d, 0]]), np.array([1.])

        X.append(x)
        W.append(w)

    if dim==1:
        x = np.array(X).reshape(1,o[0])
        w = np.array(W).reshape(o[0])
    else:
        x = combine(X).T
        w = np.prod(combine(W), -1)

    assert len(x)==dim
    assert len(w)==len(x.T)
    return x, w
Esempio n. 19
0
import cr, utils
from urlparse import urlparse

url = 'http://www.beeradvocate.com'
base_urls = [urlparse(url).netloc]
spider = cr.Spider(url, base_urls, 'beer.db', 0.1, '/beer')
spider.nstep(100000)
utils.combine('beer.db')
Esempio n. 20
0
def util_msg_handler(agent):
    """
    The util_msg_handler routine in the util_msg_propogation part; this method
    is run for non-leaf nodes; it waits till all the children of this agent
    have sent their util_msg, combines them, and then calculates and sends the
    util_msg to its parent; if this node is the root node, it waits till all
    the children have sent their util_msg, combines these messages, chooses the
    assignment for itself with the optimal utility, and then sends this
    assignment and optimal utility value to all its children and pseudo-
    children; assumes that the listening thread is active; given the 'agent'
    which runs this function.
    """

    # Wait till util_msg from all the children have arrived
    while True:
        all_children_msgs_arrived = True
        for child in agent.c:
            if ('util_msg_'+str(child)) not in agent.msgs:
                all_children_msgs_arrived = False
                break
        if all_children_msgs_arrived == True:
            break

    util_msgs = []
    for child in sorted(agent.c):
        util_msgs.append(agent.msgs['util_msg_'+str(child)])
    for child in sorted(agent.c):
        util_msgs.append(agent.msgs['pre_util_msg_'+str(child)])

    # Combine the util_msgs received from all children
    combined_msg, combined_ant = utils.combine(*util_msgs)

    info = agent.agents_info
    if agent.is_root:
        assert combined_ant == (agent.id, )

        # Choose the optimal utility
        utilities = list(combined_msg)
        max_util = max(utilities)
        xi_star = agent.domain[utilities.index(max_util)]
        agent.value = xi_star
        agent.max_util = max_util

        # Send the index of assigned value
        D = {}
        ind = agent.domain.index(xi_star)
        D[agent.id] = ind        
        for node in agent.c:
            agent.udp_send('value_msg_'+str(agent.id), D, node)
    else:
        util_cube, _ = get_util_cube(agent)

        # Combine the 2 cubes
        combined_cube, cube_ant = utils.combine(
            util_cube, combined_msg,
            tuple([agent.id] + [agent.p] + agent.pp), combined_ant
            )

        # Removing own dimension by taking maximum
        L_ant = list(cube_ant)
        ownid_index = L_ant.index(agent.id)
        msg_to_send = np.maximum.reduce(combined_cube, axis=ownid_index)
        # Ant to send in pre_util_msg
        ant_to_send = cube_ant[:ownid_index] + cube_ant[ownid_index+1:]

        # Creating the table to store
        cc = combined_cube
        table_shape = list(cc.shape[:])
        del table_shape[ownid_index]
        table_shape = tuple(table_shape)
        
        table = np.zeros(table_shape, dtype=object)
        cc_rolled = np.rollaxis(cc, ownid_index)
        for i, abc in enumerate(cc_rolled):
            for index, _ in np.ndenumerate(abc):
                if abc[index] == msg_to_send[index]:
                    table[index] = agent.domain[i]
        agent.table = table
        agent.table_ant = ant_to_send

        # Send the assignment-nodeid-tuple
        agent.udp_send('pre_util_msg_'+str(agent.id),
            ant_to_send, agent.p)

        agent.udp_send('util_msg_'+str(agent.id), msg_to_send, agent.p)
Esempio n. 21
0
def golub_welsch(order, dist, acc=100, **kws):
    """
Golub-Welsch algorithm for creating quadrature nodes and weights

Parameters
----------
order : int
    Quadrature order
dist : Dist
    Distribution nodes and weights are found for with
    `dim=len(dist)`
acc : int
    Accuracy used in discretized Stieltjes procedure.
    Will be increased by one for each itteration.

Returns
-------
x : numpy.array
    Optimal collocation nodes with `x.shape=(dim, order+1)`
w : numpy.array
    Optimal collocation weights with `w.shape=(order+1,)`

Examples
--------
>>> Z = cp.Normal()
>>> x, w = cp.golub_welsch(3, Z)
>>> print x
[[-2.33441422 -0.74196378  0.74196378  2.33441422]]
>>> print w
[ 0.04587585  0.45412415  0.45412415  0.04587585]

Multivariate
>>> Z = cp.J(cp.Uniform(), cp.Uniform())
>>> x, w = cp. golub_welsch(1, Z)
>>> print x
[[ 0.21132487  0.21132487  0.78867513  0.78867513]
 [ 0.21132487  0.78867513  0.21132487  0.78867513]]
>>> print w
[ 0.25  0.25  0.25  0.25]
    """

    o = np.array(order) * np.ones(len(dist), dtype=int) + 1
    P, g, a, b = stieltjes(dist, np.max(o), acc=acc, retall=True, **kws)

    X, W = [], []
    dim = len(dist)

    for d in xrange(dim):
        if o[d]:
            A = np.empty((2, o[d]))
            A[0] = a[d, :o[d]]
            A[1, :-1] = np.sqrt(b[d, 1:o[d]])
            vals, vecs = eig_banded(A, lower=True)

            x, w = vals.real, vecs[0, :]**2
            indices = np.argsort(x)
            x, w = x[indices], w[indices]

            # p = P[-1][d]
            # dp = po.differential(p, po.basis(1,1,dim)[d])
            #
            # x = x - p(x)/dp(x)
            # x = x - p(x)/dp(x)
            # x = x - p(x)/dp(x)
            #
            # z = np.arange(dim)
            # arg = np.array([k*(z == d) for k in range(2*o[d]-3)])
            # b_ = dist.mom(arg.T)
            #
            # X_, r = np.meshgrid(x, np.arange(2*o[d]-3))
            # X_ = X_**r
            # w = np.linalg.lstsq(X_, b_)[0].flatten()
            # print "w", w.shape
            # print np.linalg.lstsq(X_, b_)[0]

        else:
            x, w = np.array([a[d, 0]]), np.array([1.])

        X.append(x)
        W.append(w)

    if dim == 1:
        x = np.array(X).reshape(1, o[0])
        w = np.array(W).reshape(o[0])
    else:
        x = combine(X).T
        w = np.prod(combine(W), -1)

    assert len(x) == dim
    assert len(w) == len(x.T)
    return x, w
Esempio n. 22
0
from utils import combine
import sys
import os.path
from utils import read_solution_line
from shutil import copyfile

if len(sys.argv) != 2:
	print("Usage: combine_solutions.py [file1]")
else:		
	file = sys.argv[1]
	combine("COMBINED SOLUTIONS", file)
	copyfile("COMBINED SOLUTIONS", "CURRENT BEST SOLUTIONS")
	print("Copied new solutions to CURRENT BEST SOLUTIONS.")
	print("Done.")
	# Writing to solutions.out
	print("Writing to solutions.out")
	write_file = open("solutions.out", "w")
	read_file = open("COMBINED SOLUTIONS", "r")
	data = read_file.readlines()
	for line in data:
		info = read_solution_line(line)
		solution = info[3]
		if solution.isspace():
			write_file.write("None\n")
		else:
			solution = solution[1:]
			write_file.write(solution + "\n")
	print("Done.")
Esempio n. 23
0
         found = i  # linia po i-1 odstepie, ktory jest ostatnim (i-ta kolumna)
         break
     if before == None:  # (**) nie jestesmy w stanie ustalic, czy linia jest
         # przed czy po odstepie i musimy dzielic miedzy kolumny
         # poszczegolne znaki
         break
     if before:  # linia przed i-tym odstepem (i-ta kolumna)
         found = i
         break
 if found != None:  # ok, znalezlismy kolumne do ktorej nalezy linia,
     # dodajemy ja do kolumny
     assert (l.getPageId() != None)
     textgroups[i].add(l)
     #print "TUTAJ:", l.getBbox(), textgroups[i].getBbox()
     textgroups[i].setBbox(
         combine(textgroups[i].getBbox(), l.getBbox()))
     continue
 # jezeli jestesmy tutaj to wystapila sytuacja (**) powyzej
 for _ in range(self.__divNum + 1):
     line = PDFMinerNode(
         "textline")  # tworzymy linie dla kazdej kolumny,
     # bedziemy do nich przydzielac znaki
     line.setPageId(pageid)
     newLines.append(line)
 for text in l.getChildren():  # dla kazdego znaku:
     if text.getBbox() == None:  #[0.0, 0.0, 0.0, 0.0]:
         continue
     i = 0  # indeks kolumny
     for div in columnDivs:
         if self.__before(text, div, l, prev,
                          next):  # jezeli znak jest
Esempio n. 24
0
from utils import combine
import sys
import os.path
from utils import read_solution_line
from shutil import copyfile

if len(sys.argv) != 2:
    print("Usage: combine_solutions.py [file1]")
else:
    file = sys.argv[1]
    combine("COMBINED SOLUTIONS", file)
    copyfile("COMBINED SOLUTIONS", "CURRENT BEST SOLUTIONS")
    print("Copied new solutions to CURRENT BEST SOLUTIONS.")
    print("Done.")
    # Writing to solutions.out
    print("Writing to solutions.out")
    write_file = open("solutions.out", "w")
    read_file = open("COMBINED SOLUTIONS", "r")
    data = read_file.readlines()
    for line in data:
        info = read_solution_line(line)
        solution = info[3]
        if solution.isspace():
            write_file.write("None\n")
        else:
            solution = solution[1:]
            write_file.write(solution + "\n")
    print("Done.")
        b = DEFAULT_B
        match_cost = DEFAULT_MATCH_COST
        mismatch_cost = DEFAULT_MISMATCH_COST
    else:
        print("* Defining own costs *")
        a = get_input_float('Please type the value for a in f(k) = a + (b * k)')
        b = get_input_float('Please type the value for b in f(k) = a + (b * k)')
        match_cost = get_input_float('Please type the matching penalty/cost')
        mismatch_cost = get_input_float('Please type the mismatching penalty/cost')

    #%% Algorithm Timing
    start_time = timer()
    F, E, G, F_paths, E_paths, G_paths = gotoh(S1, S2, match_cost, mismatch_cost, a, b)
    st1, st2, score = reconstruct(S1, S2, F, E, G, F_paths, E_paths, G_paths)

    combined_F = combine(F, F_paths, PATH_F_CHARACTERS)
    combined_G = combine(G, G_paths, PATH_G_CHARACTERS)
    combined_E = combine(E, E_paths, PATH_E_CHARACTERS)

    end_time = timer()
    duration_sec = end_time - start_time

    #%% Results
    print("** Results **")

    print(f"Calculation took {duration_sec:.4f} seconds")
    print(f"Combined_F=\n{combined_F}")
    print(f"Combined_G=\n{combined_G}")
    print(f"Combined_E=\n{combined_E}")

    print(f"Original Inputs:")