本文整理汇总了Python中numpy.core.umath.add方法的典型用法代码示例。如果您正苦于以下问题:Python umath.add方法的具体用法?Python umath.add怎么用?Python umath.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.core.umath
的用法示例。
在下文中一共展示了umath.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ufunc_override_not_implemented
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_ufunc_override_not_implemented(self):
class A(object):
def __array_ufunc__(self, *args, **kwargs):
return NotImplemented
msg = ("operand type(s) all returned NotImplemented from "
"__array_ufunc__(<ufunc 'negative'>, '__call__', <*>): 'A'")
with assert_raises_regex(TypeError, fnmatch.translate(msg)):
np.negative(A())
msg = ("operand type(s) all returned NotImplemented from "
"__array_ufunc__(<ufunc 'add'>, '__call__', <*>, <object *>, "
"out=(1,)): 'A', 'object', 'int'")
with assert_raises_regex(TypeError, fnmatch.translate(msg)):
np.add(A(), object(), out=1)
示例2: test_prepare
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_prepare(self):
class with_prepare(np.ndarray):
__array_priority__ = 10
def __array_prepare__(self, arr, context):
# make sure we can return a new
return np.array(arr).view(type=with_prepare)
a = np.array(1).view(type=with_prepare)
x = np.add(a, a)
assert_equal(x, np.array(2))
assert_equal(type(x), with_prepare)
示例3: test_prepare_out
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_prepare_out(self):
class with_prepare(np.ndarray):
__array_priority__ = 10
def __array_prepare__(self, arr, context):
return np.array(arr).view(type=with_prepare)
a = np.array([1]).view(type=with_prepare)
x = np.add(a, a, a)
# Returned array is new, because of the strange
# __array_prepare__ above
assert_(not np.shares_memory(x, a))
assert_equal(x, np.array([2]))
assert_equal(type(x), with_prepare)
示例4: test_attributes
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_attributes(self):
add = ncu.add
assert_equal(add.__name__, 'add')
assert_(add.ntypes >= 18) # don't fail if types added
assert_('ii->i' in add.types)
assert_equal(add.nin, 2)
assert_equal(add.nout, 1)
assert_equal(add.identity, 0)
示例5: test_doc
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_doc(self):
# don't bother checking the long list of kwargs, which are likely to
# change
assert_(ncu.add.__doc__.startswith(
"add(x1, x2, /, out=None, *, where=True"))
assert_(ncu.frexp.__doc__.startswith(
"frexp(x[, out1, out2], / [, out=(None, None)], *, where=True"))
示例6: test_reduceat
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_reduceat():
"""Test bug in reduceat when structured arrays are not copied."""
db = np.dtype([('name', 'S11'), ('time', np.int64), ('value', np.float32)])
a = np.empty([100], dtype=db)
a['name'] = 'Simple'
a['time'] = 10
a['value'] = 100
indx = [0, 7, 15, 25]
h2 = []
val1 = indx[0]
for val2 in indx[1:]:
h2.append(np.add.reduce(a['value'][val1:val2]))
val1 = val2
h2.append(np.add.reduce(a['value'][val1:]))
h2 = np.array(h2)
# test buffered -- this should work
h1 = np.add.reduceat(a['value'], indx)
assert_array_almost_equal(h1, h2)
# This is when the error occurs.
# test no buffer
np.setbufsize(32)
h1 = np.add.reduceat(a['value'], indx)
np.setbufsize(np.UFUNC_BUFSIZE_DEFAULT)
assert_array_almost_equal(h1, h2)
示例7: test_reduceat_empty
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_reduceat_empty():
"""Reduceat should work with empty arrays"""
indices = np.array([], 'i4')
x = np.array([], 'f8')
result = np.add.reduceat(x, indices)
assert_equal(result.dtype, x.dtype)
assert_equal(result.shape, (0,))
# Another case with a slightly different zero-sized shape
x = np.ones((5, 2))
result = np.add.reduceat(x, [], axis=0)
assert_equal(result.dtype, x.dtype)
assert_equal(result.shape, (0, 2))
result = np.add.reduceat(x, [], axis=1)
assert_equal(result.dtype, x.dtype)
assert_equal(result.shape, (5, 0))
示例8: test_ufunc_override_disabled
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_ufunc_override_disabled(self):
# 2016-01-29: NUMPY_UFUNC_DISABLED
# This test should be removed when __numpy_ufunc__ is re-enabled.
class MyArray(object):
def __numpy_ufunc__(self, *args, **kwargs):
self._numpy_ufunc_called = True
my_array = MyArray()
real_array = np.ones(10)
assert_raises(TypeError, lambda: real_array + my_array)
assert_raises(TypeError, np.add, real_array, my_array)
assert not hasattr(my_array, "_numpy_ufunc_called")
示例9: test_attributes
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_attributes(self):
add = ncu.add
assert_equal(add.__name__, 'add')
assert_(add.__doc__.startswith('add(x1, x2[, out])\n\n'))
self.assertTrue(add.ntypes >= 18) # don't fail if types added
self.assertTrue('ii->i' in add.types)
assert_equal(add.nin, 2)
assert_equal(add.nout, 1)
assert_equal(add.identity, 0)
示例10: test_ufunc_override_disabled
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_ufunc_override_disabled(self):
class OptOut(object):
__array_ufunc__ = None
opt_out = OptOut()
# ufuncs always raise
msg = "operand 'OptOut' does not support ufuncs"
with assert_raises_regex(TypeError, msg):
np.add(opt_out, 1)
with assert_raises_regex(TypeError, msg):
np.add(1, opt_out)
with assert_raises_regex(TypeError, msg):
np.negative(opt_out)
# opt-outs still hold even when other arguments have pathological
# __array_ufunc__ implementations
class GreedyArray(object):
def __array_ufunc__(self, *args, **kwargs):
return self
greedy = GreedyArray()
assert_(np.negative(greedy) is greedy)
with assert_raises_regex(TypeError, msg):
np.add(greedy, opt_out)
with assert_raises_regex(TypeError, msg):
np.add(greedy, 1, out=opt_out)
示例11: getmaskarray
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def getmaskarray (a):
"""Mask of values in a; an array of zeros if mask is nomask
or not a masked array, and is a byte-sized integer.
Do not try to add up entries, for example.
"""
m = getmask(a)
if m is nomask:
return make_mask_none(shape(a))
else:
return m
示例12: __add__
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def __add__(self, other):
"Return add(self, other)"
return add(self, other)
示例13: count
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def count (self, axis = None):
"Count of the non-masked elements in a, or along a certain axis."
m = self._mask
s = self._data.shape
ls = len(s)
if m is nomask:
if ls == 0:
return 1
if ls == 1:
return s[0]
if axis is None:
return reduce(lambda x, y:x*y, s)
else:
n = s[axis]
t = list(s)
del t[axis]
return ones(t) * n
if axis is None:
w = fromnumeric.ravel(m).astype(int)
n1 = size(w)
if n1 == 1:
n2 = w[0]
else:
n2 = umath.add.reduce(w)
return n1 - n2
else:
n1 = size(m, axis)
n2 = sum(m.astype(int), axis)
return n1 - n2
示例14: sum
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def sum (target, axis=None, dtype=None):
if axis is None:
target = ravel(target)
axis = 0
return add.reduce(target, axis, dtype)
示例15: test_attributes
# 需要导入模块: from numpy.core import umath [as 别名]
# 或者: from numpy.core.umath import add [as 别名]
def test_attributes(self):
add = ncu.add
assert_equal(add.__name__, 'add')
assert_(add.__doc__.startswith('add(x1, x2[, out])\n\n'))
self.assertTrue(add.ntypes >= 18) # don't fail if types added
self.assertTrue('ii->i' in add.types)
assert_equal(add.nin, 2)
assert_equal(add.nout, 1)
assert_equal(add.identity, 0)