Python numpy.complex256() Examples

The following are 9 code examples of numpy.complex256(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: approximate_equality_protocol_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_numpy_dtype_compatibility():
    i_a, i_b, i_c = 0, 1, 2
    i_types = [np.intc, np.intp, np.int0, np.int8, np.int16, np.int32, np.int64]
    for i_type in i_types:
        assert cirq.approx_eq(i_type(i_a), i_type(i_b), atol=1)
        assert not cirq.approx_eq(i_type(i_a), i_type(i_c), atol=1)
    u_types = [np.uint, np.uint0, np.uint8, np.uint16, np.uint32, np.uint64]
    for u_type in u_types:
        assert cirq.approx_eq(u_type(i_a), u_type(i_b), atol=1)
        assert not cirq.approx_eq(u_type(i_a), u_type(i_c), atol=1)

    f_a, f_b, f_c = 0, 1e-8, 1
    f_types = [np.float16, np.float32, np.float64]
    if hasattr(np, 'float128'):
        f_types.append(np.float128)
    for f_type in f_types:
        assert cirq.approx_eq(f_type(f_a), f_type(f_b), atol=1e-8)
        assert not cirq.approx_eq(f_type(f_a), f_type(f_c), atol=1e-8)

    c_a, c_b, c_c = 0, 1e-8j, 1j
    c_types = [np.complex64, np.complex128]
    if hasattr(np, 'complex256'):
        c_types.append(np.complex256)
    for c_type in c_types:
        assert cirq.approx_eq(c_type(c_a), c_type(c_b), atol=1e-8)
        assert not cirq.approx_eq(c_type(c_a), c_type(c_c), atol=1e-8) 
Example #2
Source File: approximate_equality_protocol_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_approx_eq_mixed_types():
    assert cirq.approx_eq(np.float32(1), 1.0 + 1e-10, atol=1e-9)
    assert cirq.approx_eq(np.float64(1), np.complex64(1 + 1e-8j), atol=1e-4)
    assert cirq.approx_eq(np.uint8(1), np.complex64(1 + 1e-8j), atol=1e-4)
    if hasattr(np, 'complex256'):
        assert cirq.approx_eq(np.complex256(1), complex(1, 1e-8), atol=1e-4)
    assert cirq.approx_eq(np.int32(1), 1, atol=1e-9)
    assert cirq.approx_eq(complex(0.5, 0), Fraction(1, 2), atol=0.0)
    assert cirq.approx_eq(0.5 + 1e-4j, Fraction(1, 2), atol=1e-4)
    assert cirq.approx_eq(0, Fraction(1, 100000000), atol=1e-8)
    assert cirq.approx_eq(np.uint16(1), Decimal('1'), atol=0.0)
    assert cirq.approx_eq(np.float64(1.0), Decimal('1.00000001'), atol=1e-8)
    assert not cirq.approx_eq(np.complex64(1e-5j), Decimal('0.001'), atol=1e-4) 
Example #3
Source File: equal_up_to_global_phase_protocol_test.py    From Cirq with Apache License 2.0 5 votes vote down vote up
def test_equal_up_to_global_mixed_array_types():
    a = [1j, 1, -1j, -1]
    b = [-1, 1j, 1, -1j]
    c = [-1, 1, -1, 1]
    assert cirq.equal_up_to_global_phase(a, tuple(b))
    assert not cirq.equal_up_to_global_phase(a, tuple(c))

    c_types = [np.complex64, np.complex128]
    if hasattr(np, 'complex256'):
        c_types.append(np.complex256)
    for c_type in c_types:
        assert cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type),
                                             tuple(b))
        assert not cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type),
                                                 tuple(c))
        assert cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), b)
        assert not cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), c)

    # Object arrays and mixed array/scalar comparisons.
    assert not cirq.equal_up_to_global_phase([1j], 1j)
    assert not cirq.equal_up_to_global_phase(
        np.asarray([1], dtype=np.complex128), np.exp(1j))
    assert not cirq.equal_up_to_global_phase([1j, 1j], [1j, "1j"])
    assert not cirq.equal_up_to_global_phase([1j], "Non-numeric iterable")
    assert not cirq.equal_up_to_global_phase([], [[]], atol=0.0)


# Dummy container class implementing _equal_up_to_global_phase_
# for homogeneous comparison, with nontrivial getter. 
Example #4
Source File: test_codec.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_np_builtin(self):
        self.pod_util(np.int64(42))
        self.pod_util(np.int32(42))
        self.pod_util(np.int16(42))
        self.pod_util(np.int8(42))
        self.pod_util(np.uint64(42))
        self.pod_util(np.uint32(42))
        self.pod_util(np.uint16(42))
        self.pod_util(np.uint8(42))
        self.pod_util(np.float16(42))
        self.pod_util(np.float32(42))
        self.pod_util(np.float64(42))
        # self.pod_util(np.float128(42))
        self.pod_util(np.complex64(42))
        self.pod_util(np.complex128(42))
        # self.pod_util(np.complex256(42)) 
Example #5
Source File: webmodel.py    From incubator-sdap-nexus with Apache License 2.0 5 votes vote down vote up
def default(self, obj):
        """If input object is an ndarray it will be converted into a dict
        holding dtype, shape and the data, base64 encoded.
        """
        numpy_types = (
            np.bool_,
            # np.bytes_, -- python `bytes` class is not json serializable
            # np.complex64,  -- python `complex` class is not json serializable
            # np.complex128,  -- python `complex` class is not json serializable
            # np.complex256,  -- python `complex` class is not json serializable
            # np.datetime64,  -- python `datetime.datetime` class is not json serializable
            np.float16,
            np.float32,
            np.float64,
            # np.float128,  -- special handling below
            np.int8,
            np.int16,
            np.int32,
            np.int64,
            # np.object_  -- should already be evaluated as python native
            np.str_,
            np.uint8,
            np.uint16,
            np.uint32,
            np.uint64,
            np.void,
        )
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        elif isinstance(obj, numpy_types):
            return obj.item()
        elif isinstance(obj, np.float128):
            return obj.astype(np.float64).item()
        elif isinstance(obj, Decimal):
            return str(obj)
        elif isinstance(obj, datetime):
            return str(obj)
        elif obj is np.ma.masked:
            return str(np.NaN)
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj) 
Example #6
Source File: test_mrcobject.py    From mrcfile with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_complex256_dtype_raises_exception(self):
        # complex256 only exists on some platforms
        if hasattr(np, 'complex256'):
            data = np.arange(6, dtype=np.complex256).reshape(3, 2)
            self.assert_dtype_raises_exception(data) 
Example #7
Source File: dtype.py    From bifrost with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def numpy2bifrost(dtype):
    if   dtype == np.int8:       return _bf.BF_DTYPE_I8
    elif dtype == np.int16:      return _bf.BF_DTYPE_I16
    elif dtype == np.int32:      return _bf.BF_DTYPE_I32
    elif dtype == np.uint8:      return _bf.BF_DTYPE_U8
    elif dtype == np.uint16:     return _bf.BF_DTYPE_U16
    elif dtype == np.uint32:     return _bf.BF_DTYPE_U32
    elif dtype == np.float16:    return _bf.BF_DTYPE_F16
    elif dtype == np.float32:    return _bf.BF_DTYPE_F32
    elif dtype == np.float64:    return _bf.BF_DTYPE_F64
    elif dtype == np.float128:   return _bf.BF_DTYPE_F128
    elif dtype == ci8:           return _bf.BF_DTYPE_CI8
    elif dtype == ci16:          return _bf.BF_DTYPE_CI16
    elif dtype == ci32:          return _bf.BF_DTYPE_CI32
    elif dtype == cf16:          return _bf.BF_DTYPE_CF16
    elif dtype == np.complex64:  return _bf.BF_DTYPE_CF32
    elif dtype == np.complex128: return _bf.BF_DTYPE_CF64
    elif dtype == np.complex256: return _bf.BF_DTYPE_CF128
    else: raise ValueError("Unsupported dtype: " + str(dtype)) 
Example #8
Source File: dtype.py    From bifrost with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def name_nbit2numpy(name, nbit):
    if   name == 'i':
        if   nbit == 8:   return np.int8
        elif nbit == 16:  return np.int16
        elif nbit == 32:  return np.int32
        elif nbit == 64:  return np.int64
        else: raise TypeError("Invalid signed integer type size: %i" % nbit)
    elif name == 'u':
        if   nbit == 8:   return np.uint8
        elif nbit == 16:  return np.uint16
        elif nbit == 32:  return np.uint32
        elif nbit == 64:  return np.uint64
        else: raise TypeError("Invalid unsigned integer type size: %i" % nbit)
    elif name == 'f':
        if   nbit == 16:  return np.float16
        elif nbit == 32:  return np.float32
        elif nbit == 64:  return np.float64
        elif nbit == 128: return np.float128
        else: raise TypeError("Invalid floating-point type size: %i" % nbit)
    elif name == 'ci':
        if   nbit == 8:   return ci8
        elif nbit == 16:  return ci16
        elif nbit == 32:  return ci32
    # elif name in set(['ci', 'cu']):
        # Note: This gives integer types in place of proper complex types
        # return name_nbit2numpy(name[1:], nbit*2)
    elif name == 'cf':
        if   nbit == 16:  return cf16
        elif nbit == 32:  return np.complex64
        elif nbit == 64:  return np.complex128
        elif nbit == 128: return np.complex256
        else: raise TypeError("Invalid complex floating-point type size: %i" %
                              nbit)
    else:
        raise TypeError("Invalid type name: " + name) 
Example #9
Source File: dtype.py    From bifrost with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def numpy2string(dtype):
    if   dtype == np.int8:       return 'i8'
    elif dtype == np.int16:      return 'i16'
    elif dtype == np.int32:      return 'i32'
    elif dtype == np.int64:      return 'i64'
    elif dtype == np.uint8:      return 'u8'
    elif dtype == np.uint16:     return 'u16'
    elif dtype == np.uint32:     return 'u32'
    elif dtype == np.uint64:     return 'u64'
    elif dtype == np.float16:    return 'f16'
    elif dtype == np.float32:    return 'f32'
    elif dtype == np.float64:    return 'f64'
    elif dtype == np.float128:   return 'f128'
    elif dtype == np.complex64:  return 'cf32'
    elif dtype == np.complex128: return 'cf64'
    elif dtype == np.complex256: return 'cf128'
    else: raise TypeError("Unsupported dtype: " + str(dtype))