1 year ago
#76566
Franz
Questions around sympy.UndefFunction and MatMul/DotProduct
I have the following code including 4 questions/problems:
from sympy import Function
from sympy import MatMul
from sympy import Symbol
from sympy import MatrixSymbol
from sympy import DotProduct
from sympy import Add
def main():
n = Symbol('n', integer=True, positiv=True)
m = Symbol('m', integer=True, positiv=True)
t = Symbol('t', real=True)
x = MatrixSymbol('x', n, 1)
y = MatrixSymbol('y', m, 1)
eta = MatrixSymbol('eta', 1, m)
K = Function('K') # should be a function from R^n to R^m
# want to build the expression
# t + <eta, K(x) + y>
# Q1: Why is K not recognized as callable?
# Is this a problem with sympy or pylint?
# pylint: disable=not-callable
# Q2: Why is it that Add(...).doit() works but ... + ... not
works = Add(y, K(x)).doit()
print(f"works: {works}")
try:
_ = y + K(x)
except TypeError as e:
print(f"Addition fails for MatrixSymbol {y} and AppliedUndef "
f"{K(x)} due to: {e}")
# Q3: This seems resonable from the expected meaning of the classes but
# the documentation is unclear. Especially for DotProduct saying it is
# similar except for possibly not requiring transposition.
works = t + DotProduct(eta, y)
print(f"works: {works}")
try:
_ = t + MatMul(eta, y)
except TypeError as e:
print(f"Addition fails for real Symbol {t} and MatMul {MatMul(eta, y)}"
f" due to: {e}")
# Q4: Lastly the main problem is of course that neigher MatMul(eta, K(x))
# nor DotProduct(eta, K(x)) work.
for prod in [MatMul, DotProduct]:
try:
prod(eta, K(x))
except (TypeError, NotImplementedError) as e:
print(f"Could not multiply MatrixSymbol {eta} with AppliedUndef "
f"{K(x)} with {prod.__name__} due to: {e}")
if __name__ == "__main__":
main()
The main point related to Q4 is that I do not know how to specify the signature of an UnknownFunction
.
I guess this is not possible at the moment?
Is there a workaround?
Would it make sense to include anything like this into sympy?
What about the other questions. Can anyone explain? Would you consider any of them worth raising an issue at Github?
Many thanks to anyone who can help here. Franz
python
sympy
0 Answers
Your Answer