Thread: Help Required Modifying Code

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool 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?
    Last edited by strokebow; 10-05-2011 at 07:38 AM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    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?
    Last edited by strokebow; 10-05-2011 at 08:14 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to put the declaration of i in the right function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code required...!!!
    By ahmadsami in forum C Programming
    Replies: 2
    Last Post: 12-18-2009, 11:00 AM
  2. Still have a totaling issue after modifying code
    By scmurphy64 in forum C Programming
    Replies: 5
    Last Post: 09-30-2009, 05:54 PM
  3. self modifying code
    By chacham15 in forum C Programming
    Replies: 15
    Last Post: 09-07-2008, 04:05 PM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. Self modifying code
    By orbitz in forum Linux Programming
    Replies: 1
    Last Post: 06-06-2003, 06:09 PM