File: covectors.h

package info (click to toggle)
polymake 4.12-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 35,992 kB
  • sloc: cpp: 168,768; perl: 43,375; javascript: 31,575; ansic: 3,007; java: 2,654; python: 633; sh: 268; xml: 117; makefile: 61
file content (190 lines) | stat: -rw-r--r-- 7,034 bytes parent folder | download | duplicates (2)
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
/* 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/tropical/arithmetic.h"
#include "polymake/linalg.h"
#include "polymake/IncidenceMatrix.h"

namespace polymake { namespace tropical {

struct CovectorDecoration : public GenericStruct<CovectorDecoration> {
  DeclSTRUCT( DeclFIELD(face, Set<Int>)
              DeclFIELD(rank, Int)
              DeclFIELD(covector,IncidenceMatrix<>) );

  CovectorDecoration() {}
  CovectorDecoration(const Set<Int>& f, Int r, const IncidenceMatrix<>& cv)
    : face(f)
    , rank(r)
    , covector(cv) {}
};

/*
 * @brief compute the covector of a single point in tropical projective space wrt a matrix of generators
 *
 */
template <typename Addition, typename Scalar, typename Vector1, typename Vector2>
Set<Int> single_covector(const GenericVector<Vector1, TropicalNumber<Addition, Scalar>>& point,
                         const GenericVector<Vector2, TropicalNumber<Addition,Scalar>>& apex)
{
  typedef TropicalNumber<Addition, Scalar> TNumber;
  Set<Int> pt_covector = sequence(0, point.dim()) - support(point);
  Vector<TNumber> tdiff = rel_coord(apex, point);
  TNumber extremum = accumulate(tdiff, operations::add());
  Int td_index = 0;
  // determine the extremal entries
  for (auto td : tdiff) {
    if (td == extremum)
      pt_covector += td_index;
    ++td_index;
  }
  return pt_covector;
}

/*
 * @brief compute the covector of a single point in tropical projective space wrt a matrix of generators
 */

template <typename Addition, typename Scalar, typename VectorTop, typename MatrixTop>
IncidenceMatrix<> single_covector(const GenericVector<VectorTop, TropicalNumber<Addition,Scalar>>& point,
                                  const GenericMatrix<MatrixTop, TropicalNumber<Addition, Scalar>>& generators)
{
  typedef TropicalNumber<Addition, Scalar> TNumber;
  const Int dimension = generators.cols();
  Set<Int> non_support = sequence(0, point.dim()) - support(point);
  Array<Set<Int>> pt_covector(dimension);

  Int gn_index = 0;
  for (auto gn : rows(generators)) {
    Vector<TNumber> tdiff = rel_coord(gn, point);
    TNumber extremum = accumulate(tdiff, operations::add());
    Set<Int> extremal_entries = non_support;
    Int td_index = 0;
    // determine the extremal entries
    for (auto td : tdiff) {
      if (td == extremum)
        extremal_entries += td_index;
      ++td_index;
    }
    // add the containing sectors to the covector of pt
    for (auto ext_it : extremal_entries) {
      pt_covector[ext_it] += gn_index;
    }
    ++gn_index;
  }
  return IncidenceMatrix<>(pt_covector);
}

/*
 * @brief determine the covector of a 0-1-ray wrt a matrix of generators
 * for this only the index set of the 1 entries and the supports of the generators are taken into account
 */
template <typename Addition, typename Scalar>
IncidenceMatrix<> artificial_ray_covector(const Set<Int>& one_entries,
                                          const Matrix<TropicalNumber<Addition, Scalar>>& generators)
{
  const Int dimension = generators.cols();
  RestrictedIncidenceMatrix<> pt_covector(dimension);
  Int gn_index = 0;
  for (auto gn = entire(rows(generators)); !gn.at_end(); ++gn, ++gn_index) {
    if (incl(one_entries, sequence(0, dimension) - support(*gn)) <= 0) {
      for (Int r = 0; r < dimension; ++r)
        pt_covector(r, gn_index)=true;
    } else {
      for (Int covector_index : one_entries) {
        pt_covector(covector_index, gn_index) = true;
      }
    }
  }
  return IncidenceMatrix<>(std::move(pt_covector));
}

// Documentation see perl wrapper
template <typename Addition, typename Scalar, typename Matrix1, typename Matrix2>
Array<IncidenceMatrix<>> covectors(const GenericMatrix<Matrix1, TropicalNumber<Addition, Scalar>>& points,
                                   const GenericMatrix<Matrix2, TropicalNumber<Addition, Scalar>>& generators)
{
  const Int n = points.rows();
  Array<IncidenceMatrix<>> result(n);
  Int pt_index = 0;
  for (auto pt : rows(points)) {
    // call the computation of the covector for every single point
    result[pt_index] = single_covector(pt, generators);
    ++pt_index;
  }//END iterate points
  return result;
}

// Versions for scalar matrices.

template <typename Addition, typename Scalar>
Array<IncidenceMatrix<>> covectors_of_scalar_vertices(const Matrix<Scalar>& points,
                                                      const Matrix<TropicalNumber<Addition, Scalar>>& generators)
{
  const Int dimension = generators.cols();
  Array<IncidenceMatrix<>> result(points.rows());
  Int pt_index = 0;
  for (auto pt = entire(rows(points)); !pt.at_end(); ++pt, ++pt_index) {
    if ((*pt)[0] == 1) {
      result[pt_index] = single_covector(Vector<TropicalNumber<Addition, Scalar>>(pt->slice(range_from(1))), generators);
    } else {
      Set<Int> one_entries = support(pt->slice(range_from(1))); //the indices of the 1-entries of the 0/1-ray
      if ((*pt)[one_entries.front()+1] * Addition::orientation() < 0)
        one_entries = sequence(0, dimension) - one_entries;

      result[pt_index] = artificial_ray_covector (one_entries, generators);
    }
  }
  return result;
}

/*
 * @brief compute the generalized covector of a single point w.r.t. apices defining inequalities
 *
 */
template <typename Addition, typename Scalar, typename VectorTop, typename MatrixTop>
IncidenceMatrix<> generalized_apex_covector(const GenericVector<VectorTop, TropicalNumber<Addition, Scalar>>& point,
                                            const GenericMatrix<MatrixTop, TropicalNumber<Addition, Scalar>>& apices)
{
  typedef TropicalNumber<Addition, Scalar> TNumber;

  IncidenceMatrix<> pt_covector(apices.rows(),apices.cols());

  for (auto apex = entire<indexed>(rows(apices)); !apex.at_end(); ++apex) {
    TNumber extremum = *apex*point;
    if (!is_zero(extremum)) {
      Vector<TNumber> hadaprod(attach_operation(*apex, point.top(), operations::mul()));
      Set<Int> extremal_entries = indices(attach_selector(hadaprod, operations::fix2<TNumber,operations::eq>(extremum)));

      // add the containing sectors to the covector of pt
      pt_covector[apex.index()] = extremal_entries;
    }
  }
  return pt_covector;
}

} }


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
// vim: shiftwidth=3:softtabstop=3: