DiagIndx
DiagIndx
returns the indices of elements along a diagonal of a square or rectangle matrix. The indices are returned a two dimensional array of integers.
This array has two columns. First column stands for the row index and second column stands for column index.
- ܀ Interface
- ️܀ Example
- ↢ Close
Interface
INTERFACE
MODULE PURE FUNCTION DiagIndx(m, n, diagNo) RESULT(ans)
INTEGER(I4B), INTENT(IN) :: m
!! number of rows in matrix
INTEGER(I4B), INTENT(IN) :: n
!! number of columns in a matrix
INTEGER(I4B), INTENT(IN) :: diagNo
!! diagonal number
INTEGER(I4B), ALLOCATABLE :: ans(:, :)
!! size of diagonal
END FUNCTION DiagIndx
END INTERFACE
In this example we show the usage of DiagIndx routine.
program main
use easifemBase
call Display( DiagIndx(4, 4, 0), "(4,4), main diagonal = " )
call Display( DiagIndx(4, 4, 1), "(4,4), first superdiagonal = " )
call Display( DiagIndx(4, 4, 2), "(4,4), second superdiagonal = " )
Results:
(4,4), main diagonal =
-----------------------
1 1
2 2
3 3
4 4
(4,4), first superdiagonal =
-----------------------------
1 2
2 3
3 4
(4,4), second superdiagonal =
------------------------------
1 3
2 4
call Display( DiagIndx(4, 4, -1), "(4,4), first subdiagonal = " )
call Display( DiagIndx(4, 4, -2), "(4,4), second subdiagonal = " )
Results:
(4,4), first subdiagonal =
---------------------------
2 1
3 2
4 3
(4,4), second subdiagonal =
----------------------------
3 1
4 2
note
Let us now test DiagIndx
for RECTANGLE matrix.
call Display( DiagIndx(4, 7, 0), "(4, 7), main diagonal = " )
call Display( DiagIndx(4, 7, 1), "(4, 7), first superdiagonal = " )
call Display( DiagIndx(4, 7, 2), "(4, 7), second superdiagonal = " )
call Display( DiagIndx(4, 7, 3), "(4,7), third superdiagonal = " )
call Display( DiagIndx(4, 7, 4), "(4,7), fourth superdiagonal = " )
call Display( DiagIndx(4, 7, 5), "(4,7), fifth superdiagonal = " )
call Display( DiagIndx(4, 7, 6), "(4,7), sixth superdiagonal = " )
Results:
results
(4, 7), main diagonal =
------------------------
1 1
2 2
3 3
4 4
(4, 7), first superdiagonal =
------------------------------
1 2
2 3
3 4
4 5
(4, 7), second superdiagonal =
-------------------------------
1 3
2 4
3 5
4 6
(4,7), third superdiagonal =
-----------------------------
1 4
2 5
3 6
4 7
(4,7), fourth superdiagonal =
------------------------------
1 5
2 6
3 7
(4,7), fifth superdiagonal =
-----------------------------
1 6
2 7
(4,7), sixth superdiagonal =
-----------------------------
1 7
(4,7) 1:3 subdiagonal
call Display( DiagIndx(4, 7, -1), "(4,7), first subdiagonal = " )
call Display( DiagIndx(4, 7, -2), "(4,7), second subdiagonal = " )
call Display( DiagIndx(4, 7, -3), "(4,7), thrid subdiagonal = " )
results
(4,7), first subdiagonal =
---------------------------
2 1
3 2
4 3
(4,7), second subdiagonal =
----------------------------
3 1
4 2
(4,7), thrid subdiagonal =
---------------------------
4 1
end program