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

Bruce Sherwood Bruce_Sherwood@ncsu....
Thu Dec 27 09:15:07 CST 2007


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@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   


More information about the Numpy-discussion mailing list