15template <
typename C,
typename D, D M>
19template <
typename T,
typename D, D M>
24template <
typename T,
typename C,
typename D, D M>
28template <
typename C,
typename D, D M>
32template <
typename A,
typename B>
48template <
typename TA,
typename TB = TA,
typename CA,
typename CB,
typename D, D M>
50 std::type_identity_t<D> from = 0 );
54template <
typename T,
typename C,
typename D, D M>
58 const SignedProduct<SparsePolynomial<C,D,M>,
SparsePolynomial<C,D,M>> products[2] = { { &a, &b,
false }, { &c, &d,
true } };
64template <
typename T,
typename C,
typename D, D M>
73template <
typename C,
typename D, D M>
76 static_assert(
M > 0 );
79 using Term = std::pair<D, C>;
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 )
107 [[nodiscard]]
bool empty()
const {
return terms_.empty(); }
113 [[nodiscard]]
const std::vector<Term> &
get()
const {
return terms_; }
119 template <
typename T,
typename C2,
typename D2, D2 M2>
126 std::vector<Term> terms_;
129template <
typename C,
typename D, D M>
133 for (
size_t i = 0; i < terms_.size(); ++i )
135 assert( terms_[i].first <=
M );
136 assert( terms_[i].second != 0 );
137 assert( i == 0 || terms_[i - 1].first < terms_[i].first );
142template <
typename C,
typename D, D M>
148 terms_.emplace_back( D(0), c0 );
150 terms_.emplace_back( d1, c1 );
153template <
typename C,
typename D, D M>
162 terms_.emplace_back( D(0), c0 );
169 terms_.emplace_back( d1, c1 );
171 terms_.emplace_back( d2, c2 );
174template <
typename C,
typename D, D M>
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 );
185template <
typename C,
typename D, D M>
188 if ( !terms_.empty() )
189 return terms_.front().second > 0;
195template <
typename C,
typename D, D M>
196void SparsePolynomial<C,D,M>::mergeTerms_()
199 for (
size_t i = 0; i < terms_.size(); )
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;
206 terms_[out++] = { deg, std::move( cf ) };
208 terms_.resize( out );
211template <
typename C,
typename D, D M>
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 );
224template <
typename C,
typename D, D M>
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() )
233 if ( itB->first < itA->first )
235 res.emplace_back( itB->first, -itB->second );
240 res.push_back( std::move( *itA ) );
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 );
253template <
typename T,
typename C,
typename D, D M>
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_ )
263 for (
const auto & [degB, cfB] : b.terms_ )
266 const auto deg = degA + degB;
270 if constexpr ( std::is_same_v<T, C> )
271 res.emplace_back( deg, cfA * cfB );
273 res.emplace_back( deg, T( cfA ) * T( cfB ) );
276 return Res::fromUnsortedTerms( std::move( res ) );
279template <
typename TA,
typename TB,
typename CA,
typename CB,
typename D, D M>
281 std::type_identity_t<D> from )
290 auto greater = [](
const RowTerm & x,
const RowTerm & y ) {
return x.deg > y.deg; };
291 std::vector<RowTerm> heap;
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 )
298 const auto & f = products[p].a->get();
299 const auto & g = products[p].b->get();
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 )
306 const int j = f[i].first < from ? jFrom : 0;
307 if ( j == (
int)g.size() )
311 if (
const D deg = f[i].first + g[j].first; deg <=
M )
312 heap.push_back( { deg, p, i, j } );
317 std::make_heap( heap.begin(), heap.end(), greater );
320 const auto mul = [](
const CA & x,
const CB & y )
322 if constexpr ( std::is_same_v<TA, CA> && std::is_same_v<TB, CB> )
324 else if constexpr ( std::is_same_v<TA, CA> )
326 else if constexpr ( std::is_same_v<TB, CB> )
329 return TA( x ) * TB( y );
331 while ( !heap.empty() )
333 const auto deg = heap.front().deg;
334 decltype( mul( std::declval<CA>(), std::declval<CB>() ) ) coeff{};
337 std::pop_heap( heap.begin(), heap.end(), greater );
338 auto r = heap.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 )
347 if ( ++r.j < (
int)g.size() && ( r.deg = f[r.i].first + g[r.j].first ) <=
M )
350 std::push_heap( heap.begin(), heap.end(), greater );
353 while ( !heap.empty() && heap.front().deg == deg );
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