Skip to content

Commit b3d3817

Browse files
authored
Add solveWithFixedDegree wrapper (#3146)
* Add `solveWithFixedDegree` wrapper, that removes leading zeros from the polynomial before passing it to the actual solver. * Fix build.
1 parent a987ba8 commit b3d3817

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

source/MRMesh/MRBestFitPolynomial.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,39 @@ struct Solver<T, 4>
124124
};
125125

126126

127+
template <typename T, size_t degree>
128+
std::vector<std::complex<T>> solveWithFixedDegree( auto coeffsBegin, auto coeffsEnd )
129+
{
130+
assert( coeffsEnd - coeffsBegin == degree + 1 );
131+
if constexpr ( degree == 0 )
132+
{
133+
return {};
134+
}
135+
else
136+
{
137+
if ( *std::prev( coeffsEnd ) != 0 )
138+
{
139+
Eigen::Vector<T, degree + 1> c;
140+
for ( size_t i = 0; i < degree + 1; ++i )
141+
c[i] = *( coeffsBegin + i );
142+
143+
Solver<T, degree> s;
144+
auto roots = s( c );
145+
146+
std::vector<std::complex<T>> res;
147+
for ( auto v : roots )
148+
res.push_back( v );
149+
return res;
150+
}
151+
else
152+
{
153+
return solveWithFixedDegree<T, degree - 1>( coeffsBegin, std::prev( coeffsEnd ) );
154+
}
155+
}
156+
}
157+
158+
159+
127160
}
128161

129162
namespace MR
@@ -152,8 +185,7 @@ std::vector<T> Polynomial<T, degree>::solve( T tol ) const
152185
if constexpr ( canSolvePolynomial( degree ) )
153186
{
154187
#endif
155-
Solver<T, degree> solver;
156-
auto r_c = solver( a );
188+
auto r_c = solveWithFixedDegree<T, degree>( a.begin(), a.end() );
157189
std::vector<T> r;
158190
for ( std::complex<T> c : r_c )
159191
if ( std::abs( c.imag() ) < tol )

source/MRMesh/MRBestFitPolynomial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class BestFitPolynomial
9898

9999
MRMESH_API void addPoint( T x, T y, T weight );
100100

101+
/// @note The result might have leading coefficient equal to zero.
101102
MRMESH_API Polynomial<T, degree> getBestPolynomial() const;
102103

103104
private:

0 commit comments

Comments
 (0)