def test_BlockMatrix_Inverse_execution(): k, n = 2, 4 dtype = 'float32' A = diofant.MatrixSymbol('A', n, k) B = diofant.MatrixSymbol('B', n, n) inputs = A, B output = B.I * A cutsizes = { A: [(n // 2, n // 2), (k // 2, k // 2)], B: [(n // 2, n // 2), (n // 2, n // 2)] } cutinputs = [diofant.blockcut(i, *cutsizes[i]) for i in inputs] cutoutput = output.subs(dict(zip(inputs, cutinputs))) dtypes = dict(zip(inputs, [dtype] * len(inputs))) f = theano_function(inputs, [output], dtypes=dtypes, cache={}) fblocked = theano_function(inputs, [diofant.block_collapse(cutoutput)], dtypes=dtypes, cache={}) ninputs = [np.random.rand(*x.shape).astype(dtype) for x in inputs] ninputs = [ np.arange(n * k).reshape(A.shape).astype(dtype), np.eye(n).astype(dtype) ] ninputs[1] += np.ones(B.shape) * 1e-5 assert np.allclose(f(*ninputs), fblocked(*ninputs), rtol=1e-5)
def test_BlockMatrix(): n = diofant.Symbol('n', integer=True) A = diofant.MatrixSymbol('A', n, n) B = diofant.MatrixSymbol('B', n, n) C = diofant.MatrixSymbol('C', n, n) D = diofant.MatrixSymbol('D', n, n) At, Bt, Ct, Dt = map(theano_code, (A, B, C, D)) Block = diofant.BlockMatrix([[A, B], [C, D]]) Blockt = theano_code(Block) solutions = [ tt.join(0, tt.join(1, At, Bt), tt.join(1, Ct, Dt)), tt.join(1, tt.join(0, At, Ct), tt.join(0, Bt, Dt)) ] assert any(theq(Blockt, solution) for solution in solutions)
def test_MatrixSlice_2(): n = diofant.Symbol('n', integer=True) X = diofant.MatrixSymbol('X', n, n) Y = X[1:2:3, 4:5:6] Yt = theano_code(Y) assert tuple(Yt.owner.op.idx_list) == (slice(1, 2, 3), slice(4, 5, 6))
def test_MatrixSlice(): n = diofant.Symbol('n', integer=True) X = diofant.MatrixSymbol('X', n, n) Y = X[1:2:3, 4:5:6] Yt = theano_code(Y) # assert tuple(Yt.owner.op.idx_list) == (slice(1,2,3), slice(4,5,6)) assert Yt.owner.inputs[0] == theano_code(X) k = diofant.Symbol('k') kt = theano_code(k, dtypes={k: 'int32'}) start, stop, step = 4, k, 2 Y = X[start:stop:step] Yt = theano_code(Y, dtypes={n: 'int32', k: 'int32'})
def test_MatAdd(): X = diofant.MatrixSymbol('X', 4, 4) Y = diofant.MatrixSymbol('X', 4, 4) Z = diofant.MatrixSymbol('X', 4, 4) expr = X + Y + Z assert isinstance(theano_code(expr).owner.op, tt.Elemwise)
def test_Transpose(): X = diofant.MatrixSymbol('X', 4, 4) assert isinstance(theano_code(X.T).owner.op, tt.DimShuffle)
def test_MatMul(): X = diofant.MatrixSymbol('X', 4, 4) Y = diofant.MatrixSymbol('X', 4, 4) Z = diofant.MatrixSymbol('X', 4, 4) expr = X * Y * Z assert isinstance(theano_code(expr).owner.op, tt.Dot)
def test_MatrixSymbol(): X = diofant.MatrixSymbol('X', 4, 5) Xt = theano_code(X) assert isinstance(Xt, tt.TensorVariable) assert Xt.broadcastable == (False, False)