MsPASS C++ API  2.4.1.dev4+g92330b7a
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
Public Member Functions | List of all members
mspass::algorithms::deconvolution::ComplexArray Class Reference

Interfacing object to ease conversion between FORTRAN and C++ complex. More...

#include <ComplexArray.h>

Public Member Functions

 ComplexArray ()
 
 ComplexArray (std::vector< Complex64 > &d)
 
 ComplexArray (std::vector< Complex32 > &d)
 
 ComplexArray (int nsamp, FortranComplex32 *d)
 
 ComplexArray (int nsamp, FortranComplex64 *d)
 
 ComplexArray (int nsamp, float *d)
 
 ComplexArray (int nsamp, double *d)
 
 ComplexArray (int nsamp)
 
template<class T >
 ComplexArray (int nsamp, std::vector< T > d)
 
template<class T >
 ComplexArray (int nsamp, T d)
 
 ComplexArray (std::vector< double > mag, std::vector< double > phase)
 
 ComplexArray (const ComplexArray &parent)
 
ComplexArrayoperator= (const ComplexArray &parent)
 
 ~ComplexArray ()
 
Complex64 operator[] (int sample)
 
double * ptr ()
 
double * ptr (int sample)
 
ComplexArrayoperator+= (const ComplexArray &other) noexcept(false)
 
ComplexArrayoperator-= (const ComplexArray &other) noexcept(false)
 
ComplexArrayoperator*= (const ComplexArray &other) noexcept(false)
 
ComplexArrayoperator/= (const ComplexArray &other) noexcept(false)
 
const ComplexArray operator+ (const ComplexArray &other) const noexcept(false)
 
const ComplexArray operator- (const ComplexArray &other) const noexcept(false)
 
const ComplexArray operator* (const ComplexArray &other) const noexcept(false)
 
const ComplexArray operator/ (const ComplexArray &other) const noexcept(false)
 
void conj ()
 
std::vector< double > abs () const
 
double rms () const
 
double norm2 () const
 
std::vector< double > phase () const
 
int size () const
 

Detailed Description

Interfacing object to ease conversion between FORTRAN and C++ complex.

The internal representation is compatible with numerical libraries that expect interleaved real and imaginary values while exposing C++ convenience operators.

Constructor & Destructor Documentation

◆ ComplexArray() [1/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( )

Empty constructor.

8 {
9 nsamp = 0;
10 /* Note declaration of shared_ptr initializes it with a NULL
11 pointer equivalent - no initialization is needed */
12}

◆ ComplexArray() [2/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( std::vector< Complex64 > &  d)

Construct from stl vector container of complex.

13 {
14 nsamp = d.size();
15 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
16 for (std::size_t i = 0; i < nsamp; i++) {
17 data[i].real = d[i].real();
18 data[i].imag = d[i].imag();
19 }
20}

◆ ComplexArray() [3/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( std::vector< Complex32 > &  d)

Similar for 32 bit version

21 {
22 nsamp = d.size();
23 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
24 for (std::size_t i = 0; i < nsamp; i++) {
25 data[i].real = d[i].real();
26 data[i].imag = d[i].imag();
27 }
28}

◆ ComplexArray() [4/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
FortranComplex32 d 
)

Construct from a FORTRAN data array.

Fortran stores complex numbers in a mulitplexed array structure (real(1), imag(1), real(2), imag(2), etc.). The constructors below provide a mechanism for building this object from various permutations of this.

Parameters
nsampis the number of elements in the C vector
dis the pointer to the first compoment of the fortran vector.
29 {
30 nsamp = n;
31 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
32 for (std::size_t i = 0; i < nsamp; i++) {
33 data[i].real = d[i].real;
34 data[i].imag = d[i].imag;
35 }
36}

References mspass::algorithms::deconvolution::FortranComplex32::imag, and mspass::algorithms::deconvolution::FortranComplex32::real.

◆ ComplexArray() [5/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
FortranComplex64 d 
)

Construct from a double-precision FORTRAN complex array.

37 {
38 nsamp = n;
39 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
40 for (std::size_t i = 0; i < nsamp; i++) {
41 data[i].real = d[i].real;
42 data[i].imag = d[i].imag;
43 }
44}

References mspass::algorithms::deconvolution::FortranComplex64::imag, and mspass::algorithms::deconvolution::FortranComplex64::real.

◆ ComplexArray() [6/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
float *  d 
)

Construct from a real single-precision array.

45 {
46 nsamp = n;
47 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
48 for (std::size_t i = 0; i < nsamp; i++) {
49 data[i].real = d[i];
50 data[i].imag = 0.0;
51 }
52}

◆ ComplexArray() [7/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
double *  d 
)

Construct from a real double-precision array.

53 {
54 nsamp = n;
55 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
56 for (std::size_t i = 0; i < nsamp; i++) {
57 data[i].real = d[i];
58 data[i].imag = 0.0;
59 }
60}

◆ ComplexArray() [8/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp)

Construct a zero-filled complex array of length nsamp.

61 {
62 nsamp = n;
63 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
64 for (std::size_t i = 0; i < nsamp; i++) {
65 data[i].real = 0.0;
66 data[i].imag = 0.0;
67 }
68}

◆ ComplexArray() [9/12]

template<class T >
mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
std::vector< T >  d 
)

Construct from different length of vector, adds zoeros to it And construct a constant arrays

233 {
234 nsamp = n;
235 if (nsamp > d.size()) {
236 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
237 for (std::size_t i = 0; i < d.size(); i++) {
238 this->data[i].real = d[i];
239 this->data[i].imag = 0.0;
240 }
241 for (std::size_t i = d.size(); i < nsamp; i++) {
242 this->data[i].real = 0.0;
243 this->data[i].imag = 0.0;
244 }
245 } else {
246 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
247 for (std::size_t i = 0; i < nsamp; i++) {
248 this->data[i].real = d[i];
249 this->data[i].imag = 0.0;
250 }
251 }
252}

◆ ComplexArray() [10/12]

template<class T >
mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
d 
)

Construct a constant real-valued complex array of length nsamp.

253 {
254 nsamp = n;
255 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
256 for (std::size_t i = 0; i < nsamp; i++) {
257 this->data[i].real = d;
258 this->data[i].imag = 0.0;
259 }
260}

◆ ComplexArray() [11/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( std::vector< double >  mag,
std::vector< double >  phase 
)

Construct from magnitude and phase arrays.

70 {
71 nsamp = mag.size();
72 if (nsamp == phase.size()) {
73 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
74 for (std::size_t i = 0; i < nsamp; i++) {
75 Complex64 temp = polar(mag[i], phase[i]);
76 data[i].real = temp.real();
77 data[i].imag = temp.imag();
78 }
79 } else {
80 throw MsPASSError(
81 "ComplexArray::ComplexArray(vector<double> mag,vector<double> phase): "
82 "Length of magnitude vector and phase vector do not match",
83 ErrorSeverity::Invalid);
84 }
85}
std::vector< double > phase() const
Definition ComplexArray.cc:268

References phase().

◆ ComplexArray() [12/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( const ComplexArray parent)

Copy constructor.

86 {
87 nsamp = parent.nsamp;
88 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
89 for (std::size_t i = 0; i < nsamp; i++) {
90 data[i].real = parent.data[i].real;
91 data[i].imag = parent.data[i].imag;
92 }
93}

◆ ~ComplexArray()

mspass::algorithms::deconvolution::ComplexArray::~ComplexArray ( )

Destructor.

106 {
107 /*Original implementation used a raw pointer for data array. Changed
108 Dec. 2024 to shared_ptr so this destructor now does nothing.*/
109 // delete[] data;
110}

Member Function Documentation

◆ abs()

vector< double > mspass::algorithms::deconvolution::ComplexArray::abs ( ) const

Return stl vector of amplitude spectrum.

245 {
246 vector<double> result;
247 result.reserve(nsamp);
248 for (std::size_t i = 0; i < nsamp; i++)
249 result.push_back(sqrt((double)data[i].real * data[i].real +
250 data[i].imag * data[i].imag));
251 return result;
252}

◆ conj()

void mspass::algorithms::deconvolution::ComplexArray::conj ( )

product of complex and real vectors

product of complex and a number

Change vector to complex conjugates.

241 {
242 for (std::size_t i = 0; i < nsamp; i++)
243 data[i].imag = -data[i].imag;
244}

◆ norm2()

double mspass::algorithms::deconvolution::ComplexArray::norm2 ( ) const

Return 2-norm value.

261 {
262 double result = 0;
263 for (std::size_t i = 0; i < nsamp; i++)
264 result +=
265 ((double)data[i].real * data[i].real + data[i].imag * data[i].imag);
266 return sqrt(result);
267}

◆ operator*()

const ComplexArray mspass::algorithms::deconvolution::ComplexArray::operator* ( const ComplexArray other) const

Return the element-by-element product with another complex array.

222 {
223 try {
224 ComplexArray result(*this);
225 result *= other;
226 return result;
227 } catch (...) {
228 throw;
229 };
230}
ComplexArray()
Definition ComplexArray.cc:8

◆ operator*=()

ComplexArray & mspass::algorithms::deconvolution::ComplexArray::operator*= ( const ComplexArray other)

Multiply by another complex array element by element.

150 {
151 if (nsamp != other.nsamp) {
152 stringstream sserr;
153 sserr << "ComplexArray::operator*=: Inconsistent array sizes" << endl
154 << "left hand side array size=" << this->nsamp << endl
155 << "right hand side array size=" << other.nsamp << endl
156 << "Sizes must match to use this operator" << endl;
157 throw MsPASSError(sserr.str(), ErrorSeverity::Invalid);
158 }
159 for (std::size_t i = 0; i < nsamp; i++) {
160 Complex64 z1(data[i].real, data[i].imag);
161 Complex64 z2(other.data[i].real, other.data[i].imag);
162 Complex64 z3(z1 * z2);
163 data[i].real = z3.real();
164 data[i].imag = z3.imag();
165 }
166 return (*this);
167}

◆ operator+()

const ComplexArray mspass::algorithms::deconvolution::ComplexArray::operator+ ( const ComplexArray other) const

Return the element-by-element sum with another complex array.

202 {
203 try {
204 ComplexArray result(*this);
205 result += other;
206 return result;
207 } catch (...) {
208 throw;
209 };
210}

◆ operator+=()

ComplexArray & mspass::algorithms::deconvolution::ComplexArray::operator+= ( const ComplexArray other)

Add another complex array element by element.

120 {
121 if (nsamp != other.nsamp) {
122 stringstream sserr;
123 sserr << "ComplexArray::operator+=: Inconsistent array sizes" << endl
124 << "left hand side array size=" << this->nsamp << endl
125 << "right hand side array size=" << other.nsamp << endl
126 << "Sizes must match to use this operator" << endl;
127 throw MsPASSError(sserr.str(), ErrorSeverity::Invalid);
128 }
129 for (std::size_t i = 0; i < nsamp; i++) {
130 data[i].real += other.data[i].real;
131 data[i].imag += other.data[i].imag;
132 }
133 return *this;
134}

◆ operator-()

const ComplexArray mspass::algorithms::deconvolution::ComplexArray::operator- ( const ComplexArray other) const

Return the element-by-element difference from another complex array.

212 {
213 try {
214 ComplexArray result(*this);
215 result -= other;
216 return result;
217 } catch (...) {
218 throw;
219 };
220}

◆ operator-=()

ComplexArray & mspass::algorithms::deconvolution::ComplexArray::operator-= ( const ComplexArray other)

Subtract another complex array element by element.

135 {
136 if (nsamp != other.nsamp) {
137 stringstream sserr;
138 sserr << "ComplexArray::operator-=: Inconsistent array sizes" << endl
139 << "left hand side array size=" << this->nsamp << endl
140 << "right hand side array size=" << other.nsamp << endl
141 << "Sizes must match to use this operator" << endl;
142 throw MsPASSError(sserr.str(), ErrorSeverity::Invalid);
143 }
144 for (std::size_t i = 0; i < nsamp; i++) {
145 data[i].real -= other.data[i].real;
146 data[i].imag -= other.data[i].imag;
147 }
148 return *this;
149}

◆ operator/()

const ComplexArray mspass::algorithms::deconvolution::ComplexArray::operator/ ( const ComplexArray other) const

Return the element-by-element quotient with another complex array.

232 {
233 try {
234 ComplexArray result(*this);
235 result /= other;
236 return result;
237 } catch (...) {
238 throw;
239 };
240}

◆ operator/=()

ComplexArray & mspass::algorithms::deconvolution::ComplexArray::operator/= ( const ComplexArray other)

Divide by another complex array element by element.

168 {
169 if (nsamp != other.nsamp) {
170 stringstream sserr;
171 sserr << "ComplexArray::operator/=: Inconsistent array sizes" << endl
172 << "left hand side array size=" << this->nsamp << endl
173 << "right hand side array size=" << other.nsamp << endl
174 << "Sizes must match to use this operator" << endl;
175 throw MsPASSError(sserr.str(), ErrorSeverity::Invalid);
176 }
177 for (std::size_t i = 0; i < nsamp; i++) {
178 Complex64 z1(data[i].real, data[i].imag);
179 Complex64 z2(other.data[i].real, other.data[i].imag);
180 Complex64 z3(z1 / z2);
181 data[i].real = z3.real();
182 data[i].imag = z3.imag();
183 }
184 /* For efficiency we scan this array for nans rather than
185 put that in the loop above - standard advice for efficiency in
186 vector operators on modern computers */
187 for (std::size_t i = 0; i < nsamp; ++i) {
188 if (gsl_isnan(data[i].real) || gsl_isnan(data[i].imag)) {
189 stringstream ss;
190 ss << "ComplexArray::operator /=: Division yielded a NaN "
191 << "at sample number " << i << endl
192 << "Denominator used for right hand side=" << other.data[i].real
193 << " + " << other.data[i].imag << " i " << endl;
194 throw MsPASSError(ss.str(), ErrorSeverity::Suspect);
195 }
196 }
197 return (*this);
198}

◆ operator=()

ComplexArray & mspass::algorithms::deconvolution::ComplexArray::operator= ( const ComplexArray parent)

Assignment operator.

94 {
95 if (&parent != this) {
96 this->nsamp = parent.nsamp;
97 this->data =
98 std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
99 for (std::size_t i = 0; i < nsamp; i++) {
100 this->data[i].real = parent.data[i].real;
101 this->data[i].imag = parent.data[i].imag;
102 }
103 }
104 return *this;
105}

◆ operator[]()

Complex64 mspass::algorithms::deconvolution::ComplexArray::operator[] ( int  sample)

Return a pointer to a fortran array containing the data vector.

The array linked to the returned pointer should be created with the new operator and the caller should be sure to use delete [] to free this memory when finished.

Index operator. Cannot make it work by getting the address from reference. Have to call the ptr() function to get the address.

Parameters
sampleis the sample number to return.
Returns
contents of vector at position sample.
117 {
118 return *reinterpret_cast<Complex64 *>(&data[sample].real);
119}

◆ phase()

vector< double > mspass::algorithms::deconvolution::ComplexArray::phase ( ) const

Return stl vector of phase values.

268 {
269 vector<double> result;
270 result.reserve(nsamp);
271 for (std::size_t i = 0; i < nsamp; i++)
272 result.push_back(atan2((double)data[i].imag, (double)data[i].real));
273 return result;
274}

◆ ptr() [1/2]

double * mspass::algorithms::deconvolution::ComplexArray::ptr ( )

Return a pointer to the first interleaved complex value.

111 {
112 return reinterpret_cast<double *>(&data[0].real);
113}

◆ ptr() [2/2]

double * mspass::algorithms::deconvolution::ComplexArray::ptr ( int  sample)

Return a pointer to the interleaved complex value at sample.

114 {
115 return reinterpret_cast<double *>(&data[sample].real);
116}

◆ rms()

double mspass::algorithms::deconvolution::ComplexArray::rms ( ) const

Return rms value.

253 {
254 double result = 0;
255 for (std::size_t i = 0; i < nsamp; i++)
256 result +=
257 ((double)data[i].real * data[i].real + data[i].imag * data[i].imag) /
258 nsamp / nsamp;
259 return sqrt(result);
260}

◆ size()

int mspass::algorithms::deconvolution::ComplexArray::size ( ) const

Return size of the array.

275{ return nsamp; }

The documentation for this class was generated from the following files: