本文整理汇总了Python中cmath.phase方法的典型用法代码示例。如果您正苦于以下问题:Python cmath.phase方法的具体用法?Python cmath.phase怎么用?Python cmath.phase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cmath
的用法示例。
在下文中一共展示了cmath.phase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: angle_average
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def angle_average(angles: np.ndarray) -> float:
"""
Function to calculate the average value of a list of angles.
Parameters
----------
angles : numpy.ndarray
Parallactic angles (deg).
Returns
-------
float
Average angle (deg).
"""
cmath_rect = sum(cmath.rect(1, math.radians(ang)) for ang in angles)
cmath_phase = cmath.phase(cmath_rect/len(angles))
return math.degrees(cmath_phase)
示例2: arc_to
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def arc_to(self, x1, y1):
x0, y0 = self.get_current_point()
dx, dy = x1 - x0, y1 - y0
if abs(dx) < 1e-8:
self.line_to(x1, y1)
else:
center = 0.5 * (x0 + x1) + 0.5 * (y0 + y1) * dy / dx
theta0 = cmath.phase(x0 - center + y0*1j)
theta1 = cmath.phase(x1 - center + y1*1j)
r = abs(x0 - center + y0*1j)
# we must ensure that the arc ends at (x1, y1)
if x0 < x1:
self.arc_negative(center, 0, r, theta0, theta1)
else:
self.arc(center, 0, r, theta0, theta1)
示例3: phase2t
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def phase2t(self, psi):
"""Given phase -pi < psi <= pi,
returns the t value such that
exp(1j*psi) = self.u1transform(self.point(t)).
"""
def _deg(rads, domain_lower_limit):
# Convert rads to degrees in [0, 360) domain
degs = degrees(rads % (2*pi))
# Convert to [domain_lower_limit, domain_lower_limit + 360) domain
k = domain_lower_limit // 360
degs += k * 360
if degs < domain_lower_limit:
degs += 360
return degs
if self.delta > 0:
degs = _deg(psi, domain_lower_limit=self.theta)
else:
degs = _deg(psi, domain_lower_limit=self.theta)
return (degs - self.theta)/self.delta
示例4: extract_phase
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def extract_phase(complex_array, position=None):
"""Extracts global phase from `complex_array` at given `position`. If position is not specified, the `position` is
set to to an intermediate position to avoid machine-precision problems with tails of wavefunctions at beginning
or end of the array.
Parameters
----------
complex_array: ndarray
complex-valued array
position: int, optional
position where the phase is extracted (default value = None)
"""
if position is None:
flattened_position = np.argmax(
np.abs(complex_array)) # extract phase from element with largest amplitude modulus
position = np.unravel_index(flattened_position, complex_array.shape)
return cmath.phase(complex_array[position])
示例5: standardize_phases
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def standardize_phases(complex_array):
"""Uses `extract_phase` to obtain global phase from `array` and returns standardized array with global phase factor
standardized.
Parameters
----------
complex_array: ndarray
complex
Returns
-------
ndarray (complex)
"""
phase = extract_phase(complex_array)
std_array = complex_array * np.exp(-1j * phase)
return std_array
示例6: _dec_diag
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def _dec_diag(self):
"""
Call to create a circuit implementing the diagonal gate.
"""
q = QuantumRegister(self.num_qubits)
circuit = QuantumCircuit(q)
# Since the diagonal is a unitary, all its entries have absolute value one and the diagonal
# is fully specified by the phases of its entries
diag_phases = [cmath.phase(z) for z in self.params]
n = len(self.params)
while n >= 2:
angles_rz = []
for i in range(0, n, 2):
diag_phases[i // 2], rz_angle = _extract_rz(diag_phases[i], diag_phases[i + 1])
angles_rz.append(rz_angle)
num_act_qubits = int(np.log2(n))
contr_qubits = q[self.num_qubits - num_act_qubits + 1:self.num_qubits]
target_qubit = q[self.num_qubits - num_act_qubits]
circuit.ucrz(angles_rz, contr_qubits, target_qubit)
n //= 2
return circuit
# extract a Rz rotation (angle given by first output) such that exp(j*phase)*Rz(z_angle)
# is equal to the diagonal matrix with entires exp(1j*ph1) and exp(1j*ph2)
示例7: calculate_E
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def calculate_E(self, id, allcells):
theta = cmath.phase(
complex(self.simplify_pos(id, allcells)[0], self.simplify_pos(id, allcells)[1])) % (
2 * math.pi)
cos = math.cos(theta)
sin = math.sin(theta)
veloc_r = (allcells[id].veloc[0] - allcells[self.id].veloc[0]) * cos + (
allcells[self.id].veloc[1] - allcells[id].veloc[1]) * sin
d = allcells[self.id].distance_from(allcells[id]) - allcells[self.id].radius - allcells[
id].radius
if allcells[id].radius + 0.5 > allcells[self.id].radius:
E = self.switchVeloc(veloc_r) * self.switchdistance(d)
else:
q = math.exp(-veloc_r) / (1.00001 - allcells[id].radius / allcells[self.id].radius)
E = 1000 * q / d
return [E * cos, E * sin, E, veloc_r]
示例8: isBlocked
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def isBlocked(self, id, allcells):
for i in range(len(allcells)):
if self.id != i and allcells[i].radius + 0.5 > allcells[self.id].radius:
selfTheta = cmath.phase(complex(allcells[id].pos[0] - allcells[self.id].pos[0],
allcells[self.id].pos[1] - allcells[id].pos[1])) % (2 * math.pi)
deltaTheta = math.fabs(cmath.phase(complex(allcells[i].pos[0] - allcells[self.id].pos[0],
allcells[self.id].pos[1] - allcells[i].pos[1])) % (
2 * math.pi) - selfTheta)
if deltaTheta < math.pi / 2 and allcells[self.id].distance_from(allcells[i]) < allcells[
self.id].distance_from(allcells[id]):
k = (allcells[self.id].pos[1] - allcells[id].pos[1]) / (
allcells[id].pos[0] - allcells[self.id].pos[0])
d = math.fabs(k * (allcells[i].pos[0] - allcells[self.id].pos[0]) - allcells[self.id].pos[1] -
allcells[i].pos[1]) / (1 + k * k) ** 0.5 - allcells[self.id].radius - allcells[
i].radius
if d < 0.5:
return True
return False
示例9: test_common
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def test_common(self):
# random values
M12 = 0.12716600+0.08765385j
G12 = -0.34399429-0.1490931j
aM12 = abs(M12)
aG12 = abs(G12)
phi12 = cmath.phase(-M12/G12)
qp = flavio.physics.mesonmixing.common.q_over_p(M12, G12)
DM = flavio.physics.mesonmixing.common.DeltaM(M12, G12)
DG = flavio.physics.mesonmixing.common.DeltaGamma(M12, G12)
# arXiv:0904.1869
self.assertAlmostEqual(DM**2-1/4.*DG**2, 4*aM12**2-aG12**2, places=10) # (35)
self.assertAlmostEqual(qp, -(DM+1j*DG/2)/(2*M12-1j*G12), places=10) # (37)
self.assertAlmostEqual(qp, -(2*M12.conjugate()-1j*G12.conjugate())/(DM+1j*DG/2), places=10) # (37)
self.assertAlmostEqual(DM*DG, -4*(M12*G12.conjugate()).real, places=10) # (36)
self.assertAlmostEqual(DM*DG, 4*aM12*aG12*cos(phi12), places=10) # (39)
示例10: compute_g
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def compute_g(derts__, Ave, fa=0): # compute g from dx, dy
for x0, derts_ in derts__:
for derts in derts_:
dy, dx = derts[-1][-1]
if not fa:
g = hypot(dy, dx)
else:
ga = hypot(phase(dy), phase(dx))
if ga > pi: ga = two_pi - ga # translate ga scope into (0, pi), unsigned
g = int(ga * angle_coef) # transform to fit in scope (-128, 127)
derts[-1] = (g-Ave,) + derts[-1] # return
# ---------- compute_g() end -------------------------------------------------------------------------------------------
示例11: test_phase
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def test_phase(self):
self.assertAlmostEqual(phase(0), 0.)
self.assertAlmostEqual(phase(1.), 0.)
self.assertAlmostEqual(phase(-1.), pi)
self.assertAlmostEqual(phase(-1.+1E-300j), pi)
self.assertAlmostEqual(phase(-1.-1E-300j), -pi)
self.assertAlmostEqual(phase(1j), pi/2)
self.assertAlmostEqual(phase(-1j), -pi/2)
# zeros
self.assertEqual(phase(complex(0.0, 0.0)), 0.0)
self.assertEqual(phase(complex(0.0, -0.0)), -0.0)
self.assertEqual(phase(complex(-0.0, 0.0)), pi)
self.assertEqual(phase(complex(-0.0, -0.0)), -pi)
# infinities
self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi)
self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi)
self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi)
self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2)
self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2)
self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2)
self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2)
self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4)
self.assertEqual(phase(complex(INF, -2.3)), -0.0)
self.assertEqual(phase(complex(INF, -0.0)), -0.0)
self.assertEqual(phase(complex(INF, 0.0)), 0.0)
self.assertEqual(phase(complex(INF, 2.3)), 0.0)
self.assertAlmostEqual(phase(complex(INF, INF)), pi/4)
self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2)
self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2)
self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2)
self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2)
self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi)
self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi)
self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi)
# real or imaginary part NaN
for z in complex_nans:
self.assertTrue(math.isnan(phase(z)))
示例12: primes_pop
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def primes_pop(a):
if isinstance(a, int):
if a < 0:
# Primality testing
return len(primes_pop(-a)) == 1
if a < 2:
return []
def simple_factor(a):
working = a
output = []
num = 2
while num * num <= working:
while working % num == 0:
output.append(num)
working //= num
num += 1
if working != 1:
output.append(working)
return output
if a < 10 ** 4:
return simple_factor(a)
else:
try:
from sympy import factorint
factor_dict = factorint(a)
factors_with_mult = [[fact for _ in range(
factor_dict[fact])] for fact in factor_dict]
return sorted(sum(factors_with_mult, []))
except:
return simple_factor(a)
if is_num(a):
return cmath.phase(a)
if is_seq(a):
return a[:-1]
return unknown_types(primes_pop, "P", a)
示例13: curvature
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def curvature(self, t):
"""returns the curvature of the segment at t."""
return segment_curvature(self, t)
# def icurvature(self, kappa):
# """returns a list of t-values such that 0 <= t<= 1 and
# seg.curvature(t) = kappa."""
#
# a, b = self.radius.real, self.radius.imag
# if kappa > min(a, b)/max(a, b)**2 or kappa <= 0:
# return []
# if a==b:
# if kappa != 1/a:
# return []
# else:
# raise ValueError(
# "The .icurvature() method for Arc elements with "
# "radius.real == radius.imag (i.e. circle segments) "
# "will raise this exception when kappa is 1/radius.real as "
# "this is true at every point on the circle segment.")
#
# # kappa = a*b / (a^2sin^2(tau) + b^2cos^2(tau))^(3/2), tau=2*pi*phase
# sin2 = np.poly1d([1, 0])
# p = kappa**2*(a*sin2 + b*(1 - sin2))**3 - a*b
# sin2s = polyroots01(p)
# taus = []
#
# for sin2 in sin2s:
# taus += [np.arcsin(sqrt(sin2)), np.arcsin(-sqrt(sin2))]
#
# # account for the other branch of arcsin
# sgn = lambda x: x/abs(x) if x else 0
# other_taus = [sgn(tau)*np.pi - tau for tau in taus if abs(tau) != np.pi/2]
# taus = taus + other_taus
#
# # get rid of points not included in segment
# ts = [phase2t(tau) for tau in taus]
#
# return [t for t in ts if 0<=t<=1]
示例14: _get_load_coord
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def _get_load_coord(self, load_id):
sub_id = self._layout["load"][load_id]
c_nm = self._get_load_name(sub_id, load_id)
if not "elements_display" in self.subs_elements[sub_id][c_nm]:
pos_load_sub = self.subs_elements[sub_id][c_nm]["pos"]
pos_center_sub = self._layout["substations"][sub_id]
z_sub = (pos_center_sub[0] + 1j * pos_center_sub[1])
theta = cmath.phase((self.subs_elements[sub_id][c_nm]["z"] - z_sub))
pos_load = z_sub + cmath.exp(1j * theta) * self.load_prod_dist
# position of the end of the line connecting the object to the substation
pos_end_line = pos_load - cmath.exp(1j * theta) * 20
how_center = self._get_position(theta)
tmp_dict = {"pos_end_line": pos_end_line,
"pos_load_sub": pos_load_sub,
"pos_load": pos_load,
"how_center": how_center}
self.subs_elements[sub_id][c_nm]["elements_display"] = tmp_dict
else:
dict_element = self.subs_elements[sub_id][c_nm]["elements_display"]
pos_end_line = dict_element["pos_end_line"]
pos_load_sub = dict_element["pos_load_sub"]
pos_load = dict_element["pos_load"]
how_center = dict_element["how_center"]
return pos_end_line, pos_load_sub, pos_load, how_center
示例15: _get_gen_coord
# 需要导入模块: import cmath [as 别名]
# 或者: from cmath import phase [as 别名]
def _get_gen_coord(self, gen_id):
sub_id = self._layout["gen"][gen_id]
c_nm = self._get_gen_name(sub_id, gen_id)
if not "elements_display" in self.subs_elements[sub_id][c_nm]:
pos_gen_sub = self.subs_elements[sub_id][c_nm]["pos"]
pos_center_sub = self._layout["substations"][sub_id]
z_sub = (pos_center_sub[0] + 1j * pos_center_sub[1])
theta = cmath.phase((self.subs_elements[sub_id][c_nm]["z"] - z_sub))
pos_gen = z_sub + cmath.exp(1j * theta) * self.load_prod_dist
# position of the end of the line connecting the object to the substation
pos_end_line = pos_gen - cmath.exp(1j * theta) * 20
how_center = self._get_position(theta)
tmp_dict = {"pos_end_line": pos_end_line,
"pos_gen_sub": pos_gen_sub,
"pos_gen": pos_gen,
"how_center": how_center}
self.subs_elements[sub_id][c_nm]["elements_display"] = tmp_dict
else:
dict_element = self.subs_elements[sub_id][c_nm]["elements_display"]
pos_end_line = dict_element["pos_end_line"]
pos_gen_sub = dict_element["pos_gen_sub"]
pos_gen = dict_element["pos_gen"]
how_center = dict_element["how_center"]
return pos_end_line, pos_gen_sub, pos_gen, how_center