Multiplying an int with a complex double in a for loop

Hello Everybody,

I am trying to calculate a complex integral using the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <complex>
#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

complex<double> fct(complex<double> z);

int main()
{
  complex<double> a, b, h, integral, sum1, sum2, sumt, error, x;
  double n;

  cout << "Die Integrationsgrenzen und Die Anzahl der Schritte: " << endl;
  cin >> a;
  cin >> b;
  cin >> n;

  h = (b-a)/n;
  sum1 = fct(a) + fct(b);

      for(int l = 0; l < n; l++)
	{
	  if(l%2 == 0)
	    {
	      sum1 += fct(a + l * h);
	    }
	  else
	    {
	      sum2 += fct( a + l * h);
	    }
	  sumt = h/3. * ( fct(a) + fct(b) + 4. * sum2 + 2. * sum1);
	}

  cout << "the Integral is: " <<sumt;

  return 0;
}

complex<double> fct(double z)
{
  return pow(z,2.);
}


But it fails to compile because I am multiplying in the for loop an integer with a complex number.

Now how can I make the code work?
Here is an attempt at a solution, it indeed works. Any comment or idea is appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <complex>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <type_traits>

template< typename T, typename SCALAR > inline
typename std::enable_if< !std::is_same<T,SCALAR>::value, std::complex<T> >::type
operator* ( const std::complex<T>& c, SCALAR n ) { return c * T(n) ; }

template< typename T, typename SCALAR > inline
typename std::enable_if< !std::is_same<T,SCALAR>::value, std::complex<T> >::type
operator* ( SCALAR n, const std::complex<T>& c ) { return T(n) * c ; }

template< typename T, typename SCALAR > inline
typename std::enable_if< !std::is_same<T,SCALAR>::value, std::complex<T> >::type
operator/ ( const std::complex<T>& c, SCALAR n ) { return c / T(n) ; }

template< typename T, typename SCALAR > inline
typename std::enable_if< !std::is_same<T,SCALAR>::value, std::complex<T> >::type
operator/ ( SCALAR n, const std::complex<T>& c ) { return T(n) / c ; }



using namespace std;

complex<double> fct(complex<double> z);

int main()
{
  complex<double> a, b, h, integral, sum1, sum2, sumt, error, z;
  int n;

  cout << "Die Integrationsgrenzen und Die Anzahl der Schritte: " << endl;
    a = {1,1};
    b = {3,3};
    n = 1000;

  h = (b-a)/n;
  sum1 = fct(a) + fct(b);

      for(int l = 0; l < n; l++)
	{
	  if(l%2 == 0)
	    {
	      sum1 += fct(a + l * h);
	    }
	  else
	    {
	      sum2 += fct( a + l * h);
	    }
	  sumt = h/3. * ( fct(a) + fct(b) + 4. * sum2 + 2. * sum1);
	}
	cout << a <<" "<< b << endl;
    cout << h << endl;
  cout << "the Integral is: " <<sumt;

  return 0;
}

complex<double> fct(complex<double> z)
{
  return z;
}
Topic archived. No new replies allowed.