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