In Fortran, one can operate on arrays, but how can one treat the indices of a derived type as part of an array too? Code would explain what I want to do best:
type mytype
integer :: b(3,3)
real :: c(4)
endtype
integer :: a(3,3)
real :: d(2,4)
type(mytype) :: mat(2)
!do stuff so that 'mat' gets values
....
!usually one does this
a = matmul(mat(1)%b, transpose(mat(2)%b))
!multiplying two 3x3 matrices
!but how does one do this? Note the "array"
d = matmul(mat(:)%c, mat(:)%c)
I assumed that the final line is analogous to a 2x4 matrix being multiplied with itself. However, when I try to compile, gfortran complains
Error: Two or more part references with nonzero rank must not be specified
Is this possible to do in Fortran?
You want the compiler to regard
mat(:)%cas a 2 x 4 matrix? It doesn't work that way.matandcare different objects and their ranks don't merge into a single array.matis a user-defined type andcis a real matrix. Just because you are only using thec-component ofmatdoesn't mean the compiler will promotecto a higher dimensional real array, based on the dimension ofmat.You could create a new array via
X = [ mat(1)%c, mat(2)%c ]. You could usereshapeto control the shape.