Python and Complex Numbers

python

#1

I’ve checked the documentation for python and its’ respective modules and I noticed that there is no predefined complex function to utilize complex numbers. And since you can’t find the square root of any negative numbers, attempting to manually define i, like this:

import math
i = math.sqrt(-1)

just returns an error. Therefore, I was wondering if there is any way to work with complex numbers in python? I’m sure they would be useful in one way or another.


#2

There is a library which is highly recommend:

numpy

That should suit your needs


#3

Awesome! Now defining i is, not only possible, but extremely easy. Albeit, it uses j instead of i, which is understandable, but still.

import numpy
i = numpy.lib.scimath.sqrt(-1)
print i

#4

that looks easy :slight_smile: I never used numpy.

Any questions about it?


#5

Yeah, my code:

import numpy
i = numpy.lib.scimath.sqrt(-1)
print i

results in the output of 1j. I've been trying to get it so that whenever it gets a complex number, to instead print the value in terms of i. I've been messing around with it for a while, however I can't seem to get it to work. Any suggestions?


#6

seems this is more difficult then i thought:

https://bbolker.github.io/math1mp/notes/random/r3_sqrtminusone.html


#7

Nah they're there, without any third party libraries

>>> import cmath
>>> cmath.sqrt(-1)
1j

>>> 1j + 1
(1+1j)
>>> (-1)**0.5
(6.123233995736766e-17+1j)

#8

numpy also gives 1j, what is the j for?


#9

j is another way of writing i. It's used in fields such as electrical engineering where i is already used to define currents.


#10

what exactly is the purpose of your program?


#11

I'm making a calculator that calculates user input equations and I was trying to add complex number functionality.


#12

for complex number functionality i would look into using a library, see what is available, i wouldn't write your own.

Numpy is used a lot, so certain a good possibility


#13

Right now, I'm just sandboxing around with python. I wasn't going to add the complex number functionality until I myself understood how it worked within python. Which is why I was wondering if there was a way to instead of displaying the value in terms of j, to display the value in terms of i. I've been trying to get that to work for an hour and a half now, and no luck. I mean I've gotten it to go as far as turning 1j into a string ... "i" but once I do that, I can't use i in any further computations.


#14

Just format it differently when showing it in your program

>>> mynumber = 1 + 2j
>>> print(f'{mynumber.real} + {mynumber.imag}i')
1.0 + 2.0i

#15

So... something like this?

print "a + bi"
a = raw_input("a =")
b = raw_input("b =")
mynumber = "%s + %si" % (a, b)
print mynumber

#16

I wouldn't know


#17

What is numpy? it sounds like Grumpy


#18

is that really so difficult to google (or using any other search engine for that matter)?

its a python module, please don’t revive 8 months old topic for google-able questions


#19