以文本方式查看主题

-  Fortran中文网  (http://fortran.cn/bbs/index.asp)
--  Fortran语言使用经验交流  (http://fortran.cn/bbs/list.asp?boardid=2)
----  [求助]IMSL库函数中运算符号问题  (http://fortran.cn/bbs/dispbbs.asp?boardid=2&id=432)

--  作者:linmue
--  发布时间:2006/10/22 21:29:15

--  [求助]IMSL库函数中运算符号问题
不管高手低手都帮忙看一下:

program matrixmultiply

use IMSL

implicit none

  integer,parameter :: fileid=10

  character(len=80) :: filename = "data.txt"

  real :: A(3,3)=(/1,2,3,2,1,2,3,2,1/)

  real :: B(3,3)=(/1,-1,0,-1,0,1,1,1,0/)

  real :: C(3,3),AT(3,3),BT(3,3)

  real :: D(3,3),DT(3,3)

  AT=.t.A.x.B

  BT=.t.B.x.A

  DT=AT-BT

  c=.t.A.x.B-.t.B.x.A

  D=.t.A.x.B-(.t.B.x.A)

  open(fileid,file=filename)

  write(fileid,"(\'矩阵DT:\',/,3(3XF7.3))")dt

  write(fileid,"(\'矩阵C:\',/,3(3XF7.3))")c

  write(fileid,"(\'矩阵D:\',/,3(3XF7.3))")d

  close(fileid)

  stop

end

我想得到A的转置乘以B减去B的转置乘以A的正确答案.

为什么只有按照分开的形式

AT=.t.A.x.B

BT=.t.B.x.A

DT=AT-BT    才正确呢?

C=.t.A.x.B-.t.B.x.A

D=.t.A.x.B-(.t.B.x.A)    却得不出正确答案呢?? 并且矩阵C和D得出的结果还不相同.

请各位解释一下.说说IMSL库函数中运算符号(如.x.,.ix.等)的使用规则


--  作者:linmue
--  发布时间:2006/10/23 12:58:10

--  

在编程爱好者论坛有人给我的回复是以下这样的:

嗯,使用者用这个Linear Algebra Operators的确是很容易出错的
其实帮助文件(IMSL5.0)第1466页在用图表介绍完这几个操作符后用深色的底强调了下面这段话:

A parsing rule in Fortran 90 states that the result of a defined operation involving two quantities has a lower precedence than any intrinsic operation. This explains the parentheses around the next-to-last line containing the sub-expression “A .x. y” found in the example. Users are advised to always include parentheses around array expressions that are mixed with defined operations, or whenever there is possible confusion without them.

大意是说虽然自定义了操作符的内容,但是操作符的优先级是由Fortran语法解析的规则决定的。涉及两个量的自定义操作的优先级总是低于内置操作的。所以编成者必须自行负责括号的添加。


也就是说,这样的操作等价:
  myx=.t.A.x.B - .t.B.x.A
  myy=.t.A.x.(B-.t.B).x.A


京ICP备05056801号