Complex example module#
Here’s a complex Fortran module with comprehensive documentation:
!> Complex example module with multiple entities for selective documentation
module math_utilities
implicit none
private
public :: vector_type, matrix_type
public :: vector_add, vector_subtract, vector_magnitude
public :: matrix_multiply, matrix_transpose
public :: PI, E
!> Mathematical constant PI
real, parameter :: PI = 3.14159265359
!> Mathematical constant E
real, parameter :: E = 2.71828182846
!> 3D vector type for mathematical operations
type :: vector_type
real :: x = 0.0 !> X component
real :: y = 0.0 !> Y component
real :: z = 0.0 !> Z component
contains
procedure :: normalize => vector_normalize
procedure :: cross => vector_cross_product
end type vector_type
!> 3x3 matrix type for linear algebra operations
type :: matrix_type
real :: elements(3,3) = 0.0 !> Matrix elements stored in column-major order
contains
procedure :: determinant => matrix_determinant
procedure :: inverse => matrix_inverse
end type matrix_type
type(vector_type), parameter :: one_vector = vector_type(1.0, 1.0, 1.0) !> One vector constant
contains
!> Add two vectors component-wise
!>
!> .. math:: \vec{c} = \vec{a} + \vec{b} = (a_x + b_x, a_y + b_y, a_z + b_z)
!>
!> ## Examples
!> >>> use math_utilities, only: vector_type, vector_add
!> >>> type(vector_type) :: v1, v2, v3
!> >>> v1 = vector_type(1.0, 2.0, 3.0)
!> >>> v2 = vector_type(4.0, 5.0, 6.0)
!> >>> v3 = vector_add(v1, v2)
!> >>> print *, v3%x, v3%y, v3%z
!> 5.0, 7.0, 9.0
!>
pure function vector_add(a, b) result(c)
type(vector_type), intent(in) :: a !> First vector
type(vector_type), intent(in) :: b !> Second vector
type(vector_type) :: c !> Result vector
c%x = a%x + b%x
c%y = a%y + b%y
c%z = a%z + b%z
end function vector_add
!> Subtract two vectors component-wise
!>
!>```fortran
!> ! This code snippet is in-line documentation only
!> use math_utilities, only: vector_type
!>```
!>
!>## Examples
!>```fortran
!> use math_utilities, only: vector_type, vector_add
!> type(vector_type) :: v1, v2, v3
!> v1 = vector_type(1.0, 2.0, 3.0)
!> v2 = vector_type(4.0, 5.0, 6.0)
!> v3 = vector_subtract(v2, v1)
!> print *, v3%x, v3%y, v3%z
!> ```
!> 3.0, 3.0, 3.0
!>
pure function vector_subtract(a, b) result(c)
type(vector_type), intent(in) :: a !> First vector
type(vector_type), intent(in) :: b !> Second vector
type(vector_type) :: c !> Result vector
c%x = a%x - b%x
c%y = a%y - b%y
c%z = a%z - b%z
end function vector_subtract
!> Calculate magnitude of a vector.
!>
!> The magnitude is :math:`|\vec{v}| = \sqrt{x^2 + y^2 + z^2}`.
!>
!> ## References
!> Cite the relevant literature, e.g. [1]_.
!> .. [1] "Norm (mathematics)," Wikipedia. [Online]. Available: https://en.wikipedia.org/wiki/Norm_(mathematics). [Accessed: 22-Dec-2025].
!>
pure function vector_magnitude(v) result(mag)
!> Input vector
type(vector_type), intent(in) :: v
!> Magnitude
real :: mag
mag = sqrt(v%x**2 + v%y**2 + v%z**2)
end function vector_magnitude
!> Multiply two 3x3 matrices
!>
!> ## Notes
!> Some very important information to retain when multiplying matrices ...
!>
pure function matrix_multiply(a, b) result(c)
type(matrix_type), intent(in) :: a !> First matrix
type(matrix_type), intent(in) :: b !> Second matrix
type(matrix_type) :: c !> Result matrix
integer :: i, j, k
do i = 1, 3
do j = 1, 3
c%elements(i,j) = 0.0
do k = 1, 3
c%elements(i,j) = c%elements(i,j) + a%elements(i,k) * b%elements(k,j)
end do
end do
end do
end function matrix_multiply
!> Transpose a 3x3 matrix
pure function matrix_transpose(a) result(b)
type(matrix_type), intent(in) :: a !> Input matrix
type(matrix_type) :: b !> Transposed matrix
integer :: i, j
do i = 1, 3
do j = 1, 3
b%elements(i,j) = a%elements(j,i)
end do
end do
end function matrix_transpose
!> Private helper function (not publicly exported)
pure function vector_internal_helper(x) result(y)
!> Input value
real, intent(in) :: x
!> Output value
real :: y
y = x * 2.0
end function vector_internal_helper
!> Normalize vector to unit length (type-bound procedure)
!>
!> .. math:: \hat{v} = \frac{\vec{v}}{|\vec{v}|}
!>
pure subroutine vector_normalize(this)
!> Vector to normalize
class(vector_type), intent(inout) :: this
real :: mag
mag = sqrt(this%x**2 + this%y**2 + this%z**2)
if (mag > 0.0) then
this%x = this%x / mag
this%y = this%y / mag
this%z = this%z / mag
end if
end subroutine vector_normalize
!> Calculate cross product of two vectors (type-bound procedure)
pure function vector_cross_product(this, other) result(cross)
!> First vector
class(vector_type), intent(in) :: this
!> Second vector
type(vector_type), intent(in) :: other
!> Cross product result
type(vector_type) :: cross
cross%x = this%y * other%z - this%z * other%y
cross%y = this%z * other%x - this%x * other%z
cross%z = this%x * other%y - this%y * other%x
end function vector_cross_product
!> Calculate matrix determinant (type-bound procedure)
pure function matrix_determinant(this) result(det)
!> Matrix instance
class(matrix_type), intent(in) :: this
!> Determinant value
real :: det
det = this%elements(1,1) * (this%elements(2,2) * this%elements(3,3) - this%elements(2,3) * this%elements(3,2)) &
- this%elements(1,2) * (this%elements(2,1) * this%elements(3,3) - this%elements(2,3) * this%elements(3,1)) &
+ this%elements(1,3) * (this%elements(2,1) * this%elements(3,2) - this%elements(2,2) * this%elements(3,1))
end function matrix_determinant
!> Calculate matrix inverse (type-bound procedure)
!>
!> ## See Also
!> :f:func:`matrix_determinant` : some comment here.
!> :f:func:`matrix_multiply`, :f:func:`matrix_transpose`
!>
pure function matrix_inverse(this) result(inv)
!> Matrix instance
class(matrix_type), intent(in) :: this
!> Inverse matrix
type(matrix_type) :: inv
real :: det
det = this%determinant()
if (abs(det) > tiny(1.0)) then
! Calculate inverse using cofactor method
! ... (implementation details omitted for brevity)
inv%elements = 0.0 ! Placeholder
else
inv%elements = 0.0 ! Singular matrix
end if
end function matrix_inverse
end module math_utilities
math_utilities (module)#
Complex example module with multiple entities for selective documentation
Variables
- PIreal, parameter, Default = 3.14159265359
Mathematical constant PI
- Ereal, parameter, Default = 2.71828182846
Mathematical constant E
- one_vectortype(vector_type), parameter, Default = vector_type(1.0, 1.0, 1.0)
One vector constant
Types
vector_type (type)#
matrix_type (type)#
-
matrix_type (type) 3x3 matrix type for linear algebra operations
- Attributes:
- elementsreal, dimension(3,3), Default = 0.0
Matrix elements stored in column-major order
- Procedures:
Procedures
vector_add (function)#
-
pure function vector_add(a, b) -> c Add two vectors component-wise
\[\vec{c} = \vec{a} + \vec{b} = (a_x + b_x, a_y + b_y, a_z + b_z)\]- Arguments:
- atype(vector_type), intent(in)
First vector
- btype(vector_type), intent(in)
Second vector
- Returns:
- ctype(vector_type)
Result vector
Examples
use math_utilities, only: vector_type, vector_add type(vector_type) :: v1, v2, v3 v1 = vector_type(1.0, 2.0, 3.0) v2 = vector_type(4.0, 5.0, 6.0) v3 = vector_add(v1, v2) print *, v3%x, v3%y, v3%z
5.0, 7.0, 9.0
vector_subtract (function)#
-
pure function vector_subtract(a, b) -> c Subtract two vectors component-wise
! This code snippet is in-line documentation only use math_utilities, only: vector_type
- Arguments:
- atype(vector_type), intent(in)
First vector
- btype(vector_type), intent(in)
Second vector
- Returns:
- ctype(vector_type)
Result vector
Examples
use math_utilities, only: vector_type, vector_add type(vector_type) :: v1, v2, v3 v1 = vector_type(1.0, 2.0, 3.0) v2 = vector_type(4.0, 5.0, 6.0) v3 = vector_subtract(v2, v1) print *, v3%x, v3%y, v3%z
3.0, 3.0, 3.0
vector_magnitude (function)#
-
pure function vector_magnitude(v) -> mag Calculate magnitude of a vector.
The magnitude is \(|\vec{v}| = \sqrt{x^2 + y^2 + z^2}\).
- Arguments:
- vtype(vector_type), intent(in)
Input vector
- Returns:
- magreal
Magnitude
References
Cite the relevant literature, e.g. [1].
matrix_multiply (function)#
-
pure function matrix_multiply(a, b) -> c Multiply two 3x3 matrices
- Arguments:
- atype(matrix_type), intent(in)
First matrix
- btype(matrix_type), intent(in)
Second matrix
- Returns:
- ctype(matrix_type)
Result matrix
Notes
Some very important information to retain when multiplying matrices …
matrix_transpose (function)#
-
pure function matrix_transpose(a) -> b Transpose a 3x3 matrix
- Arguments:
- atype(matrix_type), intent(in)
Input matrix
- Returns:
- btype(matrix_type)
Transposed matrix
vector_internal_helper (function)#
-
pure function vector_internal_helper(x) -> y Private helper function (not publicly exported)
- Arguments:
- xreal, intent(in)
Input value
- Returns:
- yreal
Output value
vector_normalize (subroutine)#
-
pure subroutine vector_normalize(this) Normalize vector to unit length (type-bound procedure)
\[\hat{v} = \frac{\vec{v}}{|\vec{v}|}\]- Arguments:
- thisclass(vector_type), intent(inout)
Vector to normalize
vector_cross_product (function)#
-
pure function vector_cross_product(this, other) -> cross Calculate cross product of two vectors (type-bound procedure)
- Arguments:
- thisclass(vector_type), intent(in)
First vector
- othertype(vector_type), intent(in)
Second vector
- Returns:
- crosstype(vector_type)
Cross product result
matrix_determinant (function)#
-
pure function matrix_determinant(this) -> det Calculate matrix determinant (type-bound procedure)
- Arguments:
- thisclass(matrix_type), intent(in)
Matrix instance
- Returns:
- detreal
Determinant value
matrix_inverse (function)#
-
pure function matrix_inverse(this) -> inv Calculate matrix inverse (type-bound procedure)
- Arguments:
- thisclass(matrix_type), intent(in)
Matrix instance
- Returns:
- invtype(matrix_type)
Inverse matrix