0

我正在尝试编写一个程序来计算两个向量的叉积(输入是“真实”类型,例如 [1.3 3.4 1,5])。但我不断收到许多错误:

    program Q3CW
    implicit none
   REAL :: matA(3), matB(3)
   REAL :: A11, A12, A13
   REAL :: B11, B12, B13
   real :: productc(3), answer(3)
   read*,A11, A12, A13
   read*,B11, B12, B13

   matA = (/A11, A12, A13/)
   matB = (/B11, B12, B13/)
   answer = productc(matA, matB)

   print*,'Answer = ', answer(1), answer(2), answer(3)
   end program

   real function productc(matIn1, matIn2)
   real, dimension(3) :: matIn1, matIn2

   productc(1)=(/matIn1(2)*matIn2(3)-matIn1(3)*matIn2(2)/)
   productc(2)=(/matIn1(3)*matIn2(1)-matIn1(1)*matIn2(3)/)
   productc(3)=(/matIn1(1)*matIn2(2)-matIn1(2)*matIn2(1)/)
   end function

这是我得到的错误:

   Error: Q33333.f95(20) : Statement function definition for pre-existing procedure PRODUCTC; detected at )@=
   Error: Q33333.f95(21) : Statement function definition for pre-existing procedure PRODUCTC; detected at )@=
   Error: Q33333.f95(22) : Statement function definition for pre-existing procedure PRODUCTC; detected at )@=
   Warning: Q33333.f95(23) : Function PRODUCTC has not been assigned a value; detected at FUNCTION@<end-of-statement>
   Build Result Error(3) Warning(1) Extension(0)

知道问题可能是什么吗?

4

2 回答 2

0

最好将您的子例程和过程放入一个模块中,然后使用该模块,以便调用者知道该接口。

您的程序似乎没有输入。

符号 (/ /) 用于初始化数组,而不是用于计算。

这是一个非常相似的问题:Computing the cross product of two vectors in Fortran 90

于 2012-11-20T16:57:01.003 回答
0

您的代码中有几个错误。例如,您的函数的结果是一个 vetcor。因此,您必须指定(您只有一个标量,实数)。

您的功能现在包含在程序中。

这是工作代码:

program Q3CW
  implicit none
  real :: matA(3), matB(3)
  real :: A11, A12, A13
  real :: B11, B12, B13
  real :: answer(3)

  read*,A11, A12, A13
  read*,B11, B12, B13

  matA = (/A11, A12, A13/)
  matB = (/B11, B12, B13/)
  answer = productc(matA, matB)

  print*,'Answer = ', answer(1), answer(2), answer(3)

contains
  function productc(matIn1, matIn2) result(out)
    real, dimension(3) :: matIn1, matIn2, out

    out(1) = matIn1(2)*matIn2(3) - matIn1(3)*matIn2(2)
    out(2) = matIn1(3)*matIn2(1) - matIn1(1)*matIn2(3)
    out(3) = matIn1(1)*matIn2(2) - matIn1(2)*matIn2(1)
  end function
end program Q3CW
于 2013-07-31T15:27:19.697 回答