def test(): a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) a_lg = lg.array(a) b_lg = a_lg.swapaxes(0, 1) print("small") assert lg.array_equal(a_lg.sum(axis=0), b_lg.sum(axis=1)) a_tall = np.concatenate((a, ) * 100) a_tall_lg = lg.array(a_tall) b_tall_lg = a_tall_lg.swapaxes(0, 1) print("tall") assert lg.array_equal(a_tall_lg.sum(axis=0), b_tall_lg.sum(axis=1)) a_wide = np.concatenate((a, ) * 100, axis=1) a_wide_lg = lg.array(a_wide) b_wide_lg = a_wide_lg.swapaxes(0, 1) print("wide") assert lg.array_equal(a_wide_lg.sum(axis=0), b_wide_lg.sum(axis=1)) a_big = np.concatenate((a_tall, ) * 100, axis=1) a_big_lg = lg.array(a_big) b_big_lg = a_big_lg.swapaxes(0, 1) print("big") assert lg.array_equal(a_big_lg.sum(axis=0), b_big_lg.sum(axis=1))
def test(): a = lg.array([0, 1, 2]) b = lg.tile(a, 4) assert lg.array_equal(b, [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]) c = lg.tile(a, (3, 4)) assert lg.array_equal( c, [ [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], ], ) d = lg.tile(a, (3, 1, 4)) assert lg.array_equal( d, [ [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]], ], ) e = lg.array([[1, 2], [3, 4]]) f = lg.tile(e, 2) assert lg.array_equal(f, [[1, 2, 1, 2], [3, 4, 3, 4]]) g = lg.tile(e, (2, 1)) assert lg.array_equal(g, [[1, 2], [3, 4], [1, 2], [3, 4]]) return
def test(): x = lg.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]) x[0, :] = 0 assert lg.array_equal(x[0, :], [0, 0, 0, 0]) y = lg.array([[20, 30, 40, 50], [70, 80, 90, 100]]) x[1:4:2] = y assert lg.array_equal(x[1, :], [20, 30, 40, 50]) assert lg.array_equal(x[3, :], [70, 80, 90, 100]) input_size = 10 hidden_size = 4 WLSTM = lg.random.randn(input_size + hidden_size + 1, 4 * hidden_size) / lg.sqrt(input_size + hidden_size) WLSTM[0, :] = 0 # initialize biases to zero assert lg.array_equal( WLSTM[0, :], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ], ) lg.random.seed(10) WLSTM = lg.random.randn(15, 16) dIFOGt = lg.random.randn(3, 16) dHoutt_in = lg.random.randn(2, 3, 4) dHoutt = dHoutt_in.copy() dHint = dIFOGt.dot(WLSTM.transpose()) temp = dHoutt[0, :] + dHint[:, 11:] dHoutt[0, :] = temp assert not lg.array_equal(dHoutt[0, :], dHoutt_in[0, :]) return
def test(): x = lg.array([[1, 2, 3], [4, 5, 6]]) y = lg.transpose(x) assert lg.array_equal(y, [[1, 4], [2, 5], [3, 6]]) z = lg.transpose(y) assert lg.array_equal(x, z) x = lg.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = x.transpose() assert lg.array_equal(y, [[1, 4, 7], [2, 5, 8], [3, 6, 9]]) z = lg.transpose(y) assert lg.array_equal(x, z) return
def test(): sr = pd.Series([1, 2, 3]) array = np.asarray(sr) x = np.array([1, 2, 3]) assert np.array_equal(array, x) y = np.array([4, 5, 6]) z = np.add(sr, y) assert np.array_equal(z, x + y) df = pd.DataFrame({"x": x, "y": y}) z = np.add(df["x"], df["y"]) assert np.array_equal(z, x + y) return
def test(): a = lg.array([[1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9]], dtype=np.float64) b = lg.array( [[10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20, 21]], dtype=np.float64, ) c = a.dot(b) assert lg.array_equal(c, [[350, 371], [620, 659]]) d = lg.array([1, 2, 3, 4, 5, 6], dtype=np.float64) e = lg.array([1, 2, 3, 4, 5, 6], dtype=np.float64) f = d.dot(e) assert f == 91 # This test does not work ATM. It seems that setting random seed to # be the same is not sufficient to make the inputs the same. # lg.random.seed(42) # a = lg.random.randn(1, 3, 15) # b = lg.random.randn(15, 16) # c = a[0].dot(b) # np.random.seed(42) # an = np.random.randn(1, 3, 15) # bn = np.random.randn(15, 16) # cn = an[0].dot(bn) # assert lg.allclose(c, cn) return
def test(): lg.random.seed(10) origVals = lg.random.randn(2, 3, 4) sliceUpdate = lg.random.randn(3, 15) sliceView = origVals[0] origVals[0, :] += sliceUpdate[:, 11:] assert lg.array_equal(origVals[0, 0, :], sliceView[0, :]) sliceView[1, :] = lg.random.randn(4) assert lg.array_equal(origVals[0], sliceView) xnp = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) ynp = xnp[2:, 2:] x = lg.array(xnp) y = x[2:, 2:] assert np.array_equal(ynp, y) return
def test(): C = lg.random.randn(2, 3, 5) IFOGf = lg.random.randn(2, 3, 20) C[1] = 1.0 C[1] += IFOGf[1, :, :5] * IFOGf[1, :, 15:] temp = IFOGf[1, :, :5] * IFOGf[1, :, 15:] assert not lg.array_equal(C[1], temp) print(C[1]) print(temp) return
def test(shape): for ndim in range(2, 4): print(f"Testing {ndim}D") local_shape = shape[:ndim] x = lg.random.random(local_shape) a = x.__array__() for dim in range(1, ndim): y = lg.random.random(local_shape[-dim:]) b = y.__array__() print(f" {a.shape} x {b.shape}") assert lg.array_equal(x + y, a + b) for dim in range(ndim): rhs_shape = list(local_shape) rhs_shape[dim] = 1 if (np.array(rhs_shape) == 1).all(): y = np.random.random(tuple(rhs_shape)) b = lg.array(y) else: y = lg.random.random(tuple(rhs_shape)) b = y.__array__() print(f" {a.shape} x {b.shape}") assert lg.array_equal(x + y, a + b) if ndim > 2: for dim in range(ndim): rhs_shape = [1] * ndim rhs_shape[dim] = local_shape[dim] if (np.array(rhs_shape) == 1).all(): y = np.random.random(tuple(rhs_shape)) b = lg.array(y) else: y = lg.random.random(tuple(rhs_shape)) b = y.__array__() print(f" {a.shape} x {b.shape}") assert lg.array_equal(x + y, a + b)
def test(): x = lg.array([[1, 2], [3, 4], [5, 6]]) assert lg.array_equal(x[[0, 1, 2], [0, 1, 0]], [1, 4, 5]) x = lg.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]) rows = lg.array([0, 3]) columns = lg.array([0, 2]) assert lg.array_equal(x[rows[:, np.newaxis], columns], [[0, 2], [9, 11]]) zg = lg.array([[-1.2 + 0.5j, 1.2 - 2j], [-2.2 + 3.5j, 4.2 - 6.2j]]) m = lg.array([[True, False], [False, True]]) assert lg.array_equal(zg[m], [-1.2 + 0.5j, 4.2 - 6.2j]) anp = np.array([[[2, 1], [3, 2]], [[2, 4], [4, 1]]]) a = lg.array(anp) nznp = anp < 3 nzgp = a < 3 assert lg.array_equal(anp[nznp], a[nzgp]) y = lg.array([[[True, True], [False, True]], [[True, False], [False, True]]]) z = lg.nonzero(y) assert lg.array_equal(a[z], lg.array([2, 1, 2, 2, 1])) np.random.seed(42) anp = np.random.randn(10, 10, 4) a = lg.array(anp) bnp = np.array([3, 4, 6]) cnp = np.array([1, 4, 5]) b = lg.array(bnp) c = lg.array(cnp) assert lg.array_equal(a[b], anp[bnp]) assert lg.array_equal(a[(b, c)], anp[(b, c)]) onesnp = np.zeros(10, int) ones = lg.zeros(10, int) dnp = np.random.randn(20, 4) d = lg.array(dnp) assert lg.array_equal(dnp[np.where(onesnp)], d[lg.where(ones)])
def test(): np.random.seed(50) datanp = np.random.randn(2000000, 3) data = lg.array(datanp) pointsnp = np.random.choice(lg.arange(len(data)), 4, False) points = lg.array(pointsnp) centroids = data[points] centroidsnp = datanp[pointsnp] sqdists = lg.zeros((4, len(data))) sqdistsnp = np.zeros((4, len(datanp))) for i in range(4): vec = data - centroids[i] vecnp = datanp - centroidsnp[i] sqdists[i] = lg.linalg.norm(vec, axis=1) sqdistsnp[i] = np.linalg.norm(vecnp, axis=1) clusters = lg.argmin(sqdists, axis=0) clustersnp = np.argmin(sqdistsnp, axis=0) assert lg.array_equal(lg.where(clusters == 0), np.where(clustersnp == 0))
def test(): x = lg.array([1, 2, 3]) y = x < 2 assert lg.array_equal(y, [1, 0, 0]) y = x <= 2 assert lg.array_equal(y, [1, 1, 0]) y = x > 2 assert lg.array_equal(y, [0, 0, 1]) y = x >= 2 assert lg.array_equal(y, [0, 1, 1]) y = x == 2 assert lg.array_equal(y, [0, 1, 0]) y = (x + 2) * [6, 7, 8] assert lg.array_equal(y, [18, 28, 40]) return
import legate.numpy as np from legate import pandas as lp s1 = lp.Series([1, 2, 3]) data1 = s1.__legate_data_interface__ assert data1["version"] == 1 assert len(data1["data"]) == 1 for field, array in data1["data"].items(): assert field.name == "column0" assert str(field.type) == "int64" assert not field.nullable stores = array.stores() assert len(stores) == 2 assert stores[0] is None arr1 = np.array(s1) arr2 = np.array([1, 2, 3]) assert np.array_equal(arr1, arr2) s2 = lp.Series([1.0, np.nan, 3.0]) data2 = s2.__legate_data_interface__ assert len(data2["data"]) == 1 for field, array in data2["data"].items(): assert field.name == "column0" assert str(field.type) == "double" assert field.nullable stores = array.stores() assert len(stores) == 2 assert stores[0] is not None