DIAG

Constructs a square diagonal matrix.

Function Return Value

Square diagonal matrix of rank-2 if A is rank-1 or rank-3 array if A is rank-2. (Output)

Required Argument

A — This is a rank-1 or rank-2 array of type real, double, complex, or double complex, containing the diagonal elements. The output is a rank-2 or rank-3 array, respectively. (Input)

FORTRAN 90 Interface

DIAG (A)

Description

Constructs a square diagonal matrix from a rank-1 array or several diagonal matrices from a rank-2 array. The dimension of the matrix is the value of the size of the rank-1 array.

The use of DIAG may be obviated by observing that the defined operations C = diag(x) .x. A or D = B .x. diag(x) are respectively the array operations C = spread(x, DIM=1, NCOPIES=size(A,1))*A, and D = B*spread(x,DIM=2,NCOPIES=size(B,2)). These array products are not as easy to read as the defined operations using DIAG and matrix multiply, but their use results in a more efficient code.

Examples

Dense Matrix Example (operator_ex13.f90)

use linear_operators

implicit none

! This is the equivalent of Example 1 for LIN_SOL_SVD using operators

! and functions.

integer, parameter :: m=128, n=32

real(kind(1d0)) :: one=1d0, err

real(kind(1d0)) A(m,n), b(m), x(n), U(m,m), V(n,n), S(n), g(m)

! Generate a random matrix and right-hand side.

A = rand(A); b = rand(b)

! Compute the least-squares solution matrix of Ax=b.

S = SVD(A, U = U, V = V)

g = U .tx. b; x = V .x. diag(one/S) .x. g(1:n)

! Check the results.

err = norm(A .tx. (b - (A .x. x)))/(norm(A)+norm(x))

if (err <= sqrt(epsilon(one))) then

write (*,*) 'Example 1 for LIN_SOL_SVD (operators) is correct.'

end if

end