specialmatrices_tridiagonal Module


Uses

    • stdlib_linalg_constants

Interfaces

public interface Tridiagonal

This interface provides different methods to construct a Tridiagonal matrix. Only the non-zero elements of are stored, i.e.

Syntax

  • Construct a Tridiagonal matrix filled with zeros:
   integer, parameter :: n = 100
   type(Tridiagonal) :: A

   A = Tridiagonal(n)
  • Construct a Tridiagonal matrix from rank-1 arrays:
   integer, parameter :: n
   real(dp), allocatable :: dl(:), dv(:), du(:)
   type(Tridiagonal) :: A
   integer :: i

   dl = [(i, i=1, n-1)]; dv = [(2*i, i=1, n)]; du = [(3*i, i=1, n)]
   A = Tridiagonal(dl, dv, du)
  • Construct a Tridiagonal matrix with constant diagonals:
   integer, parameter :: n
   real(dp), parameter :: a = 1.0_dp, b = 1.0_dp, c = 2.0_dp
   type(Tridiagonal) :: A

   A = Tridiagonal(a, b, c, n)

Note

Only double precision is currently supported for this matrix type.

  • private pure module function construct(dl, dv, du) result(A)

    Construct a Tridiagonal matrix from the rank-1 arrays dl, dv and du.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: dl(:)

    Tridiagonal elements of the matrix.

    real(kind=dp), intent(in) :: dv(:)

    Tridiagonal elements of the matrix.

    real(kind=dp), intent(in) :: du(:)

    Tridiagonal elements of the matrix.

    Return Value type(Tridiagonal)

    Tridiagonal matrix.

  • private pure module function construct_constant(dl, dv, du, n) result(A)

    Construct a Tridiagonal matrix with constant diagonal elements.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: dl

    Tridiagonal elements of the matrix.

    real(kind=dp), intent(in) :: dv

    Tridiagonal elements of the matrix.

    real(kind=dp), intent(in) :: du

    Tridiagonal elements of the matrix.

    integer(kind=ilp), intent(in) :: n

    Dimension of the matrix.

    Return Value type(Tridiagonal)

    Tridiagonal matrix.

  • private pure module function initialize(n) result(A)

    Construct a Tridiagonal matrix filled with zeros.

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=ilp), intent(in) :: n

    Dimension of the matrix.

    Return Value type(Tridiagonal)

    Tridiagonal matrix.

public interface dense

This interface provides methods to convert a Tridiagonal matrix to a regular rank-2 array.

Syntax

   B = dense(A)

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • B : Rank-2 array representation of the matrix .

  • private module function dense_rdp(A) result(B)

    Utility function to convert a Tridiagonal matrix to a rank-2 array.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input diagonal matrix.

    Return Value real(kind=dp), allocatable, (:,:)

    Output dense rank-2 array.

public interface det

This interface overloads the det interface from stdlib_linag to compute the determinant where is of type Tridiagonal.

Syntax

   d = det(A)

Arguments

  • A : Matrix of Tridiagonal type. It is in an intent(in) argument.

  • d : Determinant of the matrix.

  • private pure module function det_rdp(A) result(d)

    Compute the determinant of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value real(kind=dp)

    Determinant of the matrix.

public interface eig

This interface overloads the eig interface from stdlib_linalg to compute the eigenvalues and eigenvectors of a real-valued matrix whose type is Tridiagonal.

Syntax

   call eig(A, lambda [, left] [, right])

Arguments

  • A : real-valued matrix of Tridiagonal. It is an intent(in) argument.

  • lambda : Rank-1 real array returning the eigenvalues of A in increasing order. It is an intent(out) argument.

  • left (optional) : complex rank-2 array of the same kind as A returning the left eigenvectors of A. It is an intent(out) argument.

  • right (optional) : complex rank-2 array of the same kind as A returning the right eigenvectors of A. It is an intent(out) argument.

Note

No specialized eigensolvers for generic Tridiagonal matrices exist in LAPACK. This routine thus falls back to wrapping the eig procedure from stdlib_linalg which uses *geev under the hood.

  • private module subroutine eig_rdp(A, lambda, left, right)

    Utility function to compute the eigenvalues and eigenvectors of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    complex(kind=dp), intent(out) :: lambda(:)

    Eigenvalues.

    complex(kind=dp), intent(out), optional :: left(:,:)

    Eigenvectors.

    complex(kind=dp), intent(out), optional :: right(:,:)

    Eigenvectors.

public interface eigvals

This interface overloads the eigvals interface from stdlib_linalg to compute the eigenvalues of a real-valued matrix whose type is Tridiagonal.

Syntax

   lambda = eigvals(A)

Arguments

  • A : real-valued matrix of Tridiagonal type. It is an intent(in) argument.

  • lambda : Vector of eigenvalues in increasing order.

  • private module function eigvals_rdp(A) result(lambda)

    Utility function to compute the eigenvalues of a real Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value complex(kind=dp), allocatable, (:)

    Eigenvalues.

public interface inv

  • private pure module function inv_rdp(A) result(B)

    Compute the inverse of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value real(kind=dp), allocatable, (:,:)

    Inverse of A.

public interface matmul

This interface overloads the Fortran intrinsic matmul for a Tridiagonal matrix, both for matrix-vector and matrix-matrix products. For a matrix-matrix product , only the matrix has to be a Tridiagonal matrix. Both and need to be standard Fortran rank-2 arrays. All the underlying functions are defined as pure.

Syntax

   y = matmul(A, x)
  • private module function spmv(A, x) result(y)

    Compute the matrix-vector product for a Tridiagonal matrix . Both x and y are rank-1 arrays with the same kind as A.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    real(kind=dp), intent(in), target :: x(:)

    Input vector.

    Return Value real(kind=dp), target, allocatable, (:)

    Output vector.

  • private pure module function spmvs(A, X) result(Y)

    Compute the matrix-matrix product for a Tridiagonal matrix and a dense matrix (rank-2 array). is also a rank-2 array with the same dimensions as .

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    real(kind=dp), intent(in) :: X(:,:)

    Input vectors.

    Return Value real(kind=dp), allocatable, (:,:)

    Output vectors.

public interface operator(*)

  • private pure module function scalar_multiplication_bis_rdp(A, alpha) result(B)

    Scalar multiplication with a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A
    real(kind=dp), intent(in) :: alpha

    Return Value type(Tridiagonal)

  • private pure module function scalar_multiplication_rdp(alpha, A) result(B)

    Scalar multiplication with a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: alpha
    type(Tridiagonal), intent(in) :: A

    Return Value type(Tridiagonal)

public interface shape

  • private pure module function shape_rdp(A) result(arr_shape)

    Return the shape of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value integer(kind=ilp), (2)

    Shape of the matrix.

public interface size

  • private pure module function size_rdp(A, dim) result(arr_size)

    Return the size of Tridiagonal matrix along a given dimension.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    integer(kind=ilp), intent(in), optional :: dim

    Queried dimension.

    Return Value integer(kind=ilp)

    Size of the matrix along the dimension dim.

public interface solve

This interface overloads the solve interface from stdlib_linalg for solving a linear system where is a Tridiagonal matrix. It also enables to solve a linear system with multiple right-hand sides.

Syntax

   x = solve(A, b [, refine])

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • b : Rank-1 or rank-2 array defining the right-hand side(s). It is an intent(in) argument.

  • refine (optional) : Logical switch to enable solution refinement. It is an intent(in) argument.

  • x : Solution of the linear system. It has the same type and shape as b.

  • private module function solve_multi_rhs(A, b, refine) result(x)

    Solve the linear system where is of type Tridiagonal and B a standard rank-2 array. The solution matrix X has the same dimensions and kind as B.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Coefficient matrix.

    real(kind=dp), intent(in) :: b(:,:)

    Right-hand side vectors.

    logical(kind=lk), intent(in), optional :: refine

    Whether iterative refined is used or not.

    Return Value real(kind=dp), allocatable, target, (:,:)

    Solution vectors.

  • private module function solve_single_rhs(A, b, refine) result(x)

    Solve the linear system where is of type Tridiagonal and b a standard rank-1 array. The solution vector x has the same dimension and kind as b.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Coefficient matrix.

    real(kind=dp), intent(in), target :: b(:)

    Right-hand side vector.

    logical(kind=lk), intent(in), optional :: refine

    Whether iterative refinement is used or not.

    Return Value real(kind=dp), allocatable, target, (:)

    Solution vector.

public interface svd

This interface overloads the svd interface from stdlib_linalg to compute the the singular value decomposition of a Tridiagonal matrix .

Syntax

   call svd(A, s [, u] [, vt])

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • s : Rank-1 array real array returning the singular values of A. It is an intent(out) argument.

  • u (optional) : Rank-2 array of the same kind as A returning the left singular vectors of A as columns. Its size should be [n, n]. It is an intent(out) argument.

  • vt (optional) : Rank-2 array of the same kind as A returning the right singular vectors of A as rows. Its size should be [n, n]. It is an intent(out) argument.

  • private module subroutine svd_rdp(A, s, u, vt)

    Compute the singular value decomposition of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    real(kind=dp), intent(out) :: s(:)

    Singular values in descending order.

    real(kind=dp), intent(out), optional :: u(:,:)

    Left singular vectors as columns.

    real(kind=dp), intent(out), optional :: vt(:,:)

    Right singular vectors as rows.

public interface svdvals

This interface overloads the svdvals interface from stdlib_linalg to compute the singular values of a Tridiagonal matrix .

Syntax

   s = svdvals(A)

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • s : Vector of singular values sorted in decreasing order.

  • private module function svdvals_rdp(A) result(s)

    Compute the singular values of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value real(kind=dp), allocatable, (:)

    Singular values in descending order.

public interface trace

This interface overloads the trace interface from stdlib_linalg to compute the trace of a matrix of type Tridiagonal.

Syntax

   tr = trace(A)

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • tr: Trace of the matrix.

  • private pure module function trace_rdp(A) result(tr)

    Compute the trace of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value real(kind=dp)

    Trace of the matrix.

public interface transpose

This interface overloads the Fortran intrinsic procedure to define the transpose operation for a Tridiagonal matrix.

Syntax

   B = transpose(A)

Arguments

  • A : Matrix of Tridiagonal type. It is an intent(in) argument.

  • B : Resulting transposed matrix. It is of the same type as A.

  • private pure module function transpose_rdp(A) result(B)

    Utility function to compute the transpose of a Tridiagonal matrix.

    Arguments

    Type IntentOptional Attributes Name
    type(Tridiagonal), intent(in) :: A

    Input matrix.

    Return Value type(Tridiagonal)

    Transpose of the matrix.


Derived Types

type, public ::  Tridiagonal

Base type used to define a Tridiagonal matrix of size [n, n] with diagonals given by rank-1 arrays dl (size n), dv (size n-1) and du (size n-1).

Constructor

This interface provides different methods to construct a Tridiagonal matrix. Only the non-zero elements of are stored, i.e.

Read more…
private pure, module function construct (dl, dv, du)

Construct a Tridiagonal matrix from the rank-1 arrays dl, dv and du.

private pure, module function construct_constant (dl, dv, du, n)

Construct a Tridiagonal matrix with constant diagonal elements.

private pure, module function initialize (n)

Construct a Tridiagonal matrix filled with zeros.