在 Fortran 90 中计算两个向量的叉积 [英] Computing the cross product of two vectors in Fortran 90

查看:54
本文介绍了在 Fortran 90 中计算两个向量的叉积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Fortran 90 中计算两个向量的叉积.例如,在单词中,(1, 2, 3) 和 (4, 5, 6) 的叉积结果是 (-3, 6, -3) 在笛卡尔坐标系中.我写了如下代码(主程序后跟函数定义):

I would like to compute the cross product of two vectors in Fortran 90. For example, in words, the cross product of (1, 2, 3) and (4, 5, 6) turns out to be (-3, 6, -3) in Cartesian coordinates. I wrote the following code (main program followed by function definition):

PROGRAM crosstest
  IMPLICIT NONE

  INTEGER, DIMENSION(3) :: m, n
  INTEGER, DIMENSION(3) :: cross
  INTEGER, DIMENSION(3) :: r

  m=(/1, 2, 3/)
  n=(/4, 5, 6/)
  r=cross(m,n)

END PROGRAM crosstest

FUNCTION cross(a, b)
  INTEGER, DIMENSION(3) :: cross
  INTEGER, DIMENSION(3), INTENT(IN) :: a, b

  cross(1) = a(2) * b(3) - a(3) * b(2)
  cross(2) = a(3) * b(1) - a(1) * b(3)
  cross(3) = a(1) * b(2) - a(2) * b(1)
END FUNCTION cross

但是,我收到一条错误消息:

But, I get an error message:

crosstest.f90:10.9:

  r=cross(m,n)
         1
Error: Rank mismatch in array reference at (1) (2/1)

其中第 10 行是 r=cross(m,n).看来我一定是错误地指定了一个维度.以下是我的一些想法:

where line 10 is r=cross(m,n). It seems that I must be specifying a dimension incorrectly. Here are a few ideas I have:

  1. 也许主程序中cross函数的声明应该只是一个整型变量,而不是一个1×3的整型数组.因此,我尝试删除主程序中 INTEGER, DIMENSION(3) :: cross 行中的 , DIMENSION(3).但我收到一条错误消息:

  1. Perhaps the declaration of the function cross in the main program should be simply an integer variable, rather than a 1by3 integer array. So I tried deleting the , DIMENSION(3) in the INTEGER, DIMENSION(3) :: cross line in the main program. But I get an error message:

crosstest.f90:10.4:

  r=cross(m,n)
    1
Error: The reference to function 'cross' at (1) either needs an
explicit INTERFACE or the rank is incorrect

所以这可能更糟.

网络上的一些(但不是全部)Fortran 函数示例在主程序中的函数声明之后放置了一个EXTERNAL 语句.所以我尝试在主程序的声明块之后放置一行 EXTERNAL cross.我收到一条错误消息:

Some (but not all) Fortran function examples on the web place an EXTERNAL statement after the function declaration in the main program. So I tried placing a line EXTERNAL cross after the declaration block in the main program. I get an error message:

crosstest.f90:8.16:

  EXTERNAL cross
                1
Error: EXTERNAL attribute conflicts with DIMENSION attribute at (1)

所以这似乎也不正确.

网络上的一些(但不是全部)Fortran 函数示例在函数定义的倒数第二行放置了一个 RETURN 语句.我试过了,但我得到了原始排名不匹配错误:

Some (but not all) Fortran function examples on the web place a RETURN statement on the second-to-last line of the function definition. I tried this, but I get the original rank mismatch error:

crosstest.f90:10.9:

  r=cross(m,n)
         1
Error: Rank mismatch in array reference at (1) (2/1)

所以这不能解决问题.

你能帮我看看我的错误吗?

Can you please help me see my error?

推荐答案

最佳实践是将过程(子例程和函数)放在一个模块中,然后从主程序或其他过程使用"该模块.您不需要使用"来自同一模块的其他过程的模块.这将使过程的接口显式,以便调用程序或过程知道"参数的特征......它允许编译器检查双方参数之间的一致性......调用者和被调用者......这个消除了很多错误.

The best practice is to place your procedures (subroutines and functions) in a module and then "use" that module from your main program or other procedures. You don't need to "use" the module from other procedures of the same module. This will make the interface of the procedure explicit so that the calling program or procedure "knows" the characteristics of the arguments ... it allows the compiler to check for consistency between the arguments on both sides ... caller and callee .. this eliminates a lot of bugs.

在语言标准之外,但在实践中是必要的:如果您使用一个文件,请将模块放在使用它的主程序之前.否则编译器将不知道它.所以:

Outside of the language standard, but in practice necessary: if you use one file, place the module before the main program that uses it. Otherwise the compiler will be unaware of it. so:

module my_subs

implicit none

contains

FUNCTION cross(a, b)
  INTEGER, DIMENSION(3) :: cross
  INTEGER, DIMENSION(3), INTENT(IN) :: a, b

  cross(1) = a(2) * b(3) - a(3) * b(2)
  cross(2) = a(3) * b(1) - a(1) * b(3)
  cross(3) = a(1) * b(2) - a(2) * b(1)
END FUNCTION cross

end module my_subs


PROGRAM crosstest
  use my_subs
  IMPLICIT NONE

  INTEGER, DIMENSION(3) :: m, n
  INTEGER, DIMENSION(3) :: r

  m= [ 1, 2, 3 ]
  n= [ 4, 5, 6 ]
  r=cross(m,n)
  write (*, *) r

END PROGRAM crosstest

这篇关于在 Fortran 90 中计算两个向量的叉积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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