sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.47k stars 488 forks source link

Can't multiply a linear morphism defined by a matrix by an element of the base field #16830

Open nthiery opened 10 years ago

nthiery commented 10 years ago
sage: K = GF(7); K
Finite Field of size 7
sage: sage: phi = End(K^2)([[1,1],[1,1]])
sage: phi
Vector space morphism represented by the matrix:
[1 1]
[1 1]
Domain: Vector space of dimension 2 over Finite Field of size 7
Codomain: Vector space of dimension 2 over Finite Field of size 7
sage: 2*phi
Vector space morphism represented by the matrix:
[2 2]
[2 2]
Domain: Vector space of dimension 2 over Finite Field of size 7
Codomain: Vector space of dimension 2 over Finite Field of size 7

So far, so good. But:

sage: K(2)*phi
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-81e4c450e31e> in <module>()
----> 1 K(Integer(2))*phi

/opt/sage-git/local/lib/python2.7/site-packages/sage/structure/element.so in sage.structure.element.RingElement.__mul__ (build/cythonized/sage/structure/element.c:15355)()

/opt/sage-git/local/lib/python2.7/site-packages/sage/structure/coerce.so in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:8323)()

TypeError: unsupported operand parent(s) for '*': 'Finite Field of size 7' and 'Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Finite Field of size 7 to Vector space of dimension 2 over Finite Field of size 7'

In particular this prevents illustrating Cayley-Hamilton on the morphism side:

sage: phi.characteristic_polynomial()(phi)
...
TypeError: unsupported operand parent(s) for '*': 'Finite Field of size 7' and 'Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Finite Field of size 7 to Vector space of dimension 2 over Finite Field of size 7'

Update 2016/09/20: things have gotten even worse. Now 2*phi does not work anymore.

CC: @tscrim

Component: linear algebra

Issue created by migration from https://trac.sagemath.org/ticket/16830

nthiery commented 10 years ago

Description changed:

--- 
+++ 
@@ -13,14 +13,13 @@
 Vector space morphism represented by the matrix:
 [2 2]
 [2 2]
+Domain: Vector space of dimension 2 over Finite Field of size 7
+Codomain: Vector space of dimension 2 over Finite Field of size 7

So far, so good. But:

-
-Domain: Vector space of dimension 2 over Finite Field of size 7
-Codomain: Vector space of dimension 2 over Finite Field of size 7
 sage: K(2)*phi
 ---------------------------------------------------------------------------
 TypeError                                 Traceback (most recent call last)
@@ -33,3 +32,11 @@

 TypeError: unsupported operand parent(s) for '*': 'Finite Field of size 7' and 'Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Finite Field of size 7 to Vector space of dimension 2 over Finite Field of size 7'

+ +In particular this prevents illustrating Cayley-Hamilton on the morphism side: + + +sage: phi.characteristic_polynomial()(phi) +... +TypeError: unsupported operand parent(s) for '*': 'Finite Field of size 7' and 'Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Finite Field of size 7 to Vector space of dimension 2 over Finite Field of size 7' +

nbruin commented 10 years ago
comment:2

That looks like the category machinery is not cooperating with coercion properly. The scalar multiplication should be there according to the categories:

sage: parent(phi).categories()
[Category of hom sets in Category of modules over Finite Field of size 7,
 Category of vector spaces over Finite Field of size 7,
 Category of modules over Finite Field of size 7,
 Category of bimodules over Finite Field of size 7 on the left and Finite Field of size 7 on the right,
 Category of right modules over Finite Field of size 7,
 Category of left modules over Finite Field of size 7,
 Category of commutative additive groups,
 Category of additive groups,
 Category of additive inverse additive unital additive magmas,
 Category of commutative additive monoids,
 Category of additive monoids,
 Category of additive unital additive magmas,
 Category of commutative additive semigroups,
 Category of additive commutative additive magmas,
 Category of additive semigroups,
 Category of additive magmas,
 Category of hom sets in Category of sets,
 Category of hom sets in Category of sets with partial maps,
 Category of hom sets in Category of objects,
 Category of sets,
 Category of sets with partial maps,
 Category of objects]

By the way, going up the inheritance in the code:

sage: sage.modules.vector_space_homspace.VectorSpaceHomspace.mro()
[sage.modules.vector_space_homspace.VectorSpaceHomspace,
 sage.modules.free_module_homspace.FreeModuleHomspace,
 sage.categories.homset.HomsetWithBase,
 sage.categories.homset.Homset,
 sage.structure.parent.Set_generic,
 sage.structure.parent.Parent,
 sage.structure.category_object.CategoryObject,
 sage.structure.sage_object.SageObject,
 object]

and reading the code, it's completely unclear how parent(phi) got those module categories attached to it. It would have to happen somewhere in sage.modules.vector_space_homspace.VectorSpaceHomspace, sage.modules.free_module_homspace.FreeModuleHomspace but none of those classes have an __init__.

nthiery commented 10 years ago
comment:3

Replying to @nbruin:

That looks like the category machinery is not cooperating with coercion properly. The scalar multiplication should be there according to the categories:

Well, categories are doing nothing here but stating the math. This issue is really about the concrete class VectorSpaceMorphism for morphisms defined by a matrix which is responsible for implementing multiplication by scalars, and apparently does not.

By the way, going up the inheritance in the code:

sage: sage.modules.vector_space_homspace.VectorSpaceHomspace.mro()
[sage.modules.vector_space_homspace.VectorSpaceHomspace,
 sage.modules.free_module_homspace.FreeModuleHomspace,
 sage.categories.homset.HomsetWithBase,
 sage.categories.homset.Homset,
 sage.structure.parent.Set_generic,
 sage.structure.parent.Parent,
 sage.structure.category_object.CategoryObject,
 sage.structure.sage_object.SageObject,
 object]

and reading the code, it's completely unclear how parent(phi) got those module categories attached to it. It would have to happen somewhere in sage.modules.vector_space_homspace.VectorSpaceHomspace, sage.modules.free_module_homspace.FreeModuleHomspace but none of those classes have an __init__.

The setting of the category for a homset is handled generically in Homset.init.

Cheers, Nicolas

nthiery commented 8 years ago

Description changed:

--- 
+++ 
@@ -40,3 +40,5 @@
 ...
 TypeError: unsupported operand parent(s) for '*': 'Finite Field of size 7' and 'Set of Morphisms (Linear Transformations) from Vector space of dimension 2 over Finite Field of size 7 to Vector space of dimension 2 over Finite Field of size 7'

+ +Update 2016/09/20: things have gotten even worse. Now 2*phi does not work anymore.

nthiery commented 8 years ago
comment:5

Having a brief look at the code, MatrixMorphism implements a __mul__ method to handle multiplication by other types of morphisms. But this prevents the coercion model to take over when needed!

Instead, MatrixMorphism should implement _mul_ for basic multiplication, handle multiplication by other types of morphisms by coercion, and implement _lmul_ or _acted_upon (I never quite remember from the top of my head which one should be used in this situation) to implement multiplication by scalar).

mkoeppe commented 3 years ago
comment:8

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.