1 | initial version |
Here is an example. It requires to load the Projection
function:
sage: from sage.geometry.polyhedron.plot import Projection
Let's say we have the following polytope:
sage: P = Polyhedron(vertices=[[-2,0,3],[2,0,3],[0,2,1],[0,-2,1]])
and we are interested in the affine space spanned by the points
sage: affine_basis = [vector([1,1,2]),vector([1,-1,2]),vector([-1,-1,2])]
we first get a linear subspace and create the projection matrix:
sage: linear_subspace = [ap - affine_points[0] for ap in affine_basis[1:]]
sage: VS = VectorSpace(QQ,3)
sage: proj_matrix = matrix([sum([v.inner_product(lv)*lv/(lv.inner_product(lv)) for lv in linear_subspace]) for v in VS.basis()]).transpose()
Then, we create a function that will emulate the affine projection (notice the addition of the first element of the affine basis).
sage: def my_proj(x): return proj_matrix*x + affine_basis[0]
Then, we project the polytope and use the transformed coordinates of the vertices to create a new polytope.
sage: proj_p = Projection(P,proj=my_proj)
sage: projected_P = Polyhedron(vertices=proj_p.transformed_coords)
Now, we can see the difference between
sage: P
A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 4 vertices
sage: P.vertices()
(A vertex at (-2, 0, 3),
A vertex at (0, -2, 1),
A vertex at (0, 2, 1),
A vertex at (2, 0, 3))
and the resulting polytope:
sage: projected_P
A 2-dimensional polyhedron in QQ^3 defined as the convex hull of 4 vertices
sage: projected_P.vertices()
(A vertex at (0, -2, 2),
A vertex at (0, 0, 2),
A vertex at (2, 2, 2),
A vertex at (2, 4, 2))