Fortran Coder

查看: 26062|回复: 28
打印 上一主题 下一主题

[讨论] 一个数组运算的问题

[复制链接]

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

楼主
发表于 2017-8-6 17:22:43 | 显示全部楼层
ia_index的第1行数据为啥非要设置重复元素?

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

沙发
发表于 2017-8-6 21:28:33 | 显示全部楼层
andy8496 发表于 2017-8-6 18:30
这个是实际应用的需要。

症结即在于此呀,没有看出来?

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

板凳
发表于 2017-8-7 11:04:45 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 11:17 编辑
andy8496 发表于 2017-8-6 21:40
真看不出来。我试验用的那段代码,咋就能按照我的理解运行呢?索引数组也是有重复元素的:
program main
  ...

左端赋值包含重复元素的切片这样的写法本身就是不正确的,起码IBM XL和Intel Fortran的用户手册都明确强调这点
传送门1: http://w3.pppl.gov/~hammett/comp/f90tut/f90.tut5.html
Note that a vector subscript with duplicate values cannot appear on the left-hand side of an assignment as it would be ambiguous. Thus,
       b( (/ 1, 7, 3, 7 /) ) = (/ 1, 2, 3, 4 /)
is illegal.
传送门2: https://www.ibm.com/support/know ... ectorsubscript.html
An array section with a vector subscript in which two or more elements of the vector subscript have the same value is called a many-one section. Such a section must not:
Appear on the left side of the equal sign in an assignment statement传送门3: https://software.intel.com/en-us/node/678553
An array section with a vector subscript that has two or more elements with the same value is called a many-one array section. For example:
  REAL A(3, 3), B(4)  INTEGER K(4)  ! Vector K has repeated values  K = (/3, 1, 1, 2/)  ! Sets all elements of A to 5.0  A = 5.0  B = A(3, K)
The array section A(3,K) consists of the elements:
  A(3, 3)  A(3, 1)  A(3, 1)  A(3, 2)
A many-one section must not appear on the left of the equal sign in an assignment statement, or as an input item in a READ statement.


490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

地板
发表于 2017-8-7 13:52:26 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 14:01 编辑

再补充一些内容
Fortran 2003 handbook的第6章,第184页如是说:
A many-one section must not appear on the left of the equal sign in an assignment statement or as an input item in a READ statement. The reason is that the result will depend on the order of evaluation of the subscripts, which is not specified by the language. The results would not be predictable and the program containing such a statement would not be portable.

也即这个属于Fortran规范的盲点,lz有心的话,不妨邮件咨询RAL的J K Reid爵士或者I Duff
btw,无需见多识广,反倒是科学上网很重要

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

5#
发表于 2017-8-7 15:34:31 | 显示全部楼层
本帖最后由 pasuka 于 2017-8-7 15:37 编辑
andy8496 发表于 2017-8-7 14:52
多谢各位了!
现在补救的办法就是依赖编译器检查了,不知道可行否?在哪里设置?

直接改回方法1不行吗?
在ia index数组包含重复元素且不能剔除的前提下,IBM XL和Intel Fortran编译器手册均明确对方法2亮红牌,这下总得遵守规则被罚下场吧?
仍然不服气的话,建议研读g95或gfortran前端代码,自行实现需要的功能。g95的代码值得看看,既能加深Fortran的了解,也能助益C语言进阶
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-5 12:00

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表