How to create operator for matmul in Fortran?

The following is the code that Iā€™m trying (new to Fortran).

program mmo
        implicit none
        interface operator (.mul.)
                 intrinsic procedure matmul
        end interface

        integer :: i
        real, dimension(5,5) :: a, b, c, d

        a = 5
        b = 6
        c = 2
        d = a .mul. b .mul. c

        do i = 1,5
                print *, c(i,:)
        end do
end program mmo

But, this results in the error

Unexpected character in variable list at (1)

in the line intrinsic procedure matmul.
I was just trying to mix and match the tutorials available in these two sites.

What should I do to overload any of the intrinsic procedures?

Also, is it possible to create a new operator for matmul, say ā€œ@ā€ similar to python (PEP 465 -- A dedicated infix operator for matrix multiplication | When I pass that as operator in line 3, it throws syntax error.

1 Like

I think the only way is something along these lines:

interface operator(.mul.)
    module procedure wrap_matmul1
end interface 

function wrap_matmul1( a, b ) 
     real, dimension(:,:), intent(in) :: a,b 
     real, dimension(size(a,1), size(b,2)) :: wrap_matmul1
     wrap_matmul1 = matmul(a,b)
end function wrap_matmul1

... similar functions for vector/matrix combinations

I used intel Fortran oneAPI and gfortran to try it - Intel Fortran produced a longer list of errors, indicating that the simple approach is not acceptable.

1 Like

Oh, the Fortran standard does not allow you to define arbitrary characters as custom operators, only names of the form ā€œ.op.ā€. You can redefine existing operators non-intrinsic types, for instance // to take the meaning of matmul(ā€¦)

1 Like

Thanks! This works well!

Ah, okay! I thought it may extend for all character sets.