import math


def get_morph_function(weights):
    """Returns a function which computes for a fixed list w of scalar weights the linear combination
                                  vertex + sum_i(w[i] * targets[i])
    where vertex and targets[i] are vectors."""

    def apply_weight(weight, target_coordinate):
        return weight * target_coordinate

    def weighted_sum(vertex_coordinate, *targets_coordinate):
        return vertex_coordinate + math.fsum(map(apply_weight, weights, targets_coordinate))

    def apply_morph_target(vertex, *targets):
        return tuple(map(weighted_sum, vertex, *targets)) + ((vertex[-1],) if len(vertex) == 4 else ())

    return apply_morph_target