我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cmath.exp()。
def _gamma_real(x): _intx = int(x) if _intx == x: if _intx <= 0: #return (-1)**_intx * INF raise ZeroDivisionError("gamma function pole") if _intx <= _max_exact_gamma: return _exact_gamma[_intx] if x < 0.5: # TODO: sinpi return pi / (_sinpi_real(x)*_gamma_real(1-x)) else: x -= 1.0 r = _lanczos_p[0] for i in range(1, _lanczos_g+2): r += _lanczos_p[i]/(x+i) t = x + _lanczos_g + 0.5 return 2.506628274631000502417 * t**(x+0.5) * math.exp(-t) * r
def idft_complex(input): width = len(input) width_inv = 1.0 / width output = [] w1d = complex(0, 2 * math.pi / width) w1 = 0 for n in xrange(width): X = 0 w2d = cmath.exp(w1) w2 = complex(1, 0) for k in xrange(width): X += input[k] * w2 w2 *= w2d output.append(X * width_inv) w1 += w1d return output
def fft_core(x): length = len(x) for l in xrange(1, log2(length) + 1): le = 1 << l le2 = le >> 1 w = 2 * math.pi / le s = cmath.exp(complex(0, -w)) u = complex(1, 0) for j in xrange(1, le2 + 1): for i in xrange(j - 1, length, le): o = i + le2 t = x[o] * u x[o] = x[i] - t x[i] = x[i] + t u *= s
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
def test_daggered_gate_init(): # Choose gate which does not have an inverse gate: not_invertible_gate = T with pytest.raises(NotInvertible): not_invertible_gate.get_inverse() # Choose gate which does have an inverse defined: invertible_gate = Y assert invertible_gate.get_inverse() == Y # Test init and matrix dagger_inv = _metagates.DaggeredGate(not_invertible_gate) assert dagger_inv._gate == not_invertible_gate assert np.array_equal(dagger_inv.matrix, np.matrix([[1, 0], [0, cmath.exp(-1j * cmath.pi / 4)]])) inv = _metagates.DaggeredGate(invertible_gate) assert inv._gate == invertible_gate assert np.array_equal(inv.matrix, np.matrix([[0, -1j], [1j, 0]])) # Test matrix no_matrix_gate = Entangle with pytest.raises(AttributeError): no_matrix_gate.matrix inv_no_matrix_gate = _metagates.DaggeredGate(no_matrix_gate) with pytest.raises(AttributeError): inv_no_matrix_gate.matrix
def test_or_gate_identity(): saving_backend = DummyEngine(save_commands=True) eng = MainEngine(backend=saving_backend, engine_list=[]) qureg = eng.allocate_qureg(4) hamiltonian = QubitOperator((), 3.4) correct_h = copy.deepcopy(hamiltonian) gate = te.TimeEvolution(2.1, hamiltonian) gate | qureg eng.flush() cmd = saving_backend.received_commands[4] assert isinstance(cmd.gate, Ph) assert cmd.gate == Ph(-3.4 * 2.1) correct = numpy.array([[cmath.exp(-1j * 3.4 * 2.1), 0], [0, cmath.exp(-1j * 3.4 * 2.1)]]) print(correct) print(cmd.gate.matrix) assert numpy.allclose(cmd.gate.matrix, correct)
def _test_parameters(matrix, a, b_half, c_half, d_half): """ It builds matrix U with parameters (a, b/2, c/2, d/2) and compares against matrix. U = [[exp(j*(a-b/2-d/2))*cos(c/2), -exp(j*(a-b/2+d/2))*sin(c/2)], [exp(j*(a+b/2-d/2))*sin(c/2), exp(j*(a+b/2+d/2))*cos(c/2)]] Args: matrix(list): 2x2 matrix a: parameter of U b_half: b/2. parameter of U c_half: c/2. parameter of U d_half: d/2. parameter of U Returns: True if matrix elements of U and `matrix` are TOLERANCE close. """ U = [[cmath.exp(1j*(a-b_half-d_half))*math.cos(c_half), -cmath.exp(1j*(a-b_half+d_half))*math.sin(c_half)], [cmath.exp(1j*(a+b_half-d_half))*math.sin(c_half), cmath.exp(1j*(a+b_half+d_half))*math.cos(c_half)]] return numpy.allclose(U, matrix, rtol=10*TOLERANCE, atol=TOLERANCE)
def _decompose_arb1qubit(cmd): """ Use Z-Y decomposition of Nielsen and Chuang (Theorem 4.1). An arbitrary one qubit gate matrix can be writen as U = [[exp(j*(a-b/2-d/2))*cos(c/2), -exp(j*(a-b/2+d/2))*sin(c/2)], [exp(j*(a+b/2-d/2))*sin(c/2), exp(j*(a+b/2+d/2))*cos(c/2)]] where a,b,c,d are real numbers. Then U = exp(j*a) Rz(b) Ry(c) Rz(d). If the matrix is element of SU(2) (determinant == 1), then we can choose a = 0. """ matrix = cmd.gate.matrix.tolist() a, b_half, c_half, d_half = _find_parameters(matrix) qb = cmd.qubits eng = cmd.engine with Control(eng, cmd.control_qubits): if Rz(2*d_half) != Rz(0): Rz(2*d_half) | qb if Ry(2*c_half) != Ry(0): Ry(2*c_half) | qb if Rz(2*b_half) != Rz(0): Rz(2*b_half) | qb if a != 0: Ph(a) | qb
def create_unitary_matrix(a, b, c, d): """ Creates a unitary 2x2 matrix given parameters. Any unitary 2x2 matrix can be parametrized by: U = exp(ia) [[exp(j*b) * cos(d), exp(j*c) * sin(d)], [-exp(-j*c) * sin(d), exp(-j*b) * cos(d)]] with 0 <= d <= pi/2 and 0 <= a,b,c < 2pi. If a==0, then det(U) == 1 and hence U is element of SU(2). Args: a,b,c (float): parameters 0 <= a,b,c < 2pi d (float): parameter 0 <= d <= pi/2 Returns: 2x2 matrix as nested lists """ ph = exp(1j*a) # global phase return [[ph * exp(1j*b) * math.cos(d), ph * exp(1j*c) * math.sin(d)], [ph * -exp(-1j*c) * math.sin(d), ph * exp(-1j*b) * math.cos(d)]]
def oscillator(self, time): ts = time - self.start te = self.end - time if ts < 0 or te < 0: return 0 # Use Hanning window function to reduce the transition. # D. T. Prescott and N. V. Shuley, "Reducing solution time in # monochromatic FDTD waveguide simulations", IEEE Trans. Microwave # Theory Tech., vol. 42, no. 8, pp. 1582-1584, 8. 1994. if ts < self.width: env = sin(0.5 * pi * ts / self.width)**2 elif te < self.width: env = sin(0.5 * pi * te / self.width)**2 else: env = 1 osc = env * cexp(2j * pi * self.freq * time + 1j * self.phase) if self.cmplx: return osc else: return osc.real
def EXP(df, price='Close'): """ Exponential """ exp_list = [] i = 0 while i < len(df[price]): exp = cmath.exp(df[price][i]).real exp_list.append(exp) i += 1 return exp_list
def c(k,d): cm1 = (cmath.exp(2*pi*1j*d)-1.0)/(4*pi*1j*(d-k)) return cm1
def fft(x): N = len(x) if N <= 1: return x even = fft(x[0::2]) odd = fft(x[1::2]) T= [exp(-2j*pi*k/N)*odd[k] for k in range(N//2)] return [even[k] + T[k] for k in range(N//2)] + [even[k] - T[k] for k in range(N//2)]
def test_exp(): assert exp(0) == 1 assert exp(10000).ae(mpf('8.8068182256629215873e4342')) assert exp(-10000).ae(mpf('1.1354838653147360985e-4343')) a = exp(mpf((1, 8198646019315405, -53, 53))) assert(a.bc == bitcount(a.man)) mp.prec = 67 a = exp(mpf((1, 1781864658064754565, -60, 61))) assert(a.bc == bitcount(a.man)) mp.prec = 53 assert exp(ln2 * 10).ae(1024) assert exp(2+2j).ae(cmath.exp(2+2j))
def test_issue_3132(): mp.dps = 512 a = exp(-1) b = exp(1) mp.dps = 15 assert (+a).ae(0.36787944117144233) assert (+b).ae(2.7182818284590451)
def test_complex_functions(): for x in (list(range(10)) + list(range(-10,0))): for y in (list(range(10)) + list(range(-10,0))): z = complex(x, y)/4.3 + 0.01j assert exp(mpc(z)).ae(cmath.exp(z)) assert log(mpc(z)).ae(cmath.log(z)) assert cos(mpc(z)).ae(cmath.cos(z)) assert sin(mpc(z)).ae(cmath.sin(z)) assert tan(mpc(z)).ae(cmath.tan(z)) assert sinh(mpc(z)).ae(cmath.sinh(z)) assert cosh(mpc(z)).ae(cmath.cosh(z)) assert tanh(mpc(z)).ae(cmath.tanh(z))
def test_mpcfun_real_imag(): mp.dps = 15 x = mpf(0.3) y = mpf(0.4) assert exp(mpc(x,0)) == exp(x) assert exp(mpc(0,y)) == mpc(cos(y),sin(y)) assert cos(mpc(x,0)) == cos(x) assert sin(mpc(x,0)) == sin(x) assert cos(mpc(0,y)) == cosh(y) assert sin(mpc(0,y)) == mpc(0,sinh(y)) assert cospi(mpc(x,0)) == cospi(x) assert sinpi(mpc(x,0)) == sinpi(x) assert cospi(mpc(0,y)).ae(cosh(pi*y)) assert sinpi(mpc(0,y)).ae(mpc(0,sinh(pi*y))) c, s = cospi_sinpi(mpc(x,0)) assert c == cospi(x) assert s == sinpi(x) c, s = cospi_sinpi(mpc(0,y)) assert c.ae(cosh(pi*y)) assert s.ae(mpc(0,sinh(pi*y))) c, s = cos_sin(mpc(x,0)) assert c == cos(x) assert s == sin(x) c, s = cos_sin(mpc(0,y)) assert c == cosh(y) assert s == mpc(0,sinh(y))
def test_call_with_dps(): mp.dps = 15 assert abs(exp(1, dps=30)-e(dps=35)) < 1e-29
def test_expm1(): mp.dps = 15 assert expm1(0) == 0 assert expm1(3).ae(exp(3)-1) assert expm1(inf) == inf assert expm1(1e-10)*1e10 assert expm1(1e-50).ae(1e-50) assert (expm1(1e-10)*1e10).ae(1.00000000005)
def _gamma_complex(x): if not x.imag: return complex(_gamma_real(x.real)) if x.real < 0.5: # TODO: sinpi return pi / (_sinpi_complex(x)*_gamma_complex(1-x)) else: x -= 1.0 r = _lanczos_p[0] for i in range(1, _lanczos_g+2): r += _lanczos_p[i]/(x+i) t = x + _lanczos_g + 0.5 return 2.506628274631000502417 * t**(x+0.5) * cmath.exp(-t) * r
def _erfc_mid(x): # Rational approximation assuming 0 <= x <= 9 return exp(-x*x)*_polyval(_erfc_coeff_P,x)/_polyval(_erfc_coeff_Q,x)
def ei_asymp(z, _e1=False): r = 1./z s = t = 1.0 k = 1 while 1: t *= k*r s += t if abs(t) < 1e-16: break k += 1 v = s*exp(z)/z if _e1: if type(z) is complex: zreal = z.real zimag = z.imag else: zreal = z zimag = 0.0 if zimag == 0.0 and zreal > 0.0: v += pi*1j else: if type(z) is complex: if z.imag > 0: v += pi*1j if z.imag < 0: v -= pi*1j return v
def ei(z, _e1=False): typez = type(z) if typez not in (float, complex): try: z = float(z) typez = float except (TypeError, ValueError): z = complex(z) typez = complex if not z: return -INF absz = abs(z) if absz > EI_ASYMP_CONVERGENCE_RADIUS: return ei_asymp(z, _e1) elif absz <= 2.0 or (typez is float and z > 0.0): return ei_taylor(z, _e1) # Integrate, starting from whichever is smaller of a Taylor # series value or an asymptotic series value if typez is complex and z.real > 0.0: zref = z / absz ref = ei_taylor(zref, _e1) else: zref = EI_ASYMP_CONVERGENCE_RADIUS * z / absz ref = ei_asymp(zref, _e1) C = (zref-z)*0.5 D = (zref+z)*0.5 s = 0.0 if type(z) is complex: _exp = cmath.exp else: _exp = math.exp for x,w in gauss42: t = C*x+D s += w*_exp(t)/t ref -= C*s return ref
def test_issue_33(): mp.dps = 512 a = exp(-1) b = exp(1) mp.dps = 15 assert (+a).ae(0.36787944117144233) assert (+b).ae(2.7182818284590451)
def test_issue_73(): mp.dps = 512 a = exp(-1) b = exp(1) mp.dps = 15 assert (+a).ae(0.36787944117144233) assert (+b).ae(2.7182818284590451)
def computeDataSetWeightDistribution(self, classifierIdx, yList, yHatList): ''' ???????????????????????????????????????? :param classifierIdx: ????????????(?0??) :param yList: ???????? :param yHatList: ??????????????? :return: ''' self.wList[classifierIdx+1] = map(lambda w, y, yHat:\ (w / self.zList[classifierIdx] *\ cmath.exp(-self.alphaList[classifierIdx] * y * yHat)).real,\ self.wList[classifierIdx], yList, yHatList)