You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Bernard GODARD <be...@gmail.com> on 2012/04/17 15:06:22 UTC

[math] UnivariateRealSolver in math3

Hello,

I am trying to port my program from commons-math 2.2 to commons-math 3.0.

I have several routines that require solvers implementing the
interface UnivariateRealSolver.

Solvers instance used in the program are BrentSolver, BisectionSolver,
SecantSolver, NewtonSolver...

Most of solvers in 3.0 now implement UnivariateSolver. However
NewtonSolver does not.
The only relevant common interface between NewtonSolver and the other
solvers is BaseUnivariateSolver<UnivariateFunction>.
However according to the API documentation, this is an internal
interface that should not be used outside the library iself.

What is the correct way to do this?

Thank you,


       Bernard

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Luc Maisonobe <Lu...@free.fr>.
Le 19/04/2012 18:43, Bernard GODARD a écrit :
>> Well, let's say you have a framework which requires solving
>> an equation deep down, and you want to pass a solver from the
>> outside. I admit this is exotic, and in all of the examples I've
>> seen the usage of a solver in such a framework was ultimately part
>> of a function evaluation, an should probably be encapsulated as
>> such.
> 
> This is exactly my problem. I have a module that computes a satellite
> orbit. The most useful method returns the satellite state at the
> desired time. To do so, it has to solve an equation deep down. The
> solver is passed to the constructor during initialization and can also
> be changed using setter/getter.

I understand your point, but in this case the solvers should either have
the same API or if you want to support both solvers that use only
function values and solvers that also use the derivative, you can use
two constructors, two setters and two fields for storing the solvers.
Only one of the fields would be non-null at any time.

> I could of course have a unique solver that is transparent to the user
> of my module (except for accuracy tuning that could be passed to the
> constructor) but then the user does not get as much freedom.

I would suggest something along this line, and especially using the Nth
order bracketing solver with a setting for 5th order. It does not need
derivatives, it is as fast (or faster) than Newton (higher order) and
provides the very interesting bracket selection feature at convergence.

best regards,
Luc

> 
>       Bernard
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Bernard GODARD <be...@gmail.com>.
> Well, let's say you have a framework which requires solving
> an equation deep down, and you want to pass a solver from the
> outside. I admit this is exotic, and in all of the examples I've
> seen the usage of a solver in such a framework was ultimately part
> of a function evaluation, an should probably be encapsulated as
> such.

This is exactly my problem. I have a module that computes a satellite
orbit. The most useful method returns the satellite state at the
desired time. To do so, it has to solve an equation deep down. The
solver is passed to the constructor during initialization and can also
be changed using setter/getter.
I could of course have a unique solver that is transparent to the user
of my module (except for accuracy tuning that could be passed to the
constructor) but then the user does not get as much freedom.

      Bernard

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
On Thu, Apr 19, 2012 at 06:19:31PM +0200, J.Pietschmann wrote:
> Am 19.04.2012 00:31, schrieb Gilles Sadowski:
> > Please give a short code example of what you mean by this.
> 
> Well, let's say you have a framework which requires solving
> an equation deep down, and you want to pass a solver from the
> outside. I admit this is exotic, and in all of the examples I've
> seen the usage of a solver in such a framework was ultimately part
> of a function evaluation, an should probably be encapsulated as
> such.

Please provide a _code_ example of how you'd want this to work.


Best regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Am 19.04.2012 00:31, schrieb Gilles Sadowski:
> Please give a short code example of what you mean by this.

Well, let's say you have a framework which requires solving
an equation deep down, and you want to pass a solver from the
outside. I admit this is exotic, and in all of the examples I've
seen the usage of a solver in such a framework was ultimately part
of a function evaluation, an should probably be encapsulated as
such.

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi.

> 
> But isn't this a regression compared to version 2.2 of commons math.
> 
> There is a lot of commonalities between the Newton solver and the other solvers.
> If I have a class representing a problem for which I have derivative,
> lower and upper bounds and initial guess, I can try to solve it with a
> Newton solver but also with the other solvers. So I should be able to
> pass any of the univariate real solvers.
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Please give a short code example of what you mean by this.

In Commons Math, you don't pass the solver to the function, you pass the
function to the solver's "solve" method(s). And doing so, nothing prevents
you from passing an instance of a "DifferentiableUnivariateFunction" to a
"solve" method that expects an instance of "UnivariateFunction" (since the
former "extends" the latter).

> However this is not possible
> in math3.0 since there is no common interface anymore bewteen
> NewtonSolver and the other solvers [...]

That's so because they don't provide interchangeable functionalities (one
requires the "derivative" in order to work, while the others do not).

The alternative would have been to check, at runtime (with "instanceof")
whether the provided function is indeed an instance of
"DifferentiableUnivariateFunction" and throw an exception if not.
This is indeed what was done before, but it is more bug-prone since this
bypasses the strong type-checking provided by the compiler.


Regards,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Bernard GODARD <be...@gmail.com>.
Thank you Gilles,

But isn't this a regression compared to version 2.2 of commons math.

There is a lot of commonalities between the Newton solver and the other solvers.
If I have a class representing a problem for which I have derivative,
lower and upper bounds and initial guess, I can try to solve it with a
Newton solver but also with the other solvers. So I should be able to
pass any of the univariate real solvers. However this is not possible
in math3.0 since there is no common interface anymore bewteen
NewtonSolver and the other solvers (except java.lang.Object but that's
not a good idea).

Thank you,

On Tue, Apr 17, 2012 at 1:32 PM, Gilles Sadowski
<gi...@harfang.homelinux.org> wrote:
> Hello.
>
>>
>> I am trying to port my program from commons-math 2.2 to commons-math 3.0.
>>
>> I have several routines that require solvers implementing the
>> interface UnivariateRealSolver.
>>
>> Solvers instance used in the program are BrentSolver, BisectionSolver,
>> SecantSolver, NewtonSolver...
>>
>> Most of solvers in 3.0 now implement UnivariateSolver. However
>> NewtonSolver does not.
>> The only relevant common interface between NewtonSolver and the other
>> solvers is BaseUnivariateSolver<UnivariateFunction>.
>
> "NewtonSolver" implements
>  BaseUnivariateSolver<DifferentiableUnivariateFunction>
> not
>  BaseUnivariateSolver<UnivariateFunction>
>
>> However according to the API documentation, this is an internal
>> interface that should not be used outside the library iself.
>>
>> What is the correct way to do this?
>
> You cannot use "NewtonSolver" as a "UnivariateRealSolver" because that
> algorithm requires the function to be differentiable. Hence there exists a
> "DifferentiableUnivariateSolver" interface which is implemented by
> "NewtonSolver".
>
>
> HTH,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] UnivariateRealSolver in math3

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hello.

> 
> I am trying to port my program from commons-math 2.2 to commons-math 3.0.
> 
> I have several routines that require solvers implementing the
> interface UnivariateRealSolver.
> 
> Solvers instance used in the program are BrentSolver, BisectionSolver,
> SecantSolver, NewtonSolver...
> 
> Most of solvers in 3.0 now implement UnivariateSolver. However
> NewtonSolver does not.
> The only relevant common interface between NewtonSolver and the other
> solvers is BaseUnivariateSolver<UnivariateFunction>.

"NewtonSolver" implements
  BaseUnivariateSolver<DifferentiableUnivariateFunction>
not
  BaseUnivariateSolver<UnivariateFunction>

> However according to the API documentation, this is an internal
> interface that should not be used outside the library iself.
> 
> What is the correct way to do this?

You cannot use "NewtonSolver" as a "UnivariateRealSolver" because that
algorithm requires the function to be differentiable. Hence there exists a
"DifferentiableUnivariateSolver" interface which is implemented by
"NewtonSolver".


HTH,
Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org