473,388 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

find the highest value within an array of complex numbers

I would like to find the highest value within an array of complex numbers.

Let assume this is the function I want to modify in order to get the highest values of array y :

Expand|Select|Wrap|Line Numbers
  1. //iterDelayEst.cpp
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6.  
  7. double iterDelayEst(int n,CArray& x, CArray& y)
  8. {
  9. /**********************************************constants************************************************
  10. *******************************************************************************************************/
  11. //exit if uncertainty below threshold
  12.     double thr_samples = 1e-7;
  13.  
  14.     //exit after fixed number of iterations
  15.     double nIter = 25;
  16.     fft(x);
  17.     fft(y);
  18.  
  19.     //frequency domain representation of signals
  20.     std::vector<double> tau;
  21.  
  22.     auto f = binFreq(n);
  23.  
  24.     std::vector<double> e;
  25.     Complex nf3(0.0,0.0);
  26.  
  27.     int j;
  28.  
  29.     for ( j = 0 ; j < n ; j++ )
  30.  
  31.     {
  32.         auto nf1 = ((x * x.apply(std::conj)) * (y * y.apply(std::conj)));
  33.         nf3 += nf1[j];
  34.     }
  35.  
  36.         auto nf2 =std::sqrt(nf3);
  37.         auto nf =nf2/(double)n;
  38.         cout << "nf3" << nf3 <<endl;
  39.         cout << "nf2" << nf2 <<endl;
  40.         cout << "nf" << nf <<endl;
  41.     double x1=-1;
  42.     double x2=-1;
  43.     double x3=-1;
  44.     double y1=-1;
  45.     double y2=-1;
  46.     double y3=-1;
  47.     int i;
  48.         int k=0;
  49.         Complex MAX =0;
  50.     /***************************************iteration loop**********************************************
  51.          **************************************************************************************************/
  52.  
  53.     //for(i=0; i<nIter; i++)
  54.                  std::vector<Complex> v;             
  55.                  x = x.apply(std::conj);
  56.          y *= x;
  57.              ifft(y);
  58.              y =std::abs(y);
  59.          y=y/nf;
  60.  
  61.          for ( i = 0 ; i < n ; i++ ){
  62.                         cout << "y[" << i <<"] =" << y[i] << endl;
  63.                         v.push_back(y);
  64.                      }
  65.  
  66.                  std::vector<Complex>::iterator result ;
  67.                  const Complex maxim = *max_element(v.begin(), v.end());
  68.                  for(result = std::find(v.begin(), v.end(), maxim); result != v.end(); result = std::find(result + 1, v.end(), maxim))
  69.                  {
  70.                  cout << result - v.begin()+1 << endl;
  71.                  //printf("found at index %f\n", result -v.begin() +1 );
  72.                  }
  73.  
  74.  
  75.  
  76.  
  77.     /**************************detect peak********************************************************/
  78.  
  79. }

[code]//main.cpp
#include <complex>
#include <iostream>
#include <valarray>
#include <malloc.h>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cmath>
#include "fft.cpp"
#include "cs_delay.cpp"
#include "iterDelayEst.cpp"
using namespace std;
char filename[] = "myfile.txt";
char filename2[] = "myfile2.txt";


typedef std::complex<double> Complex;
typedef std::valarray <Complex> CArray;
/************************************************** *********************************************
* function declarations
************************** ************************************************** ****** ***********/
void fft(CArray& x);
void ifft(CArray& x);
std::vector<double> binFreq(int n);
void cs_delay(CArray& x, int rate_hz, int delay_s, int n);
double iterDelayEst(int n, CArray& x, CArray& x2);

int main()

{
int dTest_samples;
int cTest;
int n=32;
int i;
int j;
double x [n];
double y [n];
int rate_hz=1;
int delay_s=30;

/*****************************getting x*******************************/

string line;
double Result;
ifstream myfile (filename);
if (myfile.is_open())
{
for ( i = 0 ; (i < n) && (myfile >> x[i]) ; ++i)

cout << line << '\n';
stringstream convert(line);

if ( !(convert >> Result) )
Result = 0;
x[i]=Result;


}
else cout << "Unable to open file";
/*****************************getting y******************************/
string line2;
double Result2;
ifstream myfile2 (filename2);
if (myfile2.is_open())
{
for ( i = 0 ; (i < n) && (myfile2 >> x[i]) ; ++i)

cout << line2 << '\n';
stringstream convert(line2);

if ( !(convert >> Result2) )
Result2 = 0;
y[i]=Result2;


}
else cout << "Unable to open file2";
/************************************************** *********************/
/*********************for x******************/
Complex test[n];

for ( i = 0 ; i < n ; ++i )
test[i] = x[i];

CArray data(test,n);
/*********************for y******************/
Complex test2[n];

for ( j = 0 ; j < n ; ++j )
test2[j] = y[j];

CArray data2(test2,n);

// forward fft
fft(data);

std::cout << "fft" << std::endl;
for (int i = 0; i <n; ++i)
{
cout << data[i] << endl;
}

// inverse fft
ifft(data);

std::cout << std::endl << "ifft" << std::endl;
for (int i = 0; i <n; ++i)
{
std::cout << data[i] << std::endl;
}

cs_delay(data, 1, 6, n);
for (int i = 0; i <n; ++i)
{
std::cout << data[i] << std::endl;
}

iterDelayEst(n, data, data2 );

return 0;
}[code]



Expand|Select|Wrap|Line Numbers
  1. //fft.cpp
  2. using namespace std;
  3. const double PI = 3.141592653589793238460;
  4.  
  5.  
  6. /************************************************************************************************
  7. *                      Cooley–Tukey FFT (in-place, divide-and-conquer)                          *
  8. *              Higher memory requirements and redundancy although more intuitive                *
  9. ************************************************************************************************/
  10.  
  11.  
  12. typedef std::complex<double> Complex;
  13. typedef std::valarray<Complex> CArray;
  14. // Cooley–Tukey FFT (in-place, divide-and-conquer)
  15. // Higher memory requirements and redundancy although more intuitive
  16. void fft(CArray& x)
  17. {
  18.     const size_t N = x.size();
  19.     if (N <= 1) return;
  20.  
  21.     // divide
  22.     CArray even = x[std::slice(0, N/2, 2)];
  23.     CArray  odd = x[std::slice(1, N/2, 2)];
  24.  
  25.     // conquer
  26.     fft(even);
  27.     fft(odd);
  28.  
  29.     // combine
  30.     for (size_t k = 0; k < N/2; ++k)
  31.     {
  32.         Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
  33.         x[k    ] = even[k] + t;
  34.         x[k+N/2] = even[k] - t;
  35.     }
  36. }
  37. // inverse fft (in-place)
  38. void ifft(CArray& x)
  39.  
  40. {
  41.     // conjugate the complex numbers
  42.     x = x.apply(std::conj);
  43.  
  44.     // forward fft
  45.     fft( x );
  46.  
  47.     // conjugate the complex numbers again
  48.     x = x.apply(std::conj);
  49.  
  50.     // scale the numbers
  51.     x /= x.size();
  52. }
  53.  

Expand|Select|Wrap|Line Numbers
  1. //cs_delay.cpp
  2. using namespace std;
  3.  
  4. typedef std::complex<double> Complex;
  5. typedef std::valarray<Complex> CArray;
  6.  
  7. void cs_delay(CArray& x, int rate_hz, int delay_s, int n)
  8. {
  9.  
  10. const size_t N = x.size();
  11.     if (N <= 1) return;
  12. int j;
  13. double cycLen_s;
  14. double nCyc;
  15. double* f = new double[n];
  16. double* phase = new double[n];
  17. Complex* rot = new Complex[n];
  18. cycLen_s = n/rate_hz;
  19. nCyc = delay_s / cycLen_s;
  20. /*************************************************************/
  21. for ( j = 0 ; j < n ; j++ ){
  22.  
  23.         f[j] =j+floor(n/2);
  24.         f[j] =fmod(f[j], n);
  25.         f[j] =f[j]-floor(n/2);
  26.         phase[j] = -2 * PI * f[j] * nCyc;
  27.         rot[j] = exp(1i*phase[j]);
  28.         std::cout << "rot["<<j<<"] ="<<rot[j] <<std::endl;
  29.         fft(x);
  30.         x *= rot[j];
  31.         ifft(x);
  32.         }
  33. /*************************************************************/
  34.         delete [] f;
  35.         delete [] phase;
  36.         delete [] rot;
  37. }

Expand|Select|Wrap|Line Numbers
  1. //binFreg.cpp
  2. std::vector<double> binFreq(int n)
  3. {
  4.    int j;
  5.    std::vector<double> f(n);
  6.  
  7.    for ( j = 0 ; j < n ; j++ ){
  8.  
  9.       f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
  10.    }
  11.    return f;
  12. }

While trying to run the program I am receiving following error messages :
Output:
Expand|Select|Wrap|Line Numbers
  1. serge@ubuntu:~/Downloads/OpenCV/opencv-2.4.9/build$ g++ -o myfft  myfft.cpp -std=c++14
  2. In file included from myfft.cpp:16:0:
  3. iterDelayEst.cpp: In function ‘double iterDelayEst(int, CArray&, CArray&)’:
  4. iterDelayEst.cpp:67:38: error: no matching function for call to ‘std::vector<std::complex<double> >::push_back(CArray&)’
  5.                          v.push_back(y);
  6.                                       ^
  7. iterDelayEst.cpp:67:38: note: candidates are:
  8. In file included from /usr/include/c++/4.9/vector:64:0,
  9.                  from /usr/include/c++/4.9/bits/random.h:34,
  10.                  from /usr/include/c++/4.9/random:49,
  11.                  from /usr/include/c++/4.9/bits/stl_algo.h:66,
  12.                  from /usr/include/c++/4.9/algorithm:62,
  13.                  from /usr/include/c++/4.9/valarray:38,
  14.                  from myfft.cpp:3:
  15. /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::complex<double>; _Alloc = std::allocator<std::complex<double> >; std::vector<_Tp, _Alloc>::value_type = std::complex<double>]
  16.        push_back(const value_type& __x)
  17.        ^
  18. /usr/include/c++/4.9/bits/stl_vector.h:913:7: note:   no known conversion for argument 1 from ‘CArray {aka std::valarray<std::complex<double> >}’ to ‘const value_type& {aka const std::complex<double>&}’
  19. /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::complex<double>; _Alloc = std::allocator<std::complex<double> >; std::vector<_Tp, _Alloc>::value_type = std::complex<double>]
  20.        push_back(value_type&& __x)
  21.        ^
  22. /usr/include/c++/4.9/bits/stl_vector.h:931:7: note:   no known conversion for argument 1 from ‘CArray {aka std::valarray<std::complex<double> >}’ to ‘std::vector<std::complex<double> >::value_type&& {aka std::complex<double>&&}’
  23. In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
  24.                  from /usr/include/c++/4.9/bits/char_traits.h:39,
  25.                  from /usr/include/c++/4.9/ios:40,
  26.                  from /usr/include/c++/4.9/istream:38,
  27.                  from /usr/include/c++/4.9/sstream:38,
  28.                  from /usr/include/c++/4.9/complex:45,
  29.                  from myfft.cpp:1:
  30. /usr/include/c++/4.9/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >]’:
  31. /usr/include/c++/4.9/bits/stl_algo.h:5473:30:   required from ‘_ForwardIterator std::__max_element(_ForwardIterator, _ForwardIterator, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
  32. /usr/include/c++/4.9/bits/stl_algo.h:5496:41:   required from ‘_FIter std::max_element(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >]’
  33. iterDelayEst.cpp:71:71:   required from here
  34. /usr/include/c++/4.9/bits/predefined_ops.h:42:23: error: no match for ‘operator<’ (operand types are ‘std::complex<double>’ and ‘std::complex<double>’)
  35.        { return *__it1 < *__it2; }
  36.                        ^
  37. /usr/include/c++/4.9/bits/predefined_ops.h:42:23: note: candidates are:
  38. In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
  39.                  from /usr/include/c++/4.9/bits/char_traits.h:39,
  40.                  from /usr/include/c++/4.9/ios:40,
  41.                  from /usr/include/c++/4.9/istream:38,
  42.                  from /usr/include/c++/4.9/sstream:38,
  43.                  from /usr/include/c++/4.9/complex:45,
  44.                  from myfft.cpp:1:
  45. /usr/include/c++/4.9/bits/stl_iterator.h:837:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
  46.      operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
  47.      ^
  48. /usr/include/c++/4.9/bits/stl_iterator.h:837:5: note:   template argument deduction/substitution failed:
  49. In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
  50.                  from /usr/include/c++/4.9/bits/char_traits.h:39,
  51.                  from /usr/include/c++/4.9/ios:40,
  52.                  from /usr/include/c++/4.9/istream:38,
  53.                  from /usr/include/c++/4.9/sstream:38,
  54.                  from /usr/include/c++/4.9/complex:45,
  55.                  from myfft.cpp:1:
  56. /usr/include/c++/4.9/bits/predefined_ops.h:42:23: note:   ‘std::complex<double>’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
  57.        { return *__it1 < *__it2; }
  58.                        ^
  59. In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
  60.                  from /usr/include/c++/4.9/bits/char_traits.h:39,
  61.                  from /usr/include/c++/4.9/ios:40,
  62.                  from /usr/include/c++/4.9/istream:38,
  63.                  from /usr/include/c++/4.9/sstream:38,
  64.                  from /usr/include/c++/4.9/complex:45,
  65.                  from myfft.cpp:1:
  66. /usr/include/c++/4.9/bits/stl_iterator.h:844:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
  67.      operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
  68.      ^
  69. /usr/include/c++/4.9/bits/stl_iterator.h:844:5: note:   template argument deduction/substitution failed:
  70. In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
  71.                  from /usr/include/c++/4.9/bits/char_traits.h:39,

Can someone help ?
Jul 10 '16 #1

✓ answered by Sergenj

I found a much elegant way to do that :

Expand|Select|Wrap|Line Numbers
  1. #include <cstddef>
  2. #include <numeric>
  3. #include <complex>
  4. #include <valarray>
  5. #include <iostream>
  6.  
  7. using Complex = std::complex<double>;
  8. using CArray = std::valarray<Complex>;
  9.  
  10. std::size_t max_index(CArray const& y) {
  11.     struct acc_t {
  12.         double max_value;
  13.         std::size_t max_idx, current_idx;
  14.  
  15.         constexpr acc_t next() const { return {max_value, max_idx, current_idx + 1}; }
  16.         constexpr acc_t next_with(Complex const c) const {
  17.             return {c.real(), current_idx, current_idx + 1};
  18.         }
  19.     };
  20.  
  21.     return std::accumulate(
  22.         std::begin(y), std::end(y), acc_t{},
  23.         [](acc_t const acc, Complex const c) {
  24.             return c.real() < acc.max_value
  25.               ? acc.next()
  26.               : acc.next_with(c);
  27.         }
  28.     ).max_idx;
  29. }
  30.  
  31. void test(CArray const& y) {
  32.     auto const max_idx = max_index(y);
  33.     std::cout << "The max is " << y[max_idx] << " at index " << max_idx << '\n';
  34. }
  35.  
  36. int main() {
  37.  
  38.     test({{1, 2}, {3, 4}, { 2, 0}, {9, 0}, {7, 0}, {9, 0}});
  39. }

6 1918
weaknessforcats
9,208 Expert Mod 8TB
I'm not sure exactly what you are doing. To find the max complex number in an array should look like:

Expand|Select|Wrap|Line Numbers
  1. class Complex
  2. {
  3.     double X;
  4.     double Y;
  5. public:
  6.     bool operator<(Complex& first);
  7. };
  8.  
  9. int main()
  10. {
  11.     vector<Complex> array;
  12.  
  13.     vector<Complex>::iterator itr  = array.begin();
  14.  
  15.     Complex Max = array[0];
  16.  
  17.     while (itr != array.end())
  18.     {
  19.         if (Max < *itr)
  20.         {
  21.             Max = *itr;
  22.         }
  23.                 ++itr;
  24.  
  25.     }
  26.  
  27.     //Max has the highest value
  28.  
  29. }
What am I missing?
Jul 10 '16 #2
Sorry, I am a beginner in c++,

Indeed,the title should be "How to find indices corresponding to the highest value in an array of complex numbers"
I would like to translate the following matlab code into C++:
ix = find(xcorr == max(xcorr));

the matlab code returns indices of elements corresponding to the highest value
xcorr is an array of complex numbers

I have tried to put your example in practice but it seems like I can't print that highest element:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <complex>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <stdlib.h>
  6. #include <fstream>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. class Complex
  12. {
  13.     double X;
  14.     double Y;
  15. public:
  16.     bool operator<(Complex& first);
  17.  
  18. };
  19.  
  20. std::ostream& operator<<(std::ostream& os, const Complex& obj)
  21. {
  22.     os << obj;
  23.     return os;
  24. }
  25.  
  26. int main()
  27. {
  28.     vector<Complex> array;
  29.     array.push_back((1,1));
  30.     array.push_back((-1,-1));
  31.     array.push_back((7,7));
  32.     array.push_back((3,3));
  33.     array.push_back((10,10));
  34.  
  35.     vector<Complex>::iterator itr  = array.begin();
  36.  
  37.     Complex Max = array[0];
  38.  
  39.     while (itr != array.end())
  40.     {
  41.         if (Max < *itr)
  42.         {
  43.             Max = *itr;
  44.  
  45.         }
  46.          ++itr;
  47.          //printf("found at index %f\n", Max );
  48.  
  49.     }
  50.  
  51.     //Max has the highest value
  52.  
  53. }
  54.  

Output :
Jul 10 '16 #3
weaknessforcats
9,208 Expert Mod 8TB
You can't print your element because obj is a user-defined type. The std::operator<< only works with built-in types.

You would write member functions to access the X and Y of the class Complex and then call those functions:

Expand|Select|Wrap|Line Numbers
  1. class Complex
  2. {
  3.     double X;
  4.     double Y;
  5. public:
  6.     bool operator<(Complex& first);
  7.     double GetReal() { return X; };
  8.     double GetImaginary() { return Y; };
  9.  
  10.  
  11. };
  12.  
and then in main():

Expand|Select|Wrap|Line Numbers
  1. cout << Max.GetReal() << Max.GetImaginary() << endl;
Jul 10 '16 #4
Hi,

it seems to not work :

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <complex>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <stdlib.h>
  6. #include <fstream>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. class Complex
  12. {
  13.     double X;
  14.     double Y;
  15. public:
  16.     bool operator<<(Complex& first);
  17.     double GetReal() { return X; };
  18.     double GetImaginary() { return Y; };
  19.  };
  20.  
  21.  
  22. int main()
  23. {
  24.     vector<Complex> array;
  25.     array.push_back((1,1));
  26.     array.push_back((-1,-1));
  27.     array.push_back((7,7));
  28.     array.push_back((3,3));
  29.     array.push_back((10,10));
  30.  
  31.     vector<Complex>::iterator itr  = array.begin();
  32.  
  33.     Complex Max = array[0];
  34.  
  35.     while (itr != array.end())
  36.     {
  37.         if (Max << *itr)
  38.         {
  39.             Max = *itr;
  40.  
  41.         }
  42.          ++itr;
  43.          cout << Max.GetReal() << Max.GetImaginary() << endl;
  44.  
  45.     }
  46.  
  47. }

I am getting following error messages :

Expand|Select|Wrap|Line Numbers
  1. main.cpp: In function 'int main()':
  2. main.cpp:25:24: warning: left operand of comma operator has no effect [-Wunused-value]
  3.      array.push_back((1,1));
  4.                         ^
  5. main.cpp:25:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
  6.      array.push_back((1,1));
  7.                           ^
  8. In file included from /usr/local/include/c++/6.1.0/vector:64:0,
  9.                  from main.cpp:3:
  10. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  11.        push_back(const value_type& __x)
  12.        ^~~~~~~~~
  13. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
  14. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  15.        push_back(value_type&& __x)
  16.        ^~~~~~~~~
  17. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
  18. main.cpp:26:26: warning: left operand of comma operator has no effect [-Wunused-value]
  19.      array.push_back((-1,-1));
  20.                           ^
  21. main.cpp:26:28: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
  22.      array.push_back((-1,-1));
  23.                             ^
  24. In file included from /usr/local/include/c++/6.1.0/vector:64:0,
  25.                  from main.cpp:3:
  26. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  27.        push_back(const value_type& __x)
  28.        ^~~~~~~~~
  29. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
  30. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  31.        push_back(value_type&& __x)
  32.        ^~~~~~~~~
  33. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
  34. main.cpp:27:24: warning: left operand of comma operator has no effect [-Wunused-value]
  35.      array.push_back((7,7));
  36.                         ^
  37. main.cpp:27:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
  38.      array.push_back((7,7));
  39.                           ^
  40. In file included from /usr/local/include/c++/6.1.0/vector:64:0,
  41.                  from main.cpp:3:
  42. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  43.        push_back(const value_type& __x)
  44.        ^~~~~~~~~
  45. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
  46. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  47.        push_back(value_type&& __x)
  48.        ^~~~~~~~~
  49. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
  50. main.cpp:28:24: warning: left operand of comma operator has no effect [-Wunused-value]
  51.      array.push_back((3,3));
  52.                         ^
  53. main.cpp:28:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
  54.      array.push_back((3,3));
  55.                           ^
  56. In file included from /usr/local/include/c++/6.1.0/vector:64:0,
  57.                  from main.cpp:3:
  58. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  59.        push_back(const value_type& __x)
  60.        ^~~~~~~~~
  61. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
  62. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  63.        push_back(value_type&& __x)
  64.        ^~~~~~~~~
  65. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
  66. main.cpp:29:25: warning: left operand of comma operator has no effect [-Wunused-value]
  67.      array.push_back((10,10));
  68.                          ^~
  69. main.cpp:29:28: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
  70.      array.push_back((10,10));
  71.                             ^
  72. In file included from /usr/local/include/c++/6.1.0/vector:64:0,
  73.                  from main.cpp:3:
  74. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  75.        push_back(const value_type& __x)
  76.        ^~~~~~~~~
  77. /usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
  78. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
  79.        push_back(value_type&& __x)
  80.        ^~~~~~~~~
  81. /usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
Jul 11 '16 #5
weaknessforcats
9,208 Expert Mod 8TB
You must use functions the way they were designed.

This code:

Expand|Select|Wrap|Line Numbers
  1. array.push_back((1,1));
  2.  
needs a push_back function that has integer arguments, In this program the push_back requires a Complex argument. You must build the Complex object then you can do the push_back:

Expand|Select|Wrap|Line Numbers
  1. Complex obj;
  2.         obj.SetReal(1);
  3.         obj.SetImaginary(1);
  4.  
  5. array.push_back(obj);
  6.  
You will need to write the SetReal SetImaginary member function s of Complex. Just follow the pattern for the GetReal and GetImaginary. You can use a constructor instead.

Keep posting. You are making progress.

I did not go through the whole code. Fix these errors and repost
Jul 11 '16 #6
I found a much elegant way to do that :

Expand|Select|Wrap|Line Numbers
  1. #include <cstddef>
  2. #include <numeric>
  3. #include <complex>
  4. #include <valarray>
  5. #include <iostream>
  6.  
  7. using Complex = std::complex<double>;
  8. using CArray = std::valarray<Complex>;
  9.  
  10. std::size_t max_index(CArray const& y) {
  11.     struct acc_t {
  12.         double max_value;
  13.         std::size_t max_idx, current_idx;
  14.  
  15.         constexpr acc_t next() const { return {max_value, max_idx, current_idx + 1}; }
  16.         constexpr acc_t next_with(Complex const c) const {
  17.             return {c.real(), current_idx, current_idx + 1};
  18.         }
  19.     };
  20.  
  21.     return std::accumulate(
  22.         std::begin(y), std::end(y), acc_t{},
  23.         [](acc_t const acc, Complex const c) {
  24.             return c.real() < acc.max_value
  25.               ? acc.next()
  26.               : acc.next_with(c);
  27.         }
  28.     ).max_idx;
  29. }
  30.  
  31. void test(CArray const& y) {
  32.     auto const max_idx = max_index(y);
  33.     std::cout << "The max is " << y[max_idx] << " at index " << max_idx << '\n';
  34. }
  35.  
  36. int main() {
  37.  
  38.     test({{1, 2}, {3, 4}, { 2, 0}, {9, 0}, {7, 0}, {9, 0}});
  39. }
Jul 23 '16 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
5
by: Ada | last post by:
hello folks, just wanna get some feedback from someone here who has more experience. :-) i'm developing a small app that require searching for the highest index within a directory. i was...
7
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
2
by: Ros | last post by:
Peeps, I need help with trying to find a value in array of arrays. Public Module myModule Private Site_Access() As Array Property Set_SA() Get Return Site_Access End Get
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have an int array and was just wondering if there is a way to get the highest value in the array? for instance, int myValues = new int { 0, 1, 2 } highest value is 2. thanks,
9
by: X106 | last post by:
how to using the technique of Array so that I can input 5 values and then the program will find out which value is the large one. Who can help me???? THANK!!!!!
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
1
by: mimic01 | last post by:
I can't get it to work to find the highest value in the given matrix. Any ideas? (total C beginner here). Also in this one i get the user to fill in the array values but which would be the fastest...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.