roots-fortran icon indicating copy to clipboard operation
roots-fortran copied to clipboard

Use nopass to simplify the interface for the user-defined function

Open arjenmarkus opened this issue 4 years ago • 3 comments

The abstract interface func has two arguments, "me" of type "class(root_solver)" and the proper argument of the function. The first serves no purpose for the evaluation of the function and can actually be suppressed:

procedure(func),pointer, nopass :: f => null()  !! user function to find the root of

This way the interface becomes the same as func2 and unifies the usage.

arjenmarkus avatar Aug 16 '21 17:08 arjenmarkus

The reason for it is so in the object-oriented interface you can put data required by the function in the class. Look at the oo example in the repo.

    type,extends(muller_solver_real64) :: my_solver
        integer :: ifunc = 0
        integer :: n = 1
    end type my_solver
    type(my_solver) :: solver

ifunc and n are used in the function.

In my mind, the oo interface is how it should be used, and the functional one is just a wrapper for simple use cases.

jacobwilliams avatar Aug 19 '21 15:08 jacobwilliams

Doesn't this imply that you need a different version of the function for each solver?

Op do 19 aug. 2021 om 17:38 schreef Jacob Williams @.***

:

The reason for it is so in the object-oriented interface you can put data required by the function in the class. Look at the oo example in the repo.

type,extends(muller_solver_real64) :: my_solver
    integer :: ifunc = 0
    integer :: n = 1
end type my_solver
type(my_solver) :: solver

ifunc and n are used in the function.

In my mind, the oo interface is how it should be used, and the function one is just a wrapper for simple use cases.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jacobwilliams/opt-lib/issues/1#issuecomment-902018261, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN6YR3I6N52W75YLQO5DXDT5UQPJANCNFSM5CIE2INA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

arjenmarkus avatar Aug 20 '21 13:08 arjenmarkus

No, since the function interface uses root_solver like so:

  function func(me,x)

    class(root_solver),intent(inout) :: me
    real(wp),intent(in) :: x
    real(wp) :: f

    select type (me)
    class is (my_solver)
      f = me%n * sin(x)
      me%ifunc = me%ifunc + 1
    end select 

  end function func

So the same function works for any method. But, you do have to use the annoying select type construct.

jacobwilliams avatar Sep 10 '21 12:09 jacobwilliams