/*
 * 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_complex.h>
#include <spar/sl_io.h>

void
sl_complex_write (const sl_complex c)
{
  sl_write ("C(");
  sl_write_value_with_precision (SL_COMPLEX_RE (c), 3, 3);
  sl_write (", ");
  sl_write_value_with_precision (SL_COMPLEX_IM (c), 3, 3);
  sl_writeln ("i)");
}

void
sl_complex_writeln (const sl_complex c)
{
  sl_write ("C(");
  sl_write_value_with_precision (SL_COMPLEX_RE (c), 3, 3);
  sl_write (", ");
  sl_write_value_with_precision (SL_COMPLEX_IM (c), 3, 3);
  sl_writeln ("i)");
}

void
sl_complex_write_with_message (const char *msg, const sl_complex c)
{
  sl_assert (msg != NULL);

  sl_write ("%s(", msg);
  sl_write_value_with_precision (SL_COMPLEX_RE (c), 3, 3);
  sl_write (", ");
  sl_write_value_with_precision (SL_COMPLEX_IM (c), 3, 3);
  sl_writeln ("i)");
}

void
sl_complex_writeln_with_message (const char *msg, const sl_complex c)
{
  sl_assert (msg != NULL);

  sl_write ("%s(", msg);
  sl_write_value_with_precision (SL_COMPLEX_RE (c), 3, 3);
  sl_write (", ");
  sl_write_value_with_precision (SL_COMPLEX_IM (c), 3, 3);
  sl_writeln ("i)");
}

void
sl_complex_swap (sl_complex * c1, sl_complex * c2)
{
  register double c;

  /* 
   * swaps the "real" coeff 
   */
  c = SL_COMPLEX_P_RE (c1);
  SL_COMPLEX_P_RE (c1) = SL_COMPLEX_P_RE (c2);
  SL_COMPLEX_P_RE (c2) = c;

  /* 
   * swaps the "imag" coeff 
   */
  c = SL_COMPLEX_P_IM (c1);
  SL_COMPLEX_P_IM (c1) = SL_COMPLEX_P_IM (c2);
  SL_COMPLEX_P_IM (c2) = c;
}

bool
sl_complex_is_equal (sl_complex c1, sl_complex c2)
{
  return (SL_COMPLEX_RE (c1) == SL_COMPLEX_RE (c2)) &&
    (SL_COMPLEX_IM (c1) == SL_COMPLEX_IM (c2));
}

bool
sl_complex_is_equal_p (sl_complex * c1, sl_complex * c2)
{
  return (SL_COMPLEX_P_RE (c1) == SL_COMPLEX_P_RE (c2)) &&
    (SL_COMPLEX_P_IM (c1) == SL_COMPLEX_P_IM (c2));
}

bool
sl_complex_is_not_equal (sl_complex c1, sl_complex c2)
{
  return (SL_COMPLEX_RE (c1) != SL_COMPLEX_RE (c2))
    || (SL_COMPLEX_IM (c1) != SL_COMPLEX_IM (c2));
}

bool
sl_complex_is_not_equal_p (sl_complex * c1, sl_complex * c2)
{
  return (SL_COMPLEX_P_RE (c1) != SL_COMPLEX_P_RE (c2))
    || (SL_COMPLEX_P_IM (c1) != SL_COMPLEX_P_IM (c2));
}


syntax highlighted by Code2HTML, v. 0.9.1