Hello everyone
I have a homework where we should solve the exercises 12 -2 to 12-7 from from Chapman, Fortran for Scientists and Engineers.I was able to implement the new type polar and I think I was able to get the operations to work as well. I need to program two functions/subroutines that allow the conversion of the ordinary complex number to a polar number and vice versa. I tried to do all this with the following module:
module complex
use iso_fortran_env
implicit none
private
public :: polar
type :: polar
real :: mag, theta
contains
procedure,private,pass(this) :: complex_ord_to_polar
!procedure,private,pass(this) :: complex_polar_to_ord
procedure,private,pass(this) :: polar_multiply
procedure,private,pass(this) :: polar_divide
generic,public :: operator(*) => polar_multiply
generic,public :: operator(/) => polar_divide
generic,public :: assignment(=) => complex_ord_to_polar!, complex_polar_to_ord
!generic,public :: assignment(=) => complex_polar_to_ord
end type polar
contains
subroutine complex_ord_to_polar(complex_ord,this)
complex,intent(in) :: complex_ord
class(polar),intent(out) :: this
real :: real_part, im_part
im_part = AIMAG(complex_ord)
real_part = real(complex_ord)
this%theta = atan2(im_part, real_part)
this%mag = sqrt(real_part**2+im_part**2)
end subroutine complex_ord_to_polar
subroutine complex_polar_to_ord(this,complex_ord)
complex,intent(out) :: complex_ord
class(polar),intent(in) :: this
real :: real_part, im_part
select type(this)
class is (polar)
im_part = this%mag * cos(this%theta)
real_part = this%mag * sin(this%theta)
complex_ord = cmplx(real_part,im_part)
end select
end subroutine complex_polar_to_ord
type(polar) function polar_multiply(this,b)
class(polar),intent(in) :: this,b
polar_multiply%mag = this%mag * b%mag
polar_multiply%theta = this%theta + b%theta
if ( (polar_multiply%theta >= 180).or.(polar_multiply%theta <= -180) ) then
print*, 'The multiplication of the polar coordinates went wrong. '
end if
end function polar_multiply
type(polar) function polar_divide(this,b)
class(polar),intent(in) :: this,b
polar_divide%mag = this%mag / b%mag
polar_divide%theta = this%theta - b%theta
if ( (polar_divide%theta >= 180).or.(polar_divide%theta <= -180) ) then
print*, 'The diviation of the polar coordinates went wrong. '
end if
end function polar_divide
end module complex
I then have to test it using the following file:
`program ex_10_2
use iso_fortran_env
use complex
implicit none
real theta
type(polar) :: p1, pe, p3
complex :: c1 = cmplx(-1.0,2.0), c2 = cmplx(2.0, -0.5)
!test type conversion
p1 = c1 !type conversion using overloaded =
c2 = p1 !type conversion using overloaded =
print*, p1 !should be 2.23606801 116.565041
print*, c2/c1 !should be close to (1.0, 0.0)
test mutiply and divide
p2 = c2
p3 = p1 * p2 !using overloaded *
print*, p3 !should be 5.00000048 -126.869919
p3 = p2 / p1 !using overloaded /
print*, p3 !should be close to 1.0 0.0
end program ex_10_2`
However when I try to compile the module I get the following errror:
module_complex.f90:23:31:
23 | subroutine complex_ord_to_polar(complex_ord,this)
| 1
Error: First argument of defined assignment at (1) must be INTENT(OUT) or INTENT(INOUT)
If I then comment this subroutine out I get the same error for the subroutine complex_polar_to_ord. What do I do wrong here? How can I fix this error?
I currently using the following gfortran version:
GNU Fortran (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
Many thanks already in advance for all รถyour suggestions and help.
Best regards
fidu13