Tril
This function returns the lower triangle part of a matrix.
Interface 1
- ܀ Interface
- ️܀ See example
- ↢
MODULE PURE FUNCTION Tril(A, diagNo) RESULT(ans)
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64), INTENT(IN) :: A(:, :)
INTEGER(I4B), OPTIONAL, INTENT(IN) :: diagNo
!! diagonal number, default = 0
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64) :: ans(SIZE(A, 1), SIZE(A, 2))
END FUNCTION Tril
program main
use easifemBase
implicit none
Testing for Square matrix
BLOCK
REAL( Real32 ) :: A( 4, 4 )
CALL RANDOM_NUMBER(A); A = A*10
CALl Display(MdEncode(A), "A = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A,diagNo=0)), "diagNo=0, = " // CHAR_LF // CHAR_LF )
CALL Display(MdEncode(Tril(A,diagNo=1)), "diagNo=1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A,diagNo=2)), "diagNo=2, = " // CHAR_LF // CHAR_LF )
END BLOCK
See results
A =
7.2777 | 8.6925 | 3.2953 | 9.1375 |
9.5344 | 1.8291 | 3.1941 | 2.7707 |
0.72276 | 6.2783 | 8.5105 | 1.4668 |
0.67118 | 4.1519 | 4.7936 | 4.623 |
diagNo=0, =
7.2777 | 0 | 0 | 0 |
9.5344 | 1.8291 | 0 | 0 |
0.72276 | 6.2783 | 8.5105 | 0 |
0.67118 | 4.1519 | 4.7936 | 4.623 |
diagNo=1, =
7.2777 | 8.6925 | 0 | 0 |
9.5344 | 1.8291 | 3.1941 | 0 |
0.72276 | 6.2783 | 8.5105 | 1.4668 |
0.67118 | 4.1519 | 4.7936 | 4.623 |
diagNo=2, =
7.2777 | 8.6925 | 3.2953 | 0 |
9.5344 | 1.8291 | 3.1941 | 2.7707 |
0.72276 | 6.2783 | 8.5105 | 1.4668 |
0.67118 | 4.1519 | 4.7936 | 4.623 |
Let's get the upper triangular form above Subdiagonals.
BLOCK
REAL( Real32 ) :: A( 4, 4 )
CALL RANDOM_NUMBER(A); A = A*10
CALl Display(MdEncode(A), "A = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A,diagNo=-1)), "diagNo=-1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A,diagNo=-2)), "diagNo=-2, = " // CHAR_LF // CHAR_LF )
END BLOCK
See results
A =
3.4843 | 0.21881 | 7.0136 | 8.6618 |
1.1204 | 4.0021 | 8.4773 | 3.9924 |
2.6185 | 8.0988 | 6.4968 | 2.7172 |
5.5955 | 2.0122 | 7.5929 | 4.431 |
(3,3) diagNo=-1, =
0 | 0 | 0 | 0 |
1.1204 | 0 | 0 | 0 |
2.6185 | 8.0988 | 0 | 0 |
5.5955 | 2.0122 | 7.5929 | 0 |
(3,3) diagNo=-2, =
0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 |
2.6185 | 0 | 0 | 0 |
5.5955 | 2.0122 | 0 | 0 |
end program main
Interface 2
- ܀ Interface
- ️܀ See example
- ↢
MODULE PURE FUNCTION Tril_12(A, flate, diagNo) RESULT(ans)
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64), INTENT(IN) :: A(:, :)
LOGICAL(LGT), INTENT(IN) :: flate
!! This variable is only for creating unique interface
INTEGER(I4B), OPTIONAL, INTENT(IN) :: diagNo
!! diagonal number, default = 0
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64), ALLOCATABLE :: ans(:)
END FUNCTION Tril_12
In this case the lower triangular part is returned in flat form. That is, the first n
entries of ans
denotes the main diagonal. Then next n-1
entries denotes first subdiagonal, and so on.
program main
use easifemBase
implicit none
REAL( Real32 ) :: A( 4, 4 )
CALL RANDOM_NUMBER(A); A = A*10
CALl Display(MdEncode(A), "A = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A, flate=.TRUE., diagNo=0)), "diagNo=0, = " // CHAR_LF // CHAR_LF )
CALL Display(MdEncode(Tril(A, flate=.TRUE., diagNo=1)), "diagNo=1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Tril(A, flate=.TRUE., diagNo=2)), "diagNo=2, = " // CHAR_LF // CHAR_LF )
end program main
See results
A =
1.4576 | 3.1887 | 4.4724 | 2.3238 |
3.5987 | 4.2895 | 2.7841 | 3.4316 |
2.7368 | 8.7299 | 5.3137 | 3.5759 |
7.6728 | 0.5108 | 3.7975 | 1.3629 |
diagNo=0, =
1.4576 | 4.2895 | 5.3137 | 1.3629 | 3.5987 | 8.7299 | 3.7975 | 2.7368 | 0.5108 | 7.6728 |
diagNo=1, =
3.1887 | 2.7841 | 3.5759 | 1.4576 | 4.2895 | 5.3137 | 1.3629 | 3.5987 | 8.7299 | 3.7975 | 2.7368 | 0.5108 | 7.6728 |
diagNo=2, =
4.4724 | 3.4316 | 3.1887 | 2.7841 | 3.5759 | 1.4576 | 4.2895 | 5.3137 | 1.3629 | 3.5987 | 8.7299 | 3.7975 | 2.7368 | 0.5108 | 7.6728 |