fortran在函数中传递数组的复杂性



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

你知道可能是什么问题吗?

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

您的程序似乎没有任何输入。

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

这里有一个非常相似的问题:在Fortran 90 中计算两个向量的叉积

您的代码中有几个错误。例如,函数的结果是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

相关内容

最新更新