14.7. Another function on complex_number numbers¶
Another operation we might want is multiplication. Unlike addition, multiplication is easy if the numbers are in polar coordinates and hard if they are in Cartesian coordinates (well, a little harder, anyway).
In polar coordinates, we can just multiply the magnitudes and add the angles. As usual, we can use the accessor functions without worrying about the representation of the objects.
complex_number mult (complex_number& a, complex_number& b)
{
double mag = a.get_mag() * b.get_mag();
double theta = a.get_theta() + b.get_theta();
complex_number product;
product.set_polar (mag, theta);
return product;
}
A small problem we encounter here is that we have no constructor that
accepts polar coordinates. It would be nice to write one, but remember
that we can only overload a function (even a constructor) if the
different versions take different parameters. In this case, we would
like a second constructor that also takes two doubles, and we
can’t have that.
An alternative it to provide an accessor function that sets the
instance variables. In order to do that properly, though, we have to
make sure that when mag and theta are set, we also set the
polar flag. At the same time, we have to make sure that the
cartesian flag is unset. That’s because if we change the polar
coordinates, the cartesian coordinates are no longer valid.
void complex_number::set_polar (double m, double t)
{
mag = m; theta = t;
cartesian = false; polar = true;
}
As an exercise, write the corresponding function named set_cartesian.
To test the mult function, we can try something like:
complex_number c1 (2.0, 3.0);
complex_number c2 (3.0, 4.0);
complex_number product = mult (c1, c2);
product.print_cartesian();
The output of this program is
-6 + 17i
The active code below uses the mult and set_polar functions.
Feel free to modify the code and experiment around!
1#include <iostream>
2#include <cmath>
3
4class complex_number
5{
6 double real = 0.0, imag = 0.0;
7 double mag = 0.0, theta = 0.0;
8 bool cartesian, polar;
9
10public:
11 complex_number ();
12 complex_number (double r, double i);
13 void calculate_cartesian ();
14 double get_real ();
15 double get_imag ();
16 void calculate_polar ();
17 double get_mag ();
18 double get_theta ();
19 void print_cartesian ();
20 void print_polar ();
21 void set_polar (double m, double t);
22};
23
24complex_number add (complex_number& a, complex_number& b);
25complex_number subtract (complex_number& a, complex_number& b);
26complex_number mult (complex_number& a, complex_number& b);
27
28int main() {
29 complex_number c1 (2.0, 3.0);
30 complex_number c2 (3.0, 4.0);
31 complex_number product = mult (c1, c2);
32 product.print_cartesian();
33}
There is a lot of conversion going on in this program behind the scenes.
When we call mult, both arguments get converted to polar
coordinates. The result is also in polar format, so when we invoke
print_cartesian it has to get converted back. Really, it’s amazing
that we get the right answer!
What is the correct output of the code below?
int main() {
Complex c1 (2.0, 3.0);
Complex c2 (3.0, 4.0);
Complex c3 (1.0, 0.0);
Complex c4 (3.5, 2.5);
Complex product = mult (c1, c2);
Complex diff = subtract (c4, c3);
Complex sum = add (product, diff);
sum.printCartesian();
}
Now let's try implementing the set_cartesian function. Write your
implementation in the commented area of the active code below.
Read the comments in main to test out your code! If you get stuck,
you can reveal the extra problem at the end for help.
1#include <iostream>
2#include <cmath>
3
4class complex_number
5{
6 double real = 0.0, imag = 0.0;
7 double mag = 0.0, theta = 0.0;
8 bool cartesian, polar;
9
10public:
11 complex_number ();
12 complex_number (double r, double i);
13 void calculate_cartesian ();
14 double get_real ();
15 double get_imag ();
16 void calculate_polar ();
17 double get_mag ();
18 double get_theta ();
19 void print_cartesian ();
20 void print_polar ();
21 void set_polar (double m, double t);
22 void set_cartesian (double r, double i);
23};
24
25void complex_number::set_cartesian (double r, double i) {
26 // ``set_cartesian`` should set real and imag to
27 // r and i respectively and set the cartesian flag.
28 // Write your implementation here.
29}
30
31complex_number add (complex_number& a, complex_number& b);
32complex_number subtract (complex_number& a, complex_number& b);
33complex_number mult (complex_number& a, complex_number& b);
34
35int main() {
36 complex_number c1 (2.0, 3.0);
37 complex_number c2 (3.0, 4.0);
38 complex_number product = mult (c1, c2);
39 product.print_cartesian();
40 // Should output 1.5 + 2.7i
41 product.set_cartesian(1.5, 2.7);
42 product.print_cartesian();
43}
Reveal Problem
Let's write the code for the set_cartesian function.
-
cartesian = false; polar = true; -
cartesian = true; polar = false; -
complex_number complex_number::set_cartesian (double r, double i) { -
real = i; imag = r; -
real = r; imag = i; -
void complex_number::set_cartesian (double r, double i) { -
}