1
votes

How write assignment operator for complex number matrix class. I know complex is standard in fortran. For zcomplex_type works correct. I can assign real part of complex number to real type, but in zmatrix_type doesn't work. I must create matrix of real number in subroutine zmatrix_realmatrix_assign.

Error

mat1=tab
Error: Incompatible ranks 0 and 2 in assignment at (1)
module zmatrix_module
implicit none
type, public :: zcomplex_type

    real :: realis
    real :: imaginalis

end type zcomplex_type

type, public :: zmatrix_type
    type(zcomplex_type), dimension(:,:), allocatable, public :: zmatrix_data
end type zmatrix_type

    public :: zmatrix_allocate
    public :: zmatrix_free
    public :: zmatrix_set
    public :: zmatrix_print
    public :: assignment(=)
    
interface assignment(=)
procedure zcomplex_re_assign
procedure re_zcomplex_assign
procedure zmatrix_realmatrix_assign
end interface 
    
contains

subroutine zcomplex_re_assign(zcomplex1,real2)
type(zcomplex_type), intent(out) :: zcomplex1
real, intent(in) :: real2
        zcomplex1%realis =real2
        zcomplex1%imaginalis =0.0
end subroutine zcomplex_re_assign

subroutine re_zcomplex_assign(real2,zcomplex1)
real, intent(out) :: real2
type(zcomplex_type), intent(in) :: zcomplex1
        real2=zcomplex1%realis 
end subroutine re_zcomplex_assign

subroutine zmatrix_realmatrix_assign(zmatrix1,realmatrix2)
type(zmatrix_type), intent(out) :: zmatrix1
real,  intent(in) :: realmatrix2

    zmatrix1%zmatrix_data%realis=realmatrix2
    zmatrix1%zmatrix_data%imaginalis=0
end subroutine zmatrix_realmatrix_assign

subroutine zmatrix_allocate(zarray,rows)
    type(zmatrix_type), intent(out) :: zarray
    integer, intent(in) :: rows
    allocate(zarray%zmatrix_data(1:rows, 1:rows))
end subroutine zmatrix_allocate

subroutine zmatrix_free(zarray)
    type(zmatrix_type), intent(inout) :: zarray
    deallocate(zarray%zmatrix_data)
end subroutine zmatrix_free

subroutine zmatrix_set(zarray, rows, re_values, im_values)
    type(zmatrix_type), intent(inout) :: zarray
    integer, intent(in) :: rows
    real, intent(in) :: re_values, im_values
    integer :: i,j
    do i=1, rows
        do j=1, rows
            zarray%zmatrix_data(i,j)%realis = re_values
            zarray%zmatrix_data(i,j)%imaginalis = im_values
        enddo
    enddo
end subroutine zmatrix_set

subroutine zmatrix_print(array,rows)
    type(zmatrix_type), intent(in) :: array
    integer, intent(in) :: rows
    integer i,j
    
    do i=1, rows
        write(*,*) (array%zmatrix_data(i,j), j=1, rows)
    enddo
    write(*,*)
end subroutine zmatrix_print

end module zmatrix_module

Program main
use zmatrix_module
implicit none
type(zmatrix_type) :: mat1
real :: tab(3,3)
call zmatrix_allocate(mat1,3)
tab=3
mat1=tab
print *, mat1

call zmatrix_free(mat1)

End Program main ```

1
As the error says, you have an array on one side (tab on the right) and a scalar (mat1) on the other side of the assignment. This isn't allowed. What do you think should happen? - francescalus
To take @francescalus comment further your overloaded assignment only describes the case of assigning a scalar to the matrix, not a 2D array - Ian Bush
When you assign a 3×3 matrix to a zcomplex_type what did you intend to do? - John Alexiou

1 Answers

1
votes

You created an overloaded assignment operator zmatrix_realmatrix_assign which takes a scalar real value as input and outputs a zmatrix_type.

My guess is that you probably want zmatrix_realmatrix_assign to do the following:

  1. make it accept a 2d real array: real, intent(in) :: realmatrix2(:,:)
  2. allocate zmatrix1%zmatrix_data accordingly
  3. set real/imag parts as previously done
  subroutine zmatrix_realmatrix_assign(this, rmat)
    type(zmatrix_type), intent(out) :: this
    real,               intent(in)  :: rmat(:,:)

    allocate (this%zmatrix_data(size(rmat, dim=1), size(rmat, dim=2)))
    this%zmatrix_data%realis     = rmat
    this%zmatrix_data%imaginalis = 0
  end subroutine

The assignment procedure explicitly allocates the data structure this%zmatrix_data such that the allocate call in your program is unnecessary

call zmatrix_allocate(mat1,3)

You could still use the zmatrix_allocate procedure by changing the intent in the assignment procedure but it is error prone (which is why I have added the tests for allocation)

  subroutine zmatrix_realmatrix_assign(this, rmat)
    type(zmatrix_type), intent(inout) :: this
    real,               intent(in)    :: rmat(:,:)

    if (.not. allocated(this%zmatrix_data)         ) error stop 'did not call allocate beforehand'
    if (any(shape(this%zmatrix_data) /= shape(rmat)) error stop 'wrong allocation of zmatrix_data'
    this%zmatrix_data%realis     = rmat
    this%zmatrix_data%imaginalis = 0
  end subroutine