# Thread: Help Required Modifying Code

1. ## Help Required Complex Number

Hi,

I found this code posted on the internet and I am using it (or hope to use it) to aid me. (From: Fast Numerical Integration - CodeProject)

However, I am having problems with imaginary/complex numbers.

Main.cpp
Code:
#include <iostream>
#include <cmath>
#include <iomanip>
#include "DEIntegrator.h"
#include <complex>

int main()
{
std::complex<double> i (0, 1);
DemoFunction1 f1;
int evaluations;
double errorEstimate;
std::cout << std::setprecision(15);
double integral = DEIntegrator<DemoFunction1>::Integrate(f1, 0, 10, 1e-6, evaluations, errorEstimate);
std::cout << integral << ", " << errorEstimate << ", " << evaluations << "\n";

system("pause");

return 0;
}

class DemoFunction1
{
public:
double operator()(double x) const
{
return (1/x) * ( 1 - (pow(((x-1)/(x+1)),(5))) * (pow(((x-i)/(x+i)),(1))) );
}
};
I am getting the error: 'i' : undeclared identifier

Snippet from DEIntegrator.h
Code:
#ifndef DEINTEGRATOR_H
#define DEINTEGRATOR_H

#include "DEIntegrationConstants.h"
#include <float.h>
#include <complex>

/*! Numerical integration in one dimension using the double expontial method of M. Mori. */
template<class TFunctionObject>
class DEIntegrator
{
public:
/*! Integrate an analytic function over a finite interval. @return The value of the integral. */
static double Integrate (const TFunctionObject& f, double a, double b, double targetAbsoluteError, int& numFunctionEvaluations, double& errorEstimate)
{
// Apply the linear change of variables x = ct + d
// $$\int_a^b f(x) dx = c \int_{-1}^1 f( ct + d ) dt$$
// c = (b-a)/2, d = (a+b)/2

double c = 0.5*(b - a);
double d = 0.5*(a + b);

return IntegrateCore (f, c, d, targetAbsoluteError, numFunctionEvaluations, errorEstimate, doubleExponentialAbcissas, doubleExponentialWeights);
}
etc.
// More code
etc.

Any help would be much appreciated?

2. This is the first time i've ever used complex numbers in c++ and its proving to be a bit of a nightmare.

The problem is where I ceate the function class I make use of a complex number.

Code:
class DemoFunction1
{
public:
double operator()(double x) const
{
return (1/x) * ( 1 - (pow(((x-1)/(x+1)),(5))) * (pow(((x-cmpi)/(x+cmpi)),(1))) );
}
};
where:

cmpi is sqrt(-1) as given by:

Code:
std::complex<double> cmpi (0, 1);
Any ideas? what is demo function actually returning even?

3. You need to put the declaration of i in the right function.