ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
complex_long_double.hpp
Go to the documentation of this file.
1/*
2 * $Id: complex_long_double.hpp 77 2012-01-15 15:32:13Z jdl3 $
3 * Copyright (C) 2010, 2011, 2012, 2024 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_COMPLEX_LONG_DOUBLE_HPP
21#define CCGSL_COMPLEX_LONG_DOUBLE_HPP
22
23#include<cmath>
24#include<cstring>
25#include<complex>
26#include<gsl/gsl_complex.h>
27#include<gsl/gsl_complex_math.h>
28
29namespace gsl {
42 class complex_long_double : public gsl_complex_long_double {
47 public:
48 // Default constructible: so that it can be stored in a container
57 explicit complex_long_double( long double* dat ){
58 memcpy( this->dat, dat, 2 * sizeof( long double ) );
59 }
65 complex_long_double( long double const real, long double const imag ){
66 GSL_SET_REAL( this, real );
67 GSL_SET_IMAG( this, imag );
68 }
73 complex_long_double( std::complex<long double> const& z ){
74 GSL_SET_REAL( this, std::real( z ) );
75 GSL_SET_IMAG( this, std::imag( z ) );
76 }
77 // copy constructor: default is OK
78 // assignment operator: default is OK
79 // destructor: default is OK
84 complex_long_double( gsl_complex_long_double const& z ){
85 GSL_SET_REAL( this, GSL_REAL( z ) );
86 GSL_SET_IMAG( this, GSL_IMAG( z ) );
87 }
92#ifdef __GXX_EXPERIMENTAL_CXX0X__
93 explicit operator std::complex<long double>() const {
94 return std::complex<long double>( GSL_REAL( *this ), GSL_IMAG( *this ) );
95 }
96#else
97 operator std::complex<long double>() const {
98 std::complex<long double> z = std::complex<long double>( GSL_REAL( *this ), GSL_IMAG( *this ) );
99 return z;
100 }
101#endif
106 gsl_complex_long_double& get(){ return dynamic_cast<gsl_complex_long_double&>( *this ); }
111 gsl_complex_long_double const& get() const { return dynamic_cast<gsl_complex_long_double const&>( *this ); }
118 static complex_long_double rect( long double x, long double y ){ complex_long_double z; GSL_SET_COMPLEX( &z, x, y ); return z; }
125 static complex_long_double polar( long double r, long double theta ){ complex_long_double z;
126 GSL_SET_COMPLEX( &z, r * std::cos( theta ), r * std::sin( theta ) ); return z; }
132 void set_complex_long_double( long double x, long double y ){ GSL_SET_COMPLEX( this, x, y );}
137 long double real() const { return GSL_REAL( *this ); }
142 long double imag() const { return GSL_IMAG( *this ); }
147 void set_real( long double x ){ GSL_SET_REAL( this, x ); }
152 void set_imag( long double y ){ GSL_SET_IMAG( this, y ); }
159 bool operator==( complex_long_double const& z ) const { return dat[0] == z.dat[0] and dat[1] == z.dat[1]; }
166 bool operator!=( complex_long_double const& z ) const { return dat[0] != z.dat[0] or dat[1] != z.dat[1]; }
173 bool operator<( complex_long_double const& z ) const {
174 return dat[0] < z.dat[0] or (dat[0] == z.dat[0] and dat[1] < z.dat[1]); }
181 bool operator>( complex_long_double const& z ) const {
182 return dat[0] > z.dat[0] or (dat[0] == z.dat[0] and dat[1] > z.dat[1]); }
189 bool operator<=( complex_long_double const& z ) const { return *this < z or *this == z; }
196 bool operator>=( complex_long_double const& z ) const { return *this > z or *this == z; }
197 };
198
204 protected:
208 long double* dat;
209 public:
214 complex_long_double_ref( long double* dat ) : dat( dat ){}
220 operator gsl_complex_long_double() const {
221 gsl_complex_long_double z; memcpy( z.dat, dat, 2 * sizeof( long double ) ); return z; }
227 operator complex_long_double() const {
228 complex_long_double z; memcpy( z.dat, dat, 2 * sizeof( long double ) ); return z; }
229 // Default constructible
239 // copy constructor: default is OK
240 // assignment operator: default is OK
241 // also need to copy from complex_long_double
247 memcpy( dat, z.dat, 2 * sizeof( long double ) ); return *this; }
248 // destructor: default is OK
254 void set_complex_long_double( long double x, long double y ){ GSL_SET_COMPLEX( this, x, y );}
259 long double real() const { return GSL_REAL( *this ); }
264 long double imag() const { return GSL_IMAG( *this ); }
269 void set_real( long double x ){ GSL_SET_REAL( this, x ); }
274 void set_imag( long double y ){ GSL_SET_IMAG( this, y ); }
275 };
276
282 public:
303 complex_long_double_ref const operator*() const { return *this; }
308 complex_long_double_ref const* operator->() const { return this; }
309 };
310
323 template<typename Ch,typename Tr>
324 std::basic_ostream<Ch,Tr>& operator<<( std::basic_ostream<Ch,Tr>& stream, complex_long_double const& z ){
325 long double i = z.imag();
326 if( i >= 0 ) stream << z.real() << "+" << i << "i";
327 else stream << z.real() << "-" << -i << "i";
328 return stream;
329 }
330
343 template<typename Ch,typename Tr>
344 std::basic_ostream<Ch,Tr>& operator<<( std::basic_ostream<Ch,Tr>& stream,
345 complex_long_double_ref const& z ){
346 long double i = z.imag();
347 if( i >= 0 ) stream << z.real() << "+" << i << "i";
348 else stream << z.real() << "-" << -i << "i";
349 return stream;
350 }
351
362 namespace cpx_long_double {
369 inline complex_long_double rect( long double x, long double y ){ return complex_long_double::rect( x, y ); }
376 inline complex_long_double polar( long double r, long double theta ){ return complex_long_double::polar( r, theta ); } }
377
378}
379
380#endif
This class can be used like a pointer for complex_long_double objects so that we can iterate over a v...
complex_long_double_ref const * operator->() const
Dereference the pointer.
complex_long_double_ref operator*()
Dereference the pointer.
complex_long_double_ref const operator*() const
Dereference the pointer.
complex_long_double_ref * operator->()
Dereference the pointer.
complex_long_double_ptr(long double *dat)
Typically we are given a pointer to the data storing the complex_long_double and need to construct a ...
This class can be used like a reference for complex_long_double objects so that we can iterate over a...
complex_long_double_ref & operator=(complex_long_double const &z)
Assignment from complex_long_double.
void set_complex_long_double(long double x, long double y)
C++ version of GSL_SET_COMPLEX().
complex_long_double_ref()
The default constructor is only really useful for assigning to.
long double imag() const
C++ version of GSL_IMAG().
complex_long_double_ref(complex_long_double &z)
Make sure we can construct from a complex_long_double.
complex_long_double_ref(long double *dat)
We use this in constructing complex_long_double_ptr objects.
void set_real(long double x)
C++ version of GSL_SET_REAL().
void set_imag(long double y)
C++ version of GSL_SET_IMAG().
long double real() const
C++ version of GSL_REAL().
This class handles complex_long_double numbers.
static complex_long_double rect(long double x, long double y)
C++ version of gsl_complex_long_double_rect().
bool operator<=(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
void set_imag(long double y)
C++ version of GSL_SET_IMAG().
long double imag() const
C++ version of GSL_IMAG().
void set_complex_long_double(long double x, long double y)
C++ version of GSL_SET_COMPLEX().
complex_long_double(gsl_complex_long_double const &z)
Constructor from base class.
complex_long_double(long double *dat)
Allow construction from raw data.
bool operator==(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
gsl_complex_long_double & get()
Get the base class object.
bool operator<(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
gsl_complex_long_double const & get() const
Get the base class object.
bool operator>(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
bool operator>=(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
bool operator!=(complex_long_double const &z) const
A complex_long_double object must be less than comparable so that it can be used as a container value...
long double real() const
C++ version of GSL_REAL().
complex_long_double()
The default constructor is only really useful for assigning to.
complex_long_double(std::complex< long double > const &z)
Allow construction from a std::complex<long double>.
void set_real(long double x)
C++ version of GSL_SET_REAL().
static complex_long_double polar(long double r, long double theta)
C++ version of gsl_complex_long_double_rect().
complex_long_double(long double const real, long double const imag)
Allow construction from real and imaginary values.
This class handles matrix_complex_long_double objects as shared handles.
This class handles vector_complex_long_double objects as shared handles.
complex_long_double rect(long double x, long double y)
C++ version of gsl_complex_long_double_rect().
complex_long_double polar(long double r, long double theta)
C++ version of gsl_complex_long_double_rect().
complex cos(complex const &a)
C++ version of gsl_complex_cos().
Definition: complex.hpp:999
complex sin(complex const &a)
C++ version of gsl_complex_sin().
Definition: complex.hpp:992
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34
std::basic_ostream< Ch, Tr > & operator<<(std::basic_ostream< Ch, Tr > &stream, complex const &z)
Define the << operator so that we can use expressions like.
Definition: complex.hpp:734