[Tutor] Matrix class problems
Prahlad Vaidyanathan
slime@vsnl.net
Sun, 30 Jun 2002 18:31:54 +0530
Hi,
I have recently got back to exploring python, and, being a Maths
student, I decided to test my skills on a simple Matrix class. Now, I
came across some questions which I hope you will help clear.
Firstly, I would like to know what exactly repr() does. I read the
docs, and this is what I got :
"""
>>> help(repr)
Help on built-in function repr:
repr(...)
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
"""
What does this mean and how is this different from str() ?
Also, I wrote a __mul__() function, which looks like so :
"""
def __mul__ (self, other) :
if type(other) == type(0) :
return self._scalar_multiply(other)
else :
return self._matrix_multiply(other)
"""
Now, this works, but like this :
"""
>>> import matrix
>>> m = matrix.Matrix([[1,0,0],[0,1,0],[0,0,1]])
>>> print (m*2)
[ 2, 0, 0 ]
[ 0, 2, 0 ]
[ 0, 0, 2 ]
>>> print (2*m)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for *: 'int' and 'instance'
"""
I understand that, in the first case, I am calling the function I
wrote, but in the second case I am calling the equivalent __mul__
function of an 'int' object. My question is, how do I make the latter
point to the former ? Although this is not a major issue, it bugs me
because I tend to write the number first, and the matrix later by habit.
I hope I have been clear enough.
pv.
ps. For those of you who study Maths, I have been racking my brains as
to how to get the determinant of a Matrix, and have been unsuccessful.
How do I find the minor matrix ? Aaaarggghhh !!
--
Prahlad Vaidyanathan <http://www.symonds.net/~prahladv/>
When things go well, expect something to explode, erode, collapse or
just disappear.