00:02
Good day, ladies and gentlemen.
00:06
Today we are looking at problem number 8 .21 called the complex class.
00:13
It is a bit of a work.
00:17
Basically, we're creating a complex, a class that constructs complex numbers.
00:26
Exactly, well, very similar to the real python class, which is also.
00:33
So -called complex.
00:34
Of course, in this case, we're using a capital c rather than a small c.
00:39
So it's sort of different.
00:42
Okay, so this is what we're supposed to do.
00:46
We create two data, or, yeah, two data fields, real and what i call imaginary, not complex.
00:56
And then write methods to perform addition, subtraction, multiplication, division, absolute value, and the string method, and the string method, which is supposed to return the string a plus b .i.
01:14
And finally, the get -real part and get -imaginary part.
01:19
Okay.
01:20
So it's quite involved.
01:22
I think everyone knows how to do multiplication in complex numbers, or you can look at the book if you aren't, but let's just jump over to the code.
01:34
Again, i apologize for doing this, but i have it written out beforehand rather than do it on the fly.
01:43
It's just too much work to try to do this while i'm to do this on the fly while i'm doing the presentation.
01:53
It's just too much work.
01:54
So i'm just going to talk you through the code and then we'll go to the test case.
02:02
And so, okay, so right off the bat, again, i'm going to need the square root here.
02:11
So i import math.
02:13
And the class starts here.
02:16
I've defined the class complex.
02:18
And the first thing is you always have to do is you define the initializer.
02:23
The initializer always has itself, our self.
02:31
And the data fields we have are real and imaginary.
02:37
And of course, we're initializing them to zero.
02:41
And then i'm just doing the normal stuff, which is self.
02:46
Dot real equals to real and self.
02:48
Dot imaginary equals imaginary.
02:51
In this case, since they're private, i'm using the underscores for the in front of the reel.
03:03
And these two parts, or this one, these two methods, the get methods, again, these are straightforward.
03:12
We've talked about them before.
03:14
They're just returning the values, so i'm not really going to bother too much there.
03:19
Now, when we get to the absolute value part, okay, so the absolute value part, what it's going to do is it's going to just compute, i mean, it's just returning the square root of, so i mean, the absolute value of, so if we have z equals to x, plus iy -y then the absolute value of z is just the square root of x of x times x or x squared plus y times y and of course that the the x corresponds to the the get real part here which is just this so this is the get real part here which is just this so this is the get real part and this is the get imaginary part, which is just the y part, and then you return that.
04:28
So this returns the value.
04:31
And then the add, so add what it does, of course, you know, if you have another complex number, let's call this.
04:45
So if we have another complex number, we'll call this w, and it is just u plus.
04:53
I, v, then z plus w is just equal to, sorry, it's u plus, or i should say, x plus u plus plus i times y plus v.
05:16
And again, you go through and you just, i mean, it's just the corresponding, the corresponding the values, so in the corresponding terms here, whoops, i'm sorry, where am i here? oh, right here.
05:32
So, yeah, so in the, in the ad, we just, this is the real part, minus the complex, or minus the second complex number real part.
05:43
And then in return, you just form another complex object.
05:48
So i guess this is one of the advantages of using object -oriented programming is that i can just create another object, which is the complex number formed by these objects.
06:05
So i just return that.
06:07
So i'm just returning another complex number.
06:09
And you go through each of these cases, each of these examples, or each of these methods, they're doing the same thing.
06:18
And like i said, it's just returning a complex number.
06:23
And so it's creating another complex object.
06:30
You're taking these two numbers, and you're creating another complex number.
06:35
And it's kind of the neat thing, i guess, about objectoring of programming, is i can just return another object and sort of keep doing that.
06:46
And then finally, the last one here, what it does is it just returns the string corresponding to the complex number.
07:00
So if the imaginary part zero, it's just going to return the x.
07:07
Otherwise, it'll return the string x plus y, i.
07:12
So it's kind of neat.
07:15
You can create the string.
07:17
So this is the code.
07:19
I know it's i know it's nicer to actually do it.
07:22
Do it in do it as a demo.
07:25
I guess actually you know code it while during the presentation, but it's just too much work.
07:33
So it just takes too much time.
07:36
But we'll jump to the test file now.
07:39
Again, now the test file, all i'm doing is i'm reading in the two i'm reading in two strings which correspond to the first and second complex number, the real and imaginary parts.
07:57
Then i'm stripping that string of the parts, so i get the corresponding parts.
08:04
I create two complex numbers from the corresponding parts...