Jump to content
  • Advertisement
Sign in to follow this  
kloffy

Cartesian Product of Polytopes

This topic is 612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose I have a general n-polytope, whose faces are a list of (n-1)-polytopes. For example:
 
A 1-dimensional 1-simplex (line segment) would be represented as two 1-dimensional 0-simplexes (points):
 
[(0,),(1,)]

A 2-dimensional 2-simplex (triangle) would be represented as three 2-dimensional 1-simplexes (line segments):

[
  [(0,0),(1,0)],
  [(1,0),(0,1)],
  [(0,1),(0,0)]
]

And so on...

I would like to compute the Cartesian product of two arbitrary polytopes. I came up with the following algorithm, and I am wondering whether it is correct:
 
def product(xs, ys):
    if isinstance(xs, list) and isinstance(ys, list):
        return [product(xs, y) for y in ys] + [product(x, ys) for x in xs]
    elif isinstance(ys, list):
        return [product(xs, y) for y in ys]
    elif isinstance(xs, list):
        return [product(x, ys) for x in xs]
    else:
        return xs + ys

You can make things a bit nicer by wrapping things in a class:
 
class Polytope(object):
    def __init__(self, data):
        self.data = data
    def __mul__(self, other):
        return Polytope(product(self.data, other.data))
    def __str__(self):
        import pprint
        return pprint.pformat(self.data)

With this, you can do things like:
 
cube = Polytope([(0,),(1,)])

print("1-cube:", cube, sep="\n")
print("2-cube:", cube*cube, sep="\n")
print("3-cube:", cube*cube*cube, sep="\n")

The results seem correct, but I still need to convince myself that it works in all cases. Since I couldn't find much material online, I was curious if anybody has done this kind of thing or knows any further resources. Edited by kloffy

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!