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

(Added Algol W)
Line 289:
[[0 0 0 0 1] [0 0 0 1 0] [0 0 1 0 0] [0 1 0 0 0]] => Centroid: [0 0.25 0.25 0.25 0.25]
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
With a trivial change to the last line (the one using string interpolation), the following also works with jaq, the Rust implementation of jq.
<syntaxhighlight lang="jq">
# Input: an array of points of the same dimension (i.e. numeric arrays of the same length)
def centroid:
length as $n
| if ($n == 0) then "centroid: list must contain at least one point." | error else . end
| (.[0]|length) as $d
| if any( .[]; length != $d )
then "centroid: points must all have the same dimension." | error
else .
end
| transpose
| map( add / $n ) ;
 
def points: [
[ [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] ]
];
 
points[]
| "\(.) => Centroid: \(centroid)"
</syntaxhighlight>
{{output}}
Essentially as for [[#Wren|Wren]].
 
=={{header|Julia}}==
2,465

edits