问题描述
是否可以在Fortran中将矩阵声明为派生类型?例如,可以做些什么来使通话
Is it possible to declare a matrix as a derived type in Fortran? For example, can something be done so that the call
class(four_by_four_matrix) :: A
call A%inv
有效吗?哪里inv
被声明为four_by_four_matrix
的过程?
is valid? Where inv
is declared as a procedure of four_by_four_matrix
?
推荐答案
问题可能吗?"的答案是的,有可能.只需将2d数组放入您的类型中即可:
The answer to the question "is it possible?" is yes, it is possible. Just put a 2d array into your type:
type four_by_four_matrix
real(rp) :: arr(4,4)
contains
procedure :: inv => four_by_four_matrix_inv
end type
contains
subroutine four_by_four_matrix_inv(self)
class(four_by_four_matrix), intent(inout) :: self
....
!somehow invert self%arr
end subroutine
end module
...
type(four_by_four_matrix) :: A
call A%inv
如果您需要更多详细信息,则必须对实际的详细问题提出疑问.
If you need more details, you have to make a question with your actual detailed problems.
BTW类型绑定过程和class
关键字是在Fortran 2003中引入的.请注意,您不一定需要使用class
,如果您的变量不是多态的,也可以使用type(four_by_four_matrix)
.
BTW type-bound procedures and the class
keyword were introduced in Fortran 2003. Notice you don't necessarily need to use class
, you can also use type(four_by_four_matrix)
if your variable is not polymorphic.
这篇关于是否可以在Fortran中将矩阵声明为派生类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!