2

私は、FORTRAN 2003 (F2003) のオブジェクト指向プログラミング (OOP) 機能を実装することに非常に熱心です。私の質問は、プログラムの設計に関するものです。関数f(x)=0のルート ファインダーのような、ソルバーがあるとしましょう。FORTRAN の最も単純な形式では、次のような形になります。

function solver(f,a,b) result(root)
    [some definition of variables]
    [some iterative procedures]
    root = ...
end function

f95 のような以前のバージョンの FORTRAN では、移植性を得るために、コードは個別にコンパイルされ、外部関数がソルバーに渡されます。F2003 OOP の観点から、一般的なケースでソルバーのクラスがあるとしましょう

type solver_t
   real :: a,b,root
contains
   procedure :: solve => solve_solver_t          ! root=this%solve()
   [some initialization and post-calculation procedures]
end type

そして私たちの曲線のための別のクラス

type curve_t
    [some variable definition]
contains
    [some initialization procedures]
    procedure :: func => function_curve_t    ! y=this%func(x)
    procedure :: plot => plot_curve
end type

より多くの異なる曲線クラス (タイプ) があります。さて、これらの 2 つの概念をソルバー クラスをコンパイルする方法で (曲線のクラス/タイプを知らずに) 接続するにはどうすればよいでしょうか。また、新しい別の曲線クラス (2nd_order_polynomial_curve など) を作成するたびに、変更せずにそれを実装できます。 、3rd_order_polynomial_curve、log_curve、exp_curve、...)。つまり、最後に、どういうわけか曲線の根を取得します。

4

1 に答える 1

3

F2003 OOP を使用してこのアイデアを実装する方法の一例を次に示します。共有ライブラリに組み込むモジュールから始めます。

module solver
  implicit none

  type, abstract :: curve_t
   contains
     procedure(func_f), pass(this), deferred :: f
  end type curve_t

  type :: solver_t
     class(curve_t), pointer :: curve
   contains
     procedure, pass :: solve => solve_root_bisect_method
  end type solver_t

  abstract interface
     function func_f(this, x)
       import curve_t
       class(curve_t) :: this
       real, intent(in) :: x
       real :: func_f
     end function func_f
  end interface

contains

  function solve_root_bisect_method(this, a_start, b_start) result(root)
    implicit none
    class(solver_t) :: this
    real, intent(in) :: a_start, b_start
    real :: root, c, eps, a, b
    integer :: i, imax
    imax = 100
    eps = 1e-5
    a = a_start
    b = b_start

    do i=1, imax
       c = (a+b)/2.
       if ( (this%curve%f(c) == 0) .or. ((b-a)/2. < eps)) then
          root = c
          return
       end if
       if (sign(1.,this%curve%f(c)) == sign(1.,this%curve%f(a))) then
          a = c
       else
          b = c
       end if
    end do
    ! solution did not converge, produce error
    root = -999
  end function solve_root_bisect_method
end module solver

これは、曲線を表す抽象クラスとソルバーのクラスを定義します。ソルバーを抽象化することもできますが、デモの目的のために、それを行わず、1 つのソルバーを提供することにしました。このタイプを拡張して、ソルブ インターフェイスに別の手順を提供することもできます。これを共有ライブラリにコンパイルできます。

gfortran -shared -fPIC -o solver.so solver.f90

と が得solver.soられsolver.modます。この余分な手順を実行して、曲線の知識がなくても移植性とコンパイルを実証しました。

これで、この便利なライブラリを使用して任意の曲線の根を見つけたいサードパーティのふりをすることができます。まず、独自のモジュールを定義して曲線を拡張し、いくつかの機能を提供できます。

module curves
  use solver
  implicit none

  type, extends(curve_t) :: linear_curve
     real :: m, b
   contains
     procedure, pass(this) :: f => f_linear
  end type linear_curve

  type, extends(curve_t) :: polynomial_curve
     real :: a, b, c
   contains
     procedure, pass(this) :: f => f_polynomial
  end type polynomial_curve

contains

  real function f_linear(this, x)
    use solver
    implicit none
    class(linear_curve) :: this
    real, intent(in) :: x
    f_linear = this%m * x + this%b
  end function f_linear

  real function f_polynomial(this, x)
    use solver
    implicit none
    class(polynomial_curve) :: this
    real, intent(in) :: x
    f_polynomial = this%a*x*x + this%b*x + this%c
  end function f_polynomial
end module curves

これは、パラメータを含む線形曲線と多項式曲線のタイプと、これらのパラメータの関数として計算するための関数を定義しyますx。から派生curve_tし、インターフェイスに準拠しているためf、これらのクラスをクラスで簡単に使用できますsolver_t

これを実証するための小さなプログラムを次に示します。

program test
  use solver
  use curves
  implicit none

  type(linear_curve), target :: linear
  type(polynomial_curve), target :: parabola
  type(solver_t) :: root_solver
  real :: root

  linear%m = 1.
  linear%b = 0.     ! y=x
  parabola%a = 1.
  parabola%b = 0.
  parabola%c = -1.  ! y=x^2-1

  root_solver%curve => linear
  root = root_solver%solve(-1., 1.)
  print *, "root  = ", root

  root_solver%curve => parabola
  root = root_solver%solve(-4., 0.5)
  print *, "root1 = ", root
  root = root_solver%solve(-0.5, 4.)
  print *, "root2 = ", root
end program test

ここでは、いくつかの曲線を宣言し、それらのパラメーターを設定してから、ソルバーを呼び出して根を見つけます。曲線モジュール、テスト プログラム、および以前に作成した共有ライブラリへのリンクをコンパイルすると、出力を使用して実行できます。

% ./roots                               
 root  =    0.00000000    
 root1 =   -1.00000286    
 root2 =    1.00000286

(ルートの品質は、最初のモジュールにダンプしたサンプル ソルバーの品質によって制限されます。より良い結果を得ることができます)。これは純粋な OO の最適なデモではありません。solver_t クラスの方が優れている可能性があるためです。ただし、solver_t のコンパイル時に、複数のユーザー定義曲線について何も知らなくても、これらの曲線にアプローチする方法のデモに焦点を当てました。

于 2015-08-07T21:13:16.110 に答える