File: thomog.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 (141 lines) | stat: -rw-r--r-- 4,793 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
/* 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/Rational.h"
#include "polymake/Matrix.h"
#include "polymake/TropicalNumber.h"

namespace polymake{ namespace tropical {

template <typename Coefficient, typename VType>
Vector<Coefficient> thomog_vec(const GenericVector<VType, Coefficient>& affine, Int chart = 0, bool has_leading_coordinate = true)
{
   if (affine.dim() <= 1)
      return Vector<Coefficient>(affine);
   if (chart < 0 || chart > (affine.dim()-has_leading_coordinate))
      throw std::runtime_error("Invalid chart coordinate");

   Vector<Coefficient> proj(affine.dim()+1);
   proj.slice(~scalar2set(chart+has_leading_coordinate)) = affine;
   return proj;
}

template <typename Coefficient, typename MType>
Matrix<Coefficient> thomog(const GenericMatrix<MType, Coefficient>& affine, Int chart = 0, bool has_leading_coordinate = true)
{
   if (affine.rows() == 0)
      return Matrix<Coefficient>(0,affine.cols()+1);
   if (chart < 0 || chart > (affine.cols()-has_leading_coordinate))
      throw std::runtime_error("Invalid chart coordinate.");  

   Matrix<Coefficient> proj(affine.rows(), affine.cols()+1);
   proj.minor(All,~scalar2set(chart+has_leading_coordinate)) = affine;
   return proj;
}

template <typename Affine, typename Projective>
void tdehomog_elim_col(Affine&& affine, const Projective& proj, Int chart, bool has_leading_coordinate)
{
   auto elim_col = proj.begin();
   std::advance(elim_col, chart+has_leading_coordinate);
   auto it = entire(affine);
   if (has_leading_coordinate) ++it;
   for (; !it.at_end(); ++it)
      *it -= *elim_col;
}

// This does the inverse of thomog, i.e. removes a coordinate by shifting it to 0.
template <typename TMatrix, typename Coefficient>
Matrix<Coefficient> tdehomog(const GenericMatrix<TMatrix, Coefficient>& proj, Int chart = 0, bool has_leading_coordinate = true)
{
   if (chart < 0 || chart > (proj.cols()-has_leading_coordinate-1))
      throw std::runtime_error("Invalid chart coordinate");

   Matrix<Coefficient> affine = proj.minor(All, ~scalar2set(chart+has_leading_coordinate));
   tdehomog_elim_col(cols(affine), cols(proj), chart, has_leading_coordinate);
   return affine;
}

template <typename TVector, typename Coefficient>
Vector<Coefficient> tdehomog_vec(const GenericVector<TVector, Coefficient>& proj, Int chart = 0, bool has_leading_coordinate = true)
{
   if (proj.dim() <= 1)
      return Vector<Coefficient>();
   if (chart < 0 || chart > (proj.dim()-has_leading_coordinate-1)) 
      throw std::runtime_error("Invalid chart coordinate");

   Vector<Coefficient> affine = proj.slice(~scalar2set(chart+has_leading_coordinate));
   tdehomog_elim_col(affine, proj.top(), chart, has_leading_coordinate);
   return affine;
}

/*
 * @brief: scale the rows of a matrix so that
 * the first non-null entry of each row is tropical one
 */
template <typename Addition, typename Scalar, typename MatrixTop>
Matrix<TropicalNumber<Addition, Scalar>>
normalized_first(const GenericMatrix<MatrixTop, TropicalNumber<Addition, Scalar>>& homogeneous_points)
{
  using TNumber = TropicalNumber<Addition,Scalar>;

  Matrix<TNumber> result(homogeneous_points);
  for (auto r : rows(result)) {
    TNumber value;
    for (auto entry : r) {
      if (!is_zero(entry)) {
        value = entry;
        break;
      }
    }
    if (!is_zero(value)) r /= value;
  }
  return result;
}

/*
 * @brief: scale the rows of a matrix so that
 * the first non-null entry of each row is tropical one
 */
template <typename Addition, typename Scalar, typename VectorTop>
Vector<TropicalNumber<Addition, Scalar>>
normalized_first(const GenericVector<VectorTop, TropicalNumber<Addition, Scalar>>& homogeneous_point)
{
  using TNumber = TropicalNumber<Addition,Scalar>;

  Vector<TNumber> result(homogeneous_point);
  TNumber value;
  for (auto entry : result) {
    if (!is_zero(entry)) {
      value = entry;
      break;
    }
  }
  if (!is_zero(value)) result /= value;
  return result;
}

} }


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