Diag
This function creates a diagonal matrix.
The generic syntax is given below.
Diag syntax
diagMat = Diag(d)
- where,
d
denotes the main diagonal of thediagMat
. d
can be a vector ofReal32, Real64, Int8, Int16, Int32, Int64
- The result is always
n x n
matrix ofREAL(DFP)
.
- ܀ Example 1
- ܀ Example 2
- ↢ Close
In this example we show the usage of Diag function.
We will form diagonal matrix in this example from REAL(DFP)
vector.
program main
use easifemBase
real( DFP ), allocatable :: mat(:, :)
real( DFP ), allocatable :: d(:)
call reallocate(d, 5)
call random_number(d)
d = d * 10
call display( MdEncode(d), "d = " )
mat = Diag(d)
call display( MdEncode(mat), "mat = " )
end program
Results
d =
8.2717 | 3.502 | 0.39168 | 0.51942 | 2.6892 |
mat =
8.2717 | 0 | 0 | 0 | 0 |
0 | 3.502 | 0 | 0 | 0 |
0 | 0 | 0.39168 | 0 | 0 |
0 | 0 | 0 | 0.51942 | 0 |
0 | 0 | 0 | 0 | 2.6892 |
In this example we show the usage of Diag function. We will form diagonal matrix in this example from REAL(Real32)
vector.
header
program main
use easifemBase
variables
real( DFP ), allocatable :: mat(:, :)
real( Real32 ), allocatable :: d(:)
form random diagonal
call reallocate(d, 5)
call random_number(d)
d = d * 10
call display( MdEncode(d), "d = " )
diagonal matrix
mat = Diag(d)
call display( MdEncode(mat), "mat = " )
cleanup
end program
Results
d =
5.165 | 2.5622 | 1.1153 | 1.0442 | 5.2306 |
mat =
5.165 | 0 | 0 | 0 | 0 |
0 | 2.5622 | 0 | 0 | 0 |
0 | 0 | 1.1153 | 0 | 0 |
0 | 0 | 0 | 1.0442 | 0 |
0 | 0 | 0 | 0 | 5.2306 |
info
We can also use Diag
to access the diagonal of the matrix. The syntaxt is given below:
d = Diag(mat, diagNo)
where,
mat
is a two-dimensional square matrixdiagNo
is an integer, which stands for the diagonaldiagNo == 0
denotes the main diagonaldiagNo > 0
denotes the super-diagonaldiagNo < 0
denotes the sub diagonal
- ܀ See example
- ↢ Close
In this example we show the usage of Diag function.
We will extract diagonals from a square matrix.
program main
use easifemBase
real( DFP ), allocatable :: mat(:, :)
real( DFP ), allocatable :: d(:)
call reallocate(mat, 5, 5)
call random_number(mat)
call display( MdEncode(mat), "mat = " )
mat =
0.68758 | 3.17016E-02 | 0.59785 | 4.6886E-02 | 0.61175 |
0.48573 | 0.67486 | 0.73655 | 0.52901 | 0.60769 |
0.17241 | 0.52752 | 0.79169 | 0.21098 | 0.94547 |
0.21998 | 0.67235 | 0.52645 | 0.32917 | 0.3067 |
0.50651 | 5.82701E-02 | 4.87607E-02 | 0.38279 | 0.11833 |
getting the main diagonal
d = Diag(mat, 0)
call display( MdEncode(d), "main diagonal = " )
main diagonal =
0.68758 | 0.67486 | 0.79169 | 0.32917 | 0.11833 |
getting the first super diagonal
d = Diag(mat, 1)
call display( MdEncode(d), "super diagonal 1= " )
super diagonal 1=
3.17016E-02 | 0.73655 | 0.21098 | 0.3067 |
getting the second super diagonal
d = Diag(mat, 2)
call display( MdEncode(d), "super diagonal 2= " )
super diagonal 2=
0.59785 | 0.52901 | 0.94547 |
getting the first sub diagonal
d = Diag(mat, -1)
call display( MdEncode(d), "subdiagonal 1= " )
subdiagonal 1=
0.48573 | 0.52752 | 0.52645 | 0.38279 |
getting the second subdiagonal
d = Diag(mat, -2)
call display( MdEncode(d), "subdiagonal 2= " )
subdiagonal 2=
0.17241 | 0.67235 | 4.87607E-02 |
cleanup
end program