1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* Copyright (c) 1997-2024
Ewgenij Gawrilow, Michael Joswig, and the polymake team
Technische Universität Berlin, Germany
https://polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
#pragma once
#include "polymake/client.h"
#include "polymake/linalg.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"
#include "polymake/polytope/convex_hull.h"
#include "polymake/polytope/separating_hyperplane.h"
namespace polymake{
namespace polytope{
template <typename Scalar>
bool contains_V_H(BigObject p_in, BigObject p_out){
// get the Vertex description of the second polytopr
Matrix<Scalar> V_in = p_in.give("RAYS | INPUT_RAYS");
const OptionSet opt;
for(const auto& v : rows(V_in)){
if(!cone_H_contains_point(p_out, v, opt)) return false;
}
Matrix<Scalar> L_in;
if (p_in.lookup("LINEALITY_SPACE | INPUT_LINEALITY") >> L_in){
for(const auto& l : rows(L_in)){
if(!cone_H_contains_point(p_out, l, opt)) return false;
if(!cone_H_contains_point(p_out, -l, opt)) return false;
}
}
return true;
};
template<typename Scalar>
bool solve_same_description_LPs(const Matrix<Scalar>& Arays, const Matrix<Scalar>& Alin, const Matrix<Scalar>& Brays, const Matrix<Scalar>& Blin){
// Check whether the cone A generated by the rays Arays and lineality Alin
// is contained in the cone B (= Brays +- Blin).
// Lineality must be contained in lineality, and this can be checked via
// linear algebra.
if(rank(Blin) != rank(Blin / Alin)) return false;
// Now we only have to check whether every ray of Arays is contained in the
// cone B, i.e. we make an LP to find a linear combination in terms of Brays
// and Blin. This is already implemented elsewhere.
BigObject container("Cone", mlist<Scalar>());
container.take("INPUT_RAYS") << Brays;
container.take("INPUT_LINEALITY") << Blin;
const OptionSet opt;
for(const auto& a : rows(Arays)){
if(!cone_contains_point(container, a, opt)) return false;
}
return true;
}
template <typename Scalar>
bool contains_H_H_via_LP(BigObject p_in, BigObject p_out){
Matrix<Scalar> F_out = p_out.lookup("FACETS | INEQUALITIES");
Matrix<Scalar> E_out;
if (!(p_out.lookup("LINEAR_SPAN | EQUATIONS") >> E_out)){
E_out = zero_matrix<Scalar>(0, F_out.cols());
}
Matrix<Scalar> F_in = p_in.lookup("FACETS | INEQUALITIES");
Matrix<Scalar> E_in;
if (!(p_in.lookup("LINEAR_SPAN | EQUATIONS") >> E_in)){
E_out = zero_matrix<Scalar>(0, F_out.cols());
}
// solve_same_description_LPs tries to find a linear combination for every
// ray/lineality of the small cone in terms of the rays/linealities of the
// larger cone. To apply this here we check the opposite containment of the
// dual cones.
return solve_same_description_LPs(F_out, E_out, F_in, E_in);
};
template <typename Scalar>
bool contains_V_V_via_LP(BigObject p_in, BigObject p_out)
{
Matrix<Scalar> V_out = p_out.lookup("RAYS | INPUT_RAYS");
Matrix<Scalar> L_out;
if (!(p_out.lookup("LINEALITY_SPACE | INPUT_LINEALITY") >> L_out)){
L_out = zero_matrix<Scalar>(0, V_out.cols());
}
Matrix<Scalar> V_in = p_in.lookup("RAYS | INPUT_RAYS");
Matrix<Scalar> L_in;
if (!(p_in.lookup("LINEALITY_SPACE | INPUT_LINEALITY") >> L_in)){
L_in = zero_matrix<Scalar>(0, V_out.cols());
}
// solve_same_description_LPs tries to find a linear combination for every
// ray/lineality of the small cone in terms of the rays/linealities of the
// larger cone.
return solve_same_description_LPs(V_in, L_in, V_out, L_out);
};
// Checks if a given Polytope p_in is a subset of a other given Polytope p_out.
// For each combination of discriptions (by Vertices or by Facets) it use another algorithm.
// @param BigObject p_in The inner Polytope
// @param BigObject p_out the outer Polytope
// @return Bool
template <typename Scalar>
bool contains(BigObject p_in, BigObject p_out)
{
// Small sanity check to avoid segfaults
const Int dim_in = p_in.give("CONE_AMBIENT_DIM");
const Int dim_out = p_out.give("CONE_AMBIENT_DIM");
if(dim_in != dim_out) throw std::runtime_error("Cones/Polytopes do no live in the same ambient space.");
if(p_in.isa("Polytope") && p_out.isa("Polytope")){
const bool feasible_in = p_in.give("FEASIBLE");
if(!feasible_in) return true;
const bool feasible_out = p_out.give("FEASIBLE");
if(!feasible_out) return false;
}
// Ensure that we have a V-description of p_in and an H-description of
// p_out.
p_in.give("RAYS | INPUT_RAYS");
p_out.give("FACETS | INEQUALITIES");
return contains_V_H<Scalar>(p_in, p_out);
}
// now comes the contains function for balls und polytopes
template <typename Scalar>
bool contains_ball_dual(Vector<Scalar> c, Scalar r, BigObject p_out){
// homogenize center of ball
c = c/c[0];
// get the outer description of p_out
Matrix<Scalar> F_out = p_out.lookup("FACETS | INEQUALITIES");
Matrix<Scalar> E_out;
if (p_out.lookup("AFFINE_HULL | EQUATIONS") >> E_out){
// work with inequalities
if(E_out.rows()>0){
return false;
}
}
// scalar product with worst case direktion for each
// inequality
Vector<Scalar> F_out_norms = zero_vector<Scalar>(
F_out.rows());
for(int i=0; i<F_out.rows(); ++i){
for(int j=1; j<F_out.cols(); ++j){
F_out_norms[i] += sqr(F_out(i,j));
}
}
Vector<Scalar> b = F_out*c;
// compute (F_out*c)^2 - r^2 * F_out_norm
// and use this to check if F_out*c >= r * F_out_norm^(1/2)
for(int i=0; i<b.size(); ++i){
b[i] = sqr(b[i]) - sqr(r)*F_out_norms[i];
if(b[i]<0){
return false;
}
}
return true;
}
template <typename Scalar>
bool contains_ball_primal(Vector<Scalar> c, Scalar r, BigObject p_out){
// Since this problem is co-NP complete it will be changed to
// the case of ball_dual
p_out.give("FACETS | LINEAR_SPAN");
return contains_ball_dual<Scalar>(c, r, p_out);
}
// Checks if a given Ball B(c,r) is a subset of a other given Polytope p_out.
// For each combination of discriptions (by Vertices or by Facets) it use another algorithm.
// @param Vector c the center of the ball
// @param Scalar r the radius of the ball
// @param BigObject p_out the outer Polytope
// @return Bool
template <typename Scalar>
bool polytope_contains_ball(Vector<Scalar> c, Scalar r, BigObject p_out)
{
// check in which way p_out was given
if (p_out.exists("FACETS | INEQUALITIES")){
return contains_ball_dual<Scalar>(c, r, p_out);
}else{
// p_out is given by vertices
return contains_ball_primal<Scalar>(c, r, p_out);
}
}
template <typename Scalar>
bool contains_primal_ball(BigObject p_in, Vector<Scalar> c, Scalar r){
// get the vertex descrition of p_in
Matrix<Scalar> V_in = p_in.give("RAYS | INPUT_RAYS");
Matrix<Scalar> L_in;
// check if p_in has rays
for(int i=0; i<V_in.rows(); ++i){
if( is_zero(V_in(i,0)) ){
return false;
}
}
// check p_in has a not empty lineality space
if (p_in.lookup("LINEALITY_SPACE | INPUT_LINEALITY") >> L_in){
if (L_in.rows()>0){
return false;
}
}
// check if the ball contains all vertices of p_in
// so we check if for each vertex v
// ||c-v||² <= r²
r = sqr(r);
c= c/c[0];
Scalar r_c;
for(int i=0; i<V_in.rows(); ++i){
r_c = sqr(c-V_in.row(i));
if(r_c > r){
return false;
}
}
return true;
}
template <typename Scalar>
bool contains_dual_ball(BigObject p_in, Vector<Scalar> c, Scalar r){
// Since this problem is co-NP complete it will be changed to
// the case of primal_ball
p_in.give("RAYS | INPUT_RAYS");
return contains_primal_ball<Scalar>(p_in, c, r);
}
// Checks if a given Polytope p_in is a subset of a given Ball B(c,r).
// For each combination of discriptions (by Vertices or by Facets) it use another algorithm.
// @param BigObject p_in the inner Polytope
// @param Vector c the center of the ball
// @param Scalar r the radius of the ball
// @return Bool
template <typename Scalar>
bool polytope_contained_in_ball(BigObject p_in, Vector<Scalar> c, Scalar r)
{
// check in which way p_in was given
if (p_in.exists("RAYS | INPUT_RAYS")){
return contains_primal_ball<Scalar>(p_in, c, r);
}else{
// p_out is given by inequalities
return contains_dual_ball<Scalar>(p_in, c, r);
}
}
} // namespace polytope
} // namespace polymake
|