473,397 Members | 2,077 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,397 software developers and data experts.

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
Feb 15 '07 #1
8 2924
horace1
1,510 Expert 1GB
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
Feb 15 '07 #2
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?
Feb 16 '07 #3
horace1
1,510 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <complex>
  3. using namespace std;
  4. int main() {
  5.  complex<double> c1(0.0,1.0), c2; // complex numbers with double components
  6.  c2=pow(c1,2.0);
  7.  cout << "The square of " << c1 << " is " << c2 << endl; 
  8.  system("pause");
  9. }
  10.  
Feb 16 '07 #4
RRick
463 Expert 256MB
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.
Feb 16 '07 #5
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
Feb 16 '07 #6
kindly someone reply
Feb 16 '07 #7
Ganon11
3,652 Expert 2GB
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.
Feb 16 '07 #8
horace1
1,510 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <complex>
  4. #include <iomanip>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8.  
  9. // check root value
  10. complex<double> check(double a, double b, double c, double d, complex<double> x)
  11. {
  12.     return  a*x*x*x + b*x*x + c * x + d;
  13. }
  14.  
  15. // determine roots of cubic equation
  16. //   a*x^3 + b*x^2 + c*x + d =0
  17. // algorithm from http://www.1728.com/cubic2.htm
  18. void cubic(double a, double b, double c, double d)
  19. {
  20.     double x1, x2, x3, x1r, x1i=0, x2i, x3i=0;
  21.     cout << a << " " << b << " " << c << " d " << d << endl;
  22.     double f =((3*c/a) - (b*b/(a*a)))/3;
  23.     cout << "f " << f << endl;
  24.     double g=(2*b*b*b/(a*a*a)-9*(b*c/(a*a))+27*d/a)/27;
  25.     cout << "g " << g << endl;
  26.     double h=(g*g)/4+f*f*f/27;
  27.     cout << "h " << h << endl;
  28.     if(h <= 0)
  29.        {
  30.        if(f == 0 && g == 0 &&  h == 0) 
  31.            // all three roots real and equal
  32.            x1 = x2 = x3 =-pow(d/a, 1/3.0);
  33.        else
  34.            // three real roots
  35.            {   
  36.            double i=sqrt((g*g/4)-h);
  37.            cout << "i " << i << endl;
  38.            double j=pow(i, 1/3.0);
  39.            cout << "j " << j << endl;
  40.            double k=acos(-(g/(2*i)));
  41.            cout << "k " << k << endl;
  42.            double l=-j;
  43.            cout << "l " << l << endl;
  44.            double m=cos(k/3);
  45.            cout << "m " << m << endl;
  46.            double n=sqrt(3.0)*sin(k/3);
  47.            cout << "n " << n << endl;
  48.            double p=-(b/(3*a));
  49.            cout << "p " << p << endl;
  50.            x1=2*j*cos(k/3)-(b/(3*a));
  51.            x2=l*(m+n)+p;
  52.            x3=l*(m-n)+p;
  53.            }
  54.        cout << "x1 " << x1 << endl;
  55.        cout << "x2 " << x2 << endl;
  56.        cout << "x3 " <<x3 << endl;
  57.        }
  58.     else
  59.        // one real, two complex roots
  60.        {
  61.         double r=-(g/2)+sqrt(h);
  62.         cout << "r " << r << endl;
  63.         double s=pow(r, 1/3.0);
  64.         cout << "s " << s << endl;
  65.         double t=-(g/2)-sqrt(h);
  66.         cout << "t " << t << endl;
  67.         double u;
  68.         if(t<0) u=-pow(-t, 1/3.0);
  69.         else    u=-pow(t, 1/3.0);
  70.         cout << "u " << u << endl;
  71.         x1=(s+u)-(b/(3*a));
  72.         cout << "x1 " << x1 << endl;
  73.         x2=-(s+u)/2 -(b/(3*a));
  74.         x2i= (s-u)*sqrt(3.0)/2;
  75.         cout << "x2 " << x2 << " +i " << x2i << endl;
  76.         x3=x2;;
  77.         x3i=-x2i;
  78.         cout << "x3 " << x3 << " +i " << x3i << endl;
  79.        }
  80.     cout << "check x1 " << check(a, b, c, d, complex<double>(x1, 0)) << endl;
  81.     cout << "check x2 " << check(a, b, c, d, complex<double>(x2, x3i)) << endl;
  82.     cout << "check x3 " << check(a, b, c, d, complex<double>(x3, x3i)) << endl;
  83. }
  84.  
  85.  
  86. int main()
  87. {
  88.     cout << "enter a, b, c, and d ";
  89.     double a, b, c, d;
  90.     cin >> a >> b >> c >> d;
  91.     cubic(a, b, c, d);
  92.     system("pause");
  93. }
  94.  
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
Feb 16 '07 #9

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

Similar topics

6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
17
by: Julian V. Noble | last post by:
Dear C Mavens, I am writing a Computing Prescription for CiSE on complex arithmetic. I would like to be sure that I do not include any gaffes about C99--especially about what is in the header...
14
by: David Marsh | last post by:
Can someone show me or point me to an example of declaring and initializing complex numbers in C99? Also, in looking at complex.h, I don't see any library calls for + - * or /. How are the basic...
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
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...
6
by: jacob navia | last post by:
Consider this code: < code > #include <complex.h> int main(void) { double complex c = 0.4+2.9I; c = ~c; } < end code >
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...
4
by: BiDi | last post by:
I have been trying to subclass complex, but I am not able to get the right-hand arithmetic operators working. As shown below, if an object of my subclass 'xcomplex' is added on the right of a...
4
by: Reshmi | last post by:
Hi, I have this C code which does complex number arithmetic. When I try to write a similar file for C++, it says that "creal’ was not declared in this scope". Can anyone give a better idea to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.