'Apply `self` to `elem`.'

def _apply(self, elem):

    if (not (elem in self.domain)):
        raise ValueError("The    supplied    element    doesn't    belong    to    the    domain")
    if elem.is_identity:
        return self.codomain.identity
    else:
        images = self.images
        value = self.codomain.identity
        if isinstance(self.domain, PermutationGroup):
            gens = self.domain.generator_product(elem, original=True)
            for g in gens:
                if (g in self.images):
                    value = (images[g] * value)
                else:
                    value = ((images[(g ** (-1))] ** (-1)) * value)
        else:
            i = 0
            for (_, p) in elem.array_form:
                if (p < 0):
                    g = (elem[i] ** (-1))
                else:
                    g = elem[i]
                value = (value * (images[g] ** p))
                i += abs(p)
    return value
