是否可以在Fortran中将矩阵声明为派生类型? [英] Is it possible to declare a matrix as a derived type in Fortran?

查看:114
本文介绍了是否可以在Fortran中将矩阵声明为派生类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在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中将矩阵声明为派生类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆