Differentiating a matrix holding functions

from sympy import * import numpy as np def diff_matrix(A): xs, ys = A.shape return np.array([f.diff(x) for f in A.ravel()]).reshape(xs, ys) x = Symbol('x') y1 = x**2 + 3*x + 2 y2 = x**3 + 7*x - 12 y3 = x**4 + 5*x**3 + 2*x**2 - 10 y4 = x**7 + 12*x**4 - 30*x**3 + 45 y5 = 312*x**2 - 624*x + 14 y6 = 43*x**4 - 31*x**2 + 54 A = np.array([ [y1, y2, y3], [y4, y5, y6] ]) print(diff_matrix(A)) # [[2*x + 3, 3*x**2 + 7, 4*x**3 + 15*x**2 + 4*x] # [7*x**6 + 48*x**3 - 90*x**2, 624*x - 624, 172*x**3 - 62*x]]