Ticket #15305: coercion_for_tensor_product-nb.patch

File coercion_for_tensor_product-nb.patch, 2.4 KB (added by nborie, 6 years ago)
  • sage/combinat/free_module.py

    # HG changeset patch
    # Parent c79a95523d6e00de590bd130a8fb1e92fcb9d185
    
    diff --git a/sage/combinat/free_module.py b/sage/combinat/free_module.py
    a b class CombinatorialFreeModule_Tensor(Com 
    25362536            """
    25372537            return self.tensor_constructor(tuple(element.parent() for element in elements))(*elements)
    25382538
     2539        def _coerce_map_from_(self, TensorParent):
     2540            r"""
     2541            EXAMPLES::
     2542
     2543                sage: C = CombinatorialFreeModule(ZZ, Set([1,2])) 
     2544                sage: D = CombinatorialFreeModule(ZZ, Set([2,4])) 
     2545                sage: f = C.module_morphism(on_basis=lambda x: D.monomial(2*x), codomain=D)
     2546                sage: f.register_as_coercion()
     2547                sage: T = tensor((C,C))
     2548                sage: p = D.an_element()
     2549                sage: T(tensor((p,p)))
     2550                Traceback (most recent call last):
     2551                ...
     2552                AssertionError: do not know how to coerce Free module generated by {2, 4} over Integer Ring # Free module generated by {2, 4} over Integer Ring into Free module generated by {1, 2} over Integer Ring # Free module generated by {1, 2} over Integer Ring (there is no coercion map from Free module generated by {2, 4} over Integer Ring into Free module generated by {1, 2} over Integer Ring)
     2553                sage: T = tensor((D,D))
     2554                sage: p = C.an_element()
     2555                sage: T(tensor((p,p)))
     2556                4*B[2] # B[2] + 4*B[2] # B[4] + 4*B[4] # B[2] + 4*B[4] # B[4]
     2557            """
     2558            assert(isinstance(TensorParent, CombinatorialFreeModule_Tensor))
     2559            modules = TensorParent._sets
     2560            assert(module in ModulesWithBasis(self.base_ring()) for module in modules)
     2561            for i, Parent_i in enumerate(modules):
     2562                assert (self._sets[i]).has_coerce_map_from(Parent_i), "do not know how to coerce %s into %s (there is no coercion map from %s into %s)"%(TensorParent, self, Parent_i, self._sets[i])
     2563            vector_map = [(self._sets[i]).coerce_map_from(module) for i,module in enumerate(modules)]
     2564            result = TensorParent.module_morphism(on_basis=lambda x: self._tensor_of_elements([vector_map[i](modules[i].monomial(x[i])) for i in range(len(modules))]), codomain=self)
     2565            return result
     2566
     2567
    25392568class CartesianProductWithFlattening(object):
    25402569    """
    25412570    A class for cartesian product constructor, with partial flattening