泛型和特定函数可获取复杂变量的实部和虚部 [英] Generic and specific functions to get real and imaginary parts of complex variables

查看:79
本文介绍了泛型和特定函数可获取复杂变量的实部和虚部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran中,我始终以双精度工作,因此我一直在使用诸如dbledimag之类的特定函数来获取复杂变量的实部和虚部.但是,对于sin之类的其他函数,我不再使用dsin,因为前者返回正确类型的值(即sin是泛型函数).复杂变量似乎也是如此.所以我的问题是:

In Fortran, I always work with double precision, so I have been using specific functions like dble and dimag to get real and imaginary parts of complex variables. However, for other functions like sin, I no longer use dsin because the former returns a value of proper kind (i.e., sin is a generic function). The same seems to hold for complex variables. So my question is:

1)获得实部和虚部的最推荐通用函数是什么?

1) What are the most recommended generic functions for getting real and imaginary parts?

-似乎real(z)aimag(z)conjg(z)总是返回适当的类型(通过gfortran的实验),即,如果z是双精度的,则这些函数将返回双精度.这样可以保证吗?另外,行为是否取决于编译器使用的标准? (即Fortran 77与90或更高版本,尤其是对于real(z)?)

-- It seems that real(z), aimag(z), and conjg(z) return a proper kind always (via experiments with gfortran), i.e., if z is double precision, those functions return double precision. Is this guaranteed? Also, is the behavior dependent on the standard used by the compiler? (i.e., Fortran 77 vs 90 or later, particularly for real(z)?)

2)如果我(尽管如此)想要使用仅接收双精度参数并且总是返回双精度值的特定函数,那么这些特定函数是什么?

2) If I (nevertheless) want to use specific functions that receives only double precision arguments and always return double precision values, what are the specific functions?

-到目前为止,我一直在使用dble(z)dreal(z)dimag(z)dconjg(z),但是一些网页说它们是供应商扩展(尽管许多编译器通常都支持).

-- I have been using dble(z) and dreal(z), dimag(z), dconjg(z) up to now, but some web pages say that they are vendor extensions (though commonly supported by many compilers).

我已经阅读了不同的页面,但是信息相当混乱(即,尚不清楚标准"方式是什么),因此,我希望您能对选择此类功能提出任何建议.

I have read various pages but the information is rather confusing (i.e., it is not very clear what is the "standard" way), so I would appreciate any advice on the choice of such functions.

推荐答案

作为背景,我们所说的各种实变量和复变量是什么意思?当然,您知道真实对象的种类是什么意思.

As background, what do we mean by kinds of real and complex variables? Of course, you know what is meant by the kind of a real object.

一个复杂的对象由实部和虚部组成.如果复杂对象具有给定的种类,则每个组件都是对应于复杂对象种类的实数.

A complex object consists of a real and an imaginary part. If a complex object has a given kind then each component is a real of kind corresponding to the kind of the complex object.

如果说

complex(kind=k) z

然后KIND(z%re)KIND(z%im)都计算为k(为清楚起见,使用了Fortran 2008引入的复杂零件代号).

then KIND(z%re) and KIND(z%im) both evaluate to k (using the complex part designators introduced by Fortran 2008 for clarity).

现在,real内在泛型采用一个复杂的表达式并返回其实部.它遵循以下F2008规则(13.7.138),其中A是自变量:

Now, the real intrinsic generic takes a complex expression and returns its real component. It does so subject to the following F2008 rule (13.7.138), where A is the argument:

如果A的类型为complex而KIND不存在,则kind类型参数为A的kind类型参数.

If A is of type complex and KIND is not present, the kind type parameter is the kind type parameter of A.

所以,是的:在当前的Fortran real中,如果没有所请求的种类,将始终为您提供复杂的真实组件的真实种类.无论是双精度还是其他精度.

So, yes: in current Fortran real without a requested kind will always give you a real of kind that of the complex's real component. Whether that's double precision or otherwise.

同样,aimag返回的实数(对应于虚部)是复数类型的实数.与real不同,aimag不接受控制结果类型的kind=参数.

Similarly, aimag returns a real (corresponding to the imaginary part) of kind that of the complex number. Unlike real, aimag doesn't accept a kind= argument controlling the result kind.

Fortran 77的情况有所不同:没有类似的实物概念,只有一个complex.

Things are different for Fortran 77: there was no similar concept of kind, and just one complex.

dble是标准内在函数.尽管这总是返回双精度,但它仍然是通用的,可以接受任何数字.不论a的类型如何,dble(a)real(a,kind(0d0))相同.没有(标准)特定内容.

dble is a standard intrinsic. Although this always returns a double precision it is still generic and will accept any numeric. dble(a) is the same as real(a,kind(0d0)), whatever the type of a. There is no (standard) specific.

drealdimagdconjg不是标准内在函数.

dreal, dimag and dconjg are not standard intrinsics.

我想如果一个人很在意的话,可以在real周围创建特定的包装器.

I suppose one could create specific wrappers around real if one cared greatly.

这篇关于泛型和特定函数可获取复杂变量的实部和虚部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆