numarray :: multiplying all the elements in 1d array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh Jeevanandam

    #1

    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

  • Mandus

    #2
    Re: numarray :: multiplying all the elements in 1d array

    Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam:[color=blue]
    > 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.[/color]

    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.

    Comment

    • Robert Kern

      #3
      Re: numarray :: multiplying all the elements in 1d array

      Suresh Jeevanandam wrote:[color=blue]
      > 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.[/color]

      r = multiply.reduce (a)

      --
      Robert Kern
      robert.kern@gma il.com

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

      Comment

      • Tim Hochberg

        #4
        Re: numarray :: multiplying all the elements in 1d array

        Mandus wrote:[color=blue]
        > Tue, 20 Dec 2005 19:32:13 +0530 skrev Suresh Jeevanandam:
        >[color=green]
        >>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.[/color]
        >
        >
        > 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.[/color]


        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

        [color=blue][color=green][color=darkred]
        >>> a = na.arange(10000 0)
        >>> l = range(100000)
        >>> t0 = time.clock(); na.multiply.red uce(a); print time.clock() - t0[/color][/color][/color]
        0
        0.0038896004939 2[color=blue][color=green][color=darkred]
        >>> t0 = time.clock(); na.multiply.red uce(l); print time.clock() - t0[/color][/color][/color]
        0
        0.042208716471[color=blue][color=green][color=darkred]
        >>> t0 = time.clock(); reduce(mul, a); print time.clock() - t0[/color][/color][/color]
        0
        0.0943455103931[color=blue][color=green][color=darkred]
        >>> t0 = time.clock(); reduce(mul, l); print time.clock() - t0[/color][/color][/color]
        0
        0.0146099574108

        Comment

        Working...