From e4fc513c7e81361e57cc8d3bfdf7b98373f74d40 Mon Sep 17 00:00:00 2001 From: AJ Kelly Date: Thu, 5 Aug 2010 23:23:47 -0500 Subject: [PATCH] table sum, average, and standard deviation functions from shakesoda. (I also try to attempt to alias table.slice to tableslice and table.shuffle to tableshuffle) --- Themes/_fallback/Scripts/02 Utilities.lua | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Themes/_fallback/Scripts/02 Utilities.lua b/Themes/_fallback/Scripts/02 Utilities.lua index 1616bdca24..31f5384db0 100644 --- a/Themes/_fallback/Scripts/02 Utilities.lua +++ b/Themes/_fallback/Scripts/02 Utilities.lua @@ -90,6 +90,7 @@ function tableshuffle( t ) end return ret end +table.shuffle = tableshuffle function tableslice( t, num ) local ret = { } @@ -98,6 +99,30 @@ function tableslice( t, num ) end return ret end +table.slice = tableslice + +-- add together the contents of a table +function table.sum( t ) + local sum = 0 + for i=1,#t do + sum = sum + t[i] + end + return sum +end + +-- average of all table values +function table.average( t ) + return table.sum( t ) / #t +end + +-- furthest value from the average of a given table +function table.deviation( t ) + local offset = math.abs(table.average(t)) + for i=1,#t do + offset = math.max(math.abs(t[i]), offset) + end + return offset +end function round(val, decimal) if (decimal) then