本文整理汇总了Python中numpy.may_share_memory函数的典型用法代码示例。如果您正苦于以下问题:Python may_share_memory函数的具体用法?Python may_share_memory怎么用?Python may_share_memory使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了may_share_memory函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_BandMat_sub
def test_BandMat_sub(self, its=100):
for it in range(its):
size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
a_bm = gen_BandMat(size)
b_bm = gen_BandMat(size)
a_full = a_bm.full()
b_full = b_bm.full()
c_bm = a_bm - b_bm
c_full = a_full - b_full
assert_allclose(c_bm.full(), c_full)
assert not np.may_share_memory(c_bm.data, a_bm.data)
assert not np.may_share_memory(c_bm.data, b_bm.data)
c_bm = a_bm - 0
c_full = a_full - 0
assert_allclose(c_bm.full(), c_full)
assert not np.may_share_memory(c_bm.data, a_bm.data)
c_bm = 0 - a_bm
c_full = 0 - a_full
assert_allclose(c_bm.full(), c_full)
assert not np.may_share_memory(c_bm.data, a_bm.data)
with self.assertRaises(TypeError):
a_bm - 1.0
with self.assertRaises(TypeError):
1.0 - a_bm
示例2: test_no_aliasing_2
def test_no_aliasing_2(self):
# B and A take one another's values
# no copying is necessary since each one is updated.
orig_a = numpy.zeros((2,2))+.5
orig_b = numpy.zeros((2,2))-.5
A = self.shared(orig_a)
B = self.shared(orig_b)
C = tensor.dmatrix()
z = numpy.zeros((2,2))
data_of_a = data_of(A)
data_of_b = data_of(B)
f = pfunc([C], [], updates=[(A,B),(B,A)])
f(z)
# correctness
assert numpy.all(data_of(A) == -.5)
assert numpy.all(data_of(B) == +.5)
# shared vars may not be aliased
assert not numpy.may_share_memory(data_of(A), data_of(B))
# theano should have been smart enough to not make copies
assert numpy.may_share_memory(data_of(A), data_of_b)
assert numpy.may_share_memory(data_of(B), data_of_a)
示例3: test_nodes_at_link
def test_nodes_at_link():
"""Test nodes_at_link shares data with tail and head."""
assert_array_equal(rmg.nodes_at_link[:, 0], rmg.node_at_link_tail)
assert_array_equal(rmg.nodes_at_link[:, 1], rmg.node_at_link_head)
assert_true(np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_tail))
assert_true(np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_head))
示例4: check_may_share_memory_easy_fuzz
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count):
# Check that overlap problems with common strides are solved with
# little work.
x = np.zeros([17,34,71,97], dtype=np.int16)
feasible = 0
infeasible = 0
pair_iter = iter_random_view_pairs(x, same_steps)
while min(feasible, infeasible) < min_count:
a, b = next(pair_iter)
bounds_overlap = np.may_share_memory(a, b)
may_share_answer = np.may_share_memory(a, b)
easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b))
exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)
if easy_answer != exact_answer:
# assert_equal is slow...
assert_equal(easy_answer, exact_answer)
if may_share_answer != bounds_overlap:
assert_equal(may_share_answer, bounds_overlap)
if bounds_overlap:
if exact_answer:
feasible += 1
else:
infeasible += 1
示例5: test_transpose
def test_transpose(self):
s0_transpose = self.s0.transpose()
s0_diff = s0_transpose.differentials['s']
assert s0_transpose.shape == (7, 6)
assert s0_diff.shape == s0_transpose.shape
assert np.all(s0_transpose.lon == self.s0.lon.transpose())
assert np.all(s0_diff.d_lon == self.diff.d_lon.transpose())
assert np.may_share_memory(s0_transpose.distance, self.s0.distance)
assert np.may_share_memory(s0_diff.d_lon, self.diff.d_lon)
s1_transpose = self.s1.transpose()
s1_diff = s1_transpose.differentials['s']
assert s1_transpose.shape == (7, 6)
assert s1_diff.shape == s1_transpose.shape
assert np.all(s1_transpose.lat == self.s1.lat.transpose())
assert np.all(s1_diff.d_lon == self.diff.d_lon.transpose())
assert np.may_share_memory(s1_transpose.lat, self.s1.lat)
assert np.may_share_memory(s1_diff.d_lon, self.diff.d_lon)
# Only one check on T, since it just calls transpose anyway.
# Doing it on the CartesianRepresentation just for variety's sake.
c0_T = self.c0.T
assert c0_T.shape == (7, 6)
assert np.all(c0_T.x == self.c0.x.T)
assert np.may_share_memory(c0_T.y, self.c0.y)
示例6: test_non_ndarray_inputs
def test_non_ndarray_inputs():
# Regression check for gh-5604
class MyArray(object):
def __init__(self, data):
self.data = data
@property
def __array_interface__(self):
return self.data.__array_interface__
class MyArray2(object):
def __init__(self, data):
self.data = data
def __array__(self):
return self.data
for cls in [MyArray, MyArray2]:
x = np.arange(5)
assert_(np.may_share_memory(cls(x[::2]), x[1::2]))
assert_(not np.shares_memory(cls(x[::2]), x[1::2]))
assert_(np.shares_memory(cls(x[1::3]), x[::2]))
assert_(np.may_share_memory(cls(x[1::3]), x[::2]))
示例7: test_dot_mm_partial
def test_dot_mm_partial(self, its=50):
for it in range(its):
size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
a_bm = gen_BandMat(size)
b_bm = gen_BandMat(size)
l = random.choice([0, 1, randint(0, 10)])
u = random.choice([0, 1, randint(0, 10)])
diag = None if rand_bool() else randn(size)
diag_value = np.ones((size,)) if diag is None else diag
a_full = a_bm.full()
b_full = b_bm.full()
c_bm = bm.dot_mm_partial(l, u, a_bm, b_bm, diag=diag)
c_full = fl.band_ec(
l, u,
np.dot(np.dot(a_full, np.diag(diag_value)), b_full)
)
assert c_bm.l == l
assert c_bm.u == u
assert c_bm.size == size
assert_allclose(c_bm.full(), c_full)
assert not np.may_share_memory(c_bm.data, a_bm.data)
assert not np.may_share_memory(c_bm.data, b_bm.data)
if diag is not None:
assert not np.may_share_memory(c_bm.data, diag)
示例8: test_nodes_at_link
def test_nodes_at_link():
"""Test nodes_at_link shares data with tail and head."""
rmg = RasterModelGrid((4, 5), spacing=1.)
assert_array_equal(rmg.nodes_at_link[:, 0], rmg.node_at_link_tail)
assert_array_equal(rmg.nodes_at_link[:, 1], rmg.node_at_link_head)
assert np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_tail)
assert np.may_share_memory(rmg.nodes_at_link, rmg.node_at_link_head)
示例9: test_no_copy
def test_no_copy():
c1 = SkyCoord(np.arange(10.) * u.hourangle, np.arange(20., 30.) * u.deg)
c2 = SkyCoord(c1, copy=False)
# Note: c1.ra and c2.ra will *not* share memory, as these are recalculated
# to be in "preferred" units. See discussion in #4883.
assert np.may_share_memory(c1.data.lon, c2.data.lon)
c3 = SkyCoord(c1, copy=True)
assert not np.may_share_memory(c1.data.lon, c3.data.lon)
示例10: test__solve_triangular_banded
def test__solve_triangular_banded(self, its=100):
for it in range(its):
size = random.choice([0, 1, randint(0, 10), randint(0, 100)])
b = randn(size)
chol_bm = gen_chol_factor_BandMat(size, transposed=False)
chol_data = chol_bm.data
depth = chol_bm.l + chol_bm.u
lower = (chol_bm.u == 0)
if size > 0 and rand_bool() and rand_bool():
badFrame = randint(size)
chol_data[0 if lower else depth, badFrame] = 0.0
else:
badFrame = None
transposed = rand_bool()
overwrite_b = rand_bool()
chol_full = chol_bm.full()
b_arg = b.copy()
if badFrame is not None:
msg = (
'singular matrix: resolution failed at diagonal %d' %
badFrame
)
msgRe = '^' + re.escape(msg) + '$'
with self.assertRaisesRegexp(la.LinAlgError, msgRe):
bla._solve_triangular_banded(
chol_data, b_arg, transposed=transposed, lower=lower,
overwrite_b=overwrite_b
)
with self.assertRaisesRegexp(la.LinAlgError, msgRe):
sla.solve_triangular(
chol_full, b, trans=transposed, lower=lower
)
else:
x = bla._solve_triangular_banded(
chol_data, b_arg, transposed=transposed, lower=lower,
overwrite_b=overwrite_b
)
if transposed:
assert_allclose(bm.dot_mv(chol_bm.T, x), b)
else:
assert_allclose(bm.dot_mv(chol_bm, x), b)
if size == 0:
x_good = np.zeros((size,))
else:
x_good = sla.solve_triangular(
chol_full, b, trans=transposed, lower=lower
)
assert_allclose(x, x_good)
assert not np.may_share_memory(x, chol_data)
if size > 0:
self.assertEquals(
np.may_share_memory(x, b_arg),
overwrite_b
)
if not overwrite_b:
assert np.all(b_arg == b)
示例11: check_may_share_memory_easy_fuzz
def check_may_share_memory_easy_fuzz(get_max_work, same_steps, min_count):
# Check that overlap problems with common strides are solved with
# little work.
x = np.zeros([17,34,71,97], dtype=np.int16)
rng = np.random.RandomState(1234)
def random_slice(n, step):
start = rng.randint(0, n+1, dtype=np.intp)
stop = rng.randint(start, n+1, dtype=np.intp)
if rng.randint(0, 2, dtype=np.intp) == 0:
stop, start = start, stop
step *= -1
return slice(start, stop, step)
feasible = 0
infeasible = 0
while min(feasible, infeasible) < min_count:
steps = tuple(rng.randint(1, 11, dtype=np.intp)
if rng.randint(0, 5, dtype=np.intp) == 0 else 1
for j in range(x.ndim))
if same_steps:
steps2 = steps
else:
steps2 = tuple(rng.randint(1, 11, dtype=np.intp)
if rng.randint(0, 5, dtype=np.intp) == 0 else 1
for j in range(x.ndim))
t1 = np.arange(x.ndim)
rng.shuffle(t1)
t2 = np.arange(x.ndim)
rng.shuffle(t2)
s1 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps))
s2 = tuple(random_slice(p, s) for p, s in zip(x.shape, steps2))
a = x[s1].transpose(t1)
b = x[s2].transpose(t2)
bounds_overlap = np.may_share_memory(a, b)
may_share_answer = np.may_share_memory(a, b)
easy_answer = np.may_share_memory(a, b, max_work=get_max_work(a, b))
exact_answer = np.may_share_memory(a, b, max_work=MAY_SHARE_EXACT)
if easy_answer != exact_answer:
# assert_equal is slow...
assert_equal(easy_answer, exact_answer, err_msg=repr((s1, s2)))
if may_share_answer != bounds_overlap:
assert_equal(may_share_answer, bounds_overlap,
err_msg=repr((s1, s2)))
if bounds_overlap:
if exact_answer:
feasible += 1
else:
infeasible += 1
示例12: test_shared_constructor_copies
def test_shared_constructor_copies(self):
# shared constructor makes copy
# (rule #2)
orig_a = numpy.zeros((2,2))
A = self.shared(orig_a)
assert not numpy.may_share_memory(orig_a, data_of(A))
# rule #2 reading back from theano-managed memory
assert not numpy.may_share_memory(A.get_value(borrow=False), data_of(A))
示例13: test_diagonal
def test_diagonal(self):
s0_diagonal = self.s0.diagonal()
s0_diff = s0_diagonal.differentials['s']
assert s0_diagonal.shape == (6,)
assert s0_diff.shape == s0_diagonal.shape
assert np.all(s0_diagonal.lat == self.s0.lat.diagonal())
assert np.all(s0_diff.d_lon == self.diff.d_lon.diagonal())
assert np.may_share_memory(s0_diagonal.lat, self.s0.lat)
assert np.may_share_memory(s0_diff.d_lon, self.diff.d_lon)
示例14: test_swapaxes
def test_swapaxes(self):
s1_swapaxes = self.s1.swapaxes(0, 1)
s1_diff = s1_swapaxes.differentials['s']
assert s1_swapaxes.shape == (7, 6)
assert s1_diff.shape == s1_swapaxes.shape
assert np.all(s1_swapaxes.lat == self.s1.lat.swapaxes(0, 1))
assert np.all(s1_diff.d_lon == self.diff.d_lon.swapaxes(0, 1))
assert np.may_share_memory(s1_swapaxes.lat, self.s1.lat)
assert np.may_share_memory(s1_diff.d_lon, self.diff.d_lon)
示例15: _assert_copies
def _assert_copies(t, t2, deep=True):
assert t.colnames == t2.colnames
np.testing.assert_array_equal(t._data, t2._data)
assert t.meta == t2.meta
if deep:
assert not np.may_share_memory(t._data, t2._data)
else:
assert np.may_share_memory(t._data, t2._data)