{"action":"created","comment":{"url":"https://api.github.com/repos/numpy/numpy/pulls/comments/159162593","pull_request_review_id":86068481,"id":159162593,"diff_hunk":"@@ -1738,6 +1753,156 @@ make_arr_prep_args(npy_intp nin, PyObject *args, PyObject *kwds)\n     }\n }\n \n+/*\n+ * Check whether any of the outputs of a gufunc has core dimensions.\n+ */\n+static int\n+_has_output_coredims(PyUFuncObject *ufunc) {\n+    int i;\n+    for (i = ufunc->nin; i < ufunc->nin + ufunc->nout; ++i) {\n+        if (ufunc->core_num_dims[i] > 0) {\n+            return 1;\n+        }\n+    }\n+    return 0;\n+}\n+\n+/*\n+ * Interpret a possible axes keyword argument, using it to fill the remap_axis\n+ * array which maps default to actual axes for each operand, indexed as\n+ * as remap_axis[iop][iaxis]. The default axis order has first all broadcast\n+ * axes and then the core axes the gufunc operates on.\n+ *\n+ * Returns 0 on success, and -1 on failure\n+ */\n+static int\n+_parse_axes_arg(PyUFuncObject *ufunc, PyObject *axes, PyArrayObject **op,\n+                int broadcast_ndim, int **remap_axis) {\n+    int nin = ufunc->nin;\n+    int nout = ufunc->nout;\n+    int nop = nin + nout;\n+    int iop, list_size;\n+    PyObject *op_axes_tuple;\n+\n+    if (!PyList_Check(axes)) {\n+        PyErr_SetString(PyExc_TypeError, \"axes should be a list.\");\n+        return -1;\n+    }\n+    list_size = PyList_Size(axes);\n+    if (list_size != nop) {\n+        if (list_size != nin || _has_output_coredims(ufunc)) {\n+            PyErr_Format(PyExc_ValueError,\n+                         \"axes should be a list with an entry for all \"\n+                         \"%d inputs and outputs; entries for outputs can only \"\n+                         \"be omitted if none of them has core axes.\",\n+                         nop);\n+            return -1;\n+        }\n+        for (iop = nin; iop < nop; iop++) {\n+            remap_axis[iop] = NULL;\n+        }\n+    }\n+    for (iop = 0; iop < list_size; ++iop) {\n+        int op_ndim, op_ncore, op_nbroadcast;\n+        int have_seen_axis[NPY_MAXDIMS] = {0};\n+        PyObject *axis_item;\n+        int axis, op_axis;\n+\n+        op_ncore = ufunc->core_num_dims[iop];\n+        if (op[iop] != NULL) {\n+            op_ndim = PyArray_NDIM(op[iop]);\n+            op_nbroadcast = op_ndim - op_ncore;","path":"numpy/core/src/umath/ufunc_object.c","position":107,"original_position":107,"commit_id":"8b7361b57b49b36a5eb5651301cb937d46575b51","original_commit_id":"8b7361b57b49b36a5eb5651301cb937d46575b51","user":{"login":"eric-wieser","id":425260,"avatar_url":"https://avatars3.githubusercontent.com/u/425260?v=4","gravatar_id":"","url":"https://api.github.com/users/eric-wieser","html_url":"https://github.com/eric-wieser","followers_url":"https://api.github.com/users/eric-wieser/followers","following_url":"https://api.github.com/users/eric-wieser/following{/other_user}","gists_url":"https://api.github.com/users/eric-wieser/gists{/gist_id}","starred_url":"https://api.github.com/users/eric-wieser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eric-wieser/subscriptions","organizations_url":"https://api.github.com/users/eric-wieser/orgs","repos_url":"https://api.github.com/users/eric-wieser/repos","events_url":"https://api.github.com/users/eric-wieser/events{/privacy}","received_events_url":"https://api.github.com/users/eric-wieser/received_events","type":"User","site_admin":false},"body":"Can this be negative if the out argument is invalid? Is that checked before this function is called?","created_at":"2018-01-01T20:05:59Z","updated_at":"2018-01-01T20:05:59Z","html_url":"https://github.com/numpy/numpy/pull/8819#discussion_r159162593","pull_request_url":"https://api.github.com/repos/numpy/numpy/pulls/8819","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/numpy/numpy/pulls/comments/159162593"},"html":{"href":"https://github.com/numpy/numpy/pull/8819#discussion_r159162593"},"pull_request":{"href":"https://api.github.com/repos/numpy/numpy/pulls/8819"}}},"pull_request":{"url":"https://api.github.com/repos/numpy/numpy/pulls/8819","id":112336840,"html_url":"https://github.com/numpy/numpy/pull/8819","diff_url":"https://github.com/numpy/numpy/pull/8819.diff","patch_url":"https://github.com/numpy/numpy/pull/8819.patch","issue_url":"https://api.github.com/repos/numpy/numpy/issues/8819","number":8819,"state":"open","locked":false,"title":"ENH: Implement axes keyword argument for gufuncs.","user":{"login":"mhvk","id":2789820,"avatar_url":"https://avatars0.githubusercontent.com/u/2789820?v=4","gravatar_id":"","url":"https://api.github.com/users/mhvk","html_url":"https://github.com/mhvk","followers_url":"https://api.github.com/users/mhvk/followers","following_url":"https://api.github.com/users/mhvk/following{/other_user}","gists_url":"https://api.github.com/users/mhvk/gists{/gist_id}","starred_url":"https://api.github.com/users/mhvk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhvk/subscriptions","organizations_url":"https://api.github.com/users/mhvk/orgs","repos_url":"https://api.github.com/users/mhvk/repos","events_url":"https://api.github.com/users/mhvk/events{/privacy}","received_events_url":"https://api.github.com/users/mhvk/received_events","type":"User","site_admin":false},"body":"EDIT (2017-11-07): went with the least controversial solution, which is easy to extend if we wish. To avoid confusion with the normal use of `axis`, I went with an `axes` keyword, which nominally should be a list with length equal to the total number of operands (inputs & outputs), with each entry a tuple with length equal to the number of core dimensions of that operand, containing axis indices. I still allow two short-cuts that did not seem controversial:\r\n- If none of the outputs have core dimensions, the list can contain axes for just the inputs (since there is nothing to set).\r\n- If an operand has only one core dimension, one can pass in an integer instead of a tuple. \r\n\r\nPrevious attempt to resummarize, with above conclusions added in:\r\n\r\nIn general, axis should be a list with length equal to either the number of inputs or the total number of operands (inputs & outputs).  Each entry should be a ~~list~~tuple with length equal to the number of core dimensions of that operand, containing axis indices. Some shortcuts may be handy.\r\n\r\nIt is probably easiest to see for a given signature.  Assuming this is `(ij),(j)->(i)`:\r\n1. The defaults would correspond to `axis=[(-2, -1), (-1,), (-1,)]`; *still true*\r\n2. One can omit the output(s), so `axis=[(-2, -1), (-1,)]` is the same; *not generally any more; only if output has no core dimensions*\r\n3. a single integer in the list is OK if the operand has only a single core dimension, so one could also write `[(-2, -1), -1, -1]`; *still true*\r\n4. If the signature has only one core dimension, then a simple integer can replace the outer list, so for a signature `(i),(i)->()` one could just pass `axis=-1` (this is not meant to cover `(i),(j)->()`). *not implemented; maybe eventually have `axis` argument*\r\n\r\nCurrently under discussion:\r\n* Name this `axes` so we can use `axis` as shortcut in wrappers that prepare `axes`. *DONE*\r\n* Somewhat related, remove the single-integer shortcut for the list (4) for now. *DONE*\r\n* Possibly also remove the single-integer shortcut for a single operand (3)? *no, kept*\r\n* Multi-dispatch ufuncs, determined by `args`, `out`, and/or `axis` *should be safe now*\r\n important mostly as the implementation adopted here should not create ambiguity resolving dispatches in future\r\n  * useful for `matmul` and other ufuncs that right now have a special case for 1d vectors. For example, multiplication:\r\n      * `axis=[(-2,-1), (-2,-1)]` &rarr; matrix-matrix\r\n      * `axis=[(-1,), (-2, -1)]`  &rarr; vector-matrix\r\n      * `axis=[(-2, -1), (-1,)]` &rarr; matrix-vector \r\n  * could potentially implement optional arguments\r\n\r\nIdeas discarded:\r\n* `None` as a replacement for the default for a given operand; reasons:\r\n   * keep `None` for possible other use\r\n   * `None` elsewhere means \"all axes\" - a different meaning would be confusing\r\n   * this would preclude possibly having multiple signatures, among which `axes` could choose.\r\n* list of lists instead of list of tuples (to keep tuples for using multiple axes to represent one); reason: too ugly.\r\n\r\nNOTE: needs documentation before merging.","created_at":"2017-03-23T20:58:24Z","updated_at":"2018-01-01T20:05:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"39295a837fcf8c6e4a5ee5051a7b686743f6fc82","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/numpy/numpy/pulls/8819/commits","review_comments_url":"https://api.github.com/repos/numpy/numpy/pulls/8819/comments","review_comment_url":"https://api.github.com/repos/numpy/numpy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/8819/comments","statuses_url":"https://api.github.com/repos/numpy/numpy/statuses/8b7361b57b49b36a5eb5651301cb937d46575b51","head":{"label":"mhvk:gufunc-axis-argument","ref":"gufunc-axis-argument","sha":"8b7361b57b49b36a5eb5651301cb937d46575b51","user":{"login":"mhvk","id":2789820,"avatar_url":"https://avatars0.githubusercontent.com/u/2789820?v=4","gravatar_id":"","url":"https://api.github.com/users/mhvk","html_url":"https://github.com/mhvk","followers_url":"https://api.github.com/users/mhvk/followers","following_url":"https://api.github.com/users/mhvk/following{/other_user}","gists_url":"https://api.github.com/users/mhvk/gists{/gist_id}","starred_url":"https://api.github.com/users/mhvk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhvk/subscriptions","organizations_url":"https://api.github.com/users/mhvk/orgs","repos_url":"https://api.github.com/users/mhvk/repos","events_url":"https://api.github.com/users/mhvk/events{/privacy}","received_events_url":"https://api.github.com/users/mhvk/received_events","type":"User","site_admin":false},"repo":{"id":11478718,"name":"numpy","full_name":"mhvk/numpy","owner":{"login":"mhvk","id":2789820,"avatar_url":"https://avatars0.githubusercontent.com/u/2789820?v=4","gravatar_id":"","url":"https://api.github.com/users/mhvk","html_url":"https://github.com/mhvk","followers_url":"https://api.github.com/users/mhvk/followers","following_url":"https://api.github.com/users/mhvk/following{/other_user}","gists_url":"https://api.github.com/users/mhvk/gists{/gist_id}","starred_url":"https://api.github.com/users/mhvk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhvk/subscriptions","organizations_url":"https://api.github.com/users/mhvk/orgs","repos_url":"https://api.github.com/users/mhvk/repos","events_url":"https://api.github.com/users/mhvk/events{/privacy}","received_events_url":"https://api.github.com/users/mhvk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mhvk/numpy","description":"Numpy main repository","fork":true,"url":"https://api.github.com/repos/mhvk/numpy","forks_url":"https://api.github.com/repos/mhvk/numpy/forks","keys_url":"https://api.github.com/repos/mhvk/numpy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mhvk/numpy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mhvk/numpy/teams","hooks_url":"https://api.github.com/repos/mhvk/numpy/hooks","issue_events_url":"https://api.github.com/repos/mhvk/numpy/issues/events{/number}","events_url":"https://api.github.com/repos/mhvk/numpy/events","assignees_url":"https://api.github.com/repos/mhvk/numpy/assignees{/user}","branches_url":"https://api.github.com/repos/mhvk/numpy/branches{/branch}","tags_url":"https://api.github.com/repos/mhvk/numpy/tags","blobs_url":"https://api.github.com/repos/mhvk/numpy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mhvk/numpy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mhvk/numpy/git/refs{/sha}","trees_url":"https://api.github.com/repos/mhvk/numpy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mhvk/numpy/statuses/{sha}","languages_url":"https://api.github.com/repos/mhvk/numpy/languages","stargazers_url":"https://api.github.com/repos/mhvk/numpy/stargazers","contributors_url":"https://api.github.com/repos/mhvk/numpy/contributors","subscribers_url":"https://api.github.com/repos/mhvk/numpy/subscribers","subscription_url":"https://api.github.com/repos/mhvk/numpy/subscription","commits_url":"https://api.github.com/repos/mhvk/numpy/commits{/sha}","git_commits_url":"https://api.github.com/repos/mhvk/numpy/git/commits{/sha}","comments_url":"https://api.github.com/repos/mhvk/numpy/comments{/number}","issue_comment_url":"https://api.github.com/repos/mhvk/numpy/issues/comments{/number}","contents_url":"https://api.github.com/repos/mhvk/numpy/contents/{+path}","compare_url":"https://api.github.com/repos/mhvk/numpy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mhvk/numpy/merges","archive_url":"https://api.github.com/repos/mhvk/numpy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mhvk/numpy/downloads","issues_url":"https://api.github.com/repos/mhvk/numpy/issues{/number}","pulls_url":"https://api.github.com/repos/mhvk/numpy/pulls{/number}","milestones_url":"https://api.github.com/repos/mhvk/numpy/milestones{/number}","notifications_url":"https://api.github.com/repos/mhvk/numpy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mhvk/numpy/labels{/name}","releases_url":"https://api.github.com/repos/mhvk/numpy/releases{/id}","deployments_url":"https://api.github.com/repos/mhvk/numpy/deployments","created_at":"2013-07-17T14:53:04Z","updated_at":"2015-03-10T18:34:41Z","pushed_at":"2018-01-01T20:02:34Z","git_url":"git://github.com/mhvk/numpy.git","ssh_url":"git@github.com:mhvk/numpy.git","clone_url":"https://github.com/mhvk/numpy.git","svn_url":"https://github.com/mhvk/numpy","homepage":"http://www.numpy.org/","size":50498,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":0,"open_issues":0,"watchers":0,"default_branch":"placeholder"}},"base":{"label":"numpy:master","ref":"master","sha":"7255987a11c8b90ff525ad178419b22508164e23","user":{"login":"numpy","id":288276,"avatar_url":"https://avatars2.githubusercontent.com/u/288276?v=4","gravatar_id":"","url":"https://api.github.com/users/numpy","html_url":"https://github.com/numpy","followers_url":"https://api.github.com/users/numpy/followers","following_url":"https://api.github.com/users/numpy/following{/other_user}","gists_url":"https://api.github.com/users/numpy/gists{/gist_id}","starred_url":"https://api.github.com/users/numpy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/numpy/subscriptions","organizations_url":"https://api.github.com/users/numpy/orgs","repos_url":"https://api.github.com/users/numpy/repos","events_url":"https://api.github.com/users/numpy/events{/privacy}","received_events_url":"https://api.github.com/users/numpy/received_events","type":"Organization","site_admin":false},"repo":{"id":908607,"name":"numpy","full_name":"numpy/numpy","owner":{"login":"numpy","id":288276,"avatar_url":"https://avatars2.githubusercontent.com/u/288276?v=4","gravatar_id":"","url":"https://api.github.com/users/numpy","html_url":"https://github.com/numpy","followers_url":"https://api.github.com/users/numpy/followers","following_url":"https://api.github.com/users/numpy/following{/other_user}","gists_url":"https://api.github.com/users/numpy/gists{/gist_id}","starred_url":"https://api.github.com/users/numpy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/numpy/subscriptions","organizations_url":"https://api.github.com/users/numpy/orgs","repos_url":"https://api.github.com/users/numpy/repos","events_url":"https://api.github.com/users/numpy/events{/privacy}","received_events_url":"https://api.github.com/users/numpy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/numpy/numpy","description":"Numpy main repository","fork":false,"url":"https://api.github.com/repos/numpy/numpy","forks_url":"https://api.github.com/repos/numpy/numpy/forks","keys_url":"https://api.github.com/repos/numpy/numpy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/numpy/numpy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/numpy/numpy/teams","hooks_url":"https://api.github.com/repos/numpy/numpy/hooks","issue_events_url":"https://api.github.com/repos/numpy/numpy/issues/events{/number}","events_url":"https://api.github.com/repos/numpy/numpy/events","assignees_url":"https://api.github.com/repos/numpy/numpy/assignees{/user}","branches_url":"https://api.github.com/repos/numpy/numpy/branches{/branch}","tags_url":"https://api.github.com/repos/numpy/numpy/tags","blobs_url":"https://api.github.com/repos/numpy/numpy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/numpy/numpy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/numpy/numpy/git/refs{/sha}","trees_url":"https://api.github.com/repos/numpy/numpy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/numpy/numpy/statuses/{sha}","languages_url":"https://api.github.com/repos/numpy/numpy/languages","stargazers_url":"https://api.github.com/repos/numpy/numpy/stargazers","contributors_url":"https://api.github.com/repos/numpy/numpy/contributors","subscribers_url":"https://api.github.com/repos/numpy/numpy/subscribers","subscription_url":"https://api.github.com/repos/numpy/numpy/subscription","commits_url":"https://api.github.com/repos/numpy/numpy/commits{/sha}","git_commits_url":"https://api.github.com/repos/numpy/numpy/git/commits{/sha}","comments_url":"https://api.github.com/repos/numpy/numpy/comments{/number}","issue_comment_url":"https://api.github.com/repos/numpy/numpy/issues/comments{/number}","contents_url":"https://api.github.com/repos/numpy/numpy/contents/{+path}","compare_url":"https://api.github.com/repos/numpy/numpy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/numpy/numpy/merges","archive_url":"https://api.github.com/repos/numpy/numpy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/numpy/numpy/downloads","issues_url":"https://api.github.com/repos/numpy/numpy/issues{/number}","pulls_url":"https://api.github.com/repos/numpy/numpy/pulls{/number}","milestones_url":"https://api.github.com/repos/numpy/numpy/milestones{/number}","notifications_url":"https://api.github.com/repos/numpy/numpy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/numpy/numpy/labels{/name}","releases_url":"https://api.github.com/repos/numpy/numpy/releases{/id}","deployments_url":"https://api.github.com/repos/numpy/numpy/deployments","created_at":"2010-09-13T23:02:39Z","updated_at":"2018-01-01T12:58:34Z","pushed_at":"2018-01-01T20:02:37Z","git_url":"git://github.com/numpy/numpy.git","ssh_url":"git@github.com:numpy/numpy.git","clone_url":"https://github.com/numpy/numpy.git","svn_url":"https://github.com/numpy/numpy","homepage":"http://www.numpy.org/","size":54738,"stargazers_count":6064,"watchers_count":6064,"language":"C","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2481,"mirror_url":null,"archived":false,"open_issues_count":1737,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":2481,"open_issues":1737,"watchers":6064,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/numpy/numpy/pulls/8819"},"html":{"href":"https://github.com/numpy/numpy/pull/8819"},"issue":{"href":"https://api.github.com/repos/numpy/numpy/issues/8819"},"comments":{"href":"https://api.github.com/repos/numpy/numpy/issues/8819/comments"},"review_comments":{"href":"https://api.github.com/repos/numpy/numpy/pulls/8819/comments"},"review_comment":{"href":"https://api.github.com/repos/numpy/numpy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/numpy/numpy/pulls/8819/commits"},"statuses":{"href":"https://api.github.com/repos/numpy/numpy/statuses/8b7361b57b49b36a5eb5651301cb937d46575b51"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"eric-wieser"}}