File: sparse_linalg.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 (177 lines) | stat: -rw-r--r-- 5,382 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
/* 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/vector"

namespace pm {

template <typename E>
std::enable_if_t<is_field<E>::value, E>
det(SparseMatrix<E> M)
{
   const Int dim = M.rows();
   if (dim == 0) return one_value<E>();

   std::vector<Int> column_permutation(dim), column_permutation_inv(dim);
   copy_range(entire(sequence(0, dim)), column_permutation.begin());
   copy_range(entire(sequence(0, dim)), column_permutation_inv.begin());
   E result = one_value<E>();

   for (auto pivotrow = entire(rows(M)); !pivotrow.at_end(); ++pivotrow) {
      if (pivotrow->empty())
         return zero_value<E>();

      auto pivot = pivotrow->begin();
      const Int pr = pivotrow.index();
      const Int pc = pivot.index();   // row and column index
      result *= *pivot;
      if (column_permutation[pc] != pr) {
         Int i = column_permutation_inv[pr];
         std::swap(column_permutation_inv[pr], column_permutation_inv[column_permutation[pc]]);
         std::swap(column_permutation[i], column_permutation[pc]);
         negate(result);
      }

      auto beneath = cross_direction(pivot);
      ++beneath;
      while (!beneath.at_end()) {
         // delete all elements below pivot
         Int r = beneath.index();
         const E factor = (*beneath) / (*pivot);
         ++beneath;
         M[r] -= factor * M[pr];
      }
   }
   return result;
}

template <typename E>
std::enable_if_t<is_field<E>::value, SparseVector<E>>
reduce(SparseMatrix<E> M, SparseVector<E> V)
{
   const Int n_cols=M.cols();
   Int col = 0;
   for (auto pivotrow = entire(rows(M)); !pivotrow.at_end() && col < n_cols; ++pivotrow) {
      if (pivotrow->empty()) continue;

      auto pivot = pivotrow->begin();
      const E pivotelem = *pivot;

      (*pivotrow) /= pivotelem;

      auto in_col = cross_direction(pivotrow->begin());
      for (++in_col; !in_col.at_end(); ) {
         const E factor = *in_col;
         const Int r2 = in_col.index();
         ++in_col;
         M.row(r2) -= (*pivotrow) * factor;
      }
      const E factor = V[pivot.index()];
      V -= (*pivotrow) * factor;
      ++col;
   }
   return V;
}

template <typename E>
std::enable_if_t<is_field<E>::value, SparseMatrix<E>>
inv(SparseMatrix<E> M)
{
   const Int dim = M.rows();
   SparseMatrix<E> L = unit_matrix<E>(dim), R = unit_matrix<E>(dim);

   for (auto c=entire(cols(M)); !c.at_end(); ++c) {
      if (c->empty()) throw degenerate_matrix();

      auto in_col = c->begin();
      auto in_row = cross_direction(in_col);
      Int pr = in_col.index(), pc = c.index();
      const E pivotelem = *in_col;
      M.row(pr) /= pivotelem;  L.row(pr) /= pivotelem;  ++in_col;
      while (! in_col.at_end()) {
         const E factor = *in_col;
         Int r = in_col.index();  ++in_col;
         M.row(r) -= factor * M.row(pr);  L.row(r) -= factor * L.row(pr);
      }
      ++in_row;
      while (! in_row.at_end()) {
         R.col(in_row.index()) -= (*in_row) * R.col(pc);
         M.row(pr).erase(in_row++);
      }
   }
   R.permute_cols(attach_operation(rows(M), BuildUnary<operations::front_index>()));
   return R*L;
}

template <typename E, bool ensure_nondegenerate = true>
std::enable_if_t<is_field<E>::value, Vector<E>>
lin_solve(SparseMatrix<E> A, Vector<E> B)
{
   const Int m = A.rows();
   const Int n = A.cols();
   Int non_empty_rows = m-n;
   if (ensure_nondegenerate && non_empty_rows < 0)
      throw underdetermined();

   for (auto r = entire(rows(A)); !r.at_end(); ++r) {
      const Int pr = r.index();
      if (r->empty()) {
         if (ensure_nondegenerate && --non_empty_rows < 0)
            throw degenerate_matrix();
         if (!is_zero(B[pr]))
            throw infeasible();
         continue;
      }
      auto in_row = r->begin();
      auto in_col = cross_direction(in_row);
      const E pivotelem = *in_row;
      if (!is_one(pivotelem)) {
         (*r) /= pivotelem;
         B[pr] /= pivotelem;
      }

      for (++in_col; !in_col.at_end(); ) {
         const E factor = *in_col;
         const Int r2 = in_col.index();
         ++in_col;
         A.row(r2) -= (*r) * factor;
         B[r2] -= B[pr] * factor;
      }
   }

   Vector<E> result(A.cols());
   for (auto r = entire<reversed>(rows(A)); !r.at_end(); ++r) {
      if (r->empty()) continue;
      auto in_row = r->begin();
      auto in_col = cross_direction(in_row);
      const E& elem = result[in_row.index()] = B[r.index()];
      while (!(--in_col).at_end())
         B[in_col.index()] -= elem * (*in_col);
   }
   return result;
}

} // end namespace pm


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