Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrices: Allow inverse call on symbolic matrix #19217

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion sympy/core/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ def subs(self, *args, **kwargs):
from sympy.core.containers import Dict
from sympy.utilities.iterables import sift
from sympy import Dummy, Symbol
from sympy.matrices.expressions.matexpr import MatrixSymbol

unordered = False
if len(args) == 1:
Expand Down Expand Up @@ -959,7 +960,10 @@ def subs(self, *args, **kwargs):
# using d*m so Subs will be used on dummy variables
# in things like Derivative(f(x, y), x) in which x
# is both free and bound
rv = rv._subs(old, d*m, **kwargs)
if isinstance(old, MatrixSymbol):
rv = rv._subs(old, Symbol(str(d*m)))
else:
rv = rv._subs(old, d*m, **kwargs)
if not isinstance(rv, Basic):
break
reps[d] = new
Expand Down Expand Up @@ -2064,3 +2068,7 @@ def _make_find_query(query):
elif isinstance(query, Basic):
return lambda expr: expr.match(query) is not None
return query

class BadArgumentsError(TypeError):
'''Raised when a class instance is constructed with invalid arguments'''
pass
20 changes: 16 additions & 4 deletions sympy/core/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,22 +665,34 @@ def check_denominator_zeros(expression):
if expr.is_zero:
return True

def zero(wrt):
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
if wrt.is_Matrix:
return Matrix.zeros(wrt.rows, wrt.cols)
return S.Zero

def one(wrt):
from sympy.matrices.immutable import ImmutableDenseMatrix as Matrix
if wrt.is_Matrix:
return Matrix.ones(wrt.rows, wrt.cols)
return S.One

# try numerical evaluation to see if we get two different values
failing_number = None
if wrt == free:
# try 0 (for a) and 1 (for b)
try:
a = expr.subs(list(zip(free, [0]*len(free))),
simultaneous=True)
subs_dict = {x: zero(x) for x in free}
a = expr.subs(subs_dict, simultaneous=True)
if a is S.NaN:
# evaluation may succeed when substitution fails
a = expr._random(None, 0, 0, 0, 0)
except ZeroDivisionError:
a = None
if a is not None and a is not S.NaN:
try:
b = expr.subs(list(zip(free, [1]*len(free))),
simultaneous=True)
subs_dict = {x: one(x) for x in free}
b = expr.subs(subs_dict, simultaneous=True)
if b is S.NaN:
# evaluation may succeed when substitution fails
b = expr._random(None, 1, 0, 1, 0)
Expand Down
11 changes: 8 additions & 3 deletions sympy/matrices/expressions/matexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sympy.simplify import simplify
from sympy.utilities.misc import filldedent
from sympy.assumptions.ask import ask, Q

from sympy.core.basic import BadArgumentsError

def _sympifyit(arg, retval=None):
# This version of _sympifyit sympifies MutableMatrix objects
Expand Down Expand Up @@ -548,9 +548,11 @@ def recurse_expr(expr, index_ranges={}):
elif isinstance(expr, KroneckerDelta):
i1, i2 = expr.args
if dimensions is not None:
identity = Identity(dimensions[0])
size = dimensions[0]
else:
identity = S.One
range1 = index_ranges[i1]
size = range1[1] - range1[0] + 1
identity = Identity(size)
return [(MatrixElement(identity, i1, i2), (i1, i2))]
elif isinstance(expr, MatrixElement):
matrix_symbol, i1, i2 = expr.args
Expand Down Expand Up @@ -712,6 +714,9 @@ def __new__(cls, name, n, m):
if isinstance(name, str):
name = Symbol(name)
name = _sympify(name)
if not isinstance(name, (Symbol, MatrixExpr)):
msg = "MatrixElement name should be a Symbol/MatrixExpr not %s"
raise BadArgumentsError(msg % type(name))
obj = Expr.__new__(cls, name, n, m)
return obj

Expand Down