Fortran笔记,重载&自定义操作符&继承Module

参考:彭国伦:《Fortran 95程序设计》,2002年,第11章

目录

1. 重载(overload)

2. 自定义操作符

3. 继承Module


1. 重载(overload)

概念:函数名称相同,但参数类型、数目不同的函数。程序会自动根据输入的参数,来决定要调用哪一个函数。

实现:使用interface来定义一个虚拟函数(即所谓相同的函数名),并给出虚拟函数的实现方式(即参数类型、数目不同的函数具体实现)。

示例:

module mod_show
    implicit none
    
    ! 函数的重载
    interface show
        module procedure show_int
        module procedure show_character
    end interface
    
contains

subroutine show_int(n)
    implicit none
    integer :: n
    write(*,*) n
end subroutine show_int
    
subroutine show_character(string)
implicit none
    character(len=*) :: string
    write(*,*) TRIM(string)
end subroutine show_character
    
end module

program case_overload
use mod_show
implicit none
    call show(333)
    call show("666")
stop
end program

结合示例分析,虚拟函数即show。其有两个版本,一个是输入参数类型为integer,另一个为characater(len=*)。其实现时所使用的函数名并无要求,只要能对应interface中的即可。

注意,Fortran并没有将重载函数的具体实现封装起来。也就是说不允许通过private来限制具体实现的函数。在use这个module的地方,也可直接调用具体实现的函数(此处即为show_int(), show_character())。


2. 自定义操作符

自定义操作符的实现与函数的重载类似,需通过interface来完成。这里自定义操作符的应用场景多正对自定义类型(TYPE)。

示例:

module tim
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中国科学院大气所李建平的 线性相关 Linear Correlation mscorrelation_2(n,x,y,r)   Correlations between two anomaly series of 12 calendar months, 4 seasons (DJF, MAM, JJA and SON), monthly, seasonal and annual data from monthly anomaly series x(i,12) and y(i,12) (i=1,...,n) 求逐月异常序列x(n,12)和y(n,12)(n是年)的相关系数r(24),其中j=1~12是1~12个月的情形,13~22是冬、春、夏、秋、冬季逐月、春季逐月、夏季逐月、秋季逐月、逐月、年、冬半年逐月(NDJFMA)、夏半年逐月(MJJASO)的序列的情形。 llcorrelation(n,x,y,nt,rt)   Calculating lagged and leading correlation coefficients rt(-nt:nt) between two anomaly series x(i) and y(i). 求(逐日、逐月、逐季、年际)异常序列x(n)和y(n)之间的滞后超前的相关系数rt(-nt:nt),其中nt最大的滞后或超前时间(单位:日、月、季、年等)。 llyear(ny,nm,x,y,ly,rt)   ly-year lagged and leading correlation coefficients rt(-ly:ly,nm) between two anomaly series x(ny,nm) and y(ny,nm). 求逐月异常序列x(n,12)和y(n,12)(n是年)相同月份之间的滞后超前nt年的相关系数rt(-nt:nt,12),其中nt最大的滞后或超前时间(单位:年)。 mllcorrelation(n,nt,x,y,rt)   nt-month (or nt-season, or others) lagged and leading correlation coefficients rt(-nt:nt,nm) between two anomaly series x(ny,nm) and y(ny,nm). 求逐月、逐季或其他异常序列x(n,nm)和y(n,nm)(n是年)不同月份之间的滞后超前nt月的相关系数rt(-nt:nt,nm),其中nt最大的滞后或超前时间(单位:月、季或其他)。 correlationmiss(n,x,y,undef,r,nr)   Correlation coefficient r between two series with missing data 求有缺省资料的两个序列x(n)和y(n)的相关系数r。   谱分析 Spectrum Analysis   一维离散功率谱分析 Discrete Fourier spectrum of one-dimensional series fourier(n,x,a,b,c,s,cta)   The discrete Fourier spectrum of one-dimensional series x(n). 求一维序列x(n)的离散Fourier谱分析,s(0:m)离散功率谱,c(0:m)振幅谱,cta(0:m)位相谱,其中m=[n/2.]。 dspectrum(n,lw,x,tl,sl,st95)   Subroutine for discrete spectrum analysis of an one-dimensional series x(i) (i=1,...,n). 具有噪音检验的一维序列x(n)的离散功率谱分析,ol(lw)频率,tl(lw)周期,sl(lw)离散功率谱,st95(lw)红噪音或白噪音谱的95%置信上限,其中lw=[n/2.]。注意:很多同学在使用这个程序时都问及在计算滞后相关的模块中,求滞后相关为什么用n而不用n-i,其实这是由实际资料的特性和统计学的基本原理决定的(很多统计学书中也谈到这一点)。一是序列的平稳性假设就要求序列的均值和方差保持不变,二是通常资料太短,导致用较短的资料得到的相关结果具有不稳定性,掩盖了事物的真相,因此,用n要好于n-i。一些同学也做了试验证实了这一点(有时n-i还得到错误的结论,需要各自使用者注意的地方)。谢谢大家提出的问题。 一维连续功率谱分析 Continuous spectrum analysis of an one-dimensional series cspectrum(n,m,x,ol,tl,sl,st95,strw)   Subroutine for continuous spectrum analysis of an one-dimensional series x(i) (i=1,...,n). 一维序列x(n)的连续功率谱分析,ol(0:m)频率,tl(0:m)周期,sl(0:m)连续功率谱,st95(0:m)红噪音或白噪音谱的95%置信上限,strw(0:m) 红噪音或白噪音的谱密度,其中m=[n/2.]。 交叉谱分析 Continuous cross spectrum analysis of two one-dimensional series ccrossspectrum(n,m,x,y,ol,tl,px,py,px95,py95,rxy,cxy,lxy,rxy951,rxy952)   Subroutine for continuous cross spectrum analysis of two one-dimensional series x(i) and y(i) (i=1,...,n). 两序列x(n)和y(n)的交叉谱分析,ol(0:m)频率,tl(0:m)周期,px(0:m)是x(n)的连续功率谱,py(0:m)是y(n)的连续功率谱,pxy(0:m)协谱,qxy(0:m)余谱,rxy(0:m)凝聚谱,cxy(0:m)位相差谱,lxy(0:m)滞后时间长度谱,rxy951(0:m)凝聚谱F-检验的95%置信上限,rxy952(0:m)凝聚谱Goodman-检验的95%置信上限,其中m=[n/2.]。   合成分析 Composite Analysis   标量的合成分析 Composite analysis for scalar quantity differencehl1(n,x,f,coefh,coefl,fh,fl,dh,dl,dhl,tn)   求f(n)在指数x(n)为高指数年(x(n)>coefh的年)的平均值fh、低指数年(x(n)coefh的年)的平均值fh(2)、低指数年(x(n)<coefh的年)的平均值fl(2)、高指数年与气候平均的合成差dh(2)、低指数年与气候平均的合成差dl(2)、以及高低指数年的合成差dhl(2)和差的显著性tn(5,3)。 differhl2V(n,x,f,nc,fh,fl,dh,dl,dhl,tn)   求矢量f(n,2)在指数x(n)为nc个最强的指数年的平均值fh(2)、nc个最弱的指数年的平均值fl(2)、nc个最强的指数年与气候平均的合成差dh(2)、nc个最弱的指数年与气候平均的合成差dl(2)、以及强弱指数年的合成差dhl(2)和差的显著性tn(5,3)。   主分量分析 Principal Component Analysis (PCA)   经验正交函数分解 Empirical Orthogonal Functions (EOF's) eof(m,n,mnl,f,ks,er,egvt,ecof)   求时空场f(m,n)的特征向量egvt(m,mnl),时间系数ecof(mnl,n),特征值er(mnl,1),累积特征值er(mnl,2),解释方差er(mnl,3),累积解释方差er(mnl,4) 旋转经验正交函数分解 Rotated Empirical Orthogonal Functions (REOF's) reof(m,n,mnl,np,f,ks,er,egvt,ecof,rer,regvt,recof)   求时空场f(m,n)的特征向量egvt(m,mnl),时间系数ecof(mnl,n),旋转特征向量regvt(m,mnl),时间系数recof(mnl,n)   插值 Interpolation   样条内插 Spline Interpolation splinev(n,x,y,m,t,yp1,ypn,sy)   已知节点x(n)和函数值y(n),用三次样条求节点t(m)上的内插值sy(m)。   滤波分析 Filter   二阶Butterworth带通滤波器 Second Order Butterworth Band-Pass Filter Bfilter2(n,x,y,a,b1,b2)   求序列x(n)(n是资料长度)的二阶Butterworth带通滤波序列y(n) 高斯低通滤波器 M-term Guassian-Type Filter guassfilter_2(n,m,x,y)   求序列x(n)(n是资料长度)的m项高斯低通滤波序列y(n)   插值 Interpolation   样条内插 Spline Interpolation splinev(n,x,y,m,t,yp1,ypn,sy)   已知节点x(n)和函数值y(n),用三次样条求节点t(m)上的内插值sy(m)。   微分方程数值积分 Numerical Integration of Differential Equations   一到六阶定步长显式Runge-Kutta方法 Fixed Stepsize Explicit Runge-Kutta Method of Orders from 1 to 6 Runge-Kutta.f   subroutine eu1(n,yn,h) : the Euler's method subroutine rk2(n,yn,h) : the improved Euler's method subroutine rk3(n,yn,h) : a Runge-Kutta method of order 3 subroutine rk4(n,yn,h) : a Runge-Kutta method of order 4 subroutine rk5(n,yn,h) : a Runge-Kutta method of order 5 subroutine rk6(n,yn,h) : a Runge-Kutta method of order 6 subroutine rkm2(n,yn,h): another Runge-Kutta method of order 2 subroutine rkh3(n,yn,h): another Runge-Kutta method of order 3      

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值