• <i id='K8ZEa'><tr id='K8ZEa'><dt id='K8ZEa'><q id='K8ZEa'><span id='K8ZEa'><b id='K8ZEa'><form id='K8ZEa'><ins id='K8ZEa'></ins><ul id='K8ZEa'></ul><sub id='K8ZEa'></sub></form><legend id='K8ZEa'></legend><bdo id='K8ZEa'><pre id='K8ZEa'><center id='K8ZEa'></center></pre></bdo></b><th id='K8ZEa'></th></span></q></dt></tr></i><div id='K8ZEa'><tfoot id='K8ZEa'></tfoot><dl id='K8ZEa'><fieldset id='K8ZEa'></fieldset></dl></div>

      <legend id='K8ZEa'><style id='K8ZEa'><dir id='K8ZEa'><q id='K8ZEa'></q></dir></style></legend><tfoot id='K8ZEa'></tfoot>
    1. <small id='K8ZEa'></small><noframes id='K8ZEa'>

        <bdo id='K8ZEa'></bdo><ul id='K8ZEa'></ul>

        如何在 Fortran 中获取先前未知的数组作为函数的

        时间:2023-09-12

        <tfoot id='uqVKX'></tfoot>

        <i id='uqVKX'><tr id='uqVKX'><dt id='uqVKX'><q id='uqVKX'><span id='uqVKX'><b id='uqVKX'><form id='uqVKX'><ins id='uqVKX'></ins><ul id='uqVKX'></ul><sub id='uqVKX'></sub></form><legend id='uqVKX'></legend><bdo id='uqVKX'><pre id='uqVKX'><center id='uqVKX'></center></pre></bdo></b><th id='uqVKX'></th></span></q></dt></tr></i><div id='uqVKX'><tfoot id='uqVKX'></tfoot><dl id='uqVKX'><fieldset id='uqVKX'></fieldset></dl></div>

            1. <small id='uqVKX'></small><noframes id='uqVKX'>

              • <bdo id='uqVKX'></bdo><ul id='uqVKX'></ul>

                  <legend id='uqVKX'><style id='uqVKX'><dir id='uqVKX'><q id='uqVKX'></q></dir></style></legend>
                    <tbody id='uqVKX'></tbody>

                  本文介绍了如何在 Fortran 中获取先前未知的数组作为函数的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Python 中:

                  def 选择(x):y = []对于 x 中的 e:如果 e!=0:y.append(e)返回 y

                  它的作用是:

                  <上一页>x = [1,0,2,0,0,3]选择(x)[1,2,3]

                  要翻译成Fortran:

                  function select(x,n) result(y)隐式无整数:: x(n),n,i,j,y(?)j = 0做 i=1,n如果 (x(i)/=0) 那么j = j+1y(j) = x(i)万一结束结束函数

                  问题在 Fortran 中:

                  1. 如何声明 y(?)?
                  2. 如何为 x 声明预定义值
                  3. 如何避免维度信息n

                  对于 1,如果它被定义为 y(n),输出将是:

                  <上一页>x = (/1,0,2,0,0,3/)打印*,选择(x,6)1,2,3,0,0,0

                  这是不想要的!
                  !-------------------------------
                  评论:
                  1- 所有给出的答案在这篇文章中都很有用.特别是 M.S.B 和 eryksun 的.
                  2- 我尝试根据我的问题调整这些想法并使用 F2Py 进行编译,但是没有成功.我已经使用 GFortran 对它们进行了调试,并且一切都成功了.这可能是 F2Py 中的错误,或者我不知道如何正确使用它.我将尝试在另一篇文章中讨论这个问题.

                  更新:可以在这里.

                  解决方案

                  希望有真正的 Fortran 程序员出现,但是在没有更好的建议的情况下,我只会指定 x(:),使用临时数组temp(size(x)),并使输出y allocatable.然后在第一遍之后,allocate(y(j)) 并从临时数组中复制值.但是我不能强调我不是 Fortran 程序员,所以我不能说该语言是否有一个可增长的数组或者是否存在用于后者的库.

                  程序测试隐式无整数:: x(10) = (/1,0,2,0,3,0,4,0,5,0/)打印(10I2.1)",选择(x)包含函数选择(x)结果(y)隐式无整数,intent(in):: x(:)整数:: i, j, temp(size(x))整数,可分配:: y(:)j = 0我 = 1,大小(x)如果 (x(i)/= 0) 那么j = j + 1温度(j) = x(i)万一结束分配(y(j))y = 温度(:j)结束功能选择结束程序测试

                  <小时>

                  根据 M.S.B. 的回答,这里是函数的修订版本,该函数通过过度分配增加 temp y.和之前一样,它在最后将结果复制到 y. 事实证明,i 没有必要以最终大小显式分配一个新数组.相反,它可以通过分配自动完成.

                   函数选择(x) 结果(y)隐式无整数,intent(in):: x(:)整数:: i, j, dsize整数,可分配:: temp(:), y(:)尺寸 = 0;分配(y(0))j = 0我 = 1,大小(x)如果 (x(i)/= 0) 那么j = j + 1if (j >= dsize) then !grow y using tempdsize = j + j/8 + 8分配(临时(dsize))温度(:大小(y))= y调用 move_alloc(temp, y) !temp 被释放万一y(j) = x(i)万一结束y = y(:j)结束功能选择

                  In Python:

                  def select(x):
                      y = []
                      for e in x:
                          if e!=0:
                              y.append(e)
                      return y
                  

                  that works as:

                  x = [1,0,2,0,0,3]
                  select(x)
                  [1,2,3]
                  

                  to be translated into Fortran:

                  function select(x,n) result(y)
                      implicit none
                      integer:: x(n),n,i,j,y(?)
                      j = 0
                      do i=1,n
                          if (x(i)/=0) then
                              j = j+1
                              y(j) = x(i)
                          endif
                      enddo
                  end function
                  

                  The questions are in Fortran:

                  1. how to declare y(?)?
                  2. how to declare predefined values for x
                  3. how to avoid dimension info n

                  for 1 if it is defined as y(n) the output will be:

                  x = (/1,0,2,0,0,3/)
                  print *,select(x,6)
                  1,2,3,0,0,0
                  

                  which is not desired!
                  !-------------------------------
                  Comments:
                  1- All given answers are useful in this post. Specially M.S.B and eryksun's.
                  2- I tried to adapt the ideas for my problem and compile with F2Py however it was not successful. I had already debugged them using GFortran and all were successful. It might be a bug in F2Py or something that I don't know about using it properly. I will try to cover this issue in another post.

                  Update: A linked question could be found at here.

                  解决方案

                  I hope a real Fortran programmer comes along, but in the absence of better advice, I would only specify the shape and not the size of x(:), use a temporary array temp(size(x)), and make the output y allocatable. Then after the first pass, allocate(y(j)) and copy the values from the temporary array. But I can't stress enough that I'm not a Fortran programmer, so I can't say if the language has a growable array or if a library exists for the latter.

                  program test
                      implicit none
                      integer:: x(10) = (/1,0,2,0,3,0,4,0,5,0/)
                      print "(10I2.1)", select(x)
                  
                  contains
                  
                      function select(x) result(y)
                          implicit none
                          integer, intent(in):: x(:) 
                          integer:: i, j, temp(size(x))
                          integer, allocatable:: y(:)
                  
                          j = 0
                          do i = 1, size(x)
                              if (x(i) /= 0) then
                                  j = j + 1
                                  temp(j) = x(i)
                              endif
                          enddo
                  
                          allocate(y(j))
                          y = temp(:j)
                      end function select
                  
                  end program test
                  


                  Edit:

                  Based on M.S.B.'s answer, here's a revised version of the function that grows temp y with over-allocation. As before it copies the result to y at the end. It turns out i's not necessary to explicitly allocate a new array at the final size. Instead it can be done automatically with assignment.

                      function select(x) result(y)
                          implicit none
                          integer, intent(in):: x(:) 
                          integer:: i, j, dsize
                          integer, allocatable:: temp(:), y(:)
                  
                          dsize = 0; allocate(y(0))
                  
                          j = 0
                          do i = 1, size(x)
                              if (x(i) /= 0) then
                                  j = j + 1
                  
                                  if (j >= dsize) then         !grow y using temp
                                      dsize = j + j / 8 + 8 
                                      allocate(temp(dsize))
                                      temp(:size(y)) = y
                                      call move_alloc(temp, y) !temp gets deallocated
                                  endif
                  
                                  y(j) = x(i)
                              endif
                          enddo
                          y = y(:j)
                      end function select
                  

                  这篇关于如何在 Fortran 中获取先前未知的数组作为函数的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Windows 上使用 XAMPP 托管 Django 下一篇:Fortran - Cython 工作流程

                  相关文章

                  最新文章

                  <legend id='P7uSy'><style id='P7uSy'><dir id='P7uSy'><q id='P7uSy'></q></dir></style></legend>

                    <i id='P7uSy'><tr id='P7uSy'><dt id='P7uSy'><q id='P7uSy'><span id='P7uSy'><b id='P7uSy'><form id='P7uSy'><ins id='P7uSy'></ins><ul id='P7uSy'></ul><sub id='P7uSy'></sub></form><legend id='P7uSy'></legend><bdo id='P7uSy'><pre id='P7uSy'><center id='P7uSy'></center></pre></bdo></b><th id='P7uSy'></th></span></q></dt></tr></i><div id='P7uSy'><tfoot id='P7uSy'></tfoot><dl id='P7uSy'><fieldset id='P7uSy'></fieldset></dl></div>

                    <small id='P7uSy'></small><noframes id='P7uSy'>

                    1. <tfoot id='P7uSy'></tfoot>
                        <bdo id='P7uSy'></bdo><ul id='P7uSy'></ul>