本文整理汇总了Python中complex函数的典型用法代码示例。如果您正苦于以下问题:Python complex函数的具体用法?Python complex怎么用?Python complex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了complex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: potentialInfluence
def potentialInfluence(self,aq,x,y):
zin = complex(x,y)
bigZ = ( 2.0*zin - (self.z1 + self.z2) )/ (self.z2 - self.z1)
if abs(bigZ.imag) < self.tiny and abs(bigZ.real) < 1.0-self.tiny: # point is on boundary; this could be nicer by setting the log
if self.aqin == aq:
bigZ = complex(bigZ.real,self.tiny)
elif self.aqout == aq:
bigZ = complex(bigZ.real,-self.tiny)
zin = ( (self.z2-self.z1)*bigZ + (self.z1+self.z2) ) / 2.0
bigZmin1 = bigZ - 1.0; bigZplus1 = bigZ + 1.0
## if abs(bigZmin1) < self.tiny:
## # point is at right corner; move back a little along element; may fail if corner is very pointy
## # This needs to be moved by the PolygonInhom class; after all, that class decided it was inside.
## if self.aqin == aq:
## zin = self.z2 + complex(4.0,-1.0) * self.tiny * (self.z1 - self.z2)
## else:
## zin = self.z2 + complex(4.0,1.0) * self.tiny * (self.z1 - self.z2)
## elif abs(bigZplus1) < self.tiny: # point is at left corner, move up a little along ldLeft
## if self.aqin == aq:
## zin = self.ldLeft.z2 + complex(4.0,-1.0) * self.tiny * (self.ldLeft.z1 - self.ldLeft.z2)
## else:
## zin = self.ldLeft.z2 + complex(4.0,1.0) * self.tiny * (self.ldLeft.z1 - self.ldLeft.z2)
if abs(bigZmin1) < self.tiny or abs(bigZplus1) < self.tiny: # gotta be inside; move point
# zin = self.aqin.movePoint(zin)
# doesn't have to be inside when inhoms butt-up
zin = aq.movePoint(zin)
# Only works for line-doublet in one layer, so that is hardcoded (the zero lists)
rv = zeros((self.Ndegree+1,aq.Naquifers),'d')
x = zin.real; y = zin.imag
potlapldho(x,y,self.x1,self.y1,self.x2,self.y2,self.Ndegree,self.potInf)
rv[:,0] = self.potInf
return rv
示例2: test_power_zero
def test_power_zero(self):
# ticket #1271
zero = np.array([0j])
one = np.array([1 + 0j])
cinf = np.array([complex(np.inf, 0)])
cnan = np.array([complex(np.nan, np.nan)])
def assert_complex_equal(x, y):
x, y = np.asarray(x), np.asarray(y)
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
# positive powers
for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
assert_complex_equal(np.power(zero, p), zero)
# zero power
assert_complex_equal(np.power(zero, 0), one)
assert_complex_equal(np.power(zero, 0 + 1j), cnan)
# negative power
for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
assert_complex_equal(np.power(zero, -p), cnan)
assert_complex_equal(np.power(zero, -1 + 0.2j), cnan)
def test_fast_power(self):
x = np.array([1, 2, 3], np.int16)
assert (x ** 2.00001).dtype is (x ** 2.0).dtype
示例3: test_power_complex
def test_power_complex(self):
x = np.array([1 + 2j, 2 + 3j, 3 + 4j])
assert_equal(x ** 0, [1.0, 1.0, 1.0])
assert_equal(x ** 1, x)
assert_almost_equal(x ** 2, [-3 + 4j, -5 + 12j, -7 + 24j])
assert_almost_equal(x ** 3, [(1 + 2j) ** 3, (2 + 3j) ** 3, (3 + 4j) ** 3])
assert_almost_equal(x ** 4, [(1 + 2j) ** 4, (2 + 3j) ** 4, (3 + 4j) ** 4])
assert_almost_equal(x ** (-1), [1 / (1 + 2j), 1 / (2 + 3j), 1 / (3 + 4j)])
assert_almost_equal(x ** (-2), [1 / (1 + 2j) ** 2, 1 / (2 + 3j) ** 2, 1 / (3 + 4j) ** 2])
assert_almost_equal(x ** (-3), [(-11 + 2j) / 125, (-46 - 9j) / 2197, (-117 - 44j) / 15625])
assert_almost_equal(x ** (0.5), [ncu.sqrt(1 + 2j), ncu.sqrt(2 + 3j), ncu.sqrt(3 + 4j)])
norm = 1.0 / ((x ** 14)[0])
assert_almost_equal(
x ** 14 * norm, [i * norm for i in [-76443 + 16124j, 23161315 + 58317492j, 5583548873 + 2465133864j]]
)
# Ticket #836
def assert_complex_equal(x, y):
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
for z in [complex(0, np.inf), complex(1, np.inf)]:
err = np.seterr(invalid="ignore")
z = np.array([z], dtype=np.complex_)
try:
assert_complex_equal(z ** 1, z)
assert_complex_equal(z ** 2, z * z)
assert_complex_equal(z ** 3, z * z * z)
finally:
np.seterr(**err)
示例4: test_branches
def test_branches(self):
with np.errstate(all="ignore"):
for t in [np.complex64, np.complex128]:
# tupled (numerator, denominator, expected)
# for testing as expected == numerator/denominator
data = list()
# trigger branch: real(fabs(denom)) > imag(fabs(denom))
# followed by else condition as neither are == 0
data.append((( 2.0, 1.0), ( 2.0, 1.0), (1.0, 0.0)))
# trigger branch: real(fabs(denom)) > imag(fabs(denom))
# followed by if condition as both are == 0
# is performed in test_zero_division(), so this is skipped
# trigger else if branch: real(fabs(denom)) < imag(fabs(denom))
data.append((( 1.0, 2.0), ( 1.0, 2.0), (1.0, 0.0)))
for cases in data:
n = cases[0]
d = cases[1]
ex = cases[2]
result = t(complex(n[0], n[1])) / t(complex(d[0], d[1]))
# check real and imag parts separately to avoid comparison
# in array context, which does not account for signed zeros
assert_equal(result.real, ex[0])
assert_equal(result.imag, ex[1])
示例5: combined
def combined(c1, exp1, offset1, c2, exp2, offset2, width=500, height=500, real_min=-2.0, real_max=2.0, imag_min=-2.0, imag_max=2.0, pickColor=julia.timeBased, allPerlin = False):
# Generate evenly spaced values over real and imaginary ranges
real_range = numpy.arange(real_min, real_max, (real_max - real_min) / width)
imag_range = numpy.arange(imag_max, imag_min, (imag_min - imag_max) / height)
# Obtain image to work with
image = Image.new('RGB', (width, height), (0, 0, 0))
drawer = ImageDraw.Draw(image)
# Generate pixel values
for imag, ipix in itertools.izip(imag_range, range(height)):
for real, rpix in itertools.izip(real_range, range(width)):
z = complex(real, imag) + offset1
n = 255
while abs(z) < 10 and n >= 5:
z = z ** exp1 + c1
n -= 5
m = 255
z = (complex(real, imag) + offset2) * 2
while abs(z) < 10 and n >= 5:
z = z ** exp2 + c2
n -= 5
n = n - (m * 5)
n = n % 255
drawer.point((ipix, rpix), fill=pickColor(n, 0, real, imag)) # n varies between 255 and 5
#time.increase()
# And return results
return image
示例6: test_power_zero
def test_power_zero(self):
# ticket #1271
zero = np.array([0j])
one = np.array([1+0j])
cinf = np.array([complex(np.inf, 0)])
cnan = np.array([complex(np.nan, np.nan)])
def assert_complex_equal(x, y):
x, y = np.asarray(x), np.asarray(y)
assert_array_equal(x.real, y.real)
assert_array_equal(x.imag, y.imag)
# positive powers
for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
assert_complex_equal(np.power(zero, p), zero)
# zero power
assert_complex_equal(np.power(zero, 0), one)
with np.errstate(invalid="ignore"):
assert_complex_equal(np.power(zero, 0+1j), cnan)
# negative power
for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
assert_complex_equal(np.power(zero, -p), cnan)
assert_complex_equal(np.power(zero, -1+0.2j), cnan)
示例7: getTeardropPath
def getTeardropPath(inclination, overhangRadians, overhangSpan, radiusArealized, sides):
"Get vector3 teardrop path."
sideAngle = 2.0 * math.pi / float(sides)
overhangPlaneAngle = euclidean.getWiddershinsUnitPolar(overhangRadians)
overhangRadians = math.atan2(overhangPlaneAngle.imag, overhangPlaneAngle.real * math.cos(inclination))
tanOverhangAngle = math.tan(overhangRadians)
beginAngle = overhangRadians
beginMinusEndAngle = math.pi + overhangRadians + overhangRadians
withinSides = int(math.ceil(beginMinusEndAngle / sideAngle))
withinSideAngle = -beginMinusEndAngle / float(withinSides)
teardropPath = []
for side in xrange(withinSides + 1):
unitPolar = euclidean.getWiddershinsUnitPolar(beginAngle)
teardropPath.append(unitPolar * radiusArealized)
beginAngle += withinSideAngle
firstPoint = teardropPath[0]
if overhangSpan <= 0.0:
teardropPath.append(complex(0.0, firstPoint.imag + firstPoint.real / tanOverhangAngle))
else:
deltaX = (radiusArealized - firstPoint.imag) * tanOverhangAngle
overhangPoint = complex(firstPoint.real - deltaX, radiusArealized)
remainingDeltaX = max(0.0, overhangPoint.real - 0.5 * overhangSpan )
overhangPoint += complex(-remainingDeltaX, remainingDeltaX / tanOverhangAngle)
teardropPath.append(complex(-overhangPoint.real, overhangPoint.imag))
teardropPath.append(overhangPoint)
return euclidean.getVector3Path(teardropPath)
示例8: coherency_elements
def coherency_elements (self,observation):
"""helper method: returns the four components of the coherency matrix""";
i,q,u,v = [ self.stokes(st) for st in STOKES ];
# diagonal = (len(Context.active_correlations) == 2);
diagonal = False; # the above was bothersome -- even if we only use 2 corrs, we still want to do all intermediate computations in 2x2
if observation.circular():
if self._constant_flux:
return (i+v,0,0,i-v) if diagonal else (i+v,complex(q,u),complex(q,-u),i-v);
rr = self.ns.rr ** (self.stokes("I") + self.stokes("V"));
if diagonal:
rl = lr = 0;
else:
rl = self.ns.rl ** Meq.ToComplex(self.stokes("Q"),self.stokes("U"));
lr = self.ns.lr ** Meq.Conj(rl);
ll = self.ns.ll ** (self.stokes("I") - self.stokes("V"));
return rr,rl,lr,ll;
else:
if self._constant_flux:
return (i+q,0,0,i-q) if diagonal else (i+q,complex(u,v),complex(u,-v),i-q);
xx = self.ns.xx ** (self.stokes("I") + self.stokes("Q"));
if diagonal:
xy = yx = 0;
else:
xy = self.ns.xy ** Meq.ToComplex(self.stokes("U"),self.stokes("V"));
yx = self.ns.yx ** Meq.Conj(xy);
yy = self.ns.yy ** (self.stokes("I") - self.stokes("Q"));
return xx,xy,yx,yy;
示例9: test_spectrum
def test_spectrum(self):
port = self.__get_free_port()
spectrum_dialog = self.__get_spectrum_dialog()
spectrum_dialog.device.set_server_port(port)
spectrum_dialog.ui.btnStart.click()
self.assertEqual(len(spectrum_dialog.scene_manager.peak), 0)
data = np.array([complex(1, 1), complex(2, 2), complex(3, 3)], dtype=np.complex64)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.connect(("127.0.0.1", port))
sock.sendall(data.tostring())
sock.shutdown(socket.SHUT_RDWR)
sock.close()
QApplication.instance().processEvents()
QTest.qWait(self.SEND_RECV_TIMEOUT)
self.assertGreater(len(spectrum_dialog.scene_manager.peak), 0)
spectrum_dialog.ui.btnStop.click()
self.__close_dialog(spectrum_dialog)
示例10: test_receive
def test_receive(self):
port = self.__get_free_port()
receive_dialog = self.__get_recv_dialog()
receive_dialog.device.set_server_port(port)
receive_dialog.ui.btnStart.click()
data = np.array([complex(1, 2), complex(3, 4), complex(5, 6)], dtype=np.complex64)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.connect(("127.0.0.1", port))
sock.sendall(data.tostring())
sock.shutdown(socket.SHUT_RDWR)
sock.close()
QApplication.instance().processEvents()
QTest.qWait(self.SEND_RECV_TIMEOUT)
self.assertEqual(receive_dialog.device.current_index, 3)
self.assertTrue(np.array_equal(receive_dialog.device.data[:3], data))
receive_dialog.ui.btnStop.click()
receive_dialog.ui.btnClear.click()
self.assertEqual(receive_dialog.device.current_index, 0)
self.__close_dialog(receive_dialog)
示例11: test_inversion
def test_inversion():
N = 2**2 # Make mgrids without q1 = q2 = 0, since that case can be
# problematic
Q1, Q2 = np.mgrid[-8:8:complex(0, N), -8:8:complex(0, N)]
epsilons = np.linspace(.1, .7, N)
for epsilon in epsilons:
check_inversions(Q1, Q2, epsilon)
示例12: divComplexNumbers
def divComplexNumbers(rA, iA, rB, iB):
'''
z.w = (a+bi).(c+di) = (ac-bd) + (ad+bc)i
complex((rA*rB-iA*iB), (rA*iB+iA*rB))
z/w = (z * conj(w)) / (w*conj(w))
conjugado de w (a+bi) eh (a+(-b)i)
ans = "{0}+{1}i".format( _sReal , _sImag)
'''
#A = complex(rA, iA)
#B = complex(rB, iB)
#cB = complex(rB, -iB)
#cB = B.conjugate()
num = mulComplexNumbers(rA, iA, rB, -iB) #A.__mul__(cB)
dem = mulComplexNumbers(rB, iB, rB, -iB) #B.__mul__(cB)
if num.imag == 0 and dem.imag == 0:
return num.real / dem.real
elif dem.imag == 0:
rs = complex(num.real/dem.real, num.imag/dem.real)
return rs
else:
rs = complex(num.real/dem.real, num.imag/dem.imag)
return rs
示例13: createImage
def createImage(self):
self.updateProgress(0, self.height)
for y in range(self.height):
for x in range(self.width):
z = complex(0.0, 0.0)
k = complex(self.origin.real + \
float(x)/float(self.width)*self.range,
self.origin.imag - \
float(y) / float(self.height)*self.range)
# calculate z = (z +k) * (z + k) over and over
for iteration in range(self.depth):
real_part = z.real + k.real
imag_part = z.imag + k.imag
del z
z = complex(real_part * real_part - imag_part * \
imag_part, 2 * real_part * imag_part)
distance = z.real * z.real + z.imag * z.imag
if distance >= self.maxDistance:
cidx = int(distance % self.ncolors)
self.pixel(x, y, cidx)
break
self.updateProgress(y)
self.updateProgress(self.height, self.height)
self.im.putpalette(self.rgb.getpalette())
self.im.save("out.gif")
self.img = PhotoImage(file="out.gif")
self.label['image'] = self.img
示例14: _follow_horizontal_line
def _follow_horizontal_line(self, x, y, thick=False):
"""Find a horizontal line with optional arrow heads."""
if thick:
line_character = '='
else:
line_character = '-'
# follow line to the right
end_x, _, line_end_style = self._follow_line(x, y, dx=1, line_character=line_character)
# follow line to the left
start_x, _, line_start_style = self._follow_line(x, y, dx=-1, line_character=line_character)
start_x_fix = end_x_fix = 0
if self.get(start_x - 1, y) == '+':
start_x_fix = -0.5
if self.get(end_x + 1, y) == '+':
end_x_fix = 0.5
self.tag([(x, y) for x in range(start_x, end_x+1)], CLASS_LINE)
# return the new shape object with arrows etc.
p1 = complex(self.left(start_x + start_x_fix), self.vcenter(y))
p2 = complex(self.right(end_x + end_x_fix), self.vcenter(y))
shapes = []
if line_start_style:
p1, arrow_shapes = line_start_style(p1, p2)
shapes.extend(arrow_shapes)
if line_end_style:
p2, arrow_shapes = line_end_style(p2, p1)
shapes.extend(arrow_shapes)
shapes.append(Line(p1, p2, thick=thick))
return group(shapes)
示例15: drawMandelbrot
def drawMandelbrot(self):
start = self.start
end = self.end
print "drawing mandelbrot! ", start, end
scale_x = complex((end-start).real/self.size, 0)
scale_y = complex(0, (end-start).imag/self.size)
for y in range(self.size):
for x in range(self.size):
z = 0j
c = start + x*scale_x + y*scale_y
for count in range(self.iterations):
if abs(z) <= 2:
z = z * z + c
else:
break
color = self.getColor(count)
ps = self.pointsize
self.canvas.create_rectangle(x*ps, y*ps, x*ps + ps, y*ps + ps, fill=color, outline=color)
if y % 32 == 0:
root.update()