Question

I'm trying to write a program to calculate a cross product of two vectors (input is of "real" type, so for example [1.3 3.4 1,5]). But I keep getting numerous errors:

    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

This is the error I get:

   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)

Any idea what the problem could be ?

Was it helpful?

Solution

It best to put your subroutines and procedures into a module and then use that module so that the interface is known to the caller.

There seems to be no input to your program.

The notation (/ /) is used to initialize an array, not for a calculation.

Here is a very similar question: Computing the cross product of two vectors in Fortran 90

OTHER TIPS

There were several errors in your code. For example the result of your function is a vetcor. Thus you have to specify that (you just had a scalar, real).

And your function is now conained in the program.

Here is the working code:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top