Fitting a polynomial

57 views
Skip to first unread message

Martin Shetty

unread,
Apr 6, 2017, 1:00:19 PM4/6/17
to Ceres Solver
Dear Ceres community,

I am attempting to use Ceres for peak fitting in gamma spectroscopy. Sorry if my question is naive, as I am totally new to this framework, and I am not even sure that this is the right tool in the first place.

How would one define an AutoDiff function for a polynomial of degree N? In a more generalized sense, for a function that is a sum of N components, as in, for example, a sum of N Gaussian peaks, each with a different set of parameters? Having defined such a function as y=f(x), I would then like to fit it to a set of (x,y) points.

So far, I am concluding this is impossible with Ceres, because:
a) If each coefficient is a parameter, I must hardcode each one as const T* in the operator() function, and I can only have up to 9 of them, which is not scalable or
b) If I put them all into one parameter block, I must already known the number of parameters at compile-time, because I must initialize it as "AutoDiffCostFunction<PolynomialResidual, 1, N>", so I must also know N at compile time and on top of that then still hard-code array element access in operator() since the functor is not informed about the number of parameters even at compile time, much less dynamically.

Maybe my C++fu is weak or I am not getting something? If this is, after all, possible, perhaps there could be an example of such a case? I have tried googling "Ceres Solver polynomial" only to discover internal classes for root-finding.

Thanks,
Martin

Sameer Agarwal

unread,
Apr 6, 2017, 1:05:04 PM4/6/17
to ceres-...@googlegroups.com
Martin,
Are you just trying to fit a single polynomial to some data using L2 errr? That does not require the use of ceres, since it can be solved via a single linear solve. 

I am guessing that is not the case here.


How would one define an AutoDiff function for a polynomial of degree N? In a more generalized sense, for a function that is a sum of N components, as in, for example, a sum of N Gaussian peaks, each with a different set of parameters? Having defined such a function as y=f(x), I would then like to fit it to a set of (x,y) points. 

So far, I am concluding this is impossible with Ceres, because:
a) If each coefficient is a parameter, I must hardcode each one as const T* in the operator() function, and I can only have up to 9 of them, which is not scalable or
b) If I put them all into one parameter block, I must already known the number of parameters at compile-time, because I must initialize it as "AutoDiffCostFunction<PolynomialResidual, 1, N>", so I must also know N at compile time and on top of that then still hard-code array element access in operator() since the functor is not informed about the number of parameters even at compile time, much less dynamically.

Use DynamicAutoDiffCostFunction. It is meant precisely for this.

Sameer

 

Maybe my C++fu is weak or I am not getting something? If this is, after all, possible, perhaps there could be an example of such a case? I have tried googling "Ceres Solver polynomial" only to discover internal classes for root-finding.

Thanks,
Martin

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/faca010e-ccc1-447c-81ad-c71d93ea95f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages