First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 0 years ago

JTS gravatar image

How to inherit from polytopes

Dear all, I want to write a very simple class that inherits from existing polytopes functionality, creates one particular type of polytopes, and adds some functionality which is only relevant for this type of polytopes.

However, I am a python beginner, and am having a hard time navigating the sagemath src directory to find exactly from which class (Polyhedron or Polyhedra) that I should inherit, and how to do this.

Here is a toy example: suppose I want to investigate "depleted cubes", ie polytopes combinatorially equivalent with hypercubes with one corner neatly sliced off. I also want to easily retrieve the cut-off facet.

This works:

def depleted_cube(n: Integer) -> Polyhedron:
       """Returns the n-dimensional hypercube minus one vertex"""
       H = polytopes.hypercube(n)
       Verts = [v for v in H.vertices_list() if add(v) < n]
       C = Polyhedron(vertices=Verts)
       return C

def _strange_facet(P: Polyhedron):
      """Returns the strange facet of P, but only of P depleted cube"""
      n = P.dimension()
      L = [F for F in P.facets() if len(F.vertices()) == n]
      return L[0]

I can now go

sage: _strange_facet(depleted_cube(6)).vertices()
(A vertex at (-1, 1, 1, 1, 1, 1),
A vertex at (1, -1, 1, 1, 1, 1),
A vertex at (1, 1, -1, 1, 1, 1),
A vertex at (1, 1, 1, -1, 1, 1),
A vertex at (1, 1, 1, 1, -1, 1),
A vertex at (1, 1, 1, 1, 1, -1))

but I would like to be able to do

polytopes.depleted_cube(6).strange_facet().vertices()

so as not to dilute the precious namespace! I.e., the function should be a "method" to some "class" that inherits from, either Polyhedron or Polyhedra or polyhedron_base_with_backend_cpp or ... Can someone provide an example, skeleton implementation of a polytope class that inherits from somewhere? As I mentioned, I tried looking at the src directory, but the relevant files are extremely long and littered with docstrings and doctests and cruft; I can make neither head nor tails of them.

How to inherit from polytopes

Dear all, I want to write a very simple class that inherits from existing polytopes functionality, creates one particular type of polytopes, and adds some functionality which is only relevant for this type of polytopes.

However, I am a python beginner, and am having a hard time navigating the sagemath src directory to find exactly from which class (Polyhedron or Polyhedra) that I should inherit, and how to do this.

Here is a toy example: suppose I want to investigate "depleted cubes", ie polytopes combinatorially equivalent with hypercubes with one corner neatly sliced off. I also want to easily retrieve the cut-off facet.

This works:

def depleted_cube(n: Integer) -> Polyhedron:
       """Returns the n-dimensional hypercube minus one vertex"""
       H = polytopes.hypercube(n)
       Verts = [v for v in H.vertices_list() if add(v) < n]
       C = Polyhedron(vertices=Verts)
       return C

def _strange_facet(P: Polyhedron):
      """Returns the strange facet of P, but only of P depleted cube"""
      n = P.dimension()
      L = [F for F in P.facets() if len(F.vertices()) == n]
      return L[0]

I can now go

sage: _strange_facet(depleted_cube(6)).vertices()
(A vertex at (-1, 1, 1, 1, 1, 1),
A vertex at (1, -1, 1, 1, 1, 1),
A vertex at (1, 1, -1, 1, 1, 1),
A vertex at (1, 1, 1, -1, 1, 1),
A vertex at (1, 1, 1, 1, -1, 1),
A vertex at (1, 1, 1, 1, 1, -1))

but I would like to be able to do

polytopes.depleted_cube(6).strange_facet().vertices()

so as not to dilute the precious namespace! I.e., the function should be a "method" to some "class" that inherits from, either Polyhedron or Polyhedra or polyhedron_base_with_backend_cpp or ... Can someone provide an example, skeleton implementation of a polytope class that inherits from somewhere? As I mentioned, I tried looking at the src directory, but the relevant files are extremely long and littered with docstrings and doctests and cruft; I can make neither head nor tails of them.

Edit: As per the answer by dan_fulea, it is not worthwhile to try to turn the functions into a method. I will undogmatically refrain from doing so, then. Thank you for the tour through the internals of sagemath; I was not aware of the f.?? method of getting the code of a function/object.

click to hide/show revision 3
retagged

updated 0 years ago

FrédéricC gravatar image

How to inherit from polytopes

Dear all, I want to write a very simple class that inherits from existing polytopes functionality, creates one particular type of polytopes, and adds some functionality which is only relevant for this type of polytopes.

However, I am a python beginner, and am having a hard time navigating the sagemath src directory to find exactly from which class (Polyhedron or Polyhedra) that I should inherit, and how to do this.

Here is a toy example: suppose I want to investigate "depleted cubes", ie polytopes combinatorially equivalent with hypercubes with one corner neatly sliced off. I also want to easily retrieve the cut-off facet.

This works:

def depleted_cube(n: Integer) -> Polyhedron:
       """Returns the n-dimensional hypercube minus one vertex"""
       H = polytopes.hypercube(n)
       Verts = [v for v in H.vertices_list() if add(v) < n]
       C = Polyhedron(vertices=Verts)
       return C

def _strange_facet(P: Polyhedron):
      """Returns the strange facet of P, but only of P depleted cube"""
      n = P.dimension()
      L = [F for F in P.facets() if len(F.vertices()) == n]
      return L[0]

I can now go

sage: _strange_facet(depleted_cube(6)).vertices()
(A vertex at (-1, 1, 1, 1, 1, 1),
A vertex at (1, -1, 1, 1, 1, 1),
A vertex at (1, 1, -1, 1, 1, 1),
A vertex at (1, 1, 1, -1, 1, 1),
A vertex at (1, 1, 1, 1, -1, 1),
A vertex at (1, 1, 1, 1, 1, -1))

but I would like to be able to do

polytopes.depleted_cube(6).strange_facet().vertices()

so as not to dilute the precious namespace! I.e., the function should be a "method" to some "class" that inherits from, either Polyhedron or Polyhedra or polyhedron_base_with_backend_cpp or ... Can someone provide an example, skeleton implementation of a polytope class that inherits from somewhere? As I mentioned, I tried looking at the src directory, but the relevant files are extremely long and littered with docstrings and doctests and cruft; I can make neither head nor tails of them.

Edit: As per the answer by dan_fulea, it is not worthwhile to try to turn the functions into a method. I will undogmatically refrain from doing so, then. Thank you for the tour through the internals of sagemath; I was not aware of the f.?? method of getting the code of a function/object.