cartesian product

  • Thread starter walter a kehowski
  • Start date
B

Bertram Scharpf

Hi,

Am Sonntag, 07. Aug 2005, 18:06:07 +0900 schrieb walter a kehowski:
How would I create a function that will take list (array) of integers and
return their cartesian product?

As long as you can be sure your arrays have the same length
you could use Array#transpose:

va = [ 3, 5, 8, 7, 20]
vb = [ 4, 12, 15, 24, 21]
vc = [ va, vb].transpose
vc.map { |a,b| Math.sqrt a*a+b*b }.each { |c| puts c }

Bertram
 
R

Robert Klemme

walter said:
Gene Tani said:

Here's the nugget embedded in the thread:

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

class Array

def cartprod(b)

self.inject(a=[]){|a,x| b.inject(a){|a,y| a << [x,y]}}

end

end

p a.cartprod(b)

Very nice! This is why we like Ruby! [IMHO]

Walter Kehowski

You can do it a bit shorter. Note that you don't need the "a=[]" and the
"self.":

aa=[1,2,3]
bb=[4,5,6]

class Array
def cartprod(b)
self.inject(a=[]){|a,x| b.inject(a){|a,y| a << [x,y]}}
end

def cartprod2(b)
inject([]){|a,x| b.inject(a){|a,y| a << [x,y]}}
end
end

p aa.cartprod(bb)
p aa.cartprod2(bb)

Kind regards

robert
 
W

walter a kehowski

You can do it a bit shorter. Note that you don't need the "a=[]" and the
"self.":

Thanks. I have made those changes. Here's a version that finally behaves the
way I want it to.

class Array

def cartprod(*b)

if b.empty? then

inject {|cp,x| cp.cartprod(x) }

else
#assume self an array of arrays
# use b.flatten or b[0]?
# b[0] is probably safer

inject([]){|a,x| b[0].inject(a){|a,y| a << [x,y]}}

end #if

end #cartprod

end #Array

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

p a.cartprod(b)

p [a,b].cartprod()
 
W

walter a kehowski

You can do it a bit shorter. Note that you don't need the "a=[]" and the
"self.":

DOH! One of my comment lines was misplaced. So here's what I want:

class Array

def cartprod(*b)

if b.empty? then

#assume self an array of arrays

inject {|cp,x| cp.cartprod(x) }

else
# use b.flatten or b[0]?
# b[0] is probably safer

inject([]){|a,x| b[0].inject(a){|a,y| a << [x,y]}}

end #if

end #cartprod

end #Array

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

p a.cartprod(b)

p [a,b].cartprod
 
W

walter a kehowski

You can do it a bit shorter. Note that you don't need the "a=[]" and the
"self.":

The two versions both work. Which is better?

class Array

def cartprod(b=[])
# def cartprod(*b)

if b.empty? then
#assume self an array of arrays
inject {|cp,x| cp.cartprod(x) }
else
inject([]) {|a,x| b.inject(a) {|a,y| a << [x,y]}}
# inject([]) {|a,x| b[0].inject(a) {|a,y| a << [x,y]}}
end

end

end #Array

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

p a.cartprod(b)

p [a,b].cartprod
 
T

tony summerfelt

walter a kehowski wrote on 8/9/2005 3:01 AM:
Here's the nugget embedded in the thread:
p a.cartprod(b)

Very nice! This is why we like Ruby! [IMHO]

this is another one that should go in the 'numerical recipies in ruby'
book i mentioned a few weeks ago :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top