Forum: Ruby Multiplying array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-01-23 16:05
HI,

Does anyone know of a function that multiplies the contents of an array.
For example:

one = [1,2,3]
two = [[2],[3],[4]]

output = [[2],[6],[12]]

I've written a simple function that does this, however I'm sure there is
a better way, instead of casting the item to a float.

def multiplyArray(arr1,arr2)
  newArr = []
  i=0
  arr1.each do |x|
    x = x.to_s.to_f
    item = arr2[i].to_s.to_f
    ele = x * item
    newArr.push(ele)
    i = i + 1
   end
  puts newArr
end

Thanks,
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-01-23 16:25
(Received via mailing list)
On 23.01.2007 16:05, WKC CCC wrote:
> Does anyone know of a function that multiplies the contents of an array.
> For example:
>
> one = [1,2,3]
> two = [[2],[3],[4]]
>
> output = [[2],[6],[12]]
>
> I've written a simple function that does this, however I'm sure there is
> a better way, instead of casting the item to a float.

Why do you cast to float?  You can multiply integers directly.

First with a straightforward array "two":

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> one = [1,2,3]
=> [1, 2, 3]
irb(main):003:0> two = [2,3,4]
=> [2, 3, 4]
irb(main):004:0> one.to_enum(:zip, two).map {|a,b| a*b}
=> [2, 6, 12]

Now with your array:

irb(main):005:0> two.map! {|i| [i]}
=> [[2], [3], [4]]
irb(main):006:0> one.to_enum(:zip, two).map {|a,b| a * b[0]}
=> [2, 6, 12]

Kind regards

  robert
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2007-01-23 16:36
(Received via mailing list)
On Wed, 24 Jan 2007, WKC CCC wrote:

> I've written a simple function that does this, however I'm sure there is
>    i = i + 1
>   end
>  puts newArr
> end
>
> Thanks,

if you are doing lots of this then check out narray:

   harp:~ > cat a.rb
   require 'narray'

   a = NArray.to_na [1,2,3]
   b = NArray.to_na [2,3,4]

   p( a * b )

   harp:~ > ruby a.rb
   NArray.int(3):
   [ 2, 6, 12 ]

regards.

-a
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-23 17:26
(Received via mailing list)
WKC CCC wrote:
> Does anyone know of a function that multiplies the contents of an array.

module Enumerable
  def product
    inject{ |piece, prod| prod*piece }
  end
  def sum
    inject(0){ |piece, total| total+piece }
  end
end

a = (1..10)
p a.sum
#=> 55
p a.product
#=> 3628800
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-01-23 17:34
(Received via mailing list)
On Jan 23, 2007, at 11:25 AM, Phrogz wrote:

>   end
> end
>
> a = (1..10)
> p a.sum
> #=> 55
> p a.product
> #=> 3628800

module Enumerable
   def product
     inject(1) { |prod, piece| prod*piece }
   end
   def sum
     inject(0) { |total, piece| total+piece }
   end
end

True, except the block parameters were reversed.  In these cases, the
final result wasn't affected because the operations are commutative.

I also added the argument to the inject within product for symmetry.
You could equally remove the 0 argument from the sum method's use of
inject.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-23 17:51
(Received via mailing list)
Rob Biedenharn wrote:
> final result wasn't affected because the operations are commutative.
Man, I swear that every time I use #inject I get it backwards, and
think that I've fixed the answer in my mind for the next time. Thanks
for the correction. (Anyone got a good _why-like mnemonic they use for
remembering the order?)

> I also added the argument to the inject within product for symmetry.
> You could equally remove the 0 argument from the sum method's use of
> inject.

I thought about that, but in my mind, the sum of an empty array is
zero, but the product of an empty array is nil (or 0?), not 1. Of
course, totally up the the OP as to how s/he wants to handle this edge
case.
0395dce49ee7fa03ad9c10e8105d9245?d=identicon&s=25 WKC CCC (wkc)
on 2007-01-23 17:56
How can the inject function work on 2 arrays?
5282a59bd311c99bf650bddfbc307f6a?d=identicon&s=25 Patrick Gundlach (pgundlach)
on 2007-01-23 17:57
[inject confusion]

 (Anyone got a good _why-like mnemonic they use for
> remembering the order?)

not good, but it helps me:

"memo" comes first (alphabetically), so it is

inject(memo, obj)


P.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-23 18:15
(Received via mailing list)
WKC CCC wrote:
> How can the inject function work on 2 arrays?

It can't; I only read the first line of your post (which described a
single array) and rushed off my answer. As penance, I give you another
solution:

a = [ 1, 2, [3], [4], 5]
b = [ 6, [7], [8], 9, 10 ]

p a.flatten.zip(b.flatten).map{ |a,b| a*b }
#=> [6, 14, 24, 36, 50]
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2007-01-23 21:09
(Received via mailing list)
Hi,

Am Mittwoch, 24. Jan 2007, 01:57:36 +0900 schrieb Patrick Gundlach:
> [inject confusion]
>
> > (Anyone got a good _why-like mnemonic they use for
> > remembering the order?)
>
> not good, but it helps me:
>
> "memo" comes first (alphabetically), so it is
>
> inject(memo, obj)

I'm trying to help myself with the order invariance in

  a == a.inject([]) { |r,e| r+[e] }
  a == a.inject([]) { |r,e| r << e }

Bertram
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-23 22:43
(Received via mailing list)
WKC CCC wrote:
> I've written a simple function that does this, however I'm sure there is
>     i = i + 1
>    end
>   puts newArr
> end
>
> Thanks,
>
> --
> Posted via http://www.ruby-forum.com/.

[1,2,3].zip([2,3,4]).map{|a,b| a*b}
    ==>[2, 6, 12]
This topic is locked and can not be replied to.