我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用cmath.pi()。
def calculate_fourier_trasform_cartesian( symbols: Vector, coords: Vector, dictCGFs: Dict, mo_i: Vector, kpoints: Vector, chikdic=None) -> Vector: """ Calculate the Fourier transform projecting the MO in a set of plane waves mo_fourier(k) = < phi(r) | exp(i k . r)> """ if (chikdic is None): chikdic = get_fourier_basis(symbols, dictCGFs, kpoints) result = np.zeros(len(kpoints), dtype=np.complex128) acc = 0 for symbol, xyz in zip(symbols, coords): krs = -2j * pi * np.dot(kpoints, xyz) eikrs = np.exp(krs) cfgks = chikdic[symbol] dim_cgfs = len(cfgks) coefs = mo_i[acc: acc + dim_cgfs] prod = coefs[:, None] * cfgks * eikrs[None, :] result += np.sum(prod, axis=0) acc += dim_cgfs return result
def calculate_fourier_trasform_primitive(l: int, ks: Vector, alpha: float) -> complex: """ calculate 1D Fourier transform of single gausian primitive function centred in zero for given ste of kpoints "ks" """ gauss = np.exp(-(pi ** 2 / alpha) * ks ** 2) if l == 0: return np.sqrt(pi / alpha) * gauss elif l == 1: c = -1j * np.sqrt((pi / alpha) ** 3) return c * ks * gauss elif l == 2: c = np.sqrt(pi / alpha ** 5) return c * ((alpha / 2.0) - (pi ** 2) * (ks ** 2)) * gauss else: msg = ("there is not implementation for the primivite fourier " "transform of l: {}".format(l)) raise NotImplementedError(msg)
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 draw_perpendicular(self, start, guideLine, stepDistance, invert = False): polR, polPhi = cmath.polar(guideLine) polR = stepDistance debugMsg(polPhi) if invert: polPhi += (cmath.pi / 2) else: polPhi -= (cmath.pi / 2) debugMsg(polPhi) debugMsg(cmath.rect(polR, polPhi)) return (cmath.rect(polR, polPhi) + start)
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_constants(self): e_expected = 2.71828182845904523536 pi_expected = 3.14159265358979323846 self.assertAlmostEqual(cmath.pi, pi_expected, places=9, msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected)) self.assertAlmostEqual(cmath.e, e_expected, places=9, msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
def test_polar(self): self.assertCISEqual(polar(0), (0., 0.)) self.assertCISEqual(polar(1.), (1., 0.)) self.assertCISEqual(polar(-1.), (1., pi)) self.assertCISEqual(polar(1j), (1., pi/2)) self.assertCISEqual(polar(-1j), (1., -pi/2))
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)))
def test_rect(self): self.assertCEqual(rect(0, 0), (0, 0)) self.assertCEqual(rect(1, 0), (1., 0)) self.assertCEqual(rect(1, -pi), (-1., 0)) self.assertCEqual(rect(1, pi/2), (0, 1.)) self.assertCEqual(rect(1, -pi/2), (0, -1.))
def set_value(self, number: float): """ Sets the value of the graphic :param number: the number (must be between 0 and 'max_range' or the scale will peg the limits :return: None """ self.canvas.delete('all') self.canvas.create_image(0, 0, image=self.image, anchor='nw') number = number if number <= self.max_value else self.max_value number = 0.0 if number < 0.0 else number radius = 0.9 * self.size/2.0 angle_in_radians = (2.0 * cmath.pi / 3.0) \ + number / self.max_value * (5.0 * cmath.pi / 3.0) center = cmath.rect(0, 0) outer = cmath.rect(radius, angle_in_radians) if self.needle_thickness == 0: line_width = int(5 * self.size / 200) line_width = 1 if line_width < 1 else line_width else: line_width = self.needle_thickness self.canvas.create_line( *self.to_absolute(center.real, center.imag), *self.to_absolute(outer.real, outer.imag), width=line_width, fill=self.needle_color ) self.readout['text'] = '{}{}'.format(number, self.unit)
def draw_background(self, divisions=10): """ Draws the background of the dial :param divisions: the number of divisions between 'ticks' shown on the dial :return: None """ self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=-60, extent=30, fill='red') self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=-30, extent=60, fill='yellow') self.canvas.create_arc(2, 2, self.size-2, self.size-2, style=tk.PIESLICE, start=30, extent=210, fill='green') # find the distance between the center and the inner tick radius inner_tick_radius = int(self.size * 0.4) outer_tick_radius = int(self.size * 0.5) for tick in range(divisions): angle_in_radians = (2.0 * cmath.pi / 3.0) \ + tick/divisions * (5.0 * cmath.pi / 3.0) inner_point = cmath.rect(inner_tick_radius, angle_in_radians) outer_point = cmath.rect(outer_tick_radius, angle_in_radians) self.canvas.create_line( *self.to_absolute(inner_point.real, inner_point.imag), *self.to_absolute(outer_point.real, outer_point.imag), width=1 )
def check_polar(self, func): def check(arg, expected): got = func(arg) for e, g in zip(expected, got): self.rAssertAlmostEqual(e, g) check(0, (0., 0.)) check(1, (1., 0.)) check(-1, (1., pi)) check(1j, (1., pi / 2)) check(-3j, (3., -pi / 2)) inf = float('inf') check(complex(inf, 0), (inf, 0.)) check(complex(-inf, 0), (inf, pi)) check(complex(3, inf), (inf, pi / 2)) check(complex(5, -inf), (inf, -pi / 2)) check(complex(inf, inf), (inf, pi / 4)) check(complex(inf, -inf), (inf, -pi / 4)) check(complex(-inf, inf), (inf, 3 * pi / 4)) check(complex(-inf, -inf), (inf, -3 * pi / 4)) nan = float('nan') check(complex(nan, 0), (nan, nan)) check(complex(0, nan), (nan, nan)) check(complex(nan, nan), (nan, nan)) check(complex(inf, nan), (inf, nan)) check(complex(-inf, nan), (inf, nan)) check(complex(nan, inf), (inf, nan)) check(complex(nan, -inf), (inf, nan))
def circulate(pos): theta = 2*pi*pos/len(chords) x = int(abs(scale+margin+scale*cos(theta))) y = int(abs(scale+margin+scale*sin(theta))) return x,y
def matrix(self): return np.matrix([[1, 0], [0, cmath.exp(1j * cmath.pi / 4)]])
def test_controlled_gate_get_inverse(): one_control = _metagates.ControlledGate(Rx(0.5), 1) expected = _metagates.ControlledGate(Rx(-0.5 + 4 * math.pi), 1) assert one_control.get_inverse() == expected
def test_tensor_get_inverse(): gate = _metagates.Tensor(Rx(0.6)) inverse = gate.get_inverse() assert isinstance(inverse, _metagates.Tensor) assert inverse._gate == Rx(-0.6 + 4 * math.pi)
def test_tensor_comparison(): gate1 = _metagates.Tensor(Rx(0.6)) gate2 = _metagates.Tensor(Rx(0.6 + 4 * math.pi)) assert gate1 == gate2 assert gate1 != Rx(0.6)
def test_ph(angle): gate = _gates.Ph(angle) gate2 = _gates.Ph(angle + 2 * math.pi) expected_matrix = np.matrix([[cmath.exp(1j * angle), 0], [0, cmath.exp(1j * angle)]]) assert gate.matrix.shape == expected_matrix.shape assert np.allclose(gate.matrix, expected_matrix) assert gate2.matrix.shape == expected_matrix.shape assert np.allclose(gate2.matrix, expected_matrix) assert gate == gate2
def test_r(angle): gate = _gates.R(angle) gate2 = _gates.R(angle + 2 * math.pi) expected_matrix = np.matrix([[1, 0], [0, cmath.exp(1j * angle)]]) assert gate.matrix.shape == expected_matrix.shape assert np.allclose(gate.matrix, expected_matrix) assert gate2.matrix.shape == expected_matrix.shape assert np.allclose(gate2.matrix, expected_matrix) assert gate == gate2
def draw_box(self, start, guideLine, xDistance, yDistance, kerf): polR, polPhi = cmath.polar(guideLine) #Kerf expansion if self.flipside: start -= cmath.rect(kerf / 2, polPhi) start -= cmath.rect(kerf / 2, polPhi + (cmath.pi / 2)) else: start -= cmath.rect(kerf / 2, polPhi) start -= cmath.rect(kerf / 2, polPhi - (cmath.pi / 2)) lines = [] lines.append(['M', [start.real, start.imag]]) #Horizontal polR = xDistance move = cmath.rect(polR + kerf, polPhi) + start lines.append(['L', [move.real, move.imag]]) start = move #Vertical polR = yDistance if self.flipside: polPhi += (cmath.pi / 2) else: polPhi -= (cmath.pi / 2) move = cmath.rect(polR + kerf, polPhi) + start lines.append(['L', [move.real, move.imag]]) start = move #Horizontal polR = xDistance if self.flipside: polPhi += (cmath.pi / 2) else: polPhi -= (cmath.pi / 2) move = cmath.rect(polR + kerf, polPhi) + start lines.append(['L', [move.real, move.imag]]) start = move lines.append(['Z', []]) return lines