Example #1
0
 def score(self,n_ijk,alpha_ijk_in):
     alpha_ijk = copy.deepcopy(alpha_ijk_in)
     prod_k = sum22(subtract(matrixGammaLog(add(n_ijk,alpha_ijk)),matrixGammaLog(alpha_ijk)))
     alpha_ij = sum22(alpha_ijk_in)
     n_ij = sum22(n_ijk)
     prod_ij = subtract(matrixGammaLog(alpha_ij), matrixGammaLog(add(alpha_ij,n_ij)))
     return sum2(add(prod_ij,prod_k))
Example #2
0
def multiply_karatsuba(x, y):
    """
    Multiplies two numbers represented as arrays
    using the Karatsuba algorithm, falling back
    on grade school algorithm for the base case

    :param x: []int
    :param y: []int
    :rtype []int
    """
    x, y = match_padding(x, y)
    a, b = split(x)
    c, d = split(y)

    if len(x) == 1:
        return multiply_simple(x, y)

    res_1 = multiply_karatsuba(a, c)
    res_2 = multiply_karatsuba(b, d)

    partial = multiply_karatsuba(add(a, b), add(c, d))
    # res_3 is partial - res_1 - res_2.
    # To simplify, just add res_1 and res_2 then subtract that sum from partial
    res_3 = subtract(partial, add(res_1, res_2))

    res = add(pad(res_1, len(x), 'right'), res_2,
              pad(res_3, (len(x) + 1) // 2, 'right'))

    return res
def multiply_karatsuba_parallel(x, y, key=None):
    """
    Multiplies two numbers represented as arrays
    using the Karatsuba algorithm, falling back
    on grade school algorithm for the base case

    :param x: []int
    :param y: []int
    :rtype []int
    """
    x, y = match_padding(x, y)
    a, b = split(x)
    c, d = split(y)

    # for base case, go simple
    if len(x) == 1:
        return multiply_simple(x, y)

    # for big numbers, go parallel
    if len(x) > 300:
        # generate random ids for the subprocess outputs
        r1 = random.random()
        r2 = random.random()
        r3 = random.random()

        # run the sub-multiplications in parallel
        p1 = Process(target=multiply_karatsuba_parallel, args=[a, c, r1])
        p2 = Process(target=multiply_karatsuba_parallel, args=[b, d, r2])
        p3 = Process(target=multiply_karatsuba_parallel,
                     args=[add(a, b), add(c, d), r3])

        p1.start()
        p2.start()
        p3.start()
        p1.join()
        p2.join()
        p3.join()

        # get the results
        res_1 = return_dict[r1]
        res_2 = return_dict[r2]
        partial = return_dict[r3]

    # for smaller numbers, don't bother parallelizing
    else:
        res_1 = multiply_karatsuba_parallel(a, c)
        res_2 = multiply_karatsuba_parallel(b, d)
        partial = multiply_karatsuba_parallel(add(a, b), add(c, d))

    # do the karatsuba shuffle
    res_3 = subtract(partial, add(res_1, res_2))
    res = add(pad(res_1, len(x), 'right'), res_2,
              pad(res_3, (len(x) + 1) // 2, 'right'))

    # if we are in parallel mode, write the result to the global dict
    if key is not None:
        return_dict[key] = res

    return res
Example #4
0
def distance_along_edge_to_point(edge, distance_along_edge):
    edge_start, edge_end = edge
    edge_vector = util.subtract(edge_end, edge_start)
    edge_length = util.norm(edge_vector)    
    scale_factor = distance_along_edge / edge_length
    scaled_vector = util.scale(scale_factor, edge_vector)
    point = util.add(scaled_vector, edge_start)
    return point
Example #5
0
 def compute_edge_length(self, tube_point_a, tube_point_b):
     tube_coords_a = [tube_point_a.geospatial[0], tube_point_b.geospatial[1],
                      tube_point_a.tube_elevation]
     tube_coords_b = [tube_point_b.geospatial[0], tube_point_b.geospatial[1],
                      tube_point_b.tube_elevation]
     edge_vector = util.subtract(tube_coords_a, tube_coords_b)
     edge_length = np.linalg.norm(edge_vector)
     return edge_length
    def get_alignment(self):
        """
        :return: alignment represented as a tuple of aligned strings
        """
        # start from point with maximum value
        result = ("", "",
                  np.unravel_index(self.scores.argmax(), self.scores.shape))
        self.alignment_points = []

        # add candidates according to scores matrix
        while True:
            aligned1, aligned2, point = result
            self.alignment_points.append(point)

            if self.scores[point] == 0:
                return result[0:2]

            else:
                letter1 = self.string1[point[1] - 1]
                letter2 = self.string2[point[0] - 1]

                if self.scores[point] - self.scores[subtract(
                        point, (1, 1))] == self.subs[letter1][letter2]:
                    result = (letter1 + aligned1, letter2 + aligned2,
                              subtract(point, (1, 1)))

                elif self.scores[point] - self.scores[subtract(
                        point, (1, 0))] == self.gap_penalty:
                    result = ("-" + aligned1, letter2 + aligned2,
                              subtract(point, (1, 0)))

                elif self.scores[point] - self.scores[subtract(
                        point, (0, 1))] == self.gap_penalty:
                    result = (letter1 + aligned1, "-" + aligned2,
                              subtract(point, (0, 1)))
Example #7
0
    def get_alignments(self):
        """
        :return: list of alignments, where each alignment is represented
                 as a tuple of aligned strings
        """
        # keep candidates in stack
        stack = [("", "", (self.rows - 1, self.cols - 1))]
        alignments = []

        # loop candidates until stack is empty
        while stack:
            aligned1, aligned2, point = stack.pop()

            if point == (0, 0):
                # stop condition
                alignments.append((aligned1, aligned2))
                continue

            letter1 = self.string1[point[1] - 1]
            letter2 = self.string2[point[0] - 1]

            # add candidates according to scores matrix

            if self.scores[point] - self.scores[subtract(
                    point, (1, 1))] == self.subs[letter1][letter2]:
                stack.append((letter1 + aligned1, letter2 + aligned2,
                              subtract(point, (1, 1))))

            if self.scores[point] - self.scores[subtract(
                    point, (1, 0))] == self.gap_penalty:
                stack.append(("-" + aligned1, letter2 + aligned2,
                              subtract(point, (1, 0))))

            if self.scores[point] - self.scores[subtract(
                    point, (0, 1))] == self.gap_penalty:
                stack.append((letter1 + aligned1, "-" + aligned2,
                              subtract(point, (0, 1))))

        return alignments
Example #8
0
import torch