Sum of elements below main diagonal of matrix: Difference between revisions
(julia example) |
m (spacing) |
||
Line 13: | Line 13: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
The |
The tril function is part of Julia's built-in LinearAlgebra package. tril(A) includes the main diagonal and |
||
the components of the matrix |
the components of the matrix A to the right and above the main diagonal. tril(A, -1) returns the lower triangular |
||
elements of |
elements of A, which excludes the main diagonal. |
||
<lang julia>using LinearAlgebra |
<lang julia>using LinearAlgebra |
||
Line 22: | Line 22: | ||
3 1 9 18 11; |
3 1 9 18 11; |
||
12 14 17 18 20; |
12 14 17 18 20; |
||
7 1 3 9 5] |
7 1 3 9 5 ] |
||
@show tril(A) |
@show tril(A) |
Revision as of 06:45, 20 July 2021
- Task
- Find sum of elements below main diagonal of matrix.
The matrix should be square matrix.
Matrix =
[[1,3,7,8,10], [2,4,16,14,4], [3,1,9,18,11], [12,14,17,18,20], [7,1,3,9,5]]
Julia
The tril function is part of Julia's built-in LinearAlgebra package. tril(A) includes the main diagonal and the components of the matrix A to the right and above the main diagonal. tril(A, -1) returns the lower triangular elements of A, which excludes the main diagonal. <lang julia>using LinearAlgebra
A = [ 1 3 7 8 10;
2 4 16 14 4; 3 1 9 18 11; 12 14 17 18 20; 7 1 3 9 5 ]
@show tril(A)
@show tril(A, -1)
@show sum(tril(A, -1)) # 69
</lang>
- Output:
tril(A) = [1 0 0 0 0; 2 4 0 0 0; 3 1 9 0 0; 12 14 17 18 0; 7 1 3 9 5] tril(A, -1) = [0 0 0 0 0; 2 0 0 0 0; 3 1 0 0 0; 12 14 17 0 0; 7 1 3 9 0] sum(tril(A, -1)) = 69
Ring
<lang ring> see "working..." + nl see "Sum of elements below main diagonal of matrix:" + nl diag = [[1,3,7,8,10],
[2,4,16,14,4], [3,1,9,18,11], [12,14,17,18,20], [7,1,3,9,5]]
lenDiag = len(diag) ind = lenDiag sumDiag = 0
for n=1 to lenDiag
for m=1 to lenDiag-ind sumDiag += diag[n][m] next ind--
next
see "" + sumDiag + nl see "done..." + nl </lang>
- Output:
working... Sum of elements below main diagonal of matrix: 69 done...