# Multiplying array

**URL:** <https://rubytalk.org/t/multiplying-array/34726>\
**Category:** ruby-talk\
**Created:** [23 January 2007 15:05 UTC](https://rubytalk.org/t/multiplying-array/34726 "2007-01-23T15:05:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![WKC\_CCC](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@WKC\_CCC](https://rubytalk.org/u/WKC_CCC)\
**Post date:** [23 January 2007 15:05 UTC](https://rubytalk.org/t/multiplying-array/34726/1 "2007-01-23T15:05:47Z")

</div>

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)  
&nbsp;&nbsp;newArr = []  
&nbsp;&nbsp;i=0  
&nbsp;&nbsp;arr1.each do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;x = x.to\_s.to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;item = arr2[i].to\_s.to\_f  
&nbsp;&nbsp;&nbsp;&nbsp;ele = x \* item  
&nbsp;&nbsp;&nbsp;&nbsp;newArr.push(ele)  
&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;puts newArr  
end

Thanks,

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 January 2007 15:25 UTC](https://rubytalk.org/t/multiplying-array/34726/2 "2007-01-23T15:25:06Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [23 January 2007 15:36 UTC](https://rubytalk.org/t/multiplying-array/34726/3 "2007-01-23T15:36:04Z")

</div>

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

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;require 'narray'

&nbsp;&nbsp;&nbsp;a = NArray.to\_na [1,2,3]  
&nbsp;&nbsp;&nbsp;b = NArray.to\_na [2,3,4]

&nbsp;&nbsp;&nbsp;p( a \* b )

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

regards.

-a

> **···**
>
> On Wed, 24 Jan 2007, WKC CCC wrote:
> 
> > 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|  
> > &nbsp;&nbsp;&nbsp;x = x.to\_s.to\_f  
> > &nbsp;&nbsp;&nbsp;item = arr2[i].to\_s.to\_f  
> > &nbsp;&nbsp;&nbsp;ele = x \* item  
> > &nbsp;&nbsp;&nbsp;newArr.push(ele)  
> > &nbsp;&nbsp;&nbsp;i = i + 1  
> > &nbsp;&nbsp;end  
> > puts newArr  
> > end
> > 
> > Thanks,
> 
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [23 January 2007 16:25 UTC](https://rubytalk.org/t/multiplying-array/34726/4 "2007-01-23T16:25:10Z")

</div>

WKC CCC wrote:

> Does anyone know of a function that multiplies the contents of an array.

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

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

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [23 January 2007 21:40 UTC](https://rubytalk.org/t/multiplying-array/34726/5 "2007-01-23T21:40:08Z")

</div>

WKC CCC wrote:

> 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)  
> &nbsp;&nbsp;newArr =   
> &nbsp;&nbsp;i=0  
> &nbsp;&nbsp;arr1.each do |x|  
> &nbsp;&nbsp;&nbsp;&nbsp;x = x.to\_s.to\_f  
> &nbsp;&nbsp;&nbsp;&nbsp;item = arr2[i].to\_s.to\_f  
> &nbsp;&nbsp;&nbsp;&nbsp;ele = x \* item  
> &nbsp;&nbsp;&nbsp;&nbsp;newArr.push(ele)  
> &nbsp;&nbsp;&nbsp;&nbsp;i = i + 1  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;puts newArr  
> end
> 
> Thanks,
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

[1,2,3].zip([2,3,4]).map{|a,b| a\*b}  
&nbsp;&nbsp;&nbsp;&nbsp;==\>[2, 6, 12]

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [23 January 2007 16:33 UTC](https://rubytalk.org/t/multiplying-array/34726/6 "2007-01-23T16:33:10Z")

</div>

module Enumerable  
&nbsp;&nbsp;&nbsp;def product  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(1) { |prod, piece| prod\*piece }  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;def sum  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(0) { |total, piece| total+piece }  
&nbsp;&nbsp;&nbsp;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](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Jan 23, 2007, at 11:25 AM, Phrogz wrote:
> 
> > WKC CCC wrote:
> > 
> > > Does anyone know of a function that multiplies the contents of an array.
> > 
> > module Enumerable  
> > &nbsp;&nbsp;def product  
> > &nbsp;&nbsp;&nbsp;&nbsp;inject{ |piece, prod| prod\*piece }  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;def sum  
> > &nbsp;&nbsp;&nbsp;&nbsp;inject(0){ |piece, total| total+piece }  
> > &nbsp;&nbsp;end  
> > end
> > 
> > a = (1..10)  
> > p a.sum  
> > #=\> 55  
> > p a.product  
> > #=\> 3628800

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [23 January 2007 16:50 UTC](https://rubytalk.org/t/multiplying-array/34726/7 "2007-01-23T16:50:12Z")

</div>

Rob Biedenharn wrote:

> module Enumerable  
> &nbsp;&nbsp;&nbsp;def product  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(1) { |prod, piece| prod\*piece }  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;def sum  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject(0) { |total, piece| total+piece }  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> True, except the block parameters were reversed. In these cases, the  
> 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.

---

<div class="post-metadata">

**Author:** ![WKC\_CCC](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@WKC\_CCC](https://rubytalk.org/u/WKC_CCC)\
**Post date:** [23 January 2007 16:57 UTC](https://rubytalk.org/t/multiplying-array/34726/8 "2007-01-23T16:57:12Z")

</div>

How can the inject function work on 2 arrays?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Patrick\_Gundlach13](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@Patrick\_Gundlach13](https://rubytalk.org/u/Patrick_Gundlach13)\
**Post date:** [23 January 2007 16:57 UTC](https://rubytalk.org/t/multiplying-array/34726/9 "2007-01-23T16:57:36Z")

</div>

[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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [23 January 2007 17:15 UTC](https://rubytalk.org/t/multiplying-array/34726/10 "2007-01-23T17:15:09Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [23 January 2007 20:07 UTC](https://rubytalk.org/t/multiplying-array/34726/11 "2007-01-23T20:07:44Z")

</div>

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
> 
> &nbsp;&nbsp;a == a.inject() { |r,e| r+[e] }  
> &nbsp;&nbsp;a == a.inject() { |r,e| r \<\< e }
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
