Fortran 基本函数 - 这种行为正常吗? [英] Fortran elemental function - is this behavior normal?

查看:31
本文介绍了Fortran 基本函数 - 这种行为正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的函数

ELEMENTAL FUNCTION int2str(i) RESULT(s)
    INTEGER, INTENT(IN) :: i
    CHARACTER(LEN = 50) :: appo
    CHARACTER(LEN = :), ALLOCATABLE :: s
    WRITE(appo, '(I0)') i
    ALLOCATE(s, source = TRIM(appo))
END FUNCTION int2str

作为元素的函数是一个标量函数(在我的例子中它需要一个标量整数并返回一个标量字符,即使长度可以分配),它按元素应用于数组.

The function, being elemental, is a scalar function (in my case it takes one scalar integer and gives back one scalar character, even though allocatable in length) that applies elementwise to arrays.

为什么输出

print *, '>>>'//int2str([1, 3, 5, 7])//'<<<'

是(出乎我的意料)

>>>1<<<>>>3<<<>>>5<<<>>>7<<<

print *, '>>>', int2str([1, 3, 5, 7]), '<<<'

是(预期)

>>>1357<<<

?

我的意思是该函数应该应用于组成数组的四个标量整数中的每一个,从而返回一个长度为 4 的数组,其中每个元素都是一个字符串,但在我看来它的元素性适用到三个字符串的整个串联,就好像 // 运算符优先于函数的结果.

What I mean is that the function should apply to every one of the four scalar integers composing the array, thus returning a length-4 array each element of which being a string, but it seems to me that its elementwise-ness applies to the whole concatenation of three strings, as though the // operator has precedence over the function's result.

推荐答案

对于字符连接,表达式

'>>>'//['1','3','5','7']

依赖于标量 '>>>' 和数组 ['1','3','5','7'].与操作数是标量和秩为 1 的数组的其他内在二元运算一样,表达式也是秩为 1 的数组.

depends on the scalar '>>>' and the array ['1','3','5','7']. As with other intrinsic binary operations where the operands are a scalar and a rank-1 array, the expression is a rank-1 array.

在确定表达式的值时,标量操作数被当作数组处理

In determining the value of the expression, the scalar operand is treated as an array like

['>>>','>>>','>>>','>>>']

和表达式等价于

['>>>','>>>','>>>','>>>'] // ['1','3','5','7']

最后,表达式具有值,其中元素是成对的操作数:

Finally, the expression has value where the elements are operands pairwise:

['>>>1','>>>3','>>>5','>>>7']

你可以看到与表达式的相似之处

You can see the parallels with the expression

9+[1,3,5,7]  ! [9,9,9,9]+[1,3,5,7]  --> [10,12,14,16]

当有两个浓缩操作正在进行时,明显的价值就是结果.

When there are two concentation operations going on, the obvious value is the result.

请注意,我没有根据元素函数的结果来表达这一点.这部分是因为数组来自函数的事实并不重要.此外,您的基本函数实际上是不允许的:基本函数结果可能无法分配.

Note that I didn't express this in terms of an elemental function's result. This is partly because the fact the array comes from a function isn't significant. Also, your elemental function is not actually allowed: an elemental function result may not be allocatable.

关于无效函数,Vladimir F 提交了错误报告 覆盖 gfortran 未检测到违反 Fortran 2008 的编号约束 C1290.在该报告中,您可以看到,如果删除 result(s) 并声明 int2str 具有函数被拒绝的可分配属性.其他一些编译器确实已经检测到违规.

On the invalid function, Vladimir F has submitted a bug report covering gfortran not detecting violation of the numbered constraint C1290 of Fortran 2008. In that report you can see that if you remove the result(s) and declare int2str as having the allocatable attribute the function is rejected. Some other compilers do already detect violation.

这篇关于Fortran 基本函数 - 这种行为正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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