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

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)
 
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
 

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.

◆ ComplexArray() [3/12]

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

Similar for 32 bit version

◆ 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}

◆ ComplexArray() [5/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
FortranComplex64 d 
)
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}

◆ ComplexArray() [6/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
float *  d 
)
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 
)
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)
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

211 {
212 nsamp = n;
213 if (nsamp > d.size()) {
214 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
215 for (std::size_t i = 0; i < d.size(); i++) {
216 this->data[i].real = d[i];
217 this->data[i].imag = 0.0;
218 }
219 for (std::size_t i = d.size(); i < nsamp; i++) {
220 this->data[i].real = 0.0;
221 this->data[i].imag = 0.0;
222 }
223 } else {
224 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
225 for (std::size_t i = 0; i < nsamp; i++) {
226 this->data[i].real = d[i];
227 this->data[i].imag = 0.0;
228 }
229 }
230}

◆ ComplexArray() [10/12]

template<class T >
mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( int  nsamp,
d 
)
231 {
232 nsamp = n;
233 data = std::shared_ptr<FortranComplex64[]>(new FortranComplex64[nsamp]);
234 for (std::size_t i = 0; i < nsamp; i++) {
235 this->data[i].real = d;
236 this->data[i].imag = 0.0;
237 }
238}

◆ ComplexArray() [11/12]

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

Construct from magnitude and phase arrays.

◆ ComplexArray() [12/12]

mspass::algorithms::deconvolution::ComplexArray::ComplexArray ( const ComplexArray parent)
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 ( )
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
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
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
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)
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
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)
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
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)
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
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)
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)
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
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 ( )
111 {
112 return reinterpret_cast<double *>(&data[0].real);
113}

◆ ptr() [2/2]

double * mspass::algorithms::deconvolution::ComplexArray::ptr ( int  sample)
114 {
115 return reinterpret_cast<double *>(&data[sample].real);
116}

◆ rms()

double mspass::algorithms::deconvolution::ComplexArray::rms ( ) const
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
275{ return nsamp; }

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