def test_numba_vdot(): for a, b in ((np.array([1 + 2j, 3 + 4j]), np.array([5 + 6j, 7 + 8j])), (np.array([[1, 4], [5, 6]]), np.array([[4, 1], [2, 2]]))): result_type, result = numba_vdot(a, b) assert result == np.vdot(a, b) assert result_type == typesystem.from_numpy_dtype(a.dtype).dtype result_type, result = numba_vdot(b, a) assert result == np.vdot(b, a) assert result_type == typesystem.from_numpy_dtype(b.dtype).dtype
def resolve_attribute_dtype(dtype, default=None): "Resolve the type for numpy dtype attributes" if dtype.is_numpy_dtype: return dtype if dtype.is_numpy_attribute: numpy_attr = getattr(dtype.module, dtype.attr, None) if isinstance(numpy_attr, np.dtype): return typesystem.from_numpy_dtype(numpy_attr) elif issubclass(numpy_attr, np.generic): return typesystem.from_numpy_dtype(np.dtype(numpy_attr))
def test_numba_vdot(): for a, b in ((np.array([1+2j,3+4j]), np.array([5+6j,7+8j])), (np.array([[1, 4], [5, 6]]), np.array([[4, 1], [2, 2]]))): result_type, result = numba_vdot(a, b) assert result == np.vdot(a, b) assert result_type == typesystem.from_numpy_dtype(a.dtype).dtype result_type, result = numba_vdot(b, a) assert result == np.vdot(b, a) assert result_type == typesystem.from_numpy_dtype(b.dtype).dtype
def test_numba_inner(): # Note these tests assume that the lhs' type is the same as the # promotion type for both arguments. They will fail if additional # test data doesn't adhere to this policy. for a, b in ((np.array([1, 2, 3]), np.array([0, 1, 0])), (np.arange(24).reshape( (2, 3, 4)), np.arange(4)), (np.eye(2), 7)): result_type, result = numba_inner(a, b) if result_type.is_array: assert (result == np.inner(a, b)).all() assert (result_type.dtype == typesystem.from_numpy_dtype( result.dtype).dtype) assert (result_type.dtype == typesystem.from_numpy_dtype( a.dtype).dtype) else: assert result == np.inner(a, b) assert result_type == typesystem.from_numpy_dtype(a.dtype).dtype
def test_numba_inner(): # Note these tests assume that the lhs' type is the same as the # promotion type for both arguments. They will fail if additional # test data doesn't adhere to this policy. for a, b in ((np.array([1,2,3]), np.array([0,1,0])), (np.arange(24).reshape((2,3,4)), np.arange(4)), (np.eye(2), 7)): result_type, result = numba_inner(a, b) if result_type.is_array: assert (result == np.inner(a, b)).all() assert (result_type.dtype == typesystem.from_numpy_dtype(result.dtype).dtype) assert (result_type.dtype == typesystem.from_numpy_dtype(a.dtype).dtype) else: assert result == np.inner(a, b) assert result_type == typesystem.from_numpy_dtype(a.dtype).dtype
def test_numba_outer(): for a, b in ((np.ones((5, )), np.linspace(-2, 2, 5)), (1j * np.linspace(2, -2, 5), np.ones( (5, ))), (np.array(['a', 'b', 'c'], dtype=object), np.arange(1, 4)), (np.array([1]), 1), (np.ones( (2, 2, 2)), np.linspace(-2, 2, 5))): result_type, result = numba_outer(a, b) assert (result == np.outer(a, b)).all() assert (result_type.is_array and result_type.ndim == 2) assert result_type.dtype == typesystem.from_numpy_dtype(a.dtype).dtype
def test_numba_outer(): for a, b in ((np.ones((5,)), np.linspace(-2, 2, 5)), (1j * np.linspace(2, -2, 5), np.ones((5,))), (np.array(['a', 'b', 'c'], dtype=object), np.arange(1,4)), (np.array([1]), 1), (np.ones((2,2,2)), np.linspace(-2, 2, 5))): result_type, result = numba_outer(a, b) assert (result == np.outer(a, b)).all() assert (result_type.is_array and result_type.ndim == 2) assert result_type.dtype == typesystem.from_numpy_dtype(a.dtype).dtype
def resolve_attribute_dtype(dtype, default=None): "Resolve the type for numpy dtype attributes" if dtype.is_numpy_dtype: return dtype if dtype.is_known_value: numpy_attr = dtype.value if isinstance(numpy_attr, np.dtype): return typesystem.from_numpy_dtype(numpy_attr) elif issubclass(numpy_attr, np.generic): return typesystem.from_numpy_dtype(np.dtype(numpy_attr)) elif numpy_attr is not None: try: dtype = np.dtype(numpy_attr) except TypeError: warnings.warn("Unable to infer dtype for '%s'" % numpy_attr) else: return typesystem.from_numpy_dtype(dtype) return None
def test_numba_tensordot2(): A = np.array(1) B = np.array(2) dtype = typesystem.from_numpy_dtype(A.dtype).dtype for i in range(2, 5): for j in range(2, 5): shape_A = (1, ) * i shape_B = (1, ) * j x = A.reshape(*shape_A) y = B.reshape(*shape_B) result_type, result = numba_tensordot2(x, y) control = np.tensordot(x, y) assert result == control #assert result_type == numba.typeof(control) if i + j - 4 > 0: assert result.ndim == result_type.ndim else: assert result_type == dtype
def test_numba_tensordot2(): A = np.array(1) B = np.array(2) dtype = typesystem.from_numpy_dtype(A.dtype).dtype for i in range(2, 5): for j in range(2, 5): shape_A = (1,) * i shape_B = (1,) * j x = A.reshape(*shape_A) y = B.reshape(*shape_B) result_type, result = numba_tensordot2(x, y) control = np.tensordot(x, y) assert result == control #assert result_type == numba.typeof(control) if i + j - 4 > 0: assert result.ndim == result_type.ndim else: assert result_type == dtype
def test_numba_dot(): A = np.array(1) B = np.array(2) dtype = typesystem.from_numpy_dtype(A.dtype).dtype for i in range(1, 5): for j in range(1, 5): # print i, j shape_A = (1, ) * i shape_B = (1, ) * j x = A.reshape(*shape_A) y = B.reshape(*shape_B) result_type, result = numba_dot(x, y) assert result == np.dot(x, y) if i + j - 2 > 0: assert result.ndim == result_type.ndim else: assert result_type == dtype
def test_numba_dot(): A = np.array(1) B = np.array(2) dtype = typesystem.from_numpy_dtype(A.dtype).dtype for i in range(1, 5): for j in range(1, 5): # print i, j shape_A = (1,) * i shape_B = (1,) * j x = A.reshape(*shape_A) y = B.reshape(*shape_B) result_type, result = numba_dot(x, y) assert result == np.dot(x, y) if i + j - 2 > 0: assert result.ndim == result_type.ndim else: assert result_type == dtype, (result_type, dtype)