File: print_constraints.h

package info (click to toggle)
polymake 4.6-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,288 kB
  • sloc: cpp: 156,233; perl: 42,962; javascript: 30,726; ansic: 2,907; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 61
file content (99 lines) | stat: -rw-r--r-- 3,313 bytes parent folder | download
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
/* Copyright (c) 1997-2021
   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/Matrix.h"
#include "polymake/Array.h"
#include "polymake/Rational.h"

namespace polymake { namespace common {

template <typename Scalar>
void print_constraints_sub(const Matrix<Scalar>& M, Array<std::string> coord_labels, Array<std::string> row_labels, const bool are_eqs, const bool homogeneous)
{
   if (M.cols() == 0)
      throw std::runtime_error("print_constraints - Invalid dimension 0!");

   const Int start = homogeneous ? 0 : 1;

   if (coord_labels.size() > 0) {
      if (!homogeneous && coord_labels.size() == M.cols()-1) {
         Array<std::string> new_coord(1, "inhomog_var");
         new_coord.append(coord_labels.size(), coord_labels.begin());
         coord_labels = new_coord;
      }
      if (coord_labels.size() != M.cols())
         throw std::runtime_error("print_constraints - Wrong number of variables!");
   } else {
      const std::string var("x");
      coord_labels.resize(M.cols());
      for (Int i = start, d = M.cols(); i < d; ++i)
         coord_labels[i] = var + std::to_string(i);

      // the coordinate label we assign below will not be used
      if (!homogeneous)
         coord_labels[0] = "inhomog_var";
   }

   for (Int i = 0, r = M.rows(); i < r; ++i) {
      if (i < row_labels.size())
         cout << row_labels[i];
      else
         cout << i;
      cout << ": ";
      if (is_zero(M.row(i).slice(range_from(start)))) {
         cout << "0";
      } else {
         bool first = true;
         for (Int j = start; j < M.cols(); ++j) {
           const Scalar cur_coeff = M(i, j);
            if (cur_coeff != 0) {
               if (!first)
                  cout << ' ';
               if (cur_coeff > 0) {
                  if (!first)
                     cout << "+ ";
                  if (cur_coeff != 1)
                     cout << std::setprecision(16) << cur_coeff << ' ';
               }
               if (cur_coeff < 0) {
                  if (! first)
                     cout << "- ";
                  else
                     cout << '-';
                  if (cur_coeff != -1)
                     cout << std::setprecision(16) << -cur_coeff << ' ';
               }
               first = false;
               cout << coord_labels[j];
            }
         }
      }
      if (are_eqs)
         cout << " = ";
      else
         cout << " >= ";
      const Scalar neg_rhs = homogeneous ? zero_value<Scalar>() : -M(i, 0);
      cout << std::setprecision(16) << neg_rhs << '\n';
   }
   cout << endl;
}

} }