fortran复杂功能传递数组

时间:2012-11-20 15:57:36

标签: function integer fortran product real-datatype

我正在尝试编写一个程序来计算两个向量的叉积(输入是“实数”类型,例如[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)

知道问题可能是什么?

2 个答案:

答案 0 :(得分:0)

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

您的计划似乎没有输入。

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

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

答案 1 :(得分: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