Skip to content

Commit

Permalink
Add solveWithFixedDegree wrapper (#3146)
Browse files Browse the repository at this point in the history
* Add `solveWithFixedDegree` wrapper, that removes leading zeros from the polynomial before passing it to the actual solver.

* Fix build.
  • Loading branch information
andrewerf authored Aug 8, 2024
1 parent a987ba8 commit b3d3817
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions source/MRMesh/MRBestFitPolynomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,39 @@ struct Solver<T, 4>
};


template <typename T, size_t degree>
std::vector<std::complex<T>> solveWithFixedDegree( auto coeffsBegin, auto coeffsEnd )
{
assert( coeffsEnd - coeffsBegin == degree + 1 );
if constexpr ( degree == 0 )
{
return {};
}
else
{
if ( *std::prev( coeffsEnd ) != 0 )
{
Eigen::Vector<T, degree + 1> c;
for ( size_t i = 0; i < degree + 1; ++i )
c[i] = *( coeffsBegin + i );

Solver<T, degree> s;
auto roots = s( c );

std::vector<std::complex<T>> res;
for ( auto v : roots )
res.push_back( v );
return res;
}
else
{
return solveWithFixedDegree<T, degree - 1>( coeffsBegin, std::prev( coeffsEnd ) );
}
}
}



}

namespace MR
Expand Down Expand Up @@ -152,8 +185,7 @@ std::vector<T> Polynomial<T, degree>::solve( T tol ) const
if constexpr ( canSolvePolynomial( degree ) )
{
#endif
Solver<T, degree> solver;
auto r_c = solver( a );
auto r_c = solveWithFixedDegree<T, degree>( a.begin(), a.end() );
std::vector<T> r;
for ( std::complex<T> c : r_c )
if ( std::abs( c.imag() ) < tol )
Expand Down
1 change: 1 addition & 0 deletions source/MRMesh/MRBestFitPolynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class BestFitPolynomial

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

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

private:
Expand Down

0 comments on commit b3d3817

Please sign in to comment.