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

Steve Schmerler elcorto at gmx.net
Wed Aug 3 02:14:11 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?
>
> Thanks,
>
> Ryan
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>

Hi

This seems to be because

<some_array>**2

is done by Numeric while

<some_number>**2

is not.

In [32]: import Numeric as N

In [34]: a=N.array([3.0j,4.0j])

In [35]: a
Out[35]: array([ 0.+3.j,  0.+4.j])

In [36]: a**2
Out[36]: array([ -9.+1.10214572e-015j, -16.+1.95937017e-015j])

In [37]: a[0]**2
Out[37]: (-9+0j)

In [38]: N.power(a[0],2.0)
Out[38]: (-9+1.1021457184401395e-015j)

Numeric uses C internally and therefore you are limited to double.
Though I'm not an expert and I don't know what Python really does
different Numeric when you say

a[0]**2

cheers,
steve

--
Women are like cell phones. They like to be held and talked to, but push
the wrong button, and you'll be disconnected.