/* complex/gsl_complex.h
 *
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
 *
 * 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

#ifndef __GSL_COMPLEX_H__
#define __GSL_COMPLEX_H__

#undef __BEGIN_DECLS
#undef __END_DECLS
#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS /* empty */
#define __END_DECLS   /* empty */
#endif

__BEGIN_DECLS

/* two consecutive built-in types as a complex number */
typedef double *gsl_complex_packed;
typedef float *gsl_complex_packed_float;
typedef long double *gsl_complex_packed_long_double;

typedef const double *gsl_const_complex_packed;
typedef const float *gsl_const_complex_packed_float;
typedef const long double *gsl_const_complex_packed_long_double;

/* 2N consecutive built-in types as N complex numbers */
typedef double *gsl_complex_packed_array;
typedef float *gsl_complex_packed_array_float;
typedef long double *gsl_complex_packed_array_long_double;

typedef const double *gsl_const_complex_packed_array;
typedef const float *gsl_const_complex_packed_array_float;
typedef const long double *gsl_const_complex_packed_array_long_double;

/* Yes... this seems weird. Trust us. The point is just that
   sometimes you want to make it obvious that something is
   an output value. The fact that it lacks a 'const' may not
   be enough of a clue for people in some contexts.
 */
typedef double *gsl_complex_packed_ptr;
typedef float *gsl_complex_packed_float_ptr;
typedef long double *gsl_complex_packed_long_double_ptr;

typedef const double *gsl_const_complex_packed_ptr;
typedef const float *gsl_const_complex_packed_float_ptr;
typedef const long double *gsl_const_complex_packed_long_double_ptr;

typedef struct {
    long double dat[2];
} gsl_complex_long_double;

typedef struct {
    double dat[2];
} gsl_complex;

typedef struct {
    float dat[2];
} gsl_complex_float;

#define GSL_REAL(z) ((z).dat[0])
#define GSL_IMAG(z) ((z).dat[1])
#define GSL_COMPLEX_P(zp) ((zp)->dat)
#define GSL_COMPLEX_P_REAL(zp) ((zp)->dat[0])
#define GSL_COMPLEX_P_IMAG(zp) ((zp)->dat[1])
#define GSL_COMPLEX_EQ(z1, z2)                                                 \
    (((z1).dat[0] == (z2).dat[0]) && ((z1).dat[1] == (z2).dat[1]))

#define GSL_SET_COMPLEX(zp, x, y)                                              \
    do {                                                                       \
        (zp)->dat[0] = (x);                                                    \
        (zp)->dat[1] = (y);                                                    \
    } while (0)
#define GSL_SET_REAL(zp, x)                                                    \
    do {                                                                       \
        (zp)->dat[0] = (x);                                                    \
    } while (0)
#define GSL_SET_IMAG(zp, y)                                                    \
    do {                                                                       \
        (zp)->dat[1] = (y);                                                    \
    } while (0)

#define GSL_SET_COMPLEX_PACKED(zp, n, x, y)                                    \
    do {                                                                       \
        *((zp) + 2 * (n)) = (x);                                               \
        *((zp) + (2 * (n) + 1)) = (y);                                         \
    } while (0)

__END_DECLS

#endif /* __GSL_COMPLEX_H__ */
