!------------------------------------------------------------------------------
!
!  Module name:
!
!    vector_module
!
!  Purpose:
!
!    This module defines a vector type with x and y components and the 
!    associated operators '+' and '-' overloading.
!
!------------------------------------------------------------------------------

MODULE vector_module

  ! Specifications:

  TYPE vector
    REAL :: x, y
  END TYPE vector

  ! Interfaces:

  INTERFACE OPERATOR (+)        ! overloading operator + 
    MODULE PROCEDURE vector_add
  END INTERFACE
 
  INTERFACE OPERATOR (-)        ! overloading operator - 
    MODULE PROCEDURE vector_negate, vector_sub
  END INTERFACE

CONTAINS

  ! Module procedures:

  FUNCTION vector_add(v1, v2)

    TYPE (vector) :: vector_add
    TYPE (vector), INTENT(IN) :: v1, v2

    vector_add%x = v1%x + v2%x
    vector_add%y = v1%y + v2%y

  END FUNCTION vector_add
 
  FUNCTION vector_negate(v)

    TYPE (vector) :: vector_negate
    TYPE (vector),INTENT(IN) :: v

    vector_negate%x = -v%x
    vector_negate%y = -v%y

  END FUNCTION vector_negate
 
  FUNCTION vector_sub(v1, v2)

    TYPE (vector) :: vector_sub
    TYPE (vector), INTENT(IN) :: v1, v2

    vector_sub = v1 + (-v2)

  END FUNCTION vector_sub

END MODULE vector_module






