Oop 为不同的(曲线生成)类编写一个可移植的(根查找器)解算器类
我非常渴望实现FORTRAN 2003(F2003)的面向对象编程(OOP)功能。我的问题更多的是关于程序的设计。假设我有一个解算器,就像一个函数的根查找器f(x)=0;在最简单的FORTRAN格式中,它将得到Oop 为不同的(曲线生成)类编写一个可移植的(根查找器)解算器类,oop,fortran,portable-class-library,fortran2003,Oop,Fortran,Portable Class Library,Fortran2003,我非常渴望实现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 在以前版本的FORTRA
function solver(f,a,b) result(root)
[some definition of variables]
[some iterative procedures]
root = ...
end function
在以前版本的FORTRAN(如f95)中,为了获得可移植性,代码单独编译,并将外部函数传递给解算器。
现在从F2003面向对象的角度来看,假设在一般情况下,我们有一个用于求解器的类
我们的曲线还有一节课
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
而且会有更多不同的曲线类(类型)。现在,我如何以编译解算器类(不知道曲线类/类型)的方式连接这两个概念,并且无论何时编写新的不同曲线类(如二阶多项式曲线、三阶多项式曲线、对数曲线、经验曲线等),我都能够实现它而无需更改。我的意思是,在最后,我以某种方式得到了曲线的根。这里是一个如何使用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
这将产生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\u t
派生,并且符合f
的接口,所以我们可以轻松地将这些类与solver\u 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类可以做得更好,但我重点演示了如何在编译solve_t时处理多条用户定义的曲线,而不必了解它们。现代Fortran in Practice(Markus)一书中有这样的例子。我将阅读这本书,谢谢@Casey。
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