Using Friend Functions with complex

The complex class defines a set of mathematical, trigonometric, magnitude and conversion functions as friend functions of complex objects. Because these functions are friend functions rather than member functions, you cannot use the dot or arrow operators. For example:

complex a, b, *c;

a - exp(b);     //correct - exp() is a friend function of complex
a = b.exp();    //error - exp() is not a member function of complex
a = c -> exp(); //error - exp() is not a member function of complex

Using Mathematical Functions for complex

The complex class defines four mathematical functions as friend functions of complex objects.

The following example shows uses of these mathematical functions:

// Using the complex mathematical functions
#include <complex.h>
#include <iostream.h>
void main() {
   complex a, b;
   int i;
   double f;
   //
   // prompt the user for an argument for calls to
   // exp(), log(), and sqrt()
   //
   cout << "Enter a complex value\n";
   cin >> a;
   cout << "The value of exp() for " << a << " is: " << exp(a)
        << "\nThe natural logarithm of " << a << " is: " << log(a)
        << "\nThe square root of " << a << " is: " << sqrt(a) << "\n\n";
   //
   // prompt the user for arguments for calls to pow()
   //
   cout << "Enter 2 complex values (a and b), an integer (i),"
        << " and a floating point value (f)\n";
   cin >> a >> b >> i >> f;
   cout << "a is " << a << ", b is " << b << ", i is " << i
        << ", f is " << f << '\n'
        << "The value of f**a is: " << pow(f, a) << '\n'
        << "The value of a**i is: " << pow(a, i) << '\n'
        << "The value of a**f is: " << pow(a, f) << '\n'
        << "The value of a**b is: " << pow(a, b) << endl;
   } 

This example produces the output shown below in regular type, given the input shown in bold:

Enter a complex value
(3.7,4.2)
The value of exp() for ( 3.7, 4.2)  is: ( -19.8297, -35.2529)
The natural logarithm of ( 3.7, 4.2) is: ( 1.72229, 0.848605)
The square root of ( 3.7, 4.2) is: ( 2.15608, 0.973992)
Enter 2 complex values (a and b), an integer (i), and a floating point value (f)
(2.6,9.39) (3.16,1.16) -7 33.16237 
a is ( 2.6, 9.39), b is ( 3.16, 1.16), i is -7, f is 33.1624
The value of f**a is: ( 972.681, 8935.53)
The value of a**i is: ( -1.13873e-07, -3.77441e-08)
The value of a**f is: ( 4.05451e+32, -4.60496e+32)
The value of a**b is: ( 262.846, 132.782) 

Using Trigonometric Functions for complex

The complex class defines four trigonometric functions as friend functions of complex objects.

The following example shows how you can use some of the complex trigonometric functions:

// Complex Mathematics Library trigonometric functions
#include <complex.h>
#include <istream.h>
void main() {
   complex a = (M_PI, M_PI_2); // a = (pi,pi/2)
   // display the values of cos(), cosh(), sin(), and sinh()
   // for (pi,pi/2)
   cout << "The value of cos() for (pi,pi/2) is: " << cos(a) << '\n'
        << "The value of cosh() for (pi,pi/2) is: " << cosh(a) << '\n'
        << "The value of sin() for (pi,pi/2) is: " << sin(a) << '\n'
        << "The value of sinh() for (pi,pi/2) is: " << sinh(a) << endl;
   } 

This program produces the following output:

The value of cos()  for (pi,pi/2) is: ( 6.12323e-17, 0)
The value of cosh() for (pi,pi/2) is: ( 2.50918, 0)
The value of sin()  for (pi,pi/2) is: ( 1, -0)
The value of sinh() for (pi,pi/2) is: ( 2.3013, 0)

Magnitude Functions for complex

The magnitude functions for complex are:

Conversion Functions for complex

The conversion functions in the Complex Mathematics Library allow you to convert between the polar and standard complex representations of a value and to extract the real and imaginary parts of a complex value.

The complex class provides the following conversion functions as friend functions of complex objects:

The following program shows how to use complex conversion functions:
// Using the complex conversion functions
#include <complex.h>
#include <iostream.h>
void main()   {
   complex a;
   //for a value supplied by the user, display the real part,
   //the imaginary part, and the polar representation.
   cout << "Enter a complex value" << endl;
   cin >> a;
   cout << "The real part of this value is " << real(a) << endl'
   cout << "The imaginary part of this value is " << img(a) << endl;
   cout << "The polar representation of this value is " << "( <<abs(a) << "," << arg(a) << ")" <<endl;
}

 

This example produces the output shown below, given the input shown in bold:
Enter a complex value 
(175,162)
The real part of this value is 175
The imaginary part of this value is 167
The polar representation of this value is (238,472,0.746842)