/*
 * The Spar Library - modular math parser
 * Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

#include <spar/sl_poly.h>
#include <spar/sl_util.h>
#include <spar/sl_io.h>

sl_poly *
sl_poly_new (const sl_size m, double grade)
{
  sl_assert (m > 0);

  {
    sl_poly *p;

    p = (sl_poly *) sl_malloc (sizeof (sl_poly));
    p->g = sl_vector_new (m);
    p->c = sl_vector_new (m);
    p->grade = grade;

    /*
     *  The max grade is "grade" 
     *  the first poly item gets the grade "grade" 
     */
    {
      sl_size i;

      for (i = 0; i < m; i++)
	{
	  sl_vector_set_item (p->g, i, grade--);
	  sl_vector_set_item (p->c, i, 0.0);
	}
    }

    return p;
  }
}

int
sl_poly_delete (sl_poly * p)
{
  sl_assert (p != NULL);

  sl_free (p->g, sizeof (double) * p->g->dim);
  sl_free (p->c, sizeof (double) * p->c->dim);
  sl_free (p, sizeof (sl_poly *));

  return SL_SUCCESS;
}

int
sl_poly_print (sl_poly p)
{
  sl_write ("P(");

  {
    sl_size i;

    for (i = 0; i < p.c->dim; i++)
      sl_write ("%g(x)^%g + ", p.c[i], p.g[i]);
  }

  return SL_SUCCESS;
}

int
sl_poly_print_p (sl_poly * p)
{
  sl_assert (p != NULL);

  sl_write ("P(");

  {
    sl_size i;

    for (i = 0; i < p->c->dim; i++)
      sl_write ("%g(x)^%g + ", p->c[i], p->g[i]);
  }

  return SL_SUCCESS;
}

int
sl_poly_print_with_param (sl_poly p, const char *v)
{
  sl_write ("P(");

  {
    sl_size i;

    for (i = 0; i < p.c->dim; i++)
      sl_write ("%g(%s)^%g + ", p.c[i], v, p.g[i]);
  }

  return SL_SUCCESS;
}

int
sl_poly_print_with_param_p (sl_poly * p, const char *v)
{
  sl_assert (p != NULL);

  sl_write ("P(");

  {
    sl_size i;

    for (i = 0; i < p->c->dim; i++)
      sl_write ("%g(%s)^%g + ", p->c[i], v, p->g[i]);
  }

  return SL_SUCCESS;
}


syntax highlighted by Code2HTML, v. 0.9.1