Fortran, complex array with rank one

Click For Summary

Discussion Overview

The discussion revolves around the use of the MATMUL command in Fortran for multiplying a rank one complex array with a 3x3 complex matrix. Participants explore issues related to array shape conformity and the correct definition of complex arrays in Fortran.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to use MATMUL with a rank one complex array and a 3x3 complex matrix, expressing confusion over a compilation error related to array shape conformity.
  • Another participant asserts that a rank one array cannot be multiplied by a rank two array, suggesting that for MATMUL to work, the rank of array A should be 3x1.
  • A participant references a book, NUMERICAL RECIPES IN FORTRAN 90, which states that one-dimensional arrays can be used as either row or column vectors, but not both in the multiplication.
  • There is a suggestion that the issue may depend on the compiler being used, with a query about whether a 3x1 matrix was tried.
  • A participant mentions that while they successfully used a 3x1 matrix, they are unsure how to calculate the dot product of array A in that format.
  • Another participant identifies a specific issue in their code related to the definition of the variable "c" and seeks suggestions for proper definition.
  • A later reply indicates that one participant believes they have solved the problem, but no details are provided.

Areas of Agreement / Disagreement

Participants express differing views on the correct approach to defining and using arrays in Fortran, particularly regarding the rank of the arrays involved in the MATMUL operation. The discussion remains unresolved as participants have not reached a consensus on the best solution.

Contextual Notes

There are limitations regarding the assumptions made about array ranks and the specific requirements of the MATMUL command in Fortran, which may vary by compiler. The discussion includes unresolved mathematical steps related to defining the output variable correctly.

_Andreas
Messages
141
Reaction score
1
I'm using the MATMUL command to multiplicate two arrays: array A is of rank one and has three complex elements, while array B is a 3x3 matrix with complex elements. However, the compilation is aborted because "the shapes of the array expressions do not conform". I'm pretty sure that the operation MATMUL(B,A) should yield a 3x1 array with complex elements, so I suspect that the expression defining array A isn't correct. Right now it looks like this:

A=[0.e0+0.e0, 0.e0+0.e0, 1.e-29+0.e0]

(I'm only interested in the real part of the last element for the moment.) Is this really the way to express a complex, rank one array?
 
Technology news on Phys.org
_Andreas said:
I'm using the MATMUL command to multiplicate two arrays: array A is of rank one and has three complex elements, while array B is a 3x3 matrix with complex elements. However, the compilation is aborted because "the shapes of the array expressions do not conform". I'm pretty sure that the operation MATMUL(B,A) should yield a 3x1 array with complex elements, so I suspect that the expression defining array A isn't correct. Right now it looks like this:

A=[0.e0+0.e0, 0.e0+0.e0, 1.e-29+0.e0]

(I'm only interested in the real part of the last element for the moment.) Is this really the way to express a complex, rank one array?

You can't multiply an array of rank 1 by an array of rank 2.

MATMAL(B,A) will work if B is rank 3x3 and A is rank 3x1, and the result will be rank 3x1
 
sylas said:
You can't multiply an array of rank 1 by an array of rank 2.

MATMAL(B,A) will work if B is rank 3x3 and A is rank 3x1, and the result will be rank 3x1

That's strange, because this is what it says in the book NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing
:

[Num] matmul(mata,matb)
Result of matrix-multiplying the two two-dimensional matrices mata
and matb. The shapes have to be such as to allow matrix multiplication.
Vectors (one-dimensional arrays) are additionally allowed as either the
first or second argument, but not both; they are treated as row vectors
in the first argument, and as column vectors in the second.
You might wonder how to form the outer product of two vectors, since

I guess you're right since it doesn't work, but perhaps I'm misinterpreting the quoted text?
 
It may depend on what compiler you are using. I'm not sure. Did you try using the 3x1 matrix?
 
sylas said:
It may depend on what compiler you are using. I'm not sure. Did you try using the 3x1 matrix?

Yes, I tried that before trying this method. It worked fine, but the problem is that in the same program, I want to be able to calculate the dot product of array A. I don't know how to do this if I define it as a 3X1 matrix.
 
Ok, now I know exactly what is wrong, but not how to fix it. The relevant part of the code looks like this:

c=matmul(a,b)

The part that is wrong is the "c", because the command

write(*,*) matmul(a,b)

gives the correct output. I obviously have to define "c" properly, but

real, complex(3,1) :: c

doesn't do. Any suggestions?
 
I think I solved the problem. Thanks anyway, sylas.
 
_Andreas said:
I think I solved the problem. Thanks anyway, sylas.

You're welcome. Please post the solution. I'm rusty on Fortran, and would like to know!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
7K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
17K
  • · Replies 3 ·
Replies
3
Views
8K
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K