complex number arithmetic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharadbags
    New Member
    • Dec 2006
    • 14

    complex number arithmetic

    how do we pass a complex no. to a C++ program
    the real and imaginary values are dependent on some variables which are calculated at runtime
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by sharadbags
    how do we pass a complex no. to a C++ program
    the real and imaginary values are dependent on some variables which are calculated at runtime
    have a look at the C++ complex number class
    http://www.eng.cam.ac. uk/help/tpl/languages/C++/complex.html
    http://support.sas.com/documentation/onlinedoc/sasc/doc/cplus/z0274855.htm

    Comment

    • sharadbags
      New Member
      • Dec 2006
      • 14

      #3
      I found that in the standard library
      a structure has been defined for complex no. and not as class as mentioned in 2nd website
      so
      1) do we have to create our own class?
      2)is there a tutorial that can teach how to use functions in complex.h
      are there any examples of it?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by sharadbags
        I found that in the standard library
        a structure has been defined for complex no. and not as class as mentioned in 2nd website
        so
        1) do we have to create our own class?
        2)is there a tutorial that can teach how to use functions in complex.h
        are there any examples of it?
        If you have a modern compiler <complex> should be available - what compiler are you useing?
        try a small program, e.g.
        Code:
        #include <iostream>
        #include <complex>
        using namespace std;
        int main() {
         complex<double> c1(0.0,1.0), c2; // complex numbers with double components
         c2=pow(c1,2.0);
         cout << "The square of " << c1 << " is " << c2 << endl; 
         system("pause");
        }

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          I'm not sure what you found, but Ansi C++ does support a complex datatype. The above links do have a good description of the class. There is a minor change and you should include "complex", not "complex.h" .

          This means you shouldn't have to make your own complex type.

          Comment

          • sharadbags
            New Member
            • Dec 2006
            • 14

            #6
            i have pgain as a variable of data type complex<double>
            now i want only it's real part to be put in a double variable
            how do i do that

            Comment

            • sharadbags
              New Member
              • Dec 2006
              • 14

              #7
              kindly someone reply

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                This tutorial should give you the answers you need.

                Alternatively, you could always create your own class called complex to perform calculations. All it needs is two double variables (real and imaginary) and some operator overriding.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by sharadbags
                  kindly someone reply
                  have a look at this program which determines the roots of a cubic equation
                  a*x^3 + b*x^2 + c*x + d =0

                  Code:
                  #include <iostream>
                  #include <complex>
                  #include <iomanip>
                  #include <math.h>
                  
                  using namespace std;
                  
                  // check root value
                  complex<double> check(double a, double b, double c, double d, complex<double> x)
                  {
                      return  a*x*x*x + b*x*x + c * x + d;
                  }
                  
                  // determine roots of cubic equation
                  //   a*x^3 + b*x^2 + c*x + d =0
                  // algorithm from http://www.1728.com/cubic2.htm
                  void cubic(double a, double b, double c, double d)
                  {
                      double x1, x2, x3, x1r, x1i=0, x2i, x3i=0;
                      cout << a << " " << b << " " << c << " d " << d << endl;
                      double f =((3*c/a) - (b*b/(a*a)))/3;
                      cout << "f " << f << endl;
                      double g=(2*b*b*b/(a*a*a)-9*(b*c/(a*a))+27*d/a)/27;
                      cout << "g " << g << endl;
                      double h=(g*g)/4+f*f*f/27;
                      cout << "h " << h << endl;
                      if(h <= 0)
                         {
                         if(f == 0 && g == 0 &&  h == 0) 
                             // all three roots real and equal
                             x1 = x2 = x3 =-pow(d/a, 1/3.0);
                         else
                             // three real roots
                             {   
                             double i=sqrt((g*g/4)-h);
                             cout << "i " << i << endl;
                             double j=pow(i, 1/3.0);
                             cout << "j " << j << endl;
                             double k=acos(-(g/(2*i)));
                             cout << "k " << k << endl;
                             double l=-j;
                             cout << "l " << l << endl;
                             double m=cos(k/3);
                             cout << "m " << m << endl;
                             double n=sqrt(3.0)*sin(k/3);
                             cout << "n " << n << endl;
                             double p=-(b/(3*a));
                             cout << "p " << p << endl;
                             x1=2*j*cos(k/3)-(b/(3*a));
                             x2=l*(m+n)+p;
                             x3=l*(m-n)+p;
                             }
                         cout << "x1 " << x1 << endl;
                         cout << "x2 " << x2 << endl;
                         cout << "x3 " <<x3 << endl;
                         }
                      else
                         // one real, two complex roots
                         {
                          double r=-(g/2)+sqrt(h);
                          cout << "r " << r << endl;
                          double s=pow(r, 1/3.0);
                          cout << "s " << s << endl;
                          double t=-(g/2)-sqrt(h);
                          cout << "t " << t << endl;
                          double u;
                          if(t<0) u=-pow(-t, 1/3.0);
                          else    u=-pow(t, 1/3.0);
                          cout << "u " << u << endl;
                          x1=(s+u)-(b/(3*a));
                          cout << "x1 " << x1 << endl;
                          x2=-(s+u)/2 -(b/(3*a));
                          x2i= (s-u)*sqrt(3.0)/2;
                          cout << "x2 " << x2 << " +i " << x2i << endl;
                          x3=x2;;
                          x3i=-x2i;
                          cout << "x3 " << x3 << " +i " << x3i << endl;
                         }
                      cout << "check x1 " << check(a, b, c, d, complex<double>(x1, 0)) << endl;
                      cout << "check x2 " << check(a, b, c, d, complex<double>(x2, x3i)) << endl;
                      cout << "check x3 " << check(a, b, c, d, complex<double>(x3, x3i)) << endl;
                  }
                  
                  
                  int main()
                  {
                      cout << "enter a, b, c, and d ";
                      double a, b, c, d;
                      cin >> a >> b >> c >> d;
                      cubic(a, b, c, d);
                      system("pause");
                  }
                  try a=3, b=-10, c=14, d= 27 which has one real root and two complex roots -1, 2.166+i2.0749, 2.166-i2.0749

                  Comment

                  Working...