Macaulay2 Engine
Loading...
Searching...
No Matches

◆ make_dynamic_cache()

int DetComputation::make_dynamic_cache ( )
private

Definition at line 94 of file det.cpp.

95{
96 // Traverse through matrix entries, find nonzero entries
97 int nonzero = -1;
98 for (int i = 0; i < M->n_rows(); ++i)
99 {
100 bool flag = false;
101 for (int j = 0; j < M->n_cols(); ++j)
102 {
103 if (!R->is_zero(M->elem(i, j)))
104 {
105 if (!flag)
106 {
107 flag = true;
108 dynamic_cache[0][++nonzero] = {};
109 row_lookup[i] = nonzero;
110 }
111 dynamic_cache[0][nonzero][{{i}, {j}}] = M->elem(i, j);
112 }
113 }
114 }
115 int n_nonzero_rows = dynamic_cache[0].size();
116 for (int minor_size = 1; minor_size < p; ++minor_size)
117 {
118 for (int top_row = p - (minor_size + 1);
119 top_row <= n_nonzero_rows - minor_size;
120 ++top_row)
121 {
122 dynamic_cache[minor_size].insert({top_row, {}});
123 for (const auto &[pp, map] : dynamic_cache[minor_size - 1])
124 {
125 if (system_interrupted())
126 return COMP_INTERRUPTED; // Allow interruption
127 if (pp <= top_row)
128 {
129 continue;
130 } // top_row wouldn't be the top row, so skip
131 for (auto x : dynamic_cache[0][top_row])
132 {
133 for (const auto &[Didx, Dval] : map)
134 {
135 // Check if x and D live on distinct columns
136 const std::vector<int> &Dcols = Didx.second;
137 int xcol = x.first.second[0];
138 auto col_find =
139 std::find(Dcols.begin(), Dcols.end(), xcol);
140 if (col_find != Dcols.end())
141 {
142 continue;
143 } // xcol found in Dcols, so skip
144 // if no skip, compute a term in the cofactor
145 ColRowIndices newKey = Didx;
146 newKey.first.insert(newKey.first.begin(),
147 x.first.first[0]);
148 // Get iterator to future location of xcol
149 auto xcol_position = std::upper_bound(
150 newKey.second.begin(), newKey.second.end(), xcol);
151 newKey.second.insert(xcol_position, xcol);
152 bool negate =
153 ((xcol_position - newKey.second.begin()) % 2 != 0);
154 // Insert, add or negate cofactor term
155 auto search =
156 dynamic_cache[minor_size][top_row].find(newKey);
157 if (search == dynamic_cache[minor_size][top_row].end())
158 { // not found
159 dynamic_cache[minor_size][top_row].insert(
160 {newKey, R->mult(x.second, Dval)});
161 if (negate)
162 {
163 R->negate_to(
164 dynamic_cache[minor_size][top_row][newKey]);
165 }
166 }
167 else
168 { // found
169 if (!negate)
170 {
171 R->add_to(
172 dynamic_cache[minor_size][top_row][newKey],
173 R->mult(x.second, Dval));
174 }
175 else
176 {
177 R->subtract_to(
178 dynamic_cache[minor_size][top_row][newKey],
179 R->mult(x.second, Dval));
180 }
181 }
182 }
183 }
184 }
185 }
186 }
187 return COMP_DONE;
188}
std::pair< std::vector< int >, std::vector< int > > ColRowIndices
Definition det.hpp:83
MinorsCache dynamic_cache
Definition det.hpp:99
const Ring * R
Definition det.hpp:56
std::map< int, int > row_lookup
Definition det.hpp:100
const Matrix * M
Definition det.hpp:57
@ COMP_DONE
Definition computation.h:60
@ COMP_INTERRUPTED
Definition computation.h:57
bool system_interrupted()
mpreal & negate(mpreal &x)
Definition mpreal.h:2047
volatile int x
TermIterator< Nterm > end(Nterm *)
Definition ringelem.cpp:5

References COMP_DONE, COMP_INTERRUPTED, dynamic_cache, end(), M, p, R, row_lookup, system_interrupted(), and x.

Referenced by step().