MeshLib C++ Docs
Loading...
Searching...
No Matches
MRSparsePolynomial.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4
5namespace MR
6{
7
8template <typename C, typename D, D M>
9class SparsePolynomial;
10template <typename C, typename D, D M>
11SparsePolynomial<C,D,M> operator *( const SparsePolynomial<C,D,M>& a, const SparsePolynomial<C,D,M>& b );
12
17template <typename C, typename D, D M>
19{
20 static_assert( M > 0 );
21public:
23 SparsePolynomial() = default;
24
26 SparsePolynomial( std::map<D, C> && );
27
29 SparsePolynomial( C c0, D d1, C c1 );
30
32 SparsePolynomial( C c0, D d1, C c1, D d2, C c2 );
33
35 void setZeroCoeff( D d ) { map_.erase( d ); }
36
38 [[nodiscard]] bool empty() const { return map_.empty(); }
39
41 [[nodiscard]] bool isPositive() const;
42
44 [[nodiscard]] const std::map<D, C> & get() const { return map_; }
45
49
50private:
51 std::map<D, C> map_; // degree -> not-zero coefficient
52};
53
54template <typename C, typename D, D M>
55SparsePolynomial<C,D,M>::SparsePolynomial( std::map<D, C> && m ) : map_( std::move( m ) )
56{
57#ifndef NDEBUG
58 for ( const auto & [deg, cf] : map_ )
59 {
60 assert( deg <= M );
61 assert( cf != 0 );
62 }
63#endif
64}
65
66template <typename C, typename D, D M>
68{
69 assert( c1 != 0 );
70 assert( d1 != 0 );
71 if ( c0 != 0 )
72 map_[D(0)] = c0;
73 if ( d1 <= M )
74 map_[d1] = c1;
75}
76
77template <typename C, typename D, D M>
78SparsePolynomial<C,D,M>::SparsePolynomial( C c0, D d1, C c1, D d2, C c2 )
79{
80 assert( c1 != 0 );
81 assert( d1 != 0 );
82 assert( c2 != 0 );
83 assert( d2 != 0 );
84 assert( d1 != d2 );
85 if ( c0 != 0 )
86 map_[D(0)] = c0;
87 if ( d1 <= M )
88 map_[d1] = c1;
89 if ( d2 <= M )
90 map_[d2] = c2;
91}
92
93template <typename C, typename D, D M>
95{
96 if ( !map_.empty() )
97 return map_.begin()->second > 0;
98
99 assert (false);
100 return false;
101}
102
103template <typename C, typename D, D M>
105{
106 for ( const auto & [degB, cfB] : b.map_ )
107 {
108 assert( cfB != 0 );
109 auto [it,inserted] = map_.insert( { degB, cfB } );
110 if ( !inserted )
111 {
112 const auto sum = it->second + cfB;
113 if ( sum != 0 )
114 it->second = sum;
115 else
116 map_.erase( it );
117 }
118 }
119 return * this;
120}
121
122template <typename C, typename D, D M>
124{
125 for ( const auto & [degB, cfB] : b.map_ )
126 {
127 assert( cfB != 0 );
128 auto [it,inserted] = map_.insert( { degB, -cfB } );
129 if ( !inserted )
130 {
131 const auto sum = it->second - cfB;
132 if ( sum != 0 )
133 it->second = sum;
134 else
135 map_.erase( it );
136 }
137 }
138 return * this;
139}
140
141template <typename C, typename D, D M>
143{
144 std::map<D,C> resMap;
145 for ( const auto & [degA, cfA] : a.map_ )
146 {
147 assert( cfA != 0 );
148 for ( const auto & [degB, cfB] : b.map_ )
149 {
150 assert( cfB != 0 );
151 const auto deg = degA + degB;
152 if ( deg > M )
153 break;
154 const auto cf = cfA * cfB;
155 auto [it,inserted] = resMap.insert( { deg, cf } );
156 if ( !inserted )
157 {
158 const auto sum = it->second + cf;
159 if ( sum != 0 )
160 it->second = sum;
161 else
162 resMap.erase( it );
163 }
164 }
165 }
166 return resMap;
167}
168
169} //namespace MR
#define M(T)
Definition MRSparsePolynomial.h:19
bool empty() const
returns true if no single polynomial coefficient is defined
Definition MRSparsePolynomial.h:38
SparsePolynomial & operator-=(const SparsePolynomial &b)
Definition MRSparsePolynomial.h:123
SparsePolynomial()=default
constructs zero polynomial
bool isPositive() const
returns true if the coefficient for the smallest not-zero degress is positive
Definition MRSparsePolynomial.h:94
void setZeroCoeff(D d)
sets coefficient for given degree to zero
Definition MRSparsePolynomial.h:35
SparsePolynomial & operator+=(const SparsePolynomial &b)
Definition MRSparsePolynomial.h:104
const std::map< D, C > & get() const
gets read-only access to all not-zero coefficients
Definition MRSparsePolynomial.h:44
friend SparsePolynomial operator*(const SparsePolynomial &a, const SparsePolynomial &b)
Definition MRSparsePolynomial.h:142
Definition MRCameraOrientationPlugin.h:8
Color operator*(float a, const Color &b)
Definition MRMesh/MRColor.h:116