def apply(n): i = Symbol.i(integer=True) j = Symbol.j(integer=True) return Equality( Concatenate( Concatenate(Swap(n, i, j), ZeroMatrix(n)).T, Concatenate(ZeroMatrix(n), 1)), Swap(n + 1, i, j))
def apply(n, m, b): i = Symbol.i(integer=True) return Equality( Concatenate( Concatenate(MatProduct[i:m](Swap(n, i, b[i])), ZeroMatrix(n)).T, Concatenate(ZeroMatrix(n), 1)).T, MatProduct[i:m](Swap(n + 1, i, b[i])))
def apply(W): n = W.shape[0] k = Symbol.k(integer=True) return Equality( Concatenate( Concatenate(W.T, ZeroMatrix(n)).T, LAMBDA[k:n + 1](KroneckerDelta(k, n))), Concatenate( Concatenate(W, ZeroMatrix(n)).T, LAMBDA[k:n + 1](KroneckerDelta(k, n))).T)
def any_zeros(mul): if any([ arg.is_zero or (arg.is_Matrix and arg.is_ZeroMatrix) for arg in mul.args ]): matrices = [arg for arg in mul.args if arg.is_Matrix] if len(matrices[0].shape) == 1: if len(matrices[-1].shape) == 1: from sympy import S return S.Zero return ZeroMatrix(matrices[-1].cols) if len(matrices[-1].shape) == 1: return ZeroMatrix(matrices[0].rows) return ZeroMatrix(matrices[0].rows, matrices[-1].cols) return mul
def test_MatrixPermute_doit(): p = Permutation(0, 1, 2) A = MatrixSymbol('A', 3, 3) assert MatrixPermute(A, p).doit() == MatrixPermute(A, p) p = Permutation(0, size=3) A = MatrixSymbol('A', 3, 3) assert MatrixPermute(A, p).doit().as_explicit() == \ MatrixPermute(A, p).as_explicit() p = Permutation(0, 1, 2) A = Identity(3) assert MatrixPermute(A, p, 0).doit().as_explicit() == \ MatrixPermute(A, p, 0).as_explicit() assert MatrixPermute(A, p, 1).doit().as_explicit() == \ MatrixPermute(A, p, 1).as_explicit() A = ZeroMatrix(3, 3) assert MatrixPermute(A, p).doit() == A A = OneMatrix(3, 3) assert MatrixPermute(A, p).doit() == A A = MatrixSymbol('A', 4, 4) p1 = Permutation(0, 1, 2, 3) p2 = Permutation(0, 2, 3, 1) expr = MatrixPermute(MatrixPermute(A, p1, 0), p2, 0) assert expr.as_explicit() == expr.doit().as_explicit() expr = MatrixPermute(MatrixPermute(A, p1, 1), p2, 1) assert expr.as_explicit() == expr.doit().as_explicit()
def blocks(self): from sympy.matrices.immutable import ImmutableDenseMatrix mats = self.args data = [[mats[i] if i == j else ZeroMatrix(mats[i].rows, mats[j].cols) for j in range(len(mats))] for i in range(len(mats))] return ImmutableDenseMatrix(data, evaluate=False)
def gru_recursive(x, *limits): (Wx, ), (Wh, ), (b, ), (t, ) = limits h = gru[Wx, Wh, b, t - 1](x) d = h.shape[-1] xt = x[t] Wxz = Wx[:, :d] Wxr = Wx[:, d:2 * d] Wxh = Wx[:, -d:] Whz = Wh[:, :d] Whr = Wh[:, d:2 * d] Whh = Wh[:, -d:] bz = b[:d] br = b[d:2 * d] bh = b[-d:] z = sigmoid(xt @ Wxz + h @ Whz + bz) r = sigmoid(xt @ Wxr + h @ Whr + br) gh = tanh(xt @ Wxh + (r * h) @ Whh + bh) return Piecewise(((1 - z) * gh + z * h, t > 0), (ZeroMatrix(*h.shape), True))
def test_MatrixSet(): n, m = symbols('n m', integer=True) A = MatrixSymbol('A', n, m) C = MatrixSymbol('C', n, n) M = MatrixSet(2, 2, set=S.Reals) assert M.shape == (2, 2) assert M.set == S.Reals X = Matrix([[1, 2], [3, 4]]) assert X in M X = ZeroMatrix(2, 2) assert X in M raises(TypeError, lambda: A in M) raises(TypeError, lambda: 1 in M) M = MatrixSet(n, m, set=S.Reals) assert A in M raises(TypeError, lambda: C in M) raises(TypeError, lambda: X in M) M = MatrixSet(2, 2, set={1, 2, 3}) X = Matrix([[1, 2], [3, 4]]) Y = Matrix([[1, 2]]) assert (X in M) == S.false assert (Y in M) == S.false raises(ValueError, lambda: MatrixSet(2, -2, S.Reals)) raises(ValueError, lambda: MatrixSet(2.4, -1, S.Reals)) raises(TypeError, lambda: MatrixSet(2, 2, (1, 2, 3)))
def lstm_recursive(x, *limits): (W, ), (Wh, ), (b, ), (t, ) = limits hc = lstm[W, Wh, b, t - 1](x) xt = x[t] h = Indexed(hc, 0) c = Indexed(hc, 1) d = h.shape[-1] Wi = W[:, :d] Wf = W[:, d:2 * d] Wc = W[:, 2 * d:3 * d] Wo = W[:, -d:] Whi = Wh[:, :d] Whf = Wh[:, d:2 * d] Whc = Wh[:, 2 * d:3 * d] Who = Wh[:, -d:] bi = b[:d] bf = b[d:2 * d] bc = b[2 * d:3 * d] bo = b[-d:] i = sigmoid(xt @ Wi + h @ Whi + bi) f = sigmoid(xt @ Wf + h @ Whf + bf) c = f * c + i * tanh(xt @ Wc + h @ Whc + bc) o = sigmoid(xt @ Wo + h @ Who + bo) return Piecewise((BlockMatrix(o * tanh(c), c), t > 0), (ZeroMatrix(*hc.shape), True))
def any_zeros(mul): if any([ arg.is_zero or (arg.is_Matrix and arg.is_ZeroMatrix) for arg in mul.args ]): matrices = [arg for arg in mul.args if arg.is_Matrix] return ZeroMatrix(matrices[0].rows, matrices[-1].cols) return mul
def __getitem__(self, key): from sympy.functions.elementary.piecewise import Piecewise if isinstance(key, slice): start, stop = key.start, key.stop if start is None: start = 0 if stop is None: stop = self.shape[0] if start == 0 and stop == self.shape[0]: return self rows = 0 args = [] len_self_shape = len(self.shape) for arg in self.args: if start >= stop: break index = rows if len(arg.shape) < len_self_shape: rows += 1 else: rows += arg.shape[0] if start < rows: if len(arg.shape) < len_self_shape: args.append(arg) start += 1 elif rows <= stop: if rows - start < arg.shape[0]: args.append(arg[start:]) else: args.append(arg) start = rows else: args.append(arg[start - index:stop - index]) start = stop if len(args) == 1: return args[0] if len(args) == 0: return ZeroMatrix(*self.shape) return self.func(*args) if isinstance(key, tuple): if len(key) == 1: key = key[0] elif len(key) == 2: i, j = key if isinstance(i, slice): if isinstance(j, slice): raise Exception('unimplemented method') else: assert i.step is None, 'unimplemented slice object %s' % i start, stop = i.start, i.stop if start is None: if stop is None: # v have the same columns args = [] for v in self.args: if len(v.shape) > 1: indexed = v[:, j] else: indexed = v[j] args.append(indexed) return self.func(*args) raise Exception('unimplemented slice object %s' % i) elif isinstance(j, slice): raise Exception('unimplemented method') from sympy.core.sympify import _sympify i, j = _sympify(i), _sympify(j) if self.valid_index(i, j) != False: args = [] length = 0 for arg in self.args: _length = length shape = arg.shape length += shape[0] cond = i < length if len(arg.shape) == 1: args.append([arg[j], cond]) else: if cond.is_BooleanFalse: continue args.append([arg[i - _length, j], cond]) args[-1][-1] = True return Piecewise(*args) else: raise IndexError("Invalid indices (%s, %s)" % (i, j)) if isinstance(key, int) or key.is_Integer or key.is_Symbol or key.is_Expr: if self.axis == 0: from sympy import S rows = S.Zero args = [] for arg in self.args: index = rows if len(arg.shape) < len(self.shape): rows += S.One else: rows += arg.shape[0] cond = key < rows if cond.is_BooleanFalse: continue if len(arg.shape) < len(self.shape): args.append([arg, cond]) else: args.append([arg[key - index], cond]) args[-1][-1] = True return Piecewise(*args) else: return self.func(*(a[key] for a in self.args), axis=self.axis - 1) raise IndexError("Invalid index, wanted %s[i,j]" % self)
def absorb(x): if any(isinstance(c, ZeroMatrix) for c in x.args): return ZeroMatrix(*x.shape) else: return x
def test_matrix_add_with_scalar(): raises(TypeError, lambda: Add(0, ZeroMatrix(2, 2)))
def test_zero_matrix_add(): assert Add(ZeroMatrix(2, 2), ZeroMatrix(2, 2)) == ZeroMatrix(2, 2)
def apply(n): k = Symbol.k(integer=True) return Equality(LAMBDA[k:n + 1](KroneckerDelta(k, n)), Concatenate(ZeroMatrix(n), 1))