Pass derived type as array

后端 未结 2 504
有刺的猬
有刺的猬 2021-01-22 18:03

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:

ty         


        
2条回答
  •  执笔经年
    2021-01-22 18:18

    You can't multiply nonsquare matrices by themselves. You have to transpose one of them.

    You also mix reals and integers. Your matrices are supposed to be real and your result is integer.

    It's possible to reference the matrix with a small FORTRAN STYLE hack (equivalence and sequence, assuming same storage size for default integer and real). This one compiles :))))

    type mytype
        !!!
        sequence
        integer :: b(3,3)
        real :: c(4)
    endtype
    
    integer :: a(3,3)
    real :: d(4,4)
    
    type(mytype) :: mat(2)
    real,dimension(13,2) :: newmat
    
    !!!
    equivalence (mat,newmat)
    
    !do stuff so that 'mat' gets values
    ! ....
    
    !usually one does this
    a = matmul(mat(1)%b, mat(2)%b)
    
    !multiplying two 3x3 matrices
    
    !but how does one do this? Note the "array"
     d = matmul(reshape(newmat(10:13,:),(/4,2/)),transpose(reshape(newmat(10:13,:),(/4,2/))))
    
    end
    

提交回复
热议问题