pythonsymbolic-mathsage

Unexpected error with sign() function in SageMath


I have the following rather strange issue with SageMath (mathematics software system partly based on Python). Perhaps it should be mentioned that the problem seem to be related with some specific SageMath libraries (dealing with permutations and Lie algebras). Here is the part of the code which does not compile for some reason:

gl = lie_algebras.gl(QQ, 4)
Ugl = gl.pbw_basis()
E = matrix(Ugl, 4, 4, Ugl.gens())

for sigma in Permutations(4):
    for tau in Permutations(4):
        pr = sigma.signature() * tau.signature()
        for i in range(4):
            pr = pr * E[sigma[i] - 1, tau[i] - 1]

The interesting thing is that while the code above does not work well, the issue could be resolved just by adding "1 * ", see below:

gl = lie_algebras.gl(QQ, 4)
Ugl = gl.pbw_basis()
E = matrix(Ugl, 4, 4, Ugl.gens())

for sigma in Permutations(4):
    for tau in Permutations(4):
        pr = sigma.signature() * tau.signature()
        for i in range(4):
            pr = 1 * pr * E[sigma[i] - 1, tau[i] - 1]

My experiments show that there is some issue with SageMath function signature() since if I replace the line pr = sigma.signature() * tau.signature() with pr = 1, then both versions compile successfully. The final block of the error message for the first excerpt is as follows:

/usr/lib/python3/dist-packages/sage/algebras/lie_algebras/poincare_birkhoff_witt.py in _act_on_(self, x, self_on_left)
    513             # Try the _acted_upon_ first as it might have a direct PBW action
    514             #   implemented that is faster
--> 515             ret = x._acted_upon_(self, not self_on_left)
    516             if ret is not None:
    517                 return ret

AttributeError: 'int' object has no attribute '_acted_upon_'

Therefore, it might be the case that there is something strange happening with the action of integers on element of Lie algebras (some type-related issue?).

Of course, since the second version works I can proceed, but it looks weird and I would be really interested in understanding the source of this error (to avoid similar ones in the future).


Solution

  • The underlying issue is that there are two types of integers in Sage: Python's int and Sage's Integer. The latter is better for much of the arithmetic done in Sage, but for some reason, sigma.signature() returns an int. Multiplying an int by an Integer (like 1) automatically converts the result to an Integer, which is why your second example works. You can also fix this by explicitly converting pr to an Integer:

    pr = Integer(sigma.signature() * tau.signature())
    

    In fact something is strange to me, because the signature of the signature method says

    Signature:      s.signature() -> 'Integer'
    

    So it should be returning an Integer. Looks like a bug to me, or else whoever wrote the code thought that specifying the return type in the signature would force the method to return that type, and I don't think it works that way.