Velocity Reviews > Ruby > cartesian product

# cartesian product

walter a kehowski
Guest
Posts: n/a

 08-07-2005
Hello,

How would I create a function that will take list (array) of integers and
return their cartesian product?

Thank You,

Walter Kehowski
nuby

Giovanni Intini
Guest
Posts: n/a

 08-07-2005
> How would I create a function that will take list (array) of integers and
> return their cartesian product?

You could do something like:

a =3D [1, 2, 3]

b =3D []

a.each do |elementone|
a.each do |elementtwo|
b << [elementone, elementtwo]
end
end

That should do the trick (I haven't tested it though)

walter a kehowski
Guest
Posts: n/a

 08-07-2005
"Giovanni Intini" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> How would I create a function that will take list (array) of integers and
> return their cartesian product?

>You could do something like:
>
>a = [1, 2, 3]
>
>b = []
>
>a.each do |elementone|
> a.each do |elementtwo|
> b << [elementone, elementtwo]
> end
>end
>
>That should do the trick (I haven't tested it though)

Giovanni,

Yes, that works. Another nuby question: How to make it a function? Such as
cartprod(a,b) returning c?

Walter

walter a kehowski
Guest
Posts: n/a

 08-07-2005

"Giovanni Intini" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Remember that in Ruby you seldom have standalone functions. You should
probably create a class that has the cartprod method.

Giovanni,

Here's what I have so far:

def cartprod(a,b)

c=[]

a.each do |ae|
b.each do |be|
c << [ae, be]
end
end

return c

end

a=[1,2,3]

b=[4,5,6]

c=cartprod(a,b)

c.each { |ce| print ce,"\n" }

and this does print all the elements of c. Great. Now how do I do a class?
Can I create a new method for Array?

Walter Kehowski

Giovanni Intini
Guest
Posts: n/a

 08-07-2005
Remember that in Ruby you seldom have standalone functions. You should
probably create a class that has the cartprod method.

daz
Guest
Posts: n/a

 08-07-2005

walter a kehowski wrote:
>
>
> Here's what I have so far:
> [code]
>
> and this does print all the elements of c.
> Great. Now how do I do a class?
> Can I create a new method for Array?
>

class Array
def cartprod(b)
c=[]
each do |ae|
b.each do |be|
c << [ae, be]
end
end
c
end
end

a=[1,2,3]
b=[4,5,6]

c = a.cartprod(b)

c.each { |ce| p ce }

daz

Giovanni Intini
Guest
Posts: n/a

 08-07-2005
if you want to create a new method for array just do

class Array
def cartprod(ary)
result =3D []
self.each do |ae|
ary.each do |be|
result << [ae, be]
end
end
result
end
end

this way you can have an array and do new_array =3D
source_array.cartprod(other_array)

Robert Klemme
Guest
Posts: n/a

 08-07-2005
walter a kehowski <(E-Mail Removed)> wrote:
> "Giovanni Intini" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> Remember that in Ruby you seldom have standalone functions. You should
> probably create a class that has the cartprod method.
>
> Giovanni,
>
> Here's what I have so far:
>
> def cartprod(a,b)
>
> c=[]
>
> a.each do |ae|
> b.each do |be|
> c << [ae, be]
> end
> end
>
> return c
>
> end
>
> a=[1,2,3]
>
> b=[4,5,6]
>
> c=cartprod(a,b)
>
> c.each { |ce| print ce,"\n" }
>
> and this does print all the elements of c. Great. Now how do I do a
> class? Can I create a new method for Array?
>
> Walter Kehowski

Note that it might be more memory efficient to write a iteration method:

def cartprod(a,b)
a.each {|ae| b.each {|be| yield ae, be}}
end

Then you can also do this if needed

c=[]
cartprod(a,b) {|x,y| c << [x,y]}

If you just need every combination once the iteration approach is more
efficient.

Kind regards

robert

Martin DeMello
Guest
Posts: n/a

 08-07-2005
Robert Klemme <(E-Mail Removed)> wrote:
>
> Note that it might be more memory efficient to write a iteration method:
>
> def cartprod(a,b)
> a.each {|ae| b.each {|be| yield ae, be}}
> end
>
> Then you can also do this if needed
>
> c=[]
> cartprod(a,b) {|x,y| c << [x,y]}
>
> If you just need every combination once the iteration approach is more
> efficient.

Or even better,

if block_given?
yield ae, be
else
(c ||= []) << ae, be
end

martin

Randy Kramer
Guest
Posts: n/a

 08-07-2005
On Sunday 07 August 2005 10:51 am, Martin DeMello wrote:
> if block_given?
> yield ae, be
> else
> (c ||= []) << ae, be
> end

I'm sure you know what you're talking about, but I wonder how many others
will, even with comments. (Maybe I just need to go back to Pascal?)

Randy Kramer