Samuel Tan
Samuel Tan

Reputation: 1750

Multiply part of an array as a matrix using matmul

My question is similar to this one Multiply a 3D matrix with a 2D matrix. However, I'm coding in Fortran.

Say, if I have a RxSxT matrix A and an SxU matrix B, where R,S,T,U are integers, and I want to multiply A(:,:,0) with B. How can I do this with matmul? When I do something like

    C(:,:,0) = matmul(A(:,:,0),B)

The compiler (gfortran) gives:

    Warning:Array reference at (1) is out of bounds (0 < 1) in dimension 3 
    f951: internal compiler error: Segmentation fault

Is there a way around this? Thanks.

EDIT: I should add that I'm actually transposing the second matrix. Say, A a RxSxT matrix and B a UxS matrix. Then

 C(:,:,0) = matmul(B,transpose(A(:,:,0))

That transpose might be part of the problem. Does it convert A(i,j,k) to A(k,i,j)?

Upvotes: 2

Views: 1593

Answers (2)

janneb
janneb

Reputation: 37228

Remember that in Fortran your array indices start from 1 by default. So unless you have specified your array A to have a non-default lower bound on the 3rd dimension, gfortran is entirely correct in pointing out your error.

Of course, an internal compiler error is always a compiler bug; unless you have some ancient version of gfortran please file a bug at http://gcc.gnu.org/bugzilla

Upvotes: 9

M. S. B.
M. S. B.

Reputation: 29401

transpose (A(:,:,0)) should interchange the indices A(i,j,0) to A(j,i,0). A(:,:,0) is a rank two matrix.

The compiler should never crash, whether or not the input source code is correct. Are you using the latest version of gfortran? You could report this "internal compiler error: Segmentation fault" to the gfortran development team: http://gcc.gnu.org/wiki/GFortran#bugs

Upvotes: 2

Related Questions