{"action":"created","issue":{"url":"https://api.github.com/repos/sympy/sympy/issues/9725","labels_url":"https://api.github.com/repos/sympy/sympy/issues/9725/labels{/name}","comments_url":"https://api.github.com/repos/sympy/sympy/issues/9725/comments","events_url":"https://api.github.com/repos/sympy/sympy/issues/9725/events","html_url":"https://github.com/sympy/sympy/issues/9725","id":97123343,"number":9725,"title":"lambdify'd results of jacobian() and hessian() do not broadcast correctly","user":{"login":"richardotis","id":6405510,"avatar_url":"https://avatars.githubusercontent.com/u/6405510?v=3","gravatar_id":"","url":"https://api.github.com/users/richardotis","html_url":"https://github.com/richardotis","followers_url":"https://api.github.com/users/richardotis/followers","following_url":"https://api.github.com/users/richardotis/following{/other_user}","gists_url":"https://api.github.com/users/richardotis/gists{/gist_id}","starred_url":"https://api.github.com/users/richardotis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richardotis/subscriptions","organizations_url":"https://api.github.com/users/richardotis/orgs","repos_url":"https://api.github.com/users/richardotis/repos","events_url":"https://api.github.com/users/richardotis/events{/privacy}","received_events_url":"https://api.github.com/users/richardotis/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-07-24T19:18:44Z","updated_at":"2015-07-24T20:58:52Z","closed_at":null,"body":"This example only works in master because you need `NumPyPrinter`.\r\n\r\n```python\r\nimport sympy\r\nfrom sympy.printing.lambdarepr import NumPyPrinter\r\nimport numpy as np\r\n\r\nfunc = sympy.Symbol('y') * sympy.Symbol('x')**2\r\n\r\nxy = np.random.rand(10, 12, 2)\r\n\r\ngradient = sympy.Matrix([func]).jacobian([sympy.Symbol('x'), sympy.Symbol('y')])\r\ngrad_func = sympy.lambdify(tuple([sympy.Symbol('x'), sympy.Symbol('y')]), gradient, \r\n    printer=NumPyPrinter)\r\ngrad_result = grad_func(*xy.T)\r\nassert grad_result.dtype == 'float' # ok because no gradient element is zero\r\nhess = hessian(func, [sympy.Symbol('x'), sympy.Symbol('y')])\r\nhess_func = sympy.lambdify(tuple([sympy.Symbol('x'), sympy.Symbol('y')]), hess, \r\n    printer=NumPyPrinter)\r\nhess_result = hess_func(*xy.T)\r\nprint(hess_result)\r\nassert hess_result.dtype == 'float' # AssertionError because dtype is object\r\n```\r\n\r\nThis happens because the second derivative of `func` with respect to `y` twice is analytically zero, so the returned array includes an element which is just a scalar `0` rather than an `array` broadcast to the shape of the input.\r\n\r\nWorkarounds I've been using include manually fixing the elements and recasting to the correct dtype, as well as adding a numerically negligible factor to `func` which guarantees that no second derivative is ever zero.\r\n\r\n@asmeurer @jcrist "},"comment":{"url":"https://api.github.com/repos/sympy/sympy/issues/comments/124721466","html_url":"https://github.com/sympy/sympy/issues/9725#issuecomment-124721466","issue_url":"https://api.github.com/repos/sympy/sympy/issues/9725","id":124721466,"user":{"login":"moorepants","id":276007,"avatar_url":"https://avatars.githubusercontent.com/u/276007?v=3","gravatar_id":"","url":"https://api.github.com/users/moorepants","html_url":"https://github.com/moorepants","followers_url":"https://api.github.com/users/moorepants/followers","following_url":"https://api.github.com/users/moorepants/following{/other_user}","gists_url":"https://api.github.com/users/moorepants/gists{/gist_id}","starred_url":"https://api.github.com/users/moorepants/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moorepants/subscriptions","organizations_url":"https://api.github.com/users/moorepants/orgs","repos_url":"https://api.github.com/users/moorepants/repos","events_url":"https://api.github.com/users/moorepants/events{/privacy}","received_events_url":"https://api.github.com/users/moorepants/received_events","type":"User","site_admin":false},"created_at":"2015-07-24T20:58:52Z","updated_at":"2015-07-24T20:58:52Z","body":"Go it, just `from sympy import hessian`.\r\n"}}