Centroid of a set of N-dimensional points: Difference between revisions

Added Sidef
(→‎{{header|Perl}}: Added alternative, direct calculation)
(Added Sidef)
 
(2 intermediate revisions by one other user not shown)
Line 643:
(-1.5 -1.475 5.475)
(0 0.25 0.25 0.25 0.25)</pre>
 
=={{header|Sidef}}==
 
<syntaxhighlight lang="ruby">func centroid(l) {
l.combine {|*a| a.sum } »/» l.len
}
 
[
[ [1], [2], [3] ],
[ [8, 2], [0, 0] ],
[ [5, 5, 0], [10, 10, 0] ],
[ [1, 3.1, 6.5], [-2, -5, 3.4], [-7, -4, 9], [2, 0, 3] ],
[ [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0] ],
].each {
say centroid(_)
}</syntaxhighlight>
 
{{out}}
<pre>
[2]
[4, 1]
[15/2, 15/2, 0]
[-3/2, -59/40, 219/40]
[0, 1/4, 1/4, 1/4, 1/4]
</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var centroid = Fn.new { |pts|
var n = pts.count
if (n == 0) Fiber.abort("List must contain at least one point.")
Line 686 ⟶ 711:
{{libheader|Wren-math}}
Or, more concise using library methods - output identical.
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./math" for Nums
 
2,747

edits