ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
complex_float.hpp
Go to the documentation of this file.
1/*
2 * $Id: complex_float.hpp 97 2012-01-21 16:49:17Z 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_FLOAT_HPP
21#define CCGSL_COMPLEX_FLOAT_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_float : public gsl_complex_float {
43 friend class complex_float_ptr;
44 friend class complex_float_ref;
47 public:
48 // Default constructible: so that it can be stored in a container
57 explicit complex_float( float* dat ){
58 memcpy( this->dat, dat, 2 * sizeof( float ) );
59 }
65 complex_float( float const real, float const imag ){
66 GSL_SET_REAL( this, real );
67 GSL_SET_IMAG( this, imag );
68 }
73 complex_float( std::complex<float> 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_float( gsl_complex_float 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<float>() const {
94 return std::complex<float>( GSL_REAL( *this ), GSL_IMAG( *this ) );
95 }
96#else
97 operator std::complex<float>() const {
98 std::complex<float> z = std::complex<float>( GSL_REAL( *this ), GSL_IMAG( *this ) );
99 return z;
100 }
101#endif
106 gsl_complex_float& get(){ return dynamic_cast<gsl_complex_float&>( *this ); }
111 gsl_complex_float const& get() const { return dynamic_cast<gsl_complex_float const&>( *this ); }
118 static complex_float rect( float x, float y ){ complex_float z; GSL_SET_COMPLEX( &z, x, y ); return z; }
125 static complex_float polar( float r, float theta ){ complex_float z;
126 GSL_SET_COMPLEX( &z, r * std::cos( theta ), r * std::sin( theta ) ); return z; }
132 void set_complex_float( float x, float y ){ GSL_SET_COMPLEX( this, x, y );}
137 float real() const { return GSL_REAL( *this ); }
142 float imag() const { return GSL_IMAG( *this ); }
147 void set_real( float x ){ GSL_SET_REAL( this, x ); }
152 void set_imag( float y ){ GSL_SET_IMAG( this, y ); }
159 bool operator==( complex_float const& z ) const { return dat[0] == z.dat[0] and dat[1] == z.dat[1]; }
166 bool operator!=( complex_float const& z ) const { return dat[0] != z.dat[0] or dat[1] != z.dat[1]; }
173 bool operator<( complex_float 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_float 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_float const& z ) const { return *this < z or *this == z; }
196 bool operator>=( complex_float const& z ) const { return *this > z or *this == z; }
197 };
198
204 protected:
208 float* dat;
209 public:
214 complex_float_ref( float* dat ) : dat( dat ){}
220 operator gsl_complex_float() const {
221 gsl_complex_float z; memcpy( z.dat, dat, 2 * sizeof( float ) ); return z; }
227 operator complex_float() const {
228 complex_float z; memcpy( z.dat, dat, 2 * sizeof( float ) ); return z; }
229 // Default constructible
239 // copy constructor: default is OK
240 // assignment operator: default is OK
241 // also need to copy from complex_float
247 memcpy( dat, z.dat, 2 * sizeof( float ) ); return *this; }
248 // destructor: default is OK
254 void set_complex_float( float x, float y ){ GSL_SET_COMPLEX( this, x, y );}
259 float real() const { return GSL_REAL( *this ); }
264 float imag() const { return GSL_IMAG( *this ); }
269 void set_real( float x ){ GSL_SET_REAL( this, x ); }
274 void set_imag( float y ){ GSL_SET_IMAG( this, y ); }
275 };
276
282 public:
293 complex_float_ref operator*(){ return *this; }
298 complex_float_ref* operator->(){ return this; }
303 complex_float_ref const operator*() const { return *this; }
308 complex_float_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_float const& z ){
325 float 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_float_ref const& z ){
346 float 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_float {
369 inline complex_float rect( float x, float y ){ return complex_float::rect( x, y ); }
376 inline complex_float polar( float r, float theta ){ return complex_float::polar( r, theta ); }
377 }
378
382 typedef gsl_complex_packed_float complex_packed_float;
386 typedef gsl_const_complex_packed_float complex_const_packed_float;
390 typedef gsl_complex_packed_float_ptr complex_packed_float_ptr;
394 typedef gsl_const_complex_packed_float_ptr complex_const_packed_float_ptr;
398 typedef gsl_complex_packed_array_float complex_packed_array_float;
402 typedef gsl_const_complex_packed_array_float complex_const_packed_array_float;
403
404}
405
406#endif
This class can be used like a pointer for complex_float objects so that we can iterate over a vector ...
complex_float_ref * operator->()
Dereference the pointer.
complex_float_ptr(float *dat)
Typically we are given a pointer to the data storing the complex_float and need to construct a comple...
complex_float_ref operator*()
Dereference the pointer.
complex_float_ref const * operator->() const
Dereference the pointer.
complex_float_ref const operator*() const
Dereference the pointer.
This class can be used like a reference for complex_float objects so that we can iterate over a vecto...
float real() const
C++ version of GSL_REAL().
complex_float_ref(complex_float &z)
Make sure we can construct from a complex_float.
complex_float_ref(float *dat)
We use this in constructing complex_float_ptr objects.
float imag() const
C++ version of GSL_IMAG().
void set_complex_float(float x, float y)
C++ version of GSL_SET_COMPLEX().
void set_imag(float y)
C++ version of GSL_SET_IMAG().
void set_real(float x)
C++ version of GSL_SET_REAL().
float * dat
The data.
complex_float_ref & operator=(complex_float const &z)
Assignment from complex_float.
complex_float_ref()
The default constructor is only really useful for assigning to.
This class handles complex_float numbers.
bool operator<(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
complex_float(gsl_complex_float const &z)
Constructor from base class.
gsl_complex_float & get()
Get the base class object.
complex_float(float *dat)
Allow construction from raw data.
bool operator!=(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
void set_real(float x)
C++ version of GSL_SET_REAL().
bool operator==(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
complex_float(std::complex< float > const &z)
Allow construction from a std::complex<float>.
gsl_complex_float const & get() const
Get the base class object.
float real() const
C++ version of GSL_REAL().
static complex_float rect(float x, float y)
C++ version of gsl_complex_float_rect().
void set_complex_float(float x, float y)
C++ version of GSL_SET_COMPLEX().
void set_imag(float y)
C++ version of GSL_SET_IMAG().
bool operator>=(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
bool operator<=(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
static complex_float polar(float r, float theta)
C++ version of gsl_complex_float_rect().
complex_float(float const real, float const imag)
Allow construction from real and imaginary values.
float imag() const
C++ version of GSL_IMAG().
complex_float()
The default constructor is only really useful for assigning to.
bool operator>(complex_float const &z) const
A complex_float object must be less than comparable so that it can be used as a container value type.
This class handles matrix_complex_float objects as shared handles.
This class handles vector_complex_float objects as shared handles.
complex_float rect(float x, float y)
C++ version of gsl_complex_float_rect().
complex_float polar(float r, float theta)
C++ version of gsl_complex_float_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
gsl_const_complex_packed_float complex_const_packed_float
Typedef.
gsl_const_complex_packed_array_float complex_const_packed_array_float
Typedef.
gsl_complex_packed_float_ptr complex_packed_float_ptr
Typedef.
gsl_const_complex_packed_float_ptr complex_const_packed_float_ptr
Typedef.
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
gsl_complex_packed_float complex_packed_float
Typedef.
gsl_complex_packed_array_float complex_packed_array_float
Typedef.