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

Kevin Jacobs <jacobs@bioinformed.com> bioinformed at gmail.com
Thu Dec 27 16:30:02 CET 2007


Hi Bruce,

I have to add that I don't know the answer to your question either, but I do
know that it is solvable and that once the list experts return,
enlightenment will soon follow.  My confidence comes from knowing the Python
internals for how left and right multiplication are performed.  As long as
the "left" __mul__ operator returns NotImplemented, then the __rmul__ method
will be attempted (see http://docs.python.org/ref/numeric-types.html).  Of
course, I don't know how to declare such a beast in Boost, having never used
it, but I'm sure it is possible.  My intuition is that the first problem you
need to solve is getting Boot to generate the appropriate __rmul__ method.
The second problem, if it even exists, is ensuring that __mul__ returns
NotImplemented.

Best of luck,
-Kevin



On Dec 27, 2007 10:15 AM, Bruce Sherwood <Bruce_Sherwood at ncsu.edu> wrote:

> I should have added: This structure worked with the older version of
> VPython which used Numeric, but it doesn't work in the beta version
> which uses numpy. Since I don't know enough about either numpy or Boost,
> I'm left guessing which subsystem is the source of my difficulties, and
> clueless about how to remedy them.
>
> Bruce Sherwood
>
> Bruce Sherwood wrote:
> > Thanks for the comment, which limits the range of possible solutions.
> > The VPython vector class is implemented in C++, not in Python. I made up
> > the simple test in my previous note to try out the solution that had
> > been offered and which you have usefully ruled out. Here is the relevant
> > part of the vector class, which indeed doesn't look like an ndarray:
> >
> > inline vector
> > operator*( const double s) const throw()
> > { return vector( s*x, s*y, s*z); }
> >
> > and here is the free function for right multiplication:
> >
> > inline vector
> > operator*( const double& s, const vector& v)
> > { return vector( s*v.x, s*v.y, s*v.z); }
> >
> > Maybe the unsolvable problem is in the Boost definitions:
> >
> > py::class_<vector>("vector", py::init< py::optional<double, double,
> > double> >())
> >      .def( self * double())
> >      .def( double() * self)
> >
> > Left multiplication is fine, but right multiplication isn't.
> >
> > Bruce Sherwood
> >
> > Robert Kern wrote:
> >
> >> Bruce Sherwood wrote:
> >>
> >>
> >>> Thanks for the suggestion. It hadn't occurred to me to try to override
> >>> numpy as you suggest. However, when I try the code shown below as the
> >>> start of a test of this scheme, I get the following error:
> >>>
> >>> Traceback (most recent call last):
> >>>   File "C:\Documents and Settings\Bruce\My
> >>> Documents\0VPythonWork\vectors.py", line 24, in <module>
> >>>     numpy.float64.__mul__ = new_mul
> >>> TypeError: can't set attributes of built-in/extension type '
> numpy.float64'
> >>>
> >>> I'm copying this to the numpy discussion list, as maybe someone there
> >>> will see where to go starting from your suggestion.
> >>>
> >>>
> >> Like most (or all) builtin-types, the numpy float scalars do not permit
> >> replacing their methods from Python.
> >>
> >> I'm not familiar with vpython's vector. If you can make it "not look
> like an
> >> ndarray", then you should be able to just implement __rmul__ on vector.
> >>
> >>
> >>
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071227/b620fb7c/attachment.htm>


More information about the Cplusplus-sig mailing list