[Tutor] numarray issues

bob gailer bgailer at gmail.com
Tue Dec 16 02:19:34 CET 2008


Mr Gerard Kelly wrote:
> Hello, I am a python beginner, and I have a question about scalars vs
> arrays.
>
> I am making a (very simple) code, and part of it is:
>
> from numarray import *
> from math import *
>
> def source(x,y):
>   f=sin(x)
>   return f
>
> a=4.
>
> m=10
>
> dx=a/(m+1.)
>
> x=zeros([m,1], Float)
>
> print x[0]
>
> print func(x[0])
>
> When I run this code, I get:
>
> [ 0.]
> 0.0
>
> However, if I change the line f=sin(x) to f=2*x, I will get:
>
> [ 0.]
> [ 0.]
>
> Why does func(x[0]) return a scalar for sin(x), but returns an array for
> 2*x?
>
> If I want a scalar returned for f=2*x, I need to put in func(x[0,0]).
> Why do x[0] and x[0,0] both work for sin(x), but only x[0,0] will work
> for 2*x?
>   

sin() expects a scalar value.
x[0] is a 1 element array.
numarray magically converts the 1 element array to a scalar.
x[0]*2 multiplies each element of the array by 2 giving an array result.

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   


-- 
Bob Gailer
Chapel Hill NC 
919-636-4239



More information about the Tutor mailing list