最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

用户在Fortran中定义的运算符

SEO心得admin37浏览0评论
本文介绍了用户在Fortran中定义的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对在Fortran中编程用户定义的运算符的正确方法有疑问.更具体地说,我将提供我的问题的示例.我正在为称为粒子"的球形粒子创建用户定义的数据类型.我想定义一个运算符,该运算符采用现有的粒子对象数组并向其中添加一个新的粒子对象.我想知道如何定义用户定义的运算符来执行此操作.

I had a question about the correct way of programming user-defined operators in Fortran. To be more specific, I will provide the example of my problem. I am working on creating a user-defined data type for spherical particles called 'Particle'. I want to define an operator that takes an existing array of Particle objects and adds a new Particle object to it. I was wondering how I would go about defining user defined operators to do such an action.

当前,在粒子"的类型定义中,有以下几行:

Currently I have, within the type definition for Particle, the following lines:

procedure, public:: addNewParticleTo generic:: operator(.spawn.) => addNewParticleTo

接下来,我有一个定义如下的子例程:

Following which, I have a subroutine that is defined as follows:

subroutine addNewParticleTo(a_LHS, a_RHS) implicit none class(Particle), dimension(:), allocatable, intent(in):: a_LHS class(Particle), dimension(:), allocatable, intent(inout):: a_RHS <rest of the code> end subroutine addNewParticleTo

我打算将该运算符调用为:

I intend for the operator to be invoked as:

particle .spawn. particleArray

我想知道这是否是执行此操作的正确方法.关于此的任何建议或意见将非常有帮助.

I was wondering if this is the correct way to go about doing this. Any suggestions or advise on this will be very helpful.

推荐答案

要扩展注释,您需要将operator代码作为function.此外,每个输入将需要为intent(in).这确实允许使用array = particle .spawn. array之类的东西.

To expand on the comments, you will need to have the operator code as a function. Further, each input would need to be intent(in). This would indeed allow something like array = particle .spawn. array.

但是,您的子例程需要进行另一次更改:您的参数之一应该是标量. [第一个,除非您使用pass属性.]

However, another change is required to your subroutine: one of your arguments should be a scalar. [The first, unless you play with the pass attribute.]

function addNewParticleTo(A, B) result(C) class(particle), intent(in) :: A class(particle), allocatable, intent(in) :: B(:) class(particle), allocatable :: C(:) ! ... code to do the appending end function

最后,我的建议是,将其作为类型绑定运算符会使事情变得相当复杂,包括多态等.另外,array = particle .spawn. array似乎非常不直观.

Finally, my advice is that having this as a type-bound operator is making things quite complicated, with the polymorphism and so on. Also, array = particle .spawn. array seems very unintuitive.

相反,只是一个普通的子例程,以便call add_to_particle_array(all_particles, new_particle)可以正常工作.这更接近您的原始代码,但是不能回答您有关运算符的问题.

Instead, just a plain subroutine so that call add_to_particle_array(all_particles, new_particle) works seems cleaner. This is closer to your original code, but doesn't answer your question about operators, alas.

发布评论

评论列表(0)

  1. 暂无评论