[Tutor] numarray issues

2008-12-15 Thread Mr Gerard Kelly
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?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] numarray issues

2008-12-15 Thread bob gailer

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@python.org
http://mail.python.org/mailman/listinfo/tutor

  



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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] numarray issues

2008-12-15 Thread bob gailer




Please always reply-all so a copy goes to the list.

Mr Gerard Kelly wrote:

  is there a quick way to convert a one element array into a scalar value
so that I can use x[0] for all operations?
  


x = x[0]


  
- Original Message -
From: bob gailer bgai...@gmail.com
Date: Tuesday, December 16, 2008 11:19 am
Subject: Re: [Tutor] numarray issues
  
  
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@python.org
http://mail.python.org/mailman/listinfo/tutor

  
  


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



  
  
  



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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor