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

Lambdify misinterprets some matrix expressions #17006

Open
anpandey opened this issue Jun 9, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@anpandey
Copy link
Contributor

commented Jun 9, 2019

Using lambdify on an expression containing an identity matrix gives us an unexpected result:

>>> import numpy as np
>>> n = symbols('n', integer=True)
>>> A = MatrixSymbol("A", n, n)
>>> a = np.array([[1, 2], [3, 4]])
>>> f = lambdify(A, A + Identity(n))
>>> f(a)
array([[1.+1.j, 2.+1.j],
       [3.+1.j, 4.+1.j]])

Instead, the output should be array([[2, 2], [3, 5]]), since we're adding an identity matrix to the array. Inspecting the globals and source code of f shows us why we get the result:

>>> import inspect
>>> print(inspect.getsource(f))
def _lambdifygenerated(A):
    return (I + A)
>>> f.__globals__['I']
1j

The code printer prints I, which is currently being interpreted as a Python built-in complex number. The printer should support printing identity matrices, and signal an error for unsupported expressions that might be misinterpreted.

@asmeurer

This comment has been minimized.

Copy link
Member

commented Jun 11, 2019

If the shape is an explicit number, we can just print eye(n). For unknown shape, it's harder. We can raise an exception for now. It's better to raise an exception than give a wrong answer.

@anpandey anpandey referenced a pull request that will close this issue Jun 12, 2019

Open

NumPy print support for identity matrices #17022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.