Fortran, complex array with rank one

In summary, the problem is that you are missing a "c" in the command line. You need to enter "matmul(a,b)", not "c".
  • #1
_Andreas
144
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
  • #2
_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
 
  • #3
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?
 
  • #4
It may depend on what compiler you are using. I'm not sure. Did you try using the 3x1 matrix?
 
  • #5
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.
 
  • #6
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?
 
  • #7
I think I solved the problem. Thanks anyway, sylas.
 
  • #8
_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!
 

1. What is Fortran?

Fortran is a high-level programming language commonly used for scientific and numerical computing. It was developed in the 1950s and has evolved over time to include advanced features for efficient and reliable computing.

2. What is a complex array?

A complex array is a data structure in Fortran that can hold both real and imaginary numbers. It is represented as a collection of elements arranged in one or more dimensions.

3. What does "rank one" refer to in a complex array?

"Rank one" refers to the number of dimensions in a complex array. A rank one complex array has only one dimension, while a rank two array has two dimensions, and so on.

4. How do you declare a complex array with rank one in Fortran?

To declare a complex array with rank one in Fortran, you would use the "complex" keyword followed by the desired name and size of the array. For example, "complex :: my_array(10)" would declare a complex array called "my_array" with 10 elements.

5. What are some common operations performed on a complex array with rank one in Fortran?

Common operations on a complex array with rank one in Fortran include initializing the array, accessing and modifying individual elements, performing mathematical operations on the array, and passing the array as an argument to a subroutine or function.

Similar threads

  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
15K
  • Advanced Physics Homework Help
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
7K
Back
Top