![](/img/trans.png)
[英]Is it possible to access the real and imaginary parts of a complex number with the [] operator in C++
[英]Multiply complex object with real and imaginary parts by a multiplier of type 'double' in C++
我正在嘗試將復雜數組的每個元素乘以一個乘數,以應用傅立葉變換。 我將漢寧 window 濾波器應用於波 function 的復雜文件。
我正在使用 CodeBlocks 在 C++ 中工作,我不斷得到 -->
error: invalid types 'double[int]' for array subscript
我的代碼在這里:
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <cmath>
#define PI 3.14159265359
using namespace std;
class Complex {
public:
Complex();
Complex(double realNum);
Complex(double realNum, double imagNum);
//Complex(double real = 0.0, double imaginary = 0.0); This avoids the 3 above?
Complex(const Complex& obj);
private:
double real;
double imaginary;
};
Complex::Complex(const Complex& obj) {
real = obj.real;
imaginary = obj.imaginary;
}
Complex::Complex () {
real = 0;
imaginary = 0;
}
Complex::Complex (double realNum) {
real = realNum;
imaginary = 0;
}
Complex::Complex (double realNum, double imagNum) {
real = realNum;
imaginary = imagNum;
}
int main () {
Complex *complexArray = new Complex[1000];
ifstream myfile("myfile.txt");
/* this will match the complex numbers of the form - 123.123 + 14.1244 or 123 - 1343.193 and so on,
basically assumes that both the real and the imaginary parts are both doubles*/
regex reg_obj("^[ ]*([-]?\\d+(\\.\\d+)?)\\s*([+-])\\s*(\\d+(\\.\\d+)?)i");
smatch sm;
string line;
int i = 0;
double real, imag;
if (myfile.is_open()) {
while (! myfile.eof()) {
getline(myfile, line);
if(regex_search(line, sm, reg_obj)){
real = stod(sm[1]); // this ([-]?\\d+(\\.\\d+)?) is group 1 and will match the real part of the number
imag = stod(sm[4]); // second group (\\d+(\\.\\d+)?)i is group 4 which matches the imaginary part of the complex number without matching + or - which are taken care of separately because there could be space between the +|- symbol and the imaginary part
if(sm[3]=="-") imag = -imag;
complexArray[i] = Complex(real, imag);
i++;
}
// Hanning Window
for (int i = 0; i < 1000; i++) {
double multiplier = 0.5 * (1 - cos(2*PI*i/999));
complexArray[i] = multiplier[i] * complexArray[i];
}
}
myfile.close();
}else {
cout << "Error. Could not find/open file." ;
}
cout << complexArray << endl;
return 0;
};
我想將復數 object 的每個元素乘以乘法數組中的每個元素。 我不確定這樣做的正確方法。
對於此循環中的初學者
for (int i = 0; i < 1000; i++) {
double multiplier = 0.5 * (1 - cos(2*PI*i/999));
complexArray[i] = multiplier[i] * complexArray[i];
}
變量multiplier
被聲明為雙精度類型的標量 object。 所以你需要寫
complexArray[i] = multiplier * complexArray[i];
代替
complexArray[i] = multiplier[i] * complexArray[i];
您還需要為您的 class 重載operator *
。
例如
class Complex {
public:
Complex();
Complex(double realNum);
Complex(double realNum, double imagNum);
//Complex(double real = 0.0, double imaginary = 0.0); This avoids the 3 above?
Complex(const Complex& obj);
friend const Complex operator *( double, const Complex & );
private:
double real;
double imaginary;
};
//...
const Complex operator *( double value, const Complex &c )
{
return { value * c.real, value * c.imaginary };
}
還有while循環中的條件
while (! myfile.eof()) {
getline(myfile, line);
//...
替代品
while ( getline(myfile, line) ) {
//...
而這個循環
for (int i = 0; i < 1000; i++) {
double multiplier = 0.5 * (1 - cos(2*PI*i/999));
complexArray[i] = multiplier[i] * complexArray[i];
}
應該在while循環之外。 例如
for ( int j = 0; j < i; j++) {
double multiplier = 0.5 * (1 - cos(2*PI*i/999));
complexArray[j] = multiplier * complexArray[j];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.