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 のコンパイル時に、複数のユーザー定義曲線について何も知らなくても、これらの曲線にアプローチする方法のデモに焦点を当てました。