306908677 发表于 2014-12-6 14:11:11

如何使用function重载运算符运算后返回一个数组

RT
求高手指点

306908677 发表于 2014-12-6 14:37:11

补充:数组运算得到数组

百事可乐 发表于 2014-12-6 16:13:17

我这里有一个重载 matmul 的代码,给你当例子吧

module matmul_int
INTERFACE OPERATOR(.mm.)
    module procedure fc
END INTERFACE
contains

function fc(a,b)
    real , intent( IN ) :: a(:,:) , b(:,:)
    real :: fc(size(a,dim=1),size(b,dim=2))
    fc = matmul(a,b)
end function fc
end module matmul_int

program main
use matmul_int
real :: x(3,2) , y(2,4)
x = 1 ; y = 2
write(*,*) x .mm. y
end

li913 发表于 2014-12-6 19:10:45

普通的 + - * / 都是可以运用于数组的。

real a(3,4), b(3,4), c(3,4)
c = a + b

306908677 发表于 2014-12-6 21:41:14

本帖最后由 306908677 于 2014-12-7 20:54 编辑

百事可乐 发表于 2014-12-6 16:13
我这里有一个重载 matmul 的代码,给你当例子吧

module matmul_int

program main
implicit none

   interface
      subroutine random_arr(a)
      implicit none
      integer(4),intent(out),allocatable ::a(:)
      end
   end interface

   interface operator(.U.)
      Function addy(c1, c2)
      implicit none
      integer, intent(in) :: c1(:), c2(:)
      integer, intent(out):: addy(20)



错误:
Function addy(c1, c2)
1
错误: (1)处的符号不是一个 DUMMY 变量
/Users/StarkLee/Desktop/EQU2.F90:33.1:

function addy(c1,c2)
1
错误: (1)处的符号不是一个 DUMMY 变量
Jarvis:~ StarkLee$

什么原因啊==

306908677 发表于 2014-12-7 00:11:45

li913 发表于 2014-12-6 19:10
普通的 + - * / 都是可以运用于数组的。

real a(3,4), b(3,4), c(3,4)


嗯嗯,thanx~

fcode 发表于 2014-12-7 00:31:30

照猫画虎还不会?

Module S
Interface Operator (.U.)
    Module Procedure Addy
End Interface Operator (.U.)
Contains
Function Addy(C1, C2)
    Implicit None
    Integer, Intent (In) :: C1(:), C2(:)
    Integer :: Addy(20)
    Integer :: Arry1(20), Arry2(20)
    Integer I, Len
    Logical :: Flg(20)
    Len = Size(C1)
    Arry1 = -1
    Do I = 1, Len
      Arry1(C1(I)) = C1(I)
    End Do
    Len = Size(C2)
    Arry2 = -2
    Do I = 1, Len
      Arry2(C2(I)) = C2(I)
    End Do
    Addy = -3
    Flg = All(Arry1.Eq.Arry2)
    Print *, Flg
    Addy = Merge(Arry1, Addy, Flg)
End Function Addy
End Module S
Program Main
Use S
Implicit None

Interface
    Subroutine Random_arr(A)
      Implicit None
      Integer (Kind=4), Intent (Out), Allocatable :: A(:)
    End Subroutine Random_arr
End Interface


Integer, Allocatable :: A(:)
Integer, Allocatable :: B(:)
Integer Lena, Lenb

Call Init_random_seed()
Call Random_arr(A)
Call Random_arr(B)
Lena = Size(A)
Lenb = Size(B)
Print *, 'A'
Write (*, 10) A
Print *, 'B'
Write (*, 10) B
10 Format (20I3)

End Program Main

306908677 发表于 2014-12-7 13:39:08

fcode 发表于 2014-12-7 00:31
照猫画虎还不会?

Module S


多谢大家的耐心指点~

仔细看了一遍原来function不能定义为 out~
问题已解决

vvt 发表于 2014-12-7 13:41:50

是的,in,out只能对虚参进行。返回值不是虚参
页: [1]
查看完整版本: 如何使用function重载运算符运算后返回一个数组