[SciPy-user] element by element multiplication and round off

Robert Kern rkern at ucsd.edu
Wed Aug 3 02:32:47 CDT 2005


Ryan Krauss wrote:
> I have a vector of imaginary numbers that I need to square and do 
> somethings element-by-element.  If I simply do
> In [55]: test=svect**2
> In [56]: test[-1]
> Out[56]: (-96731.992735076812+1.1845861292127511e-011j)
> I get round off error.
> 
> But if I do it element-by-element in a for loop the problem goes away:
> In [57]: test2=svect[-1]**2
> In [58]: test2
> Out[58]: (-96731.992735076812+0j)
> 
> Or
> In [59]: sqout=[]
> 
> In [60]: for ent in svect.tolist():
>   ....:     curent=ent**2
>   ....:     sqout.append(curent)
>   ....:
> In [61]: test2=array(sqout)
> 
> In [62]: test2[-1]
> Out[62]: (-96731.992735076812+0j)
> 
> Why is this and can I get rid of the round off without having to write 
> for loops for everything?

Python's complex object special-cases integer exponents by strength 
reduction if the exponent < 100. Numeric does not. You can use Scipy's 
real_if_close() to get the real component iff the imaginary part is 
close to zero (within +-1e-13 of 0 by default) and return the full array 
otherwise.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the SciPy-user mailing list