如何从两个向量(实数和图像)获取复数向量

发布于 2024-07-15 13:18:36 字数 491 浏览 7 评论 0原文

我有两个浮点数向量,我希望它们成为一个复数向量。 我被困住了。 我不介意使用迭代器,但我确信它会重新发现我不知道的轮子。 我的代码是否引导我走向正确的方向?

typedef std::vector<float> CVFloat;
CVFloat vA, vB;
//fil vectors
typedef std::complex<CVFloat> myComplexVector;
myComplexVector* vA_Complex = new myComplexVector(vA, vB);

上面的代码正确地通过了编译器,但是当我想使用迭代器从 myComplexVector 获取单个数字时,我收到错误“未定义符号'const_iterator'”(Borland C++)

myComplexVector::const_iterator it = vA_Complex->begin();

I have two vectors of floats and i want them to become one vector of Complex numbers. I'm stuck. I don't mind using iterators, but i am sure it'd be rediscovering the wheel i'm not informed about. Is my code leading me in the right direction?

typedef std::vector<float> CVFloat;
CVFloat vA, vB;
//fil vectors
typedef std::complex<CVFloat> myComplexVector;
myComplexVector* vA_Complex = new myComplexVector(vA, vB);

The code above is going through the compiler correctly, but when i want to get single numbers from myComplexVector using iterator i get error "Undefined symbol 'const_iterator'" (Borland C++)

myComplexVector::const_iterator it = vA_Complex->begin();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

请别遗忘我 2024-07-22 13:18:36

在这里,您正在创建一个“复杂”对象,其实部和虚部是浮点向量。
也许您真正想做的是创建一个复杂对象的向量,其实部和虚部都是浮点数?

编辑: myComplexVector 不是向量,而是复数。 这就是为什么没有定义它的 const_iterator 的原因。

Here you are creating a "complex" object whose real and imaginary parts are vectors of floats.
Maybe what you actually want to do is creating a vector of complex objects whose real and imaginary parts are floats?

EDIT: myComplexVector is not a vector, is a complex. That's why a const_iterator for it is not defined.

倒带 2024-07-22 13:18:36

为什么不做得更容易呢?

vector< complex<float> > result;
for( int i = 0; i < vA.size(); i++ ) {
    result.push_back( complex<float>( vA[i], vB[i] ) );
}

Whay not do it much much easier?

vector< complex<float> > result;
for( int i = 0; i < vA.size(); i++ ) {
    result.push_back( complex<float>( vA[i], vB[i] ) );
}
木格 2024-07-22 13:18:36

最简单的方法就是编写循环

myComplexVector cv;
for(CVFloat::iterator it1=vA.begin(), end1=vA.end(), 
      it2=vB.begin(), end2=vB.end();
    it1!=end1 && it2 != end2; ++it1, ++it2)
  cv.push_back(std::complex(*it1, *it2));

Edit: ... 并按照 Neil 的建议正确声明 myComplexVector 类型。

The easiest way is just write the loop

myComplexVector cv;
for(CVFloat::iterator it1=vA.begin(), end1=vA.end(), 
      it2=vB.begin(), end2=vB.end();
    it1!=end1 && it2 != end2; ++it1, ++it2)
  cv.push_back(std::complex(*it1, *it2));

Edit: ... and follow Neil's advice to declare myComplexVector type properly.

雪若未夕 2024-07-22 13:18:36

您可以创建一个通用的“zip”函数,将迭代器带到两个向量、转换器函子和输出迭代器:

template< typename at_It1, typename at_It2, typename at_Transform, typename at_Out >
void zip( at_It1 from1, const at_It1 to1, 
          at_It2 from2, const at_It2 to2,
          at_Transform  tranformer,
          at_Out& av_Out ) {
    while( from1 != to1 ) {
        av_Out = transformer( *from1, *from2 );
        ++av_Out; ++from1; ++from2;
    }
}

struct DoubleToComplex {
     complex<double> operator()( const double d1, const double d2 ) const {
         return complex<double>( d1, d2 );
     }
};



zip( vA.begin(), vA.end(),
     vB.begin(), vB.end(),
     DoubleToComplex(),
     std::back_inserter( vTarget ) );

我希望 STL 中有这样一个函数......

You can create a general "zip" function taking iterators to both vectors, and a convertor functor and an output iterator:

template< typename at_It1, typename at_It2, typename at_Transform, typename at_Out >
void zip( at_It1 from1, const at_It1 to1, 
          at_It2 from2, const at_It2 to2,
          at_Transform  tranformer,
          at_Out& av_Out ) {
    while( from1 != to1 ) {
        av_Out = transformer( *from1, *from2 );
        ++av_Out; ++from1; ++from2;
    }
}

struct DoubleToComplex {
     complex<double> operator()( const double d1, const double d2 ) const {
         return complex<double>( d1, d2 );
     }
};



zip( vA.begin(), vA.end(),
     vB.begin(), vB.end(),
     DoubleToComplex(),
     std::back_inserter( vTarget ) );

And I wish there were such a function in the STL...

手心的海 2024-07-22 13:18:36

这没有任何意义:

typedef std::complex<CVFloat> myComplexVector;

你的意思肯定是

typedef std::complex <float> ComplexFloat;
typedef std::vector <ComplexFloat> CFVector;

或者类似的东西吗?

一旦你拥有它,你可以简单地迭代浮点向量(假设它们包含匹配的值)并使用push_back()添加到你的复杂向量中:

CFVector v;

for ( int i = 0; i < vA.size(); i++ ) {
  v.push_back( ComplexFloat( vA[i], vB[i] ) );
}

This doesn't make any sense:

typedef std::complex<CVFloat> myComplexVector;

surely you mean

typedef std::complex <float> ComplexFloat;
typedef std::vector <ComplexFloat> CFVector;

or something similar?

Once ou have it you can simply iterate over the float vectors (assuming they contain matching values) and add to your complex vector using push_back():

CFVector v;

for ( int i = 0; i < vA.size(); i++ ) {
  v.push_back( ComplexFloat( vA[i], vB[i] ) );
}
卷耳 2024-07-22 13:18:36

复数只是一对两个实数ab,它们表示复数a+bi。 你到底想用这两个向量做什么?

A complex number is simply a pair of two real numbers a and b which denote the complex number a+bi. What exactly are you trying to do with the two vectors?

心清如水 2024-07-22 13:18:36

我理解你的问题,你想将实部向量与虚部向量组合成复数向量。

std::complex 有一个模板参数,可让您选择复数各部分的数字表示(即,如果您想要基于 doublefloat 的复数值 甚至一些自定义数字类型...)。 然后,复杂类型根据基础类型定义基本复杂代数。

在您的代码中,您试图基于浮点数向量构造一个复数类型(即实部和虚部为向量的单个复数值),这显然是错误的。 相反,您需要一个 float 类型的复数向量,

您必须执行以下操作:

// ...
typedef std::vector<std::complex<float> > floatComplexVector;
floatComplexVector vA_Complex; // No need to 'new' !?

for (CVFLoat::const_iterator itA = vA.begin(), itB = vB.begin(); 
     itA != vA.end() && itB != vB.end(); 
     ++itA,++itB)
  vA_Complex.push_back(std::complex<float>(*itA, *itB));

备注:

  • 在大多数情况下,无需创建容器例如堆上的向量(即使用 new) 尽量避免这种情况。

  • 不幸的是,C++ 标准库不包含组合迭代器(即“自动”组合两个序列的迭代器),这将允许更优雅的解决方案(请参阅 Boost Zip 迭代器 的总体思路)。

I understand your question that you want to combine a vector of real parts with a vector of imaginary parts into a vector of complex numbers.

std::complex has one template parameter which lets you chose the numerical represenation of the parts of the complex (i.e. if you want complex values based on double or float or even some custom number type...). The complex type then defines basic complex algebra in terms of the underlying type.

In your code you are trying to construct a complex type based on a vector of floats (i.e. a single complex value having a real and imaginary part being a vector), which is obviously wrong. Instead you want a vector of complex numbers of type float

You'd have to do something like:

// ...
typedef std::vector<std::complex<float> > floatComplexVector;
floatComplexVector vA_Complex; // No need to 'new' !?

for (CVFLoat::const_iterator itA = vA.begin(), itB = vB.begin(); 
     itA != vA.end() && itB != vB.end(); 
     ++itA,++itB)
  vA_Complex.push_back(std::complex<float>(*itA, *itB));

Remarks:

  • In most cases it isn't necessary to create containers such as vectors on the heap (i.e. using new) Try to avoid this.

  • Unfortunately the C++ standard library doesnt contain a combining iterator (i.e. one that "automatically" combines two sequences) which would allow a more elegant solution (see Boost Zip iterator for a general idea).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文