2

I have this numpy array

a=np.array([[2*y, 0],[2*x + 1, 2*y + 4]])

I want to replace (x,y) by a value, for example (1,1).

How can I have this array with this new values in order to have something like this:

a=[[2,0],[3,6]]
3

1 Answer 1

1

You can use SymPy (a symbolic math package) and it's Matrix (array):

>>> from sympy import Matrix
>>> from sympy.abc import x, y
>>> m = Matrix([[2*y, 0],[2*x + 1, 2*y + 4]])
>>> m
Matrix([
[    2*y,       0],
[2*x + 1, 2*y + 4]])

>>> n = m.subs({x: 1, y: 1})   # replace variables with values
>>> n
Matrix([
[2, 0],
[3, 6]])

>>> np.array(n).astype(int)    # convert to numpy array
array([[2, 0],
       [3, 6]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.