# AVSLib :: 'array' package :: 'powseries' 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", "array", "powseries")

global __load__ = Eval("""
	LoadModule("avslib", "string", "core")
	""")
global __load__ = IsPackageLoaded("avslib", "array") ? NOP : Eval("""
	LoadModule("avslib", "array", "core")
	LoadModule("avslib", "array", "operators")
	""")

# Power series with array coefs and Array power series functions

# Returns the power series of x specified by coefs, start, incr, 
# ie: coefs[0]*x^start + coefs[1]*x^(start+incr) + coefs[1]*x^(start+2*incr) + ...
# If coefs_array is the null array ("") the function returns zero.
#
Function PowSeriesA(val x, string coefs_array, val "start_power", \
	val "increment")
{
	start_power = Default(start_power, 0)
	increment = Default(increment, 1)
	return (coefs_array.ArrayLen() > 0) \
		? coefs_array.ArrayGet(0) * Pow(x, start_power) + \
		PowSeriesA(x, coefs_array.ArrayDelete(0), start_power + increment, \
			increment) \
		: 0
}

Function __pow_term(val coef, val power, val x)
{
	return Float(coef) * Pow(x, power)
}

# Returns the power series of x specified by coefs and powers arrays, 
# ie: coefs[0]*x^powers[0] + coefs[1]*x^powers[1] + ...
#
Function PowSeriesAA(val x, string coefs_array, string powers_array)
{
	# equality of array's lengths is checked by ArrayOpArrayFunc
	return ArrayOpArrayFunc(coefs_array, powers_array, "__pow_term", \
		String(x)).ArraySum()
}

# Returns an array of the results of applying PowSeries in each element of 
# 'x_array'. Arguments after the first are same as PowSeries.
#
Function ArrayPowSeries(string x_array, string coef_func, string power_func, \
	int start_index, int end_index, int "increment", string "coef_args", \
	string "power_args")
{
	str_args = StrQuote(coef_func) + "," + StrQuote(power_func) \
		+ "," + String(start_index) + "," + String(end_index) \
		+ (Defined(increment) ? "," + String(increment) : "") \
		+ (Defined(coef_args) ? "," + coef_args : "") \
		+ (Defined(power_args) ? "," + power_args : "")
	return ArrayOpFunc(x_array, "PowSeries", str_args)
}

# Returns an array of the results of applying PowSeriesA in each element of 
# 'x_array'. Arguments after the first are same as PowSeriesA.
#
Function ArrayPowSeriesA(string x_array, string coefs_array, \
	val "start_power", val "increment")
{
	start_power = Default(start_power, 0)
	increment = Default(increment, 1)
	return x_array.ArrayOpFunc("PowSeriesA", StrQuote(coefs_array) + \
		"," + String(start_power) + "," + String(increment))
}

# Returns an array of the results of applying PowSeriesAA in each element of 
# 'x_array'. Arguments after the first are same as PowSeriesAA.
#
Function ArrayPowSeriesAA(string x_array, string coefs_array, \
	string powers_array)
{
	return x_array.ArrayOpFunc("PowSeriesAA", \
		StrQuote(coefs_array) + "," + StrQuote(powers_array))
}
