utils_math_m Module

Various (mostly linear algebra) functions, particularly for use with small matrices.


Uses

  • module~~utils_math_m~~UsesGraph module~utils_math_m utils_math_m ieee_arithmetic ieee_arithmetic module~utils_math_m->ieee_arithmetic module~constants_m constants_m module~utils_math_m->module~constants_m iso_fortran_env iso_fortran_env module~constants_m->iso_fortran_env

Contents


Interfaces

public interface allclose

Checks if two arrays are elementwise close within tolerance

  • public function allclose_rank1(a, b, rel_tol, abs_tol)

    Checks if two rank-1 floating point arrays of type double are close within tolerance.

    Arguments

    Type IntentOptional AttributesName
    real(kind=rp), intent(in), dimension(:):: a

    (m,) array

    real(kind=rp), intent(in), dimension(:):: b

    (m,) array

    real(kind=rp), intent(in), optional :: rel_tol

    Relative tolerance; default 1e-10

    real(kind=rp), intent(in), optional :: abs_tol

    Absolute tolerance; default 0.0

    Return Value logical

  • public function allclose_rank2(a, b, rel_tol, abs_tol)

    Checks if two rank-2 floating point arrays of type double are close within tolerance.

    Arguments

    Type IntentOptional AttributesName
    real(kind=rp), intent(in), dimension(:,:):: a

    (m,n) array

    real(kind=rp), intent(in), dimension(:,:):: b

    (m,n) array

    real(kind=rp), intent(in), optional :: rel_tol

    Relative tolerance; default 1e-10

    real(kind=rp), intent(in), optional :: abs_tol

    Absolute tolerance; default 0.0

    Return Value logical

  • public function allclose_rank3(a, b, rel_tol, abs_tol)

    Checks if two rank-3 floating point arrays of type double are close within tolerance.

    Arguments

    Type IntentOptional AttributesName
    real(kind=rp), intent(in), dimension(:,:,:):: a

    (m,n,p) array

    real(kind=rp), intent(in), dimension(:,:,:):: b

    (m,n,p) array

    real(kind=rp), intent(in), optional :: rel_tol

    Relative tolerance; default 1e-10

    real(kind=rp), intent(in), optional :: abs_tol

    Absolute tolerance; default 0.0

    Return Value logical

public interface swap

Swaps two arrays

  • public elemental subroutine swap_integer(a, b)

    Arguments

    Type IntentOptional AttributesName
    integer, intent(inout) :: a
    integer, intent(inout) :: b
  • public elemental subroutine swap_real(a, b)

    Arguments

    Type IntentOptional AttributesName
    real(kind=rp), intent(inout) :: a
    real(kind=rp), intent(inout) :: b
  • public elemental subroutine swap_complex(a, b)

    Arguments

    Type IntentOptional AttributesName
    complex(kind=rp), intent(inout) :: a
    complex(kind=rp), intent(inout) :: b

Functions

public function scalar_triple_product(a, b, c) result(res)

Returns the scalar triple product a.(b x c)

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3):: a
real(kind=rp), intent(in), dimension(3):: b
real(kind=rp), intent(in), dimension(3):: c

Return Value real(kind=rp)

public function det(A) result(res)

Returns the determinant of an (N x N) matrix, where N = 2, 3, or 4.

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:):: A

(N,N) array, where N = 2, 3, or 4.

Return Value real(kind=rp)

public function trace(mat) result(res)

Returns the trace of a square matrix

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: mat(:,:)

(N,N) array

Return Value real(kind=rp)

public elemental function isclose(a, b, rel_tol, abs_tol)

Checks if two floating point numbers of type double are close within tolerance.

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: a
real(kind=rp), intent(in) :: b
real(kind=rp), intent(in), optional :: rel_tol

Relative tolerance, rel_tol >= 0, default 1e-10

real(kind=rp), intent(in), optional :: abs_tol

Absolute tolerance, abs_tol >= 0, default 0.0

Return Value logical

public function allclose_rank1(a, b, rel_tol, abs_tol)

Checks if two rank-1 floating point arrays of type double are close within tolerance.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:):: a

(m,) array

real(kind=rp), intent(in), dimension(:):: b

(m,) array

real(kind=rp), intent(in), optional :: rel_tol

Relative tolerance; default 1e-10

real(kind=rp), intent(in), optional :: abs_tol

Absolute tolerance; default 0.0

Return Value logical

public function allclose_rank2(a, b, rel_tol, abs_tol)

Checks if two rank-2 floating point arrays of type double are close within tolerance.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:):: a

(m,n) array

real(kind=rp), intent(in), dimension(:,:):: b

(m,n) array

real(kind=rp), intent(in), optional :: rel_tol

Relative tolerance; default 1e-10

real(kind=rp), intent(in), optional :: abs_tol

Absolute tolerance; default 0.0

Return Value logical

public function allclose_rank3(a, b, rel_tol, abs_tol)

Checks if two rank-3 floating point arrays of type double are close within tolerance.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:,:):: a

(m,n,p) array

real(kind=rp), intent(in), dimension(:,:,:):: b

(m,n,p) array

real(kind=rp), intent(in), optional :: rel_tol

Relative tolerance; default 1e-10

real(kind=rp), intent(in), optional :: abs_tol

Absolute tolerance; default 0.0

Return Value logical

public function get_quad_form(A, x) result(res)

Calculates the quadratic form x^T A x, where A is an n x n matrix and x is a vector of length n

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:):: A

(n,n) array

real(kind=rp), intent(in), dimension(:):: x

(n,) array

Return Value real(kind=rp)


Subroutines

public elemental subroutine rad2deg(rad, deg)

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: rad
real(kind=rp), intent(out) :: deg

public elemental subroutine deg2rad(deg, rad)

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: deg
real(kind=rp), intent(out) :: rad

public subroutine cross(a, b, c)

Calculates the cross product between two 3-element vectors

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3):: a
real(kind=rp), intent(in), dimension(3):: b
real(kind=rp), intent(out), dimension(3):: c

Cross product of a and b; c = a x b

public subroutine cross_mat(a, mat)

Calculates the cross product matrix of a 3-element vector. The cross product matrix A of a is defined as a x b = A . b, where b is another 3-element vector.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3):: a
real(kind=rp), intent(out), dimension(3,3):: mat

Cross product matrix of a

public subroutine outer(a, b, c)

Calculates the outer product of two vectors, .

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:):: a

(m,) array

real(kind=rp), intent(in), dimension(:):: b

(n,) array

real(kind=rp), intent(out), dimension(:,:):: c

(m,n) array; Outer product

public subroutine vector_triple_product(a, b, c, d)

Returns the vector triple product d = a x (b x c)

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3):: a
real(kind=rp), intent(in), dimension(3):: b
real(kind=rp), intent(in), dimension(3):: c
real(kind=rp), intent(out), dimension(3):: d

Vector triple product

public elemental subroutine swap_integer(a, b)

Arguments

Type IntentOptional AttributesName
integer, intent(inout) :: a
integer, intent(inout) :: b

public elemental subroutine swap_real(a, b)

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(inout) :: a
real(kind=rp), intent(inout) :: b

public elemental subroutine swap_complex(a, b)

Arguments

Type IntentOptional AttributesName
complex(kind=rp), intent(inout) :: a
complex(kind=rp), intent(inout) :: b

public subroutine unitize(a)

Normalizes a vector in-place. If the magnitude of the vector is nearly zero, no normalization takes place and the vector is returned as is with a warning message.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(inout), dimension(:):: a

(m,) array

public subroutine linspace(start, finish, num, val, step)

Generates evenly spaced numbers over a specified interval. Both end points are included. If start < finish, the returned step size (if step is present) will be negative.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: start

Starting point

real(kind=rp), intent(in) :: finish

Ending point, finish /= start

integer, intent(in) :: num

Number of values to generate, num >= 2

real(kind=rp), intent(out), dimension(:):: val

(num,) array; Generated values

real(kind=rp), intent(out), optional :: step

Step size

public subroutine logspace(start, finish, num, val, base)

Generates numbers spaced evenly on a log scale. In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in) :: start

Starting point, base ** start is the starting value

real(kind=rp), intent(in) :: finish

Ending point, finish /= start, base ** start is the ending value

integer, intent(in) :: num

Number of values to generate, num >= 2

real(kind=rp), intent(out), dimension(:):: val

(num,) array; Generated values

real(kind=rp), intent(in), optional :: base

Base of the logspace, default 10

public subroutine identity(mat_eye)

Creates an identity matrix of size n x n.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(out), dimension(:,:):: mat_eye

(n,n) array

public subroutine get_diagonal(mat, d)

Returns the diagonal elements of a square matrix.

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:):: mat

(n,n) array

real(kind=rp), intent(out), dimension(:):: d

(n,) array; contains the entries of the main diagonal

public subroutine add_transpose(mat)

Adds a square matrix and its transpose in place:

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(inout), dimension(:,:):: mat

(n,n) array

public subroutine subtract_transpose(mat)

Calculates the difference of a square matrix and its transpose in place:

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(inout), dimension(:,:):: mat

public subroutine multiply_transpose(A, B)

Multiplies a matrix with its transpose:

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(:,:):: A

(m,n) array

real(kind=rp), intent(out), dimension(:,:):: B

(m,m) array

public subroutine orth(a)

Orthogonalizes a set of vectors in-place using Gram-Schmidt orthonormalization

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(inout), dimension(:,:):: a

(m,n) array, where m <= n. The first m columns of the matrix are overwritten with the orthogonal basis vectors.

public subroutine invert_mat33(a, inv_a)

Inverts a 3x3 matrix.

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3,3):: a
real(kind=rp), intent(out), dimension(3,3):: inv_a

public subroutine eigval_33rsym(a, ev)

Calculates the eigenvalues of a 3 x 3 real symmetric matrix. The eigenvalues calculated are in decreasing order. Only the diagonal and lower triangular part of the matrix is accessed.

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=rp), intent(in), dimension(3,3):: a
real(kind=rp), intent(out), dimension(3):: ev

public subroutine dsyevc3(A, W)

Author
Joachim Kopp
Date
2006

Calculates the eigenvalues of a symmetric 3x3 matrix A using Cardano's analytical algorithm. Only the diagonal and upper triangular parts of A are accessed. The access is read-only.

Read more…

Arguments

Type IntentOptional AttributesName
real(kind=RP), intent(in), DIMENSION(3,3):: A
real(kind=RP), intent(out), DIMENSION(3):: W(3)