Complex numbers in python

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Complex numbers in python

I haven't really used complex numbers in python and I'm not 100% sure how they exactly work,
I'm trying to solve this puzzle below and I'm stuck at the prod_ab = part. I've not googled incase I get the answer - all I'm hoping for is a little pointer to help me solve it.

I need the sum and product of 2 complex numbers:
# (2+3j) and (4+5j)
a = 2+3j
b = 4+5j
sum_ab = a+b
prod_ab = ???

Edit* This is what they're expecting - obviously I got the sum right, but I have no idea how they got -7+22j?
product(a,b) (-7+22j)
sum(a,b) (6+8j)

liz4rdm4n
Experienced User

Posts: 66
Joined: Wed Feb 11, 2015 6:27 pm
Blog: View Blog (0)

Re: Complex numbers in python

As far as I know, you could use algebra and substitute j for x and solve it like you normally would. If you still can't solve it, I'll give you the answer, but figuring it out on your own is more fun. Just remember that i (or j in this case) is the square root of -1...
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)

Re: Complex numbers in python

For starters, your lower case "J" is throwing me off. So let's just change it to lower case "I"... or 'i'

Remember that i = sqrt(-1), thus i^2 = -1. Foil your problem 4th grade style. Simple shit.

(2+3i) * (4+5i)

8 + 10i + 12i + 15(i^2)
8 + 22i + 15(i^2)
8 + 22i -15 // remember i^2 = -1, so [15 * (-1)] = -15
-7 + 22i

The end.

Edit: My fail, cyberdrain. I stepped away from my comp for a few hours and didn't see your post.
akorshikai
Experienced User

Posts: 71
Joined: Sat Jun 28, 2014 8:21 pm
Blog: View Blog (0)

Re: Complex numbers in python

akorshikai wrote:For starters, your lower case "J" is throwing me off. So let's just change it to lower case "I"... or 'i'

Python requires this, it won't recognize i as a complex number. Apart from that, no problem, I didn't want to spoil, but I guess the cat's out of the bag now...
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)

Re: Complex numbers in python

Yeah can't believe how simple it was. Thanks.
All I needed to do was A*B... I have no words...

liz4rdm4n
Experienced User

Posts: 66
Joined: Wed Feb 11, 2015 6:27 pm
Blog: View Blog (0)

Re: Complex numbers in python

Python knows how to deal with complex numbers, manually multiplying using algebra is the same as letting Python do it for you.
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)

Re: Complex numbers in python

I can't get the answer it requires

# Write a function that does a decimal to hexadecimal conversion.
# Hint: Make use of "%x" for hexadecimal format.
def dec2hex(num):
---return "%x" %num

Test Cases Expected Result Returned Result Result
dec2hex(34)--------0x22 ------------------------------22
dec2hex(0)----------0x00--------------------------------0
dec2hex(10)--------0x0a--------------------------------a

EDIT - fixed with padding "%#04x" - bit of a stupid challenge, when I could have just used hex().. but you have to do it the way they specify, which can be counter-productive.

liz4rdm4n
Experienced User

Posts: 66
Joined: Wed Feb 11, 2015 6:27 pm
Blog: View Blog (0)