gpt4 book ai didi

arrays - 将派生类型作为数组传递

转载 作者:行者123 更新时间:2023-12-01 00:40:46 25 4
gpt4 key购买 nike

在 Fortran 中,可以对数组进行操作,但是如何将派生类型的索引也视为数组的一部分呢?代码将解释我最想做的事情:

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)

我假设最后一行类似于一个 2x4 矩阵与自身相乘。但是,当我尝试编译时,gfortran 提示

Error: Two or more part references with nonzero rank must not be specified

这可以用 Fortran 实现吗?

最佳答案

您希望编译器将 mat(:)%c 视为 2 x 4 矩阵?它不是那样工作的。 matc 是不同的对象,它们的等级不会合并到一个数组中。 mat 是用户定义的类型,c 是实数矩阵。仅仅因为您只使用 matc 组件并不意味着编译器会将 c 提升为更高维的实数数组,基于在 mat 的维度上。

您可以通过 X = [ mat(1)%c, mat(2)%c ] 创建一个新数组。您可以使用 reshape 来控制形状。

关于arrays - 将派生类型作为数组传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8814260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com