简体   繁体   English

将复数 object 与实部和虚部乘以 C++ 中“双”类型的乘数

[英]Multiply complex object with real and imaginary parts by a multiplier of type 'double' in C++

I am trying to multiple every element of a complex array by a multiplying factor, for the application of fourier transform.我正在尝试将复杂数组的每个元素乘以一个乘数,以应用傅立叶变换。 I am applying a hanning window filter to a complex file of a wave function.我将汉宁 window 滤波器应用于波 function 的复杂文件。

I am working in C++ with CodeBlocks, I keep getting -->我正在使用 CodeBlocks 在 C++ 中工作,我不断得到 -->

error: invalid types 'double[int]' for array subscript

My code is here:我的代码在这里:

#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;
};

I would like to multiply each element of the complex object by each element in the multiplication array.我想将复数 object 的每个元素乘以乘法数组中的每个元素。 I'm not sure of the correct way to do this.我不确定这样做的正确方法。

For starters in this loop对于此循环中的初学者

        for (int i = 0; i < 1000; i++) {
            double multiplier = 0.5 * (1 - cos(2*PI*i/999));
            complexArray[i] = multiplier[i] * complexArray[i];
            }

the variable multiplier is declared as a scalar object of the type double.变量multiplier被声明为双精度类型的标量 object。 So you need to write所以你需要写

complexArray[i] = multiplier * complexArray[i];

instead of代替

complexArray[i] = multiplier[i] * complexArray[i];

Also you need to overload the operator * for your class.您还需要为您的 class 重载operator *

For example例如

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

Also the condition in the while loop还有while循环中的条件

    while (! myfile.eof()) {

        getline(myfile, line);
        //...

substitute for替代品

    while ( getline(myfile, line) ) {
        //...

And this loop而这个循环

        for (int i = 0; i < 1000; i++) {
            double multiplier = 0.5 * (1 - cos(2*PI*i/999));
            complexArray[i] = multiplier[i] * complexArray[i];
            }

should be outside the while loop.应该在while循环之外。 For example例如

        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.

相关问题 是否可以使用C ++中的[]运算符访问复数的实部和虚部? - Is it possible to access the real and imaginary parts of a complex number with the [] operator in C++ 在C ++中获取指向复杂向量的实部和虚部的指针 - Getting pointers to the real and imaginary parts of a complex vector in C++ GDB - 访问复数的实部和虚部 - GDB - Accessing real and imaginary parts of a complex number 什么是C ++中的复杂数据类型和Imaginary数据类型? - What is a complex data type and an Imaginary data type in C++? 如何在CUDA中单独获取复杂矩阵的实部和虚部? - How to get the real and imaginary parts of a complex matrix separately in CUDA? 如何提取复特征张量的实部和虚部? - How to extract real and imaginary parts of complex Eigen tensor? 有没有保证很复杂 <Type> 输入实数时,对象的虚部设置为零吗? - Is there any guarantee that complex<Type> object's imaginary part is set to zero when a real number is input? 复杂 <double> 在C ++中-不能使用imag(),complex(),real() - Complex<double> in C++ - can't use imag(), complex(), real() 如何有效地提取 Eigen3 库中复矩阵的实部/虚部? - How to efficiently extract real/imaginary parts of complex matrix in Eigen3 library? C ++:如何根据复杂向量的实部对复杂矩阵进行排序 - C++: how to sort a complex matrix based on real parts of a complex vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM