How can I override the __call__ method of a matrix
I'd like to override the __call__ method of a matrix so that M(v) returns M*v. That is, I'd like to use functional notation. So I tried the following:
class MyMatrix(Matrix):
def __call__(self, v):
return self*v
but this doesn't work. Here is the error message I get when I try the above code in sage:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-88-e8d789594dd8> in <module>()
----> 1 class MyMatrix(Matrix):
2 def __call__(self, v):
3 return self*v
4
TypeError: Error when calling the metaclass bases
object() takes no parameters
I don't know anything about metaclasses. After banging my head against various walls, trying to learn, I am asking here.
The problem above comes from the fact that
Matrix
is not a class.versus
You need to inherit from a matrix class for example
Ok, I understand that Matrix is a metaclass, not a class. But I really want the functionality of Matrix, not some special case. In my particular usecase I don't know how the matrix will be given. I just know that the __init__ that comes with Matrix can handle the input. For example - what if the input has entries in some number field? Those are not integers...
Matrix is not even a metaclass, it is a class factory ;-) You can use the generic class but still the concrete matrix class constructor (i.e. __init__) does not handle the coefficient as you might think of. This is mostly done within the class factory Matrix.