/*
	(c) Carlos Rodriguez, 2007
	(c) Jose Rodriguez, 2007 (http://www.boriel.com)

    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
*/
function serie(cadena)
{
	var CadenaDeNumeros;

	CadenaDeNumeros = cadena;
	ArrayS = CadenaDeNumeros.split(/\s+/);

	k = 0;
	for (i = 0; i < ArrayS.length; i++) {
		if (ArrayS[i] != '') {
			ArrayS[k] = ArrayS[i];
			k++;
		}
	}

	p.points = [];
	ArraySerie = new Array(k);
	for (i = 0; i < k; i++) {
		ArraySerie[i] = ArrayS[i];
		p.points.push({x:(i + 1), y:ArrayS[i]});
	}

        APo = new Array(50);
        for (i = 0; i < 51; i++) {
			APo[i] = new Array(50);
			
			for(j = 0; j < 50; j++)
				APo[i][j] = 0;
        }
        
        APo[1][0] = -1;
        APo[1][1] = 1;
        for (i = 2; i < 51; i++) {
			APo[i][0] = -1 * APo[i - 1][0] * i; 
			for (j = 1; j < i; j++)
				APo[i][j] = APo[i - 1][j - 1] -  i * APo[i - 1][j];
				
			APo[i][i] = 1;
        }
        
        DiDi = new Array(50);
        for (i = 1; i < ArraySerie.length; i++) {
			DiDi[i] = new Array(ArraySerie.length);
		  
			for(j = 0; j < ArraySerie.length; j++)
				DiDi[i][j] = 0;
        }

        DiDi[0] = ArraySerie;
        for (i = 1; i < ArraySerie.length; i++)
			for (j = 0; j < ArraySerie.length - i; j++)
				DiDi[i][j] = (DiDi[i - 1][j + 1] - DiDi[i - 1][j]) / i;
		
		Resultados = new Array(ArraySerie.length);
		for (i = 0; i < ArraySerie.length; i++)
			Resultados[i] = 0;
		
		Resultados[0] = parseInt(DiDi[0][0]);
		for (j = 0; j < ArraySerie.length; j++)
			for(i = 1; i < ArraySerie.length; i++)
				Resultados[j] += DiDi[i][0] * APo[i][j];
		
		var CadenaResultado;
		var Signo;

		CadenaResultado = "An = ";
		Signo = '';
		Resultado = '';
		
		for (i = ArraySerie.length - 1; i > -1; i--)
			if (Resultados[i] != 0) {
				Resultado += ' + ' + Resultados[i] + ' * pow(x, ' + i + ')';

				if (Resultados[i] < 0) {
					Signo = ' - ';
					Resultados[i] = -Resultados[i];
				}
				
				if ((Resultados[i] == 1) && (i > 0))
					Resultados[i] = '';

				CadenaResultado += Signo + Resultados[i];
				if (i > 0)
					CadenaResultado += "n";

				if (i > 1)
					CadenaResultado += "^" + i;

				Signo = ' + ';
			}

		document.getElementById('Resultado').value = CadenaResultado;
		
		p.aPlots.push(Resultado);
		p.Draw();
      }


