# AVSLib :: 'numeric' package :: 'round' module
# Copyright (c) 2005 George Zarkadas (gzarkadas@users.sourceforge.net)

# This program is free software; you can redistribute it and/or modify it under the terms of 
# the GNU General Public License as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program; 
# if not, write to the "Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA"

DefineModule("avslib", "numeric", "statistics")

global __load__ = Eval("""
	LoadModule("avslib", "base", "core")
	LoadModule("avslib", "numeric", "core")
	""")

# Number of arguments to the function below has been reduced to 20 for performance.
# If you need more than that, use the tools provided by the array package.

# Returns the sum of xi^2
#
Function SumSquare(val x1, val "x2", val "x3", val "x4", val "x5", val "x6", \
	val "x7", val "x8", val "x9", val "x10",val "x11", val "x12", val "x13", \
	val "x14", val "x15", val "x16", val "x17", val "x18", val "x19", val "x20")
{
	_sum = x1 * x1
	_sum = Defined(x2) ? __sum2(_sum, x2 * x2) : _sum
	_sum = Defined(x3) ? __sum2(_sum, x3 * x3) : _sum
	_sum = Defined(x4) ? __sum2(_sum, x4 * x4) : _sum
	_sum = Defined(x5) ? __sum2(_sum, x5 * x5) : _sum
	_sum = Defined(x6) ? __sum2(_sum, x6 * x6) : _sum
	_sum = Defined(x7) ? __sum2(_sum, x7 * x7) : _sum
	_sum = Defined(x8) ? __sum2(_sum, x8 * x8) : _sum
	_sum = Defined(x9) ? __sum2(_sum, x9 * x9) : _sum
	_sum = Defined(x10) ? __sum2(_sum, x10 * x10) : _sum
	_sum = Defined(x11) ? __sum2(_sum, x11 * x11) : _sum
	_sum = Defined(x12) ? __sum2(_sum, x12 * x12) : _sum
	_sum = Defined(x13) ? __sum2(_sum, x13 * x13) : _sum
	_sum = Defined(x14) ? __sum2(_sum, x14 * x14) : _sum
	_sum = Defined(x15) ? __sum2(_sum, x15 * x15) : _sum
	_sum = Defined(x16) ? __sum2(_sum, x16 * x16) : _sum
	_sum = Defined(x17) ? __sum2(_sum, x17 * x17) : _sum
	_sum = Defined(x18) ? __sum2(_sum, x18 * x18) : _sum
	_sum = Defined(x19) ? __sum2(_sum, x19 * x19) : _sum
	_sum = Defined(x20) ? __sum2(_sum, x20 * x20) : _sum
	return _sum
}

# Returns the average of xi
#
Function Average(val x1, val "x2", val "x3", val "x4", val "x5", val "x6", \
	val "x7", val "x8", val "x9", val "x10",val "x11", val "x12", val "x13", \
	val "x14", val "x15", val "x16", val "x17", val "x18", val "x19", val "x20")
{
	return Sum(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, \
			x12, x13, x14, x15, x16, x17, x18, x19, x20) \
		/ Count(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, \
			x12, x13, x14, x15, x16, x17, x18, x19, x20)
}

# Returns the standard deviation of xi
#
Function StDev(val x1, val "x2", val "x3", val "x4", val "x5", val "x6", val "x7", \
	val "x8", val "x9", val "x10",val "x11", val "x12", val "x13", val "x14", \
	val "x15", val "x16", val "x17", val "x18", val "x19", val "x20")
{
	n = Count(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, \
		x12, x13, x14, x15, x16, x17, x18, x19, x20)
	sx = Sum(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, \
		x12, x13, x14, x15, x16, x17, x18, x19, x20)
	sx2 = SumSquare(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, \
		x12, x13, x14, x15, x16, x17, x18, x19, x20)
	return Sqrt( (n * sx2 - sx * sx) / (n * (n - 1)) )
}

