[SciPy-dev] Suppressing of numpy __mul__, __div__ etc
Charles R Harris
charlesr.harris@gmail....
Wed Dec 16 21:18:11 CST 2009
2009/12/16 Sebastian Walter <sebastian.walter@gmail.com>
> I have also implemented/wrapped various automatic differentiation
> tools in python
> (http://github.com/b45ch1/algopy , http://github.com/b45ch1/pyadolc,
> http://github.com/b45ch1/pycppad if someone is interested)
> and I have also come across some numpy glitches and inconsistencies.
>
> However, this particular problem I have not encountered. I'm not sure
> I understand the rationale why an expression like numpy.array([1,2] *
> oofun(1) should call the oofun.__rmul__ operator.
> Hence, I'm a little sceptical about this enhancement.
>
> What is wrong with the following implementation? It works perfectly fine...
>
> --------------- start code snippet -----------------
>
> class oofun:
> def __init__(self,x):
> self.x = x
>
> def __mul__(self, rhs):
> print 'called __mul__'
> if isinstance(rhs, oofun):
> return oofun(self.x * rhs.x)
> else:
> return rhs * self
>
> def __rmul__(self, lhs):
> print 'called __rmul__'
> return oofun(self.x * lhs)
>
> def __str__(self):
> return str(self.x)+'a'
>
> def __repr__(self):
> return str(self)
>
> ------------- end code snippet ----------------
>
> --------- output ----------
> basti@shlp:~/Desktop$ python live_demo.py
> called __mul__
> called __rmul__
> called __rmul__
> called __rmul__
> [2.0a 2.0a 2.0a]
> called __rmul__
> called __rmul__
> called __rmul__
> [2.0a 2.0a 2.0a]
> ------------- end output --------------
>
>
>
That makes the behavior consistent. But suppose as a convenience one wants
to implement left/right multiplication by python integers but doesn't want
to allow multiplication by arrays? Or wants multiplication by arrays
array-wise, not element-wise?
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-dev/attachments/20091216/bc0e1397/attachment.html
More information about the SciPy-Dev
mailing list