File: Incidence_and_SparseMatrix.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (206 lines) | stat: -rw-r--r-- 7,990 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* 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

namespace pm {

template <typename IncMatrixRef, typename ElemRef>
class SameElementSparseMatrix
   : public GenericMatrix< SameElementSparseMatrix<IncMatrixRef, ElemRef>,
                           typename object_traits<typename deref<ElemRef>::type>::persistent_type > {
protected:
   using matrix_alias_t = alias<IncMatrixRef>;
   using elem_alias_t = alias<ElemRef>;
   matrix_alias_t matrix;
   elem_alias_t apparent_elem;

public:
   using value_type = typename deref<ElemRef>::type;
   using const_reference = const value_type&;
   using reference = const_reference;

   template <typename Arg1, typename Arg2,
             typename=std::enable_if_t<std::is_constructible<matrix_alias_t, Arg1>::value &&
                                       std::is_constructible<elem_alias_t, Arg2>::value>>
   SameElementSparseMatrix(Arg1&& matrix_arg, Arg2&& data_arg)
      : matrix(std::forward<Arg1>(matrix_arg))
      , apparent_elem(std::forward<Arg2>(data_arg)) {}

   decltype(auto) get_matrix() const { return *matrix; }
   const elem_alias_t& get_apparent_element() const { return apparent_elem; }
};

template <typename IncMatrixRef, typename ElemRef>
struct spec_object_traits< SameElementSparseMatrix<IncMatrixRef, ElemRef> >
   : spec_object_traits<is_container> {
   static constexpr bool is_temporary = true, is_always_const = true;
};

template <typename IncMatrixRef, typename ElemRef>
struct check_container_feature< SameElementSparseMatrix<IncMatrixRef, ElemRef>, Symmetric >
   : check_container_ref_feature< IncMatrixRef, Symmetric > {};

template <typename IncMatrixRef, typename ElemRef>
struct check_container_feature< SameElementSparseMatrix<IncMatrixRef, ElemRef>, pure_sparse> : std::true_type {};

template <typename IncMatrixRef, typename ElemRef>
class matrix_random_access_methods< SameElementSparseMatrix<IncMatrixRef, ElemRef> > {
   typedef SameElementSparseMatrix<IncMatrixRef, ElemRef> master;
public:
   const typename deref<ElemRef>::type& operator() (Int i, Int j) const
   {
      const master& me=static_cast<const master&>(*this);
      if (me.get_matrix()(i,j)) return *me.get_apparent_element();
      return zero_value<typename deref<ElemRef>::type>();
   }
};

template <typename IncMatrixRef, typename ElemRef>
class Rows< SameElementSparseMatrix<IncMatrixRef, ElemRef> >
   : public modified_container_pair_impl< Rows< SameElementSparseMatrix<IncMatrixRef, ElemRef> >,
                                          mlist< Container1RefTag< masquerade<Rows, IncMatrixRef> >,
                                                 Container2RefTag< same_value_container<ElemRef> >,
                                                 OperationTag< operations::construct_binary<SameElementSparseVector> >,
                                                 MasqueradedTop > > {
   typedef modified_container_pair_impl<Rows> base_t;
public:
   decltype(auto) get_container1() const
   {
      return rows(this->hidden().get_matrix());
   }
   decltype(auto) get_container2() const
   {
      return as_same_value_container(this->hidden().get_apparent_element());
   }
};

template <typename IncMatrixRef, typename ElemRef>
class Cols< SameElementSparseMatrix<IncMatrixRef, ElemRef> >
   : public modified_container_pair_impl< Cols< SameElementSparseMatrix<IncMatrixRef,ElemRef> >,
                                          mlist< Container1RefTag< masquerade<Cols, IncMatrixRef> >,
                                                 Container2RefTag< same_value_container<ElemRef> >,
                                                 OperationTag< operations::construct_binary<SameElementSparseVector> >,
                                                 MasqueradedTop > > {
   typedef modified_container_pair_impl<Cols> base_t;
public:
   decltype(auto) get_container1() const
   {
      return cols(this->hidden().get_matrix());
   }
   decltype(auto) get_container2() const
   {
      return as_same_value_container(this->hidden().get_apparent_element());
   }
};

template <typename E, typename TMatrix>
auto same_element_sparse_matrix(TMatrix&& m, E&& x, std::enable_if_t<is_generic_incidence_matrix<TMatrix>::value, void**> =nullptr)
{
   return SameElementSparseMatrix<add_const_t<unwary_t<TMatrix&&>>, prevent_int_element<E&&>>(unwary(std::forward<TMatrix>(m)), std::forward<E>(x));
}

template <typename E, typename TMatrix>
auto same_element_sparse_matrix(TMatrix&& m, std::enable_if_t<is_generic_incidence_matrix<TMatrix>::value, void**> =nullptr)
{
   return same_element_sparse_matrix(std::forward<TMatrix>(m), one_value<E>(), nullptr);
}

template <typename MatrixRef>
class IndexMatrix
   : public GenericIncidenceMatrix< IndexMatrix<MatrixRef> > {
protected:
   using alias_t = alias<MatrixRef>;
   alias_t matrix;
public:
   using value_type = bool;
   using reference = bool;
   using const_reference = bool;

   template <typename Arg, typename=std::enable_if_t<std::is_constructible<alias_t, Arg>::value>>
   explicit IndexMatrix(Arg&& matrix_arg)
      : matrix(std::forward<Arg>(matrix_arg)) {}

   decltype(auto) get_matrix() const { return *matrix; }
};

template <typename MatrixRef>
struct check_container_feature< IndexMatrix<MatrixRef>, Symmetric >
   : check_container_ref_feature<MatrixRef, Symmetric> {};

template <typename MatrixRef>
struct spec_object_traits< IndexMatrix<MatrixRef> >
   : spec_object_traits<is_container> {
   static constexpr bool is_temporary = true, is_always_const = true;
};

template <typename MatrixRef>
class matrix_random_access_methods< IndexMatrix<MatrixRef> > {
   typedef IndexMatrix<MatrixRef> master;
public:
   bool operator() (Int i, Int j) const
   {
      const master& me = static_cast<const master&>(*this);
      return !me.row(i).find(j).at_end();
   }
};

template <typename MatrixRef>
class Rows< IndexMatrix<MatrixRef> >
   : public modified_container_impl< Rows< IndexMatrix<MatrixRef> >,
                                     mlist< MasqueradedTop,
                                            ContainerRefTag< masquerade<pm::Rows, MatrixRef> >,
                                            OperationTag< operations::construct_unary<Indices> > > > {
protected:
   Rows();
   ~Rows();
public:
   decltype(auto) get_container() const { return rows(this->hidden().get_matrix()); }
};

template <typename MatrixRef>
class Cols< IndexMatrix<MatrixRef> >
   : public modified_container_impl< Cols< IndexMatrix<MatrixRef> >,
                                     mlist< MasqueradedTop,
                                            ContainerRefTag< masquerade<pm::Cols, MatrixRef> >,
                                            OperationTag< operations::construct_unary<Indices> > > > {
protected:
   Cols();
   ~Cols();
public:
   decltype(auto) get_container() const { return cols(this->hidden().get_matrix()); }
};

template <typename TMatrix, typename=std::enable_if_t<check_container_feature<TMatrix, sparse>::value>>
auto index_matrix(const GenericMatrix<TMatrix>& m)
{
   return IndexMatrix<const TMatrix&>(m.top());
}

} // end namespace pm

namespace polymake {
   using pm::same_element_sparse_matrix;
   using pm::index_matrix;
}


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