Example #1
0
 def k_theta(self, Y, theta, x):
     """
     Считает K_{theta}
     :param Y:
     :param theta:
     :param x:
     :return:
     """
     H = self.jacobian(theta, x)
     W = self.weight(x)
     Q = np.dot(W.T, W)
     A = hr.prod(H.T, Q, H)
     Ainv = np.linalg.inv(A)
     k_epsilon = self.k_epsilon_block(Y, theta, x)
     k_theta = hr.prod(Ainv, H.T, Q, k_epsilon, Q, H, Ainv)
     return k_theta
Example #2
0
 def glsm_theta(self, Y, theta, x):
     """
     ОМНК
     :param theta:
     :param x:
     :param Y:
     :return:
     """
     F = self.solve(theta, x)
     H = self.jacobian(theta, x)
     W = self.weight(x)
     Q = np.dot(W.T, W)
     A = hr.prod(H.T, Q, H)
     Ainv = np.linalg.inv(A)
     E = Y - F
     dTHETA = hr.prod(Ainv, H.T, Q, E)
     return dTHETA, F
Example #3
0
def day3_2(area):
    """Determine the number of trees you would encounter for the following slopes"""
    slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
    trees = []
    for slope in slopes:
        r, d = slope
        count = day3_1(area=area, right=r, down=d)
        trees.append(count)
    print(f"Number of trees on each of the slopes: {trees}")
    return prod(trees)
Example #4
0
 def k_y(self, Y, theta, x):
     """
     Считает K_{y}
     :param Y:
     :param theta:
     :param x:
     :return:
     """
     k_theta = self.k_theta(Y, theta, x)
     h = s.singe_jacobian(THETA, X[0])
     k_y = hr.prod(h, k_theta, h.T)
     return k_y
Example #5
0
 def lsm_theta(self, Y, theta, x):
     """
     МНК
     :param theta:
     :param Y:
     :param x:
     :return:
     """
     F = self.solve(theta, x)
     H = self.jacobian(theta, x)
     A = np.dot(H.T, H)
     Ainv = np.linalg.inv(A)
     E = Y - F
     dTHETA = hr.prod(Ainv, H.T, E)
     return dTHETA, F
Example #6
0
def day10_2(text):
    """Total number of arrangements the adapters to connect the charging outlet to your device?"""
    numbers = sorted(data(text=text, parser=int, sep="\n"))
    jolts = [0] + numbers + [max(numbers) + 3]
    diffs = [y - x for x, y in zip(jolts[:-1], jolts[1:])]
    indices = [0] + [i + 1 for i, x in enumerate(diffs) if x == 3]

    print(
        "Part Two: When a 3-jolt difference occurs, we split the list in two lists."
    )

    # When split on 3-jolt differences, the lists has length 1, 2, 3, 4, 5
    mapping = {1: 1, 2: 1}
    for i in [3, 4, 5]:
        mapping[i] = calculate_nr_paths_tree(list_length=i)

    arrangements = []
    len_subseq = []

    for i, index in enumerate(indices[:-1]):
        subseq = jolts[index:indices[i + 1]]
        j = len(subseq)
        len_subseq.append(j)
        arrangements.append(mapping[j])

    nr_distinct_arrangements = prod(arrangements)

    print(f"The unique lengths of the contiguous sets are:"
          f" {sorted(list(set(len_subseq)))}")
    print(f"\t A contiguous set with length 5, e.g. {list(range(0, 5))}, "
          f"has {mapping[5]} distinct arrangements.")
    # print(len_sublist)
    # print(arrangements)
    print(f"Part Two: The total number of distinct ways I can arrange "
          f"the adapters to connect the charging outlet"
          f" to your my device {nr_distinct_arrangements}")

    return nr_distinct_arrangements
def day20_1(text):
    input_data = data(text=text, parser=parse_tile, sep="\n\n")

    borders = dict()

    for nr, tile in input_data:
        borders[nr] = rotate_and_flip(tile)

    # For each tile, return a list of IDs of adjacent tiles.
    adjacent_tiles = dict()

    for tile, value in borders.items():
        other = {k: borders[k] for k in borders if k != tile}
        adjacent_tiles[tile] = [
            k for k, v in other.items() if not set(value).isdisjoint(v)
        ]

    # Corner tiles have two borders, i.e. two adjacent tiles
    corner_tiles = [k for k, v in adjacent_tiles.items() if len(v) == 2]
    print(
        f"Part One: multiply the id's of the four corner tiles: {prod(corner_tiles)}"
    )

    return prod(corner_tiles)