Sage 10.4 matrix mult fails on matrices over an exterior algebra

47 views
Skip to first unread message

Seth Chaiken

unread,
Mar 6, 2025, 6:43:02 PM3/6/25
to sage-support
In Sage 10.4,
Ext=ExteriorAlgebra(QQ,['p'])
Ext.inject_variables()
Mp = matrix(1,1,[[p]])
Mp[0,0]*Mp[0,0]
-> 0
Mp*Mp 
FAILS!
(This did work in an older version.)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 Mp*Mp File /data/Software/Sage/sage/src/sage/structure/element.pyx:4116, in sage.structure.element.Matrix.__mul__() 4114 # better be square for the product to be defined. 4115 if (<Matrix>left)._nrows == (<Matrix>left)._ncols: -> 4116 return (<Matrix>left)._matrix_times_matrix_(<Matrix>right) 4117 else: 4118 parent = (<Matrix>left)._parent File /data/Software/Sage/sage/src/sage/matrix/matrix0.pyx:5675, in sage.matrix.matrix0.Matrix._matrix_times_matrix_() 5673 return self._multiply_strassen(right) 5674 else: -> 5675 return self._multiply_classical(right) 5676 5677 cdef bint _will_use_strassen(self, Matrix right) except -2: File /data/Software/Sage/sage/src/sage/matrix/matrix_generic_dense.pyx:323, in sage.matrix.matrix_generic_dense.Matrix_generic_dense._multiply_classical() 321 m = i*snc 322 for k in range(snc): --> 323 z += left._entries[m+k]._mul_(right._entries[k*nc+j]) 324 v[p] = z 325 p += 1 TypeError: 'NotImplementedType' object is not callable

Nils Bruin

unread,
Mar 6, 2025, 7:30:27 PM3/6/25
to sage-support
It's probably a matter that ExteriorAlgebraElement has a "cdef _mul_" now. That means in cython code it can access the method if it has enough type information. That's probably why `p*p` works. It looks like in sage.matrix.matrix_generic_dense.Matrix_generic_dense._multiply_classical not enough type info is available on left._entries[m+k] to look up this _mul_ . It may be that the correct typecast, something like "(<Element> left._entries[m+k])" perhaps, does the trick. Or perhaps _mul_ is supposed to be "def" or "cpdef". I doubt the latter, though, since for the most part, multiplication seems to work: the coercion framework can figure out how to do it:

sage: coercion_model.bin_op(p,p,operator.mul)
0

it makes sense that matrix multiplication takes a shortcut to avoid the overhead of the coercion framework. But perhaps it doesn't have enough type info.

Nils Bruin

unread,
Mar 7, 2025, 12:47:31 PM3/7/25
to sage-support
I can confirm that changing in sage.matrix.matrix_generic_dense.Matrix_generic_dense._multiply_classical the line from left._entries[m+k]._mul_ to just
left._entries[m+k]* , the code works as expected. So the problem is probably the _mul_.

I think this is a general problem that was not discovered before: as far as I can see, it's legal for element types to have a cdef _mul_. However, in the matrix code, I think it ends up being looked up as a python method. I'm not sure that matrix elements always have to inherit from Element, so I'm not sure a cython typecast to it is warranted. Changing to "*" and just let the coercion framework figure it out is definitely valid, but it should be checked if that brings a performance penalty.

Someone should make a ticket and alert the coercion gurus to it.

Trevor Karn

unread,
Mar 7, 2025, 2:28:41 PM3/7/25
to sage-support
https://github.com/sagemath/sage/issues/39648

I am no guru but I think I can tackle it.

Reply all
Reply to author
Forward
0 new messages