def isInside(c): zN = Complex() for i in range(MAX): if zN.radius() > OUT_OF_MANDELBROT: return i zN = f(c, zN) return -1
from complex import Complex a, b, c, d, e, f = map(float, input().split()) A = Complex(a, b) B = Complex(c, d) C = Complex(e, f) print("x1 = ", (((B**2 - A * C * 4)**0.5) - B) / (A * 2)) print("x2 = ", (Complex(0, 0) - ((B**2 - A * C * 4)**0.5) - B) / (A * 2))
from qubit import Qubit, tensordot, Hadamard, Cnot, Measure, Identity, RCnot, randomQubit, PauliX, PauliY, PauliZ, M0, M1 from complex import Complex import numpy as np import math #Inicjalizacja Bramek X = PauliX() Y = PauliY() Z = PauliZ() H = Hadamard() CNOT = Cnot() I = Identity() M0 = M0() M1 = M1() x = Qubit(Complex(1, 0), Complex(0, 0)) y = Qubit(Complex(1, 0), Complex(0, 0)) #Splątanie x = x * H xy = tensordot(x, y) xy = np.tensordot(CNOT, xy, axes=[1, 0]) print("splątany = ", xy) #Pierwszy krok print("What to code? type 00 , 01 , 10 or 11") opcja = input() if opcja == '00': II = np.kron(I, I) print("I and I gate: ", II)
def test_mul(): """Test to make sure the __mul__ function works. form: z1 * z2 = (a1 * a2 − b1 * b2) + (a1 * b2 + a2 * b1)i Where z=complex number, a=real, b=imaginary """ complexNumber1 = Complex(2, 0) complexNumber2 = Complex(0, 1) assert (complexNumber1 * complexNumber2) == Complex(0, 2), error complexNumber1 = Complex(0, 0) complexNumber2 = Complex(1, 0) assert (complexNumber1 * complexNumber2) == Complex(0, 0), error complexNumber1 = Complex(-3, -1) complexNumber2 = Complex(4, 4) assert (complexNumber1 * complexNumber2) == Complex(-8, -16), error complexNumber1 = Complex(-1, 2) complexNumber2 = Complex(-1, 2) assert (complexNumber1 * complexNumber2) == Complex(-3, -4), error
def test_sub(): """Test to make sure the __sub__ function works. form: z1 − z2 = (a1 − a2) + (b1 − b2)i Where z=complex number, a=real, b=imaginary """ complexNumber1 = Complex(2, 0) complexNumber2 = Complex(0, 1) assert (complexNumber1 - complexNumber2) == Complex(2, -1), error complexNumber1 = Complex(0, 0) complexNumber2 = Complex(1, 0) assert (complexNumber1 - complexNumber2) == Complex(-1, 0), error complexNumber1 = Complex(-5, -1) complexNumber2 = Complex(5, 4) assert (complexNumber1 - complexNumber2) == Complex(-10, -5), error complexNumber1 = Complex(-1, 2) complexNumber2 = Complex(-1, 2) assert (complexNumber1 - complexNumber2) == Complex(0, 0), error
def test_conj(): """Test to make sure the conjugate() function works. form: !z = a − bi Where z=complex number, a=real, b=imaginary """ complexNumber = Complex(2, 0) assert complexNumber.conjugate() == Complex(2, 0), error complexNumber = Complex(4, 7) assert complexNumber.conjugate() == Complex(4, -7), error complexNumber = Complex(0, 8) assert complexNumber.conjugate() == Complex(0, -8), error complexNumber = Complex(-1, -3) assert complexNumber.conjugate() == Complex(-1, 3), error
def test_absolute_value(self): c1 = Complex(6, 8) self.assertEqual(c1.abs(), 10, "Should equal 10")
def Hadamard(): return np.array([[Complex(1 / np.sqrt(2), 0), Complex(1 / np.sqrt(2), 0)], [Complex(1 / np.sqrt(2), 0), Complex(-1 / np.sqrt(2), 0)]])
def PauliX(): return np.array([[Complex(0, 0), Complex(1, 0)], [Complex(1, 0), Complex(0, 0)]])
def M1(): return np.array([[Complex(0, 0), Complex(0, 0)], [Complex(0, 0), Complex(1, 0)]])
def Identity(): return np.array([[Complex(1, 0), Complex(0, 0)], [Complex(0, 0), Complex(1, 0)]])
def Toffoli(): return np.array([[ Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0) ], [ Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0) ]])
def RCnot(): return np.array( [[Complex(1, 0), Complex(0, 0), Complex(0, 0), Complex(0, 0)], [Complex(0, 0), Complex(0, 0), Complex(0, 0), Complex(1, 0)], [Complex(0, 0), Complex(0, 0), Complex(1, 0), Complex(0, 0)], [Complex(0, 0), Complex(1, 0), Complex(0, 0), Complex(0, 0)]])
def TNgate(): return np.array([[Complex(1, 0), Complex(0, 0)], [Complex(0, 0), PolarToComplex(1, 45)]])
def test_add(self): c1 = Complex(10, 4) c2 = Complex(12, -2) self.assertEqual(str(c1.add(c2)), "22.00 + 2.00i", "Should equal '22.00 + 2.00i'")
def PauliY(): return np.array([[Complex(0, 0), Complex(0, -1)], [Complex(0, 1), Complex(0, 0)]])
def test_multiply(self): c1 = Complex(13, -2) c2 = Complex(-2, -5) self.assertEqual(str(c1.multiply(c2)), "-36.00 - 61.00i", "Should equal '-36.00 - 61.00i'")
def test_sub(self): self.assertEqual(Complex(10, 5).__sub__(Complex(10, 5)), Complex(0, 0)) self.assertEqual((Complex(10, 5) - Complex(10, 5)), Complex(0, 0)) self.assertEqual((Complex(103, 55) - Complex(13, 15)), Complex(90, 40)) self.assertEqual((4 - Complex(13, 15)), Complex(-9, -15)) assert Complex(2, 3) - (2 + 2j) == Complex(0, 1) assert 3 - Complex(2, 1) == Complex(1, -1) self.assertNotEqual(Complex(3, 4) - 2, Complex(10, 2))
def test_set_get(self): c = Complex() c.real = 10.0 c.imaginary = 20.0 self.assertEqual(10.0, c.real) self.assertEqual(20.0, c.imaginary)
def test_mul(self): self.assertEqual( Complex(10, 5).__mul__(Complex(10, 5)), Complex(75, 100)) self.assertEqual((Complex(10, 5) * Complex(10, 5)), Complex(75, 100)) self.assertEqual((Complex(3, 25) * Complex(10, 5)), Complex(-95, 265)) self.assertEqual(4 * Complex(10, 5), Complex(40, 20)) self.assertNotEqual(4 * Complex(3, 4) - 2, Complex(10, 2)) assert Complex(2, 3) * (2 + 2j) == Complex(-2, 10) assert 4 * (2 + 2j) == Complex(8, 8) assert (4 + 1j) * 3 == Complex(12, 3)
#!/usr/bin/env python # Author: Makenzie Brian # Date: February 20, 2018 # Class: ME 599 # File: lab6 > test.py # Description: test for complex class from complex import Complex # MAIN if __name__ == '__main__': # convenience because I didn't want to use the built in a = Complex(6, 3) b = Complex(7, -5) numb = 3 print "A: ", a print "B: ", b print "Add: ", a + b print "Subtract: ", a - b, print "Mult: ", a * b print "Divide: ", a / b print "Negate: ", -a, -b print "Compl conj: ", ~a, ~b print "\n" print "Integer second with interger as " + str(numb) print "Add: ", a + numb print "Subtract: ", a - numb print "Mult: ", a * numb print "Divide: ", a / numb print "\n"
def test_eq(self): self.assertEqual(Complex(1, 1).__eq__(Complex(1, 1)), Complex(1)) self.assertEqual(Complex(1, 1) == (Complex(1, 1)), Complex(1)) self.assertEqual(Complex(124, 112), Complex(124, 112)) self.assertNotEqual((4 + 1j), Complex(124, 112))
def test_mod(): """Test to make sure the modulus() function works. form: |z| = sqrt(a^2 + b^2) Where z=complex number, a=real, b=imaginary """ complexNumber = Complex(2, 0) assert complexNumber.modulus() == 2, error complexNumber = Complex(4, 7) assert complexNumber.modulus() == sqrt(65), error complexNumber = Complex(3, 8) assert complexNumber.modulus() == sqrt(73), error complexNumber = Complex(-1, -3) assert complexNumber.modulus() == sqrt(10), error
def test_modulus(self): self.assertEqual(Complex(3, 4).modulus(), 5) self.assertEqual(Complex(45, 24).modulus(), 51)
def test_add(): """Test to make sure the __add__ function works. form: z1 + z2 = (a1 + a2) + (b1 + b2)i Where z=complex number, a=real, b=imaginary """ complexNumber1 = Complex(2, 0) complexNumber2 = Complex(0, 1) assert (complexNumber1 + complexNumber2) == Complex(2, 1), error complexNumber1 = Complex(0, 0) complexNumber2 = Complex(1, 0) assert (complexNumber1 + complexNumber2) == Complex(1, 0), error complexNumber1 = Complex(-5, -1) complexNumber2 = Complex(4, 4) assert (complexNumber1 + complexNumber2) == Complex(-1, 3), error complexNumber1 = Complex(-1, 2) complexNumber2 = Complex(-1, 2) assert (complexNumber1 + complexNumber2) == Complex(-2, 4), error
def test_conjugate(self): self.assertEqual(Complex(1, 3).conjugate(), Complex(1, -3)) assert Complex(1, 3).conjugate() == Complex(1, -3)
def test_add(): assert int(Complex(1, 0).real) == 1 print('OK')
def test_add(self): self.assertEqual(Complex(10, 5) + Complex(10, 5), Complex(20, 10)) self.assertEqual((Complex(10, 5) + Complex(10, 5)), Complex(20, 10)) self.assertEqual((Complex(214, 411) + Complex(31, 22)), Complex(245, 433)) assert Complex(214, 411) + Complex(31, 22) == Complex(245, 433) assert 3 + Complex(3, 1) == Complex(6, 1) assert 40 + Complex(31, 22) == Complex(71, 22) self.assertEqual(Complex(1, 2) + 3, Complex(4, 2)) assert Complex(1, 3) + complex(1, 3) == Complex(2, 6) self.assertEqual(Complex(1, 3) + (1 + 3j), Complex(2, 6)) self.assertEqual(Complex(2, 3) + (2 + 2j), Complex(4, 5)) assert Complex(2, 3) + (2 + 2j) == Complex(4, 5)
def __init__(self, factory, divide_data, point_data, transform_data=None, curve_types=None, curve_data=None, transfinite_data=None, transfinite_type=None, physical_name=None, inner_volumes=None, surfaces_names=None): """ Primitive object divided into parts for boolean accuracy. :param str factory: see Primitive :param list of int divide_data: [n_parts_x, n_parts_y, n_parts_z] :param list point_data: see Primitive :param list of float transform_data: see Primitive :param list of int curve_types: see Primitive :param list curve_data: see Primitive :param list of list of float transfinite_data: see Primitive :param int transfinite_type: see Primitive :param str physical_name: see Primitive :param list of int inner_volumes: see Primitive :param list of str surfaces_names: see Primitive """ primitives = list() if len(point_data) == 3: half_lx = point_data[0] / 2.0 half_ly = point_data[1] / 2.0 half_lz = point_data[2] / 2.0 primitive_lc = None new_point_data = [[half_lx, half_ly, -half_lz], [-half_lx, half_ly, -half_lz], [-half_lx, -half_ly, -half_lz], [half_lx, -half_ly, -half_lz], [half_lx, half_ly, half_lz], [-half_lx, half_ly, half_lz], [-half_lx, -half_ly, half_lz], [half_lx, -half_ly, half_lz]] elif len(point_data) == 4: half_lx = point_data[0] / 2.0 half_ly = point_data[1] / 2.0 half_lz = point_data[2] / 2.0 primitive_lc = point_data[3] new_point_data = [[half_lx, half_ly, -half_lz], [-half_lx, half_ly, -half_lz], [-half_lx, -half_ly, -half_lz], [half_lx, -half_ly, -half_lz], [half_lx, half_ly, half_lz], [-half_lx, half_ly, half_lz], [-half_lx, -half_ly, half_lz], [half_lx, -half_ly, half_lz]] else: primitive_lc = None new_point_data = [x[:3] for x in point_data] # slice lc if exist if curve_data is None: curve_data = [[]] * 12 if physical_name is None: physical_name = ComplexPrimitive.__name__ ps_base_points, ps_curves_points = divide_primitive( divide_data, new_point_data, curve_data) for i, bps in enumerate(ps_base_points): if primitive_lc is not None: new_point_data = [ x + [primitive_lc] for x in ps_base_points[i] ] else: new_point_data = ps_base_points[i] new_curve_data = list() for cps in ps_curves_points[i]: if primitive_lc is not None: new_curve_data.append([x + [primitive_lc] for x in cps]) else: new_curve_data.append(cps) primitives.append( Primitive(factory, new_point_data, transform_data, curve_types, new_curve_data, transfinite_data, transfinite_type, physical_name, inner_volumes, surfaces_names)) Complex.__init__(self, factory, primitives)
#!/usr/bin/env python import sys, random sys.path.append("/home/pi/mcpi/api/python/mcpi") import minecraft from complex import Complex BACKGROUND_BLOCK = 155 FOREGROUND_BLOCK = 49 GROUND = 50 ITERATIONS = 10 CONSTANT = Complex(1, 1) scale = 100 resolution = 1.0 / scale x_min = -2.5 x_max = 1 x_range = (x_max - x_min) y_min = -1 y_max = 1 y_range = (y_max - y_min) WIDTH = int(x_range * scale) HEIGHT = int(y_range * scale)
def test_multiply_all_negative(self): z = Complex(-2, -5) w = Complex(-3, -8) assert (z * w).__str__() == "-34+31i"
if stage: count, _ = counter(clk, 0, m//2-1, 1, valid) s1 = s1 * twiddle_type.rom(count, *twiddles) s0 = delay(clk, s0, 1) s1 = delay(clk, s1, 1) valid = delay(clk, valid, 1) #reorder s0, s1, valid = reorder(clk, s0, s1, valid, stage) return bit_reverse_order(clk, s0, s1, valid, 2**(num_stages-1)) clk = Clock("clk") base_type = SFixed(16, 8) subtype = Complex(base_type) a = subtype.input("in") b = subtype.input("in") valid = Boolean().input("valid") s0, s1, valid_out = fft(clk, a, b, valid, 3) test = [1, 0, 1, 0, 1, 0, 1, 0] clk.initialise() for i in range(10): for i in range(4): a.set(test[i]), b.set(test[i+4]), valid.set(1) print(("%10s %10s %10s"%(s0.get(), s1.get(), valid_out.get()))) clk.tick()
def test_subtract(self): c1 = Complex(-10, 3) c2 = Complex(1, -2) self.assertEqual(str(c1.subtract(c2)), "-11.00 + 5.00i", "Should equal '-9.00 + 7.00i'")
temp = Complex(0, 0) for j in range(len(self.values)): temp = temp + (self.values[j] * other.values[j].sprz()) return temp def norma(self): temp = Complex(0, 0) for j in range(len(self.values)): temp = temp + (self.values[j] * self.values[j].sprz()) return temp**0.5 def __repr__(self): return str(self.values) a = Complex(1, 2) b = Complex(3, 4) c = Complex(5, 6) d = Complex(0, 1) e = Complex(1, 0) f = Complex(2, 3) al = 1 / math.sqrt(2) vector1 = Vector([a, b, c]) vector2 = Vector([d, e, f]) # print(vector1) # print(vector1 + vector2) # print(vector1 - vector2) # print(vector1.skalar(2)) # print(vector2.skalar(3)) # print(vector1 * vector2)
def test_divide(self): c1 = Complex(1, 2) c2 = Complex(3, 4) self.assertEqual(str(c1.divide(c2)), "0.44 + 0.08i", "Should equal 0.44 + 0.08i")
def __mul__(self, other): temp = Complex(0, 0) for j in range(len(self.values)): temp = temp + (self.values[j] * other.values[j].sprz()) return temp
from complex import Complex if __name__ == '__main__': """print(Complex(1, 2)) print(Complex(0, 2)) print(Complex(1, -2)) print(Complex(0, -2)) print(Complex(-1, -2)) print(Complex(0, -999))""" c = Complex(1, -2) b = Complex(-2, -4) print c.multiply(b)
def norma(self): temp = Complex(0, 0) for j in range(len(self.values)): temp = temp + (self.values[j] * self.values[j].sprz()) return temp**0.5
def test_conjugate_construction(self): c = Complex(real=15.0, imaginary=10.0) conjugate = c.get_conjugate() self.assertEqual(15.0, conjugate.real) self.assertEqual(-10.0, conjugate.imaginary)
def __init__(self, ap_guts = [], **kw): # Call ancestor init with the remaining keywords Complex.__init__(self, **kw) self.ap_guts = ap_guts