本文整理汇总了Python中numpy.core.array函数的典型用法代码示例。如果您正苦于以下问题:Python array函数的具体用法?Python array怎么用?Python array使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了array函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fix_with_subclass
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
if isinstance(obj, MyArray):
obj.metadata = self.metadata
return obj
def __array_finalize__(self, obj):
self.metadata = getattr(obj, 'metadata', None)
return self
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
# check 0d arrays don't decay to scalars
m0d = m[0,...]
m0d.metadata = 'bar'
f0d = ufl.fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')
示例2: assert_array_compare
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
header=''):
from numpy.core import array, isnan, any
x = array(x, copy=False, subok=True)
y = array(y, copy=False, subok=True)
def isnumber(x):
return x.dtype.char in '?bhilqpBHILQPfdgFDG'
try:
cond = (x.shape==() or y.shape==()) or x.shape == y.shape
if not cond:
msg = build_err_msg([x, y],
err_msg
+ '\n(shapes %s, %s mismatch)' % (x.shape,
y.shape),
verbose=verbose, header=header,
names=('x', 'y'))
if not cond :
raise AssertionError(msg)
if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
# Handling nan: we first check that x and y have the nan at the
# same locations, and then we mask the nan and do the comparison as
# usual.
xnanid = isnan(x)
ynanid = isnan(y)
try:
assert_array_equal(xnanid, ynanid)
except AssertionError:
msg = build_err_msg([x, y],
err_msg
+ '\n(x and y nan location mismatch %s, ' \
'%s mismatch)' % (xnanid, ynanid),
verbose=verbose, header=header,
names=('x', 'y'))
val = comparison(x[~xnanid], y[~ynanid])
else:
val = comparison(x,y)
if isinstance(val, bool):
cond = val
reduced = [0]
else:
reduced = val.ravel()
cond = reduced.all()
reduced = reduced.tolist()
if not cond:
match = 100-100.0*reduced.count(1)/len(reduced)
msg = build_err_msg([x, y],
err_msg
+ '\n(mismatch %s%%)' % (match,),
verbose=verbose, header=header,
names=('x', 'y'))
if not cond :
raise AssertionError(msg)
except ValueError:
msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
names=('x', 'y'))
raise ValueError(msg)
示例3: test_3D_array
def test_3D_array(self):
a = array([[1, 2], [1, 2]])
b = array([[2, 3], [2, 3]])
a = array([a, a])
b = array([b, b])
res = map(atleast_1d, [a, b])
desired = [a, b]
assert_array_equal(res, desired)
示例4: test_3D_array
def test_3D_array(self):
a = array([[1, 2], [1, 2]])
b = array([[2, 3], [2, 3]])
a = array([a, a])
b = array([b, b])
res = [atleast_2d(a), atleast_2d(b)]
desired = [a, b]
assert_array_equal(res, desired)
示例5: test_bad_out_shape
def test_bad_out_shape(self):
a = array([1, 2])
b = array([3, 4])
assert_raises(ValueError, concatenate, (a, b), out=np.empty(5))
assert_raises(ValueError, concatenate, (a, b), out=np.empty((4,1)))
assert_raises(ValueError, concatenate, (a, b), out=np.empty((1,4)))
concatenate((a, b), out=np.empty(4))
示例6: test_log2
def test_log2(self):
a = nx.array([4.5, 2.3, 6.5])
out = nx.zeros(a.shape, float)
tgt = nx.array([2.169925, 1.20163386, 2.70043972])
res = ufl.log2(a)
assert_almost_equal(res, tgt)
res = ufl.log2(a, out)
assert_almost_equal(res, tgt)
assert_almost_equal(out, tgt)
示例7: test_stack
def test_stack():
# non-iterable input
assert_raises(TypeError, stack, 1)
# 0d input
for input_ in [(1, 2, 3),
[np.int32(1), np.int32(2), np.int32(3)],
[np.array(1), np.array(2), np.array(3)]]:
assert_array_equal(stack(input_), [1, 2, 3])
# 1d input examples
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
r1 = array([[1, 2, 3], [4, 5, 6]])
assert_array_equal(np.stack((a, b)), r1)
assert_array_equal(np.stack((a, b), axis=1), r1.T)
# all input types
assert_array_equal(np.stack(list([a, b])), r1)
assert_array_equal(np.stack(array([a, b])), r1)
# all shapes for 1d input
arrays = [np.random.randn(3) for _ in range(10)]
axes = [0, 1, -1, -2]
expected_shapes = [(10, 3), (3, 10), (3, 10), (10, 3)]
for axis, expected_shape in zip(axes, expected_shapes):
assert_equal(np.stack(arrays, axis).shape, expected_shape)
assert_raises_regex(np.AxisError, 'out of bounds', stack, arrays, axis=2)
assert_raises_regex(np.AxisError, 'out of bounds', stack, arrays, axis=-3)
# all shapes for 2d input
arrays = [np.random.randn(3, 4) for _ in range(10)]
axes = [0, 1, 2, -1, -2, -3]
expected_shapes = [(10, 3, 4), (3, 10, 4), (3, 4, 10),
(3, 4, 10), (3, 10, 4), (10, 3, 4)]
for axis, expected_shape in zip(axes, expected_shapes):
assert_equal(np.stack(arrays, axis).shape, expected_shape)
# empty arrays
assert_(stack([[], [], []]).shape == (3, 0))
assert_(stack([[], [], []], axis=1).shape == (0, 3))
# out
out = np.zeros_like(r1)
np.stack((a, b), out=out)
assert_array_equal(out, r1)
# edge cases
assert_raises_regex(ValueError, 'need at least one array', stack, [])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [1, np.arange(3)])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(3), 1])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(3), 1], axis=1)
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.zeros((3, 3)), np.zeros(3)], axis=1)
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(2), np.arange(3)])
# generator is deprecated
with assert_warns(FutureWarning):
result = stack((x for x in range(3)))
assert_array_equal(result, np.array([0, 1, 2]))
示例8: test_isneginf
def test_isneginf(self):
a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
out = nx.zeros(a.shape, bool)
tgt = nx.array([False, True, False, False, False, False])
res = ufl.isneginf(a)
assert_equal(res, tgt)
res = ufl.isneginf(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
示例9: test_fix
def test_fix(self):
a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
out = nx.zeros(a.shape, float)
tgt = nx.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]])
res = ufl.fix(a)
assert_equal(res, tgt)
res = ufl.fix(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
assert_equal(ufl.fix(3.14), 3)
示例10: Trebuchet1
def Trebuchet1(t, state, param):
g = param
Theta + dTheta = state
f = array([(A*dTheta**2*h*n - A*dTheta**2*h*s - A*g*n + A*g*s - 2*M*g*n + 2*g*m*s)*cos(Theta)/2])
m = array([
[A*(n + s)**2/12 + A*(4*h**2 - 4*h*(n - s)*sin(Theta) + (n - s)**2)/4 + M*n**2 + m*s**2],
])
n = array([0])
if h - s*sin(Theta) < 0:
n = array([-g*m*s*cos(Theta)])
f = f + n
return concatenate((solve(m,f),[dTheta]))
示例11: test_stack
def test_stack():
# non-iterable input
assert_raises(TypeError, stack, 1)
# 0d input
for input_ in [(1, 2, 3),
[np.int32(1), np.int32(2), np.int32(3)],
[np.array(1), np.array(2), np.array(3)]]:
assert_array_equal(stack(input_), [1, 2, 3])
# 1d input examples
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
r1 = array([[1, 2, 3], [4, 5, 6]])
assert_array_equal(np.stack((a, b)), r1)
assert_array_equal(np.stack((a, b), axis=1), r1.T)
# all input types
assert_array_equal(np.stack(list([a, b])), r1)
assert_array_equal(np.stack(array([a, b])), r1)
# all shapes for 1d input
arrays = [np.random.randn(3) for _ in range(10)]
axes = [0, 1, -1, -2]
expected_shapes = [(10, 3), (3, 10), (3, 10), (10, 3)]
for axis, expected_shape in zip(axes, expected_shapes):
assert_equal(np.stack(arrays, axis).shape, expected_shape)
assert_raises_regex(IndexError, 'out of bounds', stack, arrays, axis=2)
assert_raises_regex(IndexError, 'out of bounds', stack, arrays, axis=-3)
# all shapes for 2d input
arrays = [np.random.randn(3, 4) for _ in range(10)]
axes = [0, 1, 2, -1, -2, -3]
expected_shapes = [(10, 3, 4), (3, 10, 4), (3, 4, 10),
(3, 4, 10), (3, 10, 4), (10, 3, 4)]
for axis, expected_shape in zip(axes, expected_shapes):
assert_equal(np.stack(arrays, axis).shape, expected_shape)
# empty arrays
assert_(stack([[], [], []]).shape == (3, 0))
assert_(stack([[], [], []], axis=1).shape == (0, 3))
# edge cases
assert_raises_regex(ValueError, 'need at least one array', stack, [])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [1, np.arange(3)])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(3), 1])
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(3), 1], axis=1)
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.zeros((3, 3)), np.zeros(3)], axis=1)
assert_raises_regex(ValueError, 'must have the same shape',
stack, [np.arange(2), np.arange(3)])
# np.matrix
m = np.matrix([[1, 2], [3, 4]])
assert_raises_regex(ValueError, 'shape too large to be a matrix',
stack, [m, m])
示例12: Trebuchet2
def Trebuchet2(t, state, param):
g = param
Theta + dTheta = state
f = array([A*dTheta**2*h*(n - s)*cos(Theta)/2 - A*g*(n - s)*cos(Theta)/2 - M*dPhi*dTheta*n*p*cos(Phi) - M*dPhi*n*p*(dPhi + dTheta)*cos(Phi) - M*g*(n*cos(Theta) + p*(sin(Phi)*cos(Theta) + sin(Theta)*cos(Phi))) + g*m*s*cos(Theta), M*p*(dTheta**2*n*cos(Phi) - g*sin(Phi)*cos(Theta) - g*sin(Theta)*cos(Phi))])
m = array([
[A*(n + s)**2/12 + A*(4*h**2 - 4*h*(n - s)*sin(Theta) + (n - s)**2)/4 + M*n**2 + 2*M*n*p*sin(Phi) + M*p**2 + m*s**2, M*p*(n*sin(Phi) + p)],
[M*p*(n*sin(Phi) + p), M*p**2],
])
n = array([0, 0])
if h - s*sin(Theta) < 0:
n = array([-g*m*s*cos(Theta), 0])
f = f + n
return concatenate((solve(m,f),[dTheta, dPhi]))
示例13: Trebuchet3
def Trebuchet3(t, state, param):
g = param
Theta + dTheta = state
f = array([A*dTheta**2*h*(n - s)*cos(Theta)/2 - A*g*(n - s)*cos(Theta)/2 - M*g*n*cos(Theta) + dPsi*dTheta*m*q*s*cos(Psi) + dPsi*m*q*s*(dPsi + dTheta)*cos(Psi) - g*m*(q*(sin(Psi)*cos(Theta) + sin(Theta)*cos(Psi)) - s*cos(Theta)), m*q*(-dTheta**2*s*cos(Psi) - g*sin(Psi)*cos(Theta) - g*sin(Theta)*cos(Psi))])
m = array([
[A*(n + s)**2/12 + A*(4*h**2 - 4*h*(n - s)*sin(Theta) + (n - s)**2)/4 + M*n**2 + m*q**2 - 2*m*q*s*sin(Psi) + m*s**2, m*q*(q - s*sin(Psi))],
[m*q*(q - s*sin(Psi)), m*q**2],
])
n = array([0, 0])
if h + q*(sin(Psi)*sin(Theta) - cos(Psi)*cos(Theta)) - s*sin(Theta) < 0:
n = array([g*m*(q*(sin(Psi)*cos(Theta) + sin(Theta)*cos(Psi))*((sin(Psi)*sin(Theta) - cos(Psi)*cos(Theta))**2 + (sin(Psi)*cos(Theta) + sin(Theta)*cos(Psi))**2) - s*cos(Theta)), g*m*q*(sin(Psi)*cos(Theta) + sin(Theta)*cos(Psi))*((sin(Psi)*sin(Theta) - cos(Psi)*cos(Theta))**2 + (sin(Psi)*cos(Theta) + sin(Theta)*cos(Psi))**2)])
f = f + n
return concatenate((solve(m,f),[dTheta, dPsi]))
示例14: DifferentialEquation
def DifferentialEquation(t, state, param):
[h, o, p, q, r, n, m, M, g] = param
[thetaDot, phiDot, theta, phi] = state
force = array([0, 0, -o*sin(theta)*Derivative(theta, t)**2 + o*cos(theta)*Derivative(theta, t, t) - (h - y(t))*sin(theta)*Derivative(theta, t, t) - (h - y(t))*cos(theta)*Derivative(theta, t)**2 + 2*sin(theta)*Derivative(theta, t)*Derivative(y(t), t)])
mass = array([
[0, 0, 0],
[0, 0, 0],
[0, 0, cos(theta)],
])
constraint = array([0, 0])
if -r*sin(phi) + (-p - q)*sin(theta) + y(t) < 0:
constraint = array([-g*m*(p + q)*cos(theta), -g*m*r*cos(phi)])
force = force + constraint
return concatenate((solve(mass,force),[thetaDot, phiDot]))
示例15: test_isneginf
def test_isneginf(self):
a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
out = nx.zeros(a.shape, bool)
tgt = nx.array([False, True, False, False, False, False])
res = ufl.isneginf(a)
assert_equal(res, tgt)
res = ufl.isneginf(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
a = a.astype(np.complex)
with assert_raises(TypeError):
ufl.isneginf(a)