Triu
This function returns the upper triangle part of a matrix.
Interface 1
- ܀ Interface
- ️܀ See example
- ↢
MODULE PURE FUNCTION Triu(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
!! diagNo>0 means super diagonal
!! diagNo<0 means subdiagonal
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64) :: ans(SIZE(A, 1), SIZE(A, 2))
END FUNCTION Triu
program main
use easifemBase
implicit none
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(Triu(A,diagNo=0)), "(3,3) diagNo=0, = " // CHAR_LF // CHAR_LF )
CALL Display(MdEncode(Triu(A,diagNo=1)), "(3,3) diagNo=1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Triu(A,diagNo=2)), "(3,3) diagNo=2, = " // CHAR_LF // CHAR_LF )
END BLOCK
See results
A =
7.4672 | 7.2656 | 2.6319 | 7.4388 |
3.3308 | 4.4818 | 8.0987 | 7.1544 |
5.7006 | 8.3514 | 2.9005 | 6.3054 |
2.1553 | 7.2467 | 4.8848 | 6.7111 |
(3,3) diagNo=0, =
7.4672 | 7.2656 | 2.6319 | 7.4388 |
0 | 4.4818 | 8.0987 | 7.1544 |
0 | 0 | 2.9005 | 6.3054 |
0 | 0 | 0 | 6.7111 |
(3,3) diagNo=1, =
0 | 7.2656 | 2.6319 | 7.4388 |
0 | 0 | 8.0987 | 7.1544 |
0 | 0 | 0 | 6.3054 |
0 | 0 | 0 | 0 |
(3,3) diagNo=2, =
0 | 0 | 2.6319 | 7.4388 |
0 | 0 | 0 | 7.1544 |
0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 |
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(Triu(A,diagNo=-1)), "(3,3) diagNo=-1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Triu(A,diagNo=-2)), "(3,3) diagNo=-2, = " // CHAR_LF // CHAR_LF )
END BLOCK
See results
(3,3) diagNo=-1, =
3.5096 | 8.8744 | 1.7549 | 1.5576 |
8.661 | 7.3655 | 2.6134 | 9.767 |
0 | 9.788 | 1.4703 | 3.3602 |
0 | 0 | 8.0666 | 1.1451 |
(3,3) diagNo=-2, =
3.5096 | 8.8744 | 1.7549 | 1.5576 |
8.661 | 7.3655 | 2.6134 | 9.767 |
6.0823 | 9.788 | 1.4703 | 3.3602 |
0 | 4.1367 | 8.0666 | 1.1451 |
end program main
Interface 2
- ܀ Interface
- ️܀ See example
- ↢
MODULE PURE FUNCTION Triu_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
!! diagNo>0 means super diagonal
!! diagNo<0 means subdiagonal
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64), ALLOCATABLE :: ans(:)
END FUNCTION Triu_12
In this case the upper 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 super-diagonal, 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(Triu(A, flate=.TRUE., diagNo=0)), "diagNo=0, = " // CHAR_LF // CHAR_LF )
CALL Display(MdEncode(Triu(A, flate=.TRUE., diagNo=1)), "diagNo=1, = " // CHAR_LF // CHAR_LF)
CALL Display(MdEncode(Triu(A, flate=.TRUE., diagNo=2)), "diagNo=2, = " // CHAR_LF // CHAR_LF )
end program main
See results
A =
0.99172 | 0.2666 | 4.3266 | 5.4603 |
2.5813 | 0.14775 | 1.7741 | 5.3106 |
1.9852 | 5.8142 | 4.8581 | 9.2279 |
1.4559 | 7.9147 | 7.84 | 9.2751 |
diagNo=0, =
0.99172 | 0.14775 | 4.8581 | 9.2751 | 0.2666 | 1.7741 | 9.2279 | 4.3266 | 5.3106 | 5.4603 |
diagNo=1, =
0.2666 | 1.7741 | 9.2279 | 4.3266 | 5.3106 | 5.4603 |
diagNo=2, =
4.3266 | 5.3106 | 5.4603 |