473,791 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

numarray :: multiplying all the elements in 1d array

Hi all,
Lets say I have an array:
from numarray import *
a = array([ 6, 7, 8, 9, 10, 11, 12])

I want to multiply out all the elements and get the result.

r = 1.0
for i in a:
r = r*i

Is there any faster, efficient way of doing this.

Thanks,

regards,
Suresh

Dec 20 '05 #1
3 12418
Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam:
Hi all,
Lets say I have an array:
from numarray import *
a = array([ 6, 7, 8, 9, 10, 11, 12])

I want to multiply out all the elements and get the result.

r = 1.0
for i in a:
r = r*i

Is there any faster, efficient way of doing this.


You can use multiply.reduce (a) (multiply is a function imported from
numarray).

With regular python you can also do:

from operator import mul
reduce(mul,a)

This work even when 'a' is a plain python list.
--
Mandus - the only mandus around.
Dec 20 '05 #2
Suresh Jeevanandam wrote:
Hi all,
Lets say I have an array:
from numarray import *
a = array([ 6, 7, 8, 9, 10, 11, 12])

I want to multiply out all the elements and get the result.

r = 1.0
for i in a:
r = r*i

Is there any faster, efficient way of doing this.


r = multiply.reduce (a)

--
Robert Kern
ro*********@gma il.com

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

Dec 20 '05 #3
Mandus wrote:
Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam:
Hi all,
Lets say I have an array:
from numarray import *
a = array([ 6, 7, 8, 9, 10, 11, 12])

I want to multiply out all the elements and get the result.

r = 1.0
for i in a:
r = r*i

Is there any faster, efficient way of doing this.

You can use multiply.reduce (a) (multiply is a function imported from
numarray).

With regular python you can also do:

from operator import mul
reduce(mul,a)

This work even when 'a' is a plain python list.

Actually, they'll both work with a plain python list. Multiply.reduce is
much faster (see below) when operating on arrays, but somewhat slower
when operating on lists. I'd guess from the overhead of converting the
list to an array.

-tim

a = na.arange(10000 0)
l = range(100000)
t0 = time.clock(); na.multiply.red uce(a); print time.clock() - t0 0
0.0038896004939 2 t0 = time.clock(); na.multiply.red uce(l); print time.clock() - t0 0
0.042208716471 t0 = time.clock(); reduce(mul, a); print time.clock() - t0 0
0.0943455103931 t0 = time.clock(); reduce(mul, l); print time.clock() - t0

0
0.0146099574108

Dec 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1989
by: Marco Bubke | last post by:
Hi I have tried to include numarray in Pyrex but I get allways this error: Traceback (most recent call last): File "gears.py", line 9, in ? import gl File "/home/marco/projects/test_pyrex/gl.pyx", line 40, in gl ctypedef class numarray.NumArray :
4
1992
by: Marco Bubke | last post by:
Hi Ok, I get a reproduceable seqmentation fault. Ok, fist the code: cdef NumArray array_to_double(NumArray array): # maybe here is memoty leak! cdef NumArray flat_array_obj flat_array_obj = NA_InputArray(NA_updateDataPtr(array), tFloat64,
2
2971
by: Marc Schellens | last post by:
Following the NumPy documentation, I took over some C code, but run into an error. Does anybody have a suggestion? Thanks, marc gdlpython.cpp:225: `PyArray_Type' undeclared (first use this function) #include <python2.3/Python.h>
3
1600
by: SunX | last post by:
I tried to initialize a float point array by: import numarray xur = numarray.fromfunction(lambda x,y,z:x*y*z, (2, 2, 2)) but I ended up with an integer array even though x, y, and z are all floats. BTW, how do you unzip NumTut in windows? And is there a newer version? Thank you
11
1992
by: grv | last post by:
So it is supposed to be very fast to have an array of say 5 million integers stored in a binary file and do a = numarray.fromfile('filename', (2, 2, 2)) numarray.add(a, 9, a) but how is that faster than reading the entire file into memory and then having a for loop in C: (loop over range) { *p++ += 9 }
3
1558
by: Mizrandir | last post by:
I'd like to subclass numarray's array. I'd like to add some methods and override others like __init__. I don't know how to do this and haven't found help searching the manual or the web, can someone help? For example imagine I just want to do something as simple as making a subclass "NewClass" with the __init__ method overridden so that the behaviour would be: >>> a = NewClass(2) >>> print a
1
3214
by: Chris P. | last post by:
Hi. I have a very simple task to perform and I'm having a hard time doing it. Given an array called 'x' (created using the numarray library), is there a single command that rounds each of its elements to the nearest integer? I've already tried something like >>> x_rounded = x.astype(numarray.Int) but that only truncates each element (i.e. '5.9' becomes '5'). I've read over all the relevant numarray documentation, and it
6
1488
by: Matt Feinstein | last post by:
Is there an optimal way to apply a function to the elements of a two-d array? What I'd like to do is define some function: def plone(x): return x+1 and then apply it elementwise to a 2-D numarray. I intend to treat the function as a variable, so ufuncs are probably not appropriate-- I
1
1957
by: Qiangning Hong | last post by:
To draw a large array of data on a small panel, I need to shrink it to a given size. e.g: To draw numarray.arange(10000) on a panel of width of 100, I only need to draw the points of (0, 100, 200, 300, ...) instead of (0, 1, 2, ...). So I need a method to shrink it to an 100-item array. x will not work. If the panel's width is 60, that expression will return an array of 61 elements.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.