/*
* 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_math_library.h>
#include <spar/sl_complex.h>
sl_complex
sl_complex_def (double x, double y)
{
SL_COMPLEX (z, x, y);
return z;
}
/*
* Standard functions
*
* missing: cos, log, exp, tanh, atan, asinh...
*/
sl_complex
sl_complex_conjugate (sl_complex x)
{
return sl_complex_def (SL_COMPLEX_RE (x), SL_COMPLEX_IM (x) * (-1));
}
sl_complex
sl_complex_negative (sl_complex x)
{
return sl_complex_def (SL_COMPLEX_RE (x) * (-1), SL_COMPLEX_IM (x) * (-1));
}
double
sl_complex_abs (sl_complex x)
{
return sl_hypot (SL_COMPLEX_RE (x), SL_COMPLEX_IM (x));
}
double
sl_complex_abs_2 (sl_complex x)
{
return (SL_COMPLEX_RE (x) * SL_COMPLEX_RE (x)) +
(SL_COMPLEX_IM (x) * SL_COMPLEX_IM (x));
}
double
sl_complex_arg (sl_complex x)
{
return sl_arctan (SL_COMPLEX_IM (x) / SL_COMPLEX_RE (x));
}
sl_complex
sl_complex_polar (double r, double theta)
{
return sl_complex_def (r * sl_cos (theta), r * sl_sin (theta));
}
/*
* ADD complex operator
*/
sl_complex
sl_complex_add (sl_complex x, sl_complex y)
{
return sl_complex_def (SL_COMPLEX_RE (x) + SL_COMPLEX_RE (y),
SL_COMPLEX_IM (x) + SL_COMPLEX_IM (y));
}
sl_complex
sl_complex_add_real (sl_complex x, double r)
{
return sl_complex_def (SL_COMPLEX_RE (x) + r, SL_COMPLEX_IM (x));
}
sl_complex
sl_complex_add_imag (sl_complex x, double i)
{
return sl_complex_def (SL_COMPLEX_RE (x), SL_COMPLEX_IM (x) + i);
}
/*
* SUB complex operator
*/
sl_complex
sl_complex_sub (sl_complex x, sl_complex y)
{
return sl_complex_def (SL_COMPLEX_RE (x) - SL_COMPLEX_RE (y),
SL_COMPLEX_IM (x) - SL_COMPLEX_IM (y));
}
sl_complex
sl_complex_sub_real (sl_complex x, double r)
{
return sl_complex_def (SL_COMPLEX_RE (x) - r, SL_COMPLEX_IM (x));
}
sl_complex
sl_complex_sub_imag (sl_complex x, double i)
{
return sl_complex_def (SL_COMPLEX_RE (x), SL_COMPLEX_IM (x) - i);
}
/*
* MUL complex operator
*/
sl_complex
sl_complex_mul (sl_complex x, sl_complex y)
{
return sl_complex_def ((SL_COMPLEX_RE (x) * SL_COMPLEX_RE (y) -
SL_COMPLEX_IM (x) * SL_COMPLEX_IM (y)),
(SL_COMPLEX_RE (x) * SL_COMPLEX_IM (y) +
SL_COMPLEX_IM (x) * SL_COMPLEX_RE (y)));
}
sl_complex
sl_complex_mul_real (sl_complex x, double r)
{
return sl_complex_def (SL_COMPLEX_RE (x) * r, SL_COMPLEX_IM (x) * r);
}
sl_complex
sl_complex_mul_imag (sl_complex x, double i)
{
return sl_complex_def (-i * SL_COMPLEX_IM (x), SL_COMPLEX_RE (x) * i);
}
sl_complex
sl_complex_div (sl_complex x, sl_complex y)
{
double arg_x = sl_complex_arg (x);
double arg_y = sl_complex_arg (y);
double mod_x = sl_complex_abs (x);
double mod_y = sl_complex_abs (y);
return sl_complex_def ((mod_x / mod_y), (arg_x - arg_y));
}
syntax highlighted by Code2HTML, v. 0.9.1