Sum of elements below main diagonal of matrix: Difference between revisions

m
syntax highlighting fixup automation
(Java Solution)
m (syntax highlighting fixup automation)
Line 19:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F sumBelowDiagonal(m)
V result = 0
L(i) 1 .< m.len
Line 32:
[ 7, 1, 3, 9, 5]]
 
print(sumBelowDiagonal(m))</langsyntaxhighlight>
 
{{out}}
Line 40:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintMatrix(INT ARRAY m BYTE size)
BYTE x,y
INT v
Line 84:
sum=SumBelowDiagonal(m,5)
PrintF("Sum below diagonal is %I",sum)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_elements_below_main_diagonal_of_matrix.png Screenshot from Atari 8-bit computer]
Line 99:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Numerics.Generic_Real_Arrays;
 
Line 137:
Put (Sum, Exp => 0, Aft => 1);
New_Line;
end Sum_Below_Diagonals;</langsyntaxhighlight>
{{out}}<pre>Sum below diagonal: 69.0</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # sum the elements below the main diagonal of a matrix #
# returns the sum of the elements below the main diagonal #
# of m, m must be a square matrix #
Line 171:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 179:
=={{header|ALGOL W}}==
One of the rare occasions where the lack of lower/upper bound operators in Algol W actually simplifies things, assuming the programmer gets things right...
<langsyntaxhighlight lang="algolw">begin % sum the elements below the main diagonal of a matrix %
% returns the sum of the elements below the main diagonal %
% of m, m must have bounds lb :: ub, lb :: ub %
Line 203:
write( i_w := 1, lowerSum( m, 1, 5 ) )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 211:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">sum_below_diagonal ← +/(∊⊢×(>/¨⍳∘⍴))</langsyntaxhighlight>
{{out}}
<pre> matrix ← 5 5⍴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
Line 218:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">matrx :=[[1,3,7,8,10]
,[2,4,16,14,4]
,[3,1,9,18,11]
Line 234:
. "`nsum below diagonal = " sumB
. "`nsum on diagonal = " sumD
. "`nsum all = " sumAll</langsyntaxhighlight>
{{out}}
<pre>sum above diagonal = 111
Line 242:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_ELEMENTS_BELOW_MAIN_DIAGONAL_OF_MATRIX.AWK
BEGIN {
Line 279:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 291:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">arraybase 1
dim 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}}
ind = diag[?,]
Line 304:
 
print "Sum of elements below main diagonal of matrix is "; sumDiag
end</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Dim As Integer diag(1 To 5, 1 To 5) = { _
{ 1, 3, 7, 8,10}, _
{ 2, 4,16,14, 4}, _
Line 324:
 
Print "Sum of elements below main diagonal of matrix is"; sumDiag
Sleep</langsyntaxhighlight>
{{out}}
<pre>Sum of elements below main diagonal of matrix is 69</pre>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">10 DATA 1,3,7,8,10
20 DATA 2,4,16,14,4
30 DATA 3,1,9,18,11
Line 340:
100 NEXT COL
110 NEXT ROW
120 PRINT SUM</langsyntaxhighlight>
{{out}}<pre>69</pre>
==={{header|QBasic}}===
Line 346:
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DEFINT A-Z
 
DIM diag(1 TO 5, 1 TO 5)
Line 373:
DATA 3, 1, 9,18,11
DATA 12,14,17,18,20
DATA 7, 1, 3, 9, 5</langsyntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DIM diag(5, 5)
LET lenDiag = UBOUND(diag, 1)
LET ind = lenDiag
Line 402:
 
PRINT "Sum of elements below main diagonal of matrix:"; sumDiag
END</langsyntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim diag(5, 5)
lenDiag = arraysize(diag(),1)
ind = lenDiag
Line 431:
data 3, 1, 9,18,11
data 12,14,17,18,20
data 7, 1, 3, 9, 5</langsyntaxhighlight>
 
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">SumBelowDiagonal ← +´∘⥊⊢×(>⌜´)∘(↕¨≢)
 
matrix ← >⟨⟨ 1, 3, 7, 8,10⟩,
Line 443:
⟨ 7, 1, 3, 9, 5⟩⟩
 
SumBelowDiagonal matrix</langsyntaxhighlight>
{{out}}
<pre>69</pre>
Line 449:
=={{header|C}}==
Interactive program which reads the matrix from a file :
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 513:
return 0;
}
</syntaxhighlight>
</lang>
 
Input Data file, first row specifies rows and columns :
Line 543:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 566:
std::cout << sum_below_diagonal(matrix) << std::endl;
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>69</pre>
Line 578:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">=LAMBDA(isUpper,
LAMBDA(matrix,
LET(
Line 602:
)
)
)</langsyntaxhighlight>
 
{{Out}}
Line 783:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Sum below leading diagnal. Nigel Galloway: July 21st., 2021
let _,n=[[ 1; 3; 7; 8;10];
Line 790:
[12;14;17;18;20];
[ 7; 1; 3; 9; 5]]|>List.fold(fun(n,g) i->let i,_=i|>List.splitAt n in (n+1,g+(i|>List.sum)))(0,0) in printfn "%d" n
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 798:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: kernel math math.matrices prettyprint sequences ;
 
: sum-below-diagonal ( matrix -- sum )
Line 810:
{ 12 14 17 18 20 }
{ 7 1 3 9 5 }
} sum-below-diagonal .</langsyntaxhighlight>
{{out}}
<pre>
Line 817:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 842:
}
fmt.Println("Sum of elements below main diagonal is", sum)
}</langsyntaxhighlight>
 
{{out}}
Line 852:
Defining both upper and lower triangle of a square matrix:
 
<langsyntaxhighlight lang="haskell">----------------- UPPER OR LOWER TRIANGLE ----------------
 
matrixTriangle :: Bool -> [[a]] -> Either String [[a]]
Line 890:
]
)
["Lower", "Upper"]</langsyntaxhighlight>
{{Out}}
<pre>Lower triangle:
Line 898:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">sum_below_diagonal =: [:+/@,[*>/~@i.@#</langsyntaxhighlight>
{{out}}
<pre> mat
Line 910:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public static void main(String[] args) {
int[][] matrix = {{1, 3, 7, 8, 10},
{2, 4, 16, 14, 4},
Line 923:
}
System.out.println(sum);
}</langsyntaxhighlight>
{{Out}}
<pre>69</pre>
Line 931:
Defining the lower triangle of a square matrix.
 
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,011:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>69</pre>
Line 1,018:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
def add(s): reduce s as $x (null; . + $x);
 
Line 1,024:
def sum_below_diagonal:
add( range(0;length) as $i | .[$i][:$i][] ) ;
</syntaxhighlight>
</lang>
The task:
<langsyntaxhighlight lang="jq"> [[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]]
| sum_below_diagonal</langsyntaxhighlight>
{{out}}
<pre>
Line 1,042:
the components of the matrix A to the left and below the main diagonal. tril(A, -1) returns the lower triangular
elements of A excluding the main diagonal. The excluded elements of the matrix are set to 0.
<langsyntaxhighlight lang="julia">using LinearAlgebra
 
A = [ 1 3 7 8 10;
Line 1,055:
 
@show sum(tril(A, -1)) # 69
</langsyntaxhighlight>{{out}}<pre>
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]
Line 1,062:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">m = {{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}};
Total[LowerTriangularize[m, -1], 2]</langsyntaxhighlight>
{{out}}
<pre>69</pre>
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
% Sum below leading diagnal. Nigel Galloway: July 22nd., 2021
array [1..5,1..5] of int: N=[|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|];
int: res=sum(n,g in 1..5 where n>g)(N[n,g]);
output([show(res)])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,083:
We use a generic definition for the square matrix type. The compiler insures that the matrix we provide is actually square.
 
<langsyntaxhighlight Nimlang="nim">type SquareMatrix[T: SomeNumber; N: static Positive] = array[N, array[N, T]]
 
func sumBelowDiagonal[T, N](m: SquareMatrix[T, N]): T =
Line 1,096:
[ 7, 1, 3, 9, 5]]
 
echo sumBelowDiagonal(M)</langsyntaxhighlight>
 
{{out}}
Line 1,102:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,116:
 
my $lowersum = sum map @{ $matrix->[$_] }[0 .. $_ - 1], 1 .. $#$matrix;
print "lower sum = $lowersum\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 1,123:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">M</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
Line 1,139:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">res</span>
<!--</langsyntaxhighlight>-->
You could of course start row from 2 and get the same result, for row==1 the col loop iterates zero times.<br>
Without the checks for square M expect (when not square) wrong/partial answers for height<=width+1, and (still human readable) runtime crashes for height>width+1.
Line 1,147:
</pre>
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
trap: procedure options (main); /* 17 December 2021 */
declare n fixed binary;
Line 1,168:
end;
end trap;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,183:
=={{header|PL/M}}==
This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator/clone.
<langsyntaxhighlight lang="pli">100H: /* SUM THE ELEMENTS BELOW THE MAIN DIAGONAL OF A MATRIX */
 
/* CP/M BDOS SYSTEM CALL, IGNORE THE RETURN VALUE */
Line 1,236:
CALL PR$NUMBER( LOWER$SUM( .T, 5 ) );
 
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 1,243:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from numpy import array, tril, sum
 
A = [[1,3,7,8,10],
Line 1,251:
[7,1,3,9,5]]
 
print(sum(tril(A, -1))) # 69</langsyntaxhighlight>
 
 
Or, defining the lower triangle for ourselves:
 
<langsyntaxhighlight lang="python">'''Lower triangle of a matrix'''
 
from itertools import chain, islice
Line 1,309:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>69</pre>
Line 1,315:
=={{header|R}}==
R has lots of native matrix support, so this is trivial.
<langsyntaxhighlight lang="rsplus">mat <- rbind(c(1,3,7,8,10),
c(2,4,16,14,4),
c(3,1,9,18,11),
c(12,14,17,18,20),
c(7,1,3,9,5))
print(sum(mat[lower.tri(mat)]))</langsyntaxhighlight>
{{out}}
<pre>[1] 69</pre>
Line 1,326:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>sub lower-triangle-sum (@matrix) { sum flat (1..@matrix).map( { @matrix[^$_]»[^($_-1)] } )»[*-1] }
 
say lower-triangle-sum
Line 1,335:
[ 12, 14, 17, 18, 20 ],
[ 7, 1, 3, 9, 5 ]
];</langsyntaxhighlight>
{{out}}
<pre>69</pre>
Line 1,341:
=={{header|REXX}}==
=== version 1 ===
<langsyntaxhighlight lang="rexx">/* REXX */
ml ='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'
Do i=1 To 5
Line 1,364:
End
End
Say 'Sum below main diagonal:' sum</langsyntaxhighlight>
<pre>
1 3 7 8 10
Line 1,376:
This REXX version makes no assumption about the size of the matrix, &nbsp; and it determines the maximum width of any
<br>matrix element &nbsp; (instead of assuming a width that might not properly show the true value of an element).
<langsyntaxhighlight lang="rexx">/*REXX pgm finds & shows the sum of elements below the main diagonal of a square 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'; #= words($)
do siz=1 while siz*siz<#; end /*determine the size of the matrix. */
Line 1,391:
say _ /*display a row of the matrix to term. */
end /*c*/
say 'sum of elements below main diagonal is: ' s /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 1,403:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Sum of elements below main diagonal of matrix:" + nl
Line 1,425:
see "" + sumDiag + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,435:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">arr = [
[ 1, 3, 7, 8, 10],
[ 2, 4, 16, 14, 4],
Line 1,443:
]
p arr.each_with_index.sum {|row, x| row[0, x].sum}
</syntaxhighlight>
</lang>
{{out}}
<pre>69
Line 1,449:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,468:
end for;
writeln(sum);
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,476:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var m = [
[ 1, 3, 7, 8, 10],
[ 2, 4, 16, 14, 4],
Line 1,490:
}
}
System.print("Sum of elements below main diagonal is %(sum).")</langsyntaxhighlight>
 
{{out}}
Line 1,498:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int Mat, X, Y, Sum;
[Mat:= [[1,3,7,8,10],
[2,4,16,14,4],
Line 1,510:
Sum:= Sum + Mat(Y,X);
IntOut(0, Sum);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits