[C++-sig] Overloading sqrt(5.5)*myvector

Roman Yakovenko roman.yakovenko at gmail.com
Wed Dec 26 08:40:50 CET 2007


On Dec 26, 2007 8:11 AM, Bruce Sherwood <Bruce_Sherwood at ncsu.edu> wrote:
> Sorry to repeat myself and be insistent, but could someone please at
> least comment on whether I'm doing anything obviously wrong, even if you
> don't immediately have a solution to my serious problem? There was no
> response to my question (see copy below) which I sent to both the numpy
> and Boost mailing lists.
>
> To the Boost experts: Is there something wrong, or something I
> could/should change in how I'm trying to define to Boost the overloaded
> multiplication of a numpy square root (or other numpy function) times my
> own "vector" object? I'm seeing a huge performance hit in going from
> Numeric to numpy because Numeric sqrt returned float whereas numpy sqrt
> returns numpy.float64, so that the result is not one of my vector
> objects. I don't have a problem with myvector*sqrt(5.5). Here is what I
> currently am doing:
>
> py::class_<vector>("vector", py::init< py::optional<double, double,
> double> >())
>      .def( self * double())
>      .def( double() * self)
>
> Desperately,

If I understand you right, than you can do something like this:

replace __mul__ method of numpy.float64 class:

old_mul = numpy.float64.__mul__

def new_mul( self, other ):
    if other isinstance( vector ):
       return other*self
    else:
       return old_mul( self, other )

numpy.float64.__mul__ = new_mul

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list