MeshLib C++ Docs
Loading...
Searching...
No Matches
MRSparsePolynomial.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cassert>
5#include <type_traits>
6#include <utility>
7#include <vector>
8
9namespace MR
10{
13
14
15template <typename C, typename D, D M>
17
19template <typename T, typename D, D M>
21
24template <typename T, typename C, typename D, D M>
26
28template <typename C, typename D, D M>
30
32template <typename A, typename B>
34{
35 const A* a = nullptr;
36 const B* b = nullptr;
37 bool negative = false;
38};
39
48template <typename TA, typename TB = TA, typename CA, typename CB, typename D, D M>
49[[nodiscard]] std::pair<D, int> lowestTermOfSumOfProducts( const SignedProduct<SparsePolynomial<CA,D,M>, SparsePolynomial<CB,D,M>>* products, int numProducts,
50 std::type_identity_t<D> from = 0 );
51
54template <typename T, typename C, typename D, D M>
55[[nodiscard]] std::pair<D, int> lowestTermOfProductsDiff( const SparsePolynomial<C,D,M>& a, const SparsePolynomial<C,D,M>& b,
56 const SparsePolynomial<C,D,M>& c, const SparsePolynomial<C,D,M>& d, std::type_identity_t<D> from = 0 )
57{
58 const SignedProduct<SparsePolynomial<C,D,M>, SparsePolynomial<C,D,M>> products[2] = { { &a, &b, false }, { &c, &d, true } };
59 return lowestTermOfSumOfProducts<T>( products, 2, from );
60}
61
64template <typename T, typename C, typename D, D M>
66 const SparsePolynomial<C,D,M>& c, const SparsePolynomial<C,D,M>& d ) { return lowestTermOfProductsDiff<T>( a, b, c, d ).second; }
67
73template <typename C, typename D, D M>
75{
76 static_assert( M > 0 );
77public:
79 using Term = std::pair<D, C>;
80
82 SparsePolynomial() = default;
83
86 SparsePolynomial( std::vector<Term> && );
87
89 SparsePolynomial( C c0, D d1, C c1 );
90
92 SparsePolynomial( C c0, D d1, C c1, D d2, C c2 );
93
95 [[nodiscard]] static SparsePolynomial fromUnsortedTerms( std::vector<Term> && terms );
96
98 void setZeroCoeff( D d )
99 {
100 auto it = std::lower_bound( terms_.begin(), terms_.end(), d,
101 []( const Term & t, D d ) { return t.first < d; } );
102 if ( it != terms_.end() && it->first == d )
103 terms_.erase( it );
104 }
105
107 [[nodiscard]] bool empty() const { return terms_.empty(); }
108
110 [[nodiscard]] bool isPositive() const;
111
113 [[nodiscard]] const std::vector<Term> & get() const { return terms_; }
114
117 [[nodiscard]] friend SparsePolynomial operator +( SparsePolynomial a, const SparsePolynomial& b ) { a += b; return a; }
118 [[nodiscard]] friend SparsePolynomial operator -( SparsePolynomial a, const SparsePolynomial& b ) { a -= b; return a; }
119 template <typename T, typename C2, typename D2, D2 M2>
121
122private:
124 void mergeTerms_();
125
126 std::vector<Term> terms_;
127};
128
129template <typename C, typename D, D M>
130SparsePolynomial<C,D,M>::SparsePolynomial( std::vector<Term> && terms ) : terms_( std::move( terms ) )
131{
132#ifndef NDEBUG
133 for ( size_t i = 0; i < terms_.size(); ++i )
134 {
135 assert( terms_[i].first <= M );
136 assert( terms_[i].second != 0 );
137 assert( i == 0 || terms_[i - 1].first < terms_[i].first );
138 }
139#endif
140}
141
142template <typename C, typename D, D M>
144{
145 assert( c1 != 0 );
146 assert( d1 != 0 );
147 if ( c0 != 0 )
148 terms_.emplace_back( D(0), c0 );
149 if ( d1 <= M )
150 terms_.emplace_back( d1, c1 );
151}
152
153template <typename C, typename D, D M>
154SparsePolynomial<C,D,M>::SparsePolynomial( C c0, D d1, C c1, D d2, C c2 )
155{
156 assert( c1 != 0 );
157 assert( d1 != 0 );
158 assert( c2 != 0 );
159 assert( d2 != 0 );
160 assert( d1 != d2 );
161 if ( c0 != 0 )
162 terms_.emplace_back( D(0), c0 );
163 if ( d1 > d2 )
164 {
165 std::swap( d1, d2 );
166 std::swap( c1, c2 );
167 }
168 if ( d1 <= M )
169 terms_.emplace_back( d1, c1 );
170 if ( d2 <= M )
171 terms_.emplace_back( d2, c2 );
172}
173
174template <typename C, typename D, D M>
176{
177 std::sort( terms.begin(), terms.end(), []( const Term & x, const Term & y ) { return x.first < y.first; } );
178 assert( terms.empty() || terms.back().first <= M );
180 res.terms_ = std::move( terms );
181 res.mergeTerms_();
182 return res;
183}
184
185template <typename C, typename D, D M>
187{
188 if ( !terms_.empty() )
189 return terms_.front().second > 0;
190
191 assert (false);
192 return false;
193}
194
195template <typename C, typename D, D M>
196void SparsePolynomial<C,D,M>::mergeTerms_()
197{
198 size_t out = 0;
199 for ( size_t i = 0; i < terms_.size(); )
200 {
201 auto deg = terms_[i].first;
202 auto cf = std::move( terms_[i].second );
203 for ( ++i; i < terms_.size() && terms_[i].first == deg; ++i )
204 cf += terms_[i].second;
205 if ( cf != 0 )
206 terms_[out++] = { deg, std::move( cf ) };
207 }
208 terms_.resize( out );
209}
210
211template <typename C, typename D, D M>
213{
214 std::vector<Term> res;
215 res.reserve( terms_.size() + b.terms_.size() );
216 std::merge( std::make_move_iterator( terms_.begin() ), std::make_move_iterator( terms_.end() ),
217 b.terms_.begin(), b.terms_.end(), std::back_inserter( res ),
218 []( const Term & x, const Term & y ) { return x.first < y.first; } );
219 terms_ = std::move( res );
220 mergeTerms_();
221 return * this;
222}
223
224template <typename C, typename D, D M>
226{
227 std::vector<Term> res;
228 res.reserve( terms_.size() + b.terms_.size() );
229 auto itA = terms_.begin();
230 auto itB = b.terms_.begin();
231 while ( itA != terms_.end() && itB != b.terms_.end() )
232 {
233 if ( itB->first < itA->first )
234 {
235 res.emplace_back( itB->first, -itB->second );
236 ++itB;
237 }
238 else
239 {
240 res.push_back( std::move( *itA ) );
241 ++itA;
242 }
243 }
244 for ( ; itA != terms_.end(); ++itA )
245 res.push_back( std::move( *itA ) );
246 for ( ; itB != b.terms_.end(); ++itB )
247 res.emplace_back( itB->first, -itB->second );
248 terms_ = std::move( res );
249 mergeTerms_();
250 return * this;
251}
252
253template <typename T, typename C, typename D, D M>
255{
257 using Term = typename Res::Term;
258 std::vector<Term> res;
259 res.reserve( a.terms_.size() * b.terms_.size() );
260 for ( const auto & [degA, cfA] : a.terms_ )
261 {
262 assert( cfA != 0 );
263 for ( const auto & [degB, cfB] : b.terms_ )
264 {
265 assert( cfB != 0 );
266 const auto deg = degA + degB;
267 if ( deg > M )
268 break;
270 if constexpr ( std::is_same_v<T, C> )
271 res.emplace_back( deg, cfA * cfB );
272 else
273 res.emplace_back( deg, T( cfA ) * T( cfB ) );
274 }
275 }
276 return Res::fromUnsortedTerms( std::move( res ) );
277}
278
279template <typename TA, typename TB, typename CA, typename CB, typename D, D M>
281 std::type_identity_t<D> from )
282{
285 struct RowTerm
286 {
287 D deg;
288 int p, i, j;
289 };
290 auto greater = []( const RowTerm & x, const RowTerm & y ) { return x.deg > y.deg; };
291 std::vector<RowTerm> heap;
292 size_t numRows = 0;
293 for ( int p = 0; p < numProducts; ++p )
294 numRows += products[p].a->get().size();
295 heap.reserve( numRows );
296 for ( int p = 0; p < numProducts; ++p )
297 {
298 const auto & f = products[p].a->get();
299 const auto & g = products[p].b->get();
300 if ( g.empty() )
301 continue;
303 const int jFrom = from > 0 ? int( std::lower_bound( g.begin(), g.end(), from, []( const auto & t, D d ) { return t.first < d; } ) - g.begin() ) : 0;
304 for ( int i = 0; i < (int)f.size(); ++i )
305 {
306 const int j = f[i].first < from ? jFrom : 0;
307 if ( j == (int)g.size() )
308 continue;
311 if ( const D deg = f[i].first + g[j].first; deg <= M )
312 heap.push_back( { deg, p, i, j } );
313 else if ( j == 0 )
314 break;
315 }
316 }
317 std::make_heap( heap.begin(), heap.end(), greater );
318
320 const auto mul = []( const CA & x, const CB & y )
321 {
322 if constexpr ( std::is_same_v<TA, CA> && std::is_same_v<TB, CB> )
323 return x * y;
324 else if constexpr ( std::is_same_v<TA, CA> )
325 return x * TB( y );
326 else if constexpr ( std::is_same_v<TB, CB> )
327 return TA( x ) * y;
328 else
329 return TA( x ) * TB( y );
330 };
331 while ( !heap.empty() )
332 {
333 const auto deg = heap.front().deg;
334 decltype( mul( std::declval<CA>(), std::declval<CB>() ) ) coeff{};
335 do
336 {
337 std::pop_heap( heap.begin(), heap.end(), greater );
338 auto r = heap.back();
339 heap.pop_back();
340 const auto & f = products[r.p].a->get();
341 const auto & g = products[r.p].b->get();
342 const auto prod = mul( f[r.i].second, g[r.j].second );
343 if ( products[r.p].negative )
344 coeff -= prod;
345 else
346 coeff += prod;
347 if ( ++r.j < (int)g.size() && ( r.deg = f[r.i].first + g[r.j].first ) <= M )
348 {
349 heap.push_back( r );
350 std::push_heap( heap.begin(), heap.end(), greater );
351 }
352 }
353 while ( !heap.empty() && heap.front().deg == deg );
354 if ( coeff > 0 )
355 return { deg, 1 };
356 if ( coeff < 0 )
357 return { deg, -1 };
358 }
359 return {};
360}
361
362}
#define M(T)
Definition MRSparsePolynomial.h:75
SparsePolynomialProduct< T, D, M > mulAs(const SparsePolynomial< C, D, M > &a, const SparsePolynomial< C, D, M > &b)
Definition MRSparsePolynomial.h:254
bool empty() const
returns true if no single polynomial coefficient is defined
Definition MRSparsePolynomial.h:107
SparsePolynomial & operator-=(const SparsePolynomial &b)
Definition MRSparsePolynomial.h:225
SparsePolynomial()=default
constructs zero polynomial
std::pair< D, C > Term
a not-zero coefficient with its degree
Definition MRSparsePolynomial.h:79
const std::vector< Term > & get() const
gets read-only access to all not-zero coefficients
Definition MRSparsePolynomial.h:113
bool negative
whether the product is subtracted
Definition MRSparsePolynomial.h:37
bool isPositive() const
returns true if the coefficient for the smallest not-zero degress is positive
Definition MRSparsePolynomial.h:186
friend SparsePolynomialProduct< T, D2, M2 > mulAs(const SparsePolynomial< C2, D2, M2 > &a, const SparsePolynomial< C2, D2, M2 > &b)
int signOfProductsDiff(const SparsePolynomial< C, D, M > &a, const SparsePolynomial< C, D, M > &b, const SparsePolynomial< C, D, M > &c, const SparsePolynomial< C, D, M > &d)
Definition MRSparsePolynomial.h:65
void setZeroCoeff(D d)
sets coefficient for given degree to zero
Definition MRSparsePolynomial.h:98
Color operator*(float a, const Color &b)
Definition MRColor.h:122
const B * b
Definition MRSparsePolynomial.h:36
SparsePolynomial & operator+=(const SparsePolynomial &b)
Definition MRSparsePolynomial.h:212
friend SparsePolynomial operator+(SparsePolynomial a, const SparsePolynomial &b)
Definition MRSparsePolynomial.h:117
std::pair< D, int > lowestTermOfSumOfProducts(const SignedProduct< SparsePolynomial< CA, D, M >, SparsePolynomial< CB, D, M > > *products, int numProducts, std::type_identity_t< D > from=0)
Definition MRSparsePolynomial.h:280
std::pair< D, int > lowestTermOfProductsDiff(const SparsePolynomial< C, D, M > &a, const SparsePolynomial< C, D, M > &b, const SparsePolynomial< C, D, M > &c, const SparsePolynomial< C, D, M > &d, std::type_identity_t< D > from=0)
Definition MRSparsePolynomial.h:55
friend SparsePolynomial operator-(SparsePolynomial a, const SparsePolynomial &b)
Definition MRSparsePolynomial.h:118
const A * a
Definition MRSparsePolynomial.h:35
static SparsePolynomial fromUnsortedTerms(std::vector< Term > &&terms)
constructs polynomial from arbitrary terms with degrees not above M: sorts them by degree,...
Definition MRSparsePolynomial.h:175
SparsePolynomial< decltype(std::declval< T >() *std::declval< T >()), D, M > SparsePolynomialProduct
the type of the polynomial with coefficients of the type of the product of two values of type T
Definition MRSparsePolynomial.h:20
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
one product of two polynomials in a sum, see lowestTermOfSumOfProducts
Definition MRSparsePolynomial.h:34