{"action":"created","comment":{"url":"https://api.github.com/repos/numba/numba/pulls/comments/71382945","id":71382945,"diff_hunk":"@@ -1574,3 +1574,112 @@ def pinv_impl(a, rcond=1.e-15):\n         return acpy.T.ravel().reshape(a.shape).T\n \n     return pinv_impl\n+\n+# Walks the diag of a LUP decomposed matrix\n+# uses that det(A) = prod(diag(lup(A)))\n+# and also that log(a)+log(b) = log(a*b)\n+# The return sign is adjusted based on the values found\n+# such that the log(value) stays in the real domain.\n+\n+\n+def _get_slogdet_diag_walker(a):\n+    if isinstance(a.dtype, types.scalars.Complex):\n+        @jit(nopython=True)\n+        def cmplx_diag_walker(n, a, sgn):\n+            # walk diagonal\n+            csgn = sgn + 0.j\n+            acc = 0.\n+            for k in range(n):\n+                absel = np.abs(a[k, k])\n+                csgn = csgn * (a[k, k] / absel)\n+                acc = acc + np.log(absel)\n+            return (csgn, acc)\n+        return cmplx_diag_walker\n+    else:\n+        @jit(nopython=True)\n+        def real_diag_walker(n, a, sgn):\n+            # walk diagonal\n+            acc = 0.\n+            for k in range(n):\n+                v = a[k, k]\n+                if v < 0.:\n+                    sgn = -sgn\n+                    v = -v\n+                acc = acc + np.log(v)\n+            # sgn is a float dtype\n+            return (sgn + 0., acc)\n+        return real_diag_walker\n+\n+\n+@overload(np.linalg.slogdet)\n+def slogdet_impl(a):\n+    ensure_lapack()\n+\n+    _check_linalg_matrix(a, \"slogdet\")\n+\n+    numba_xxgetrf_sig = types.intc(types.char,   # kind\n+                                   types.intp,  # m\n+                                   types.intp,  # n\n+                                   types.CPointer(a.dtype),  # a\n+                                   types.intp,  # lda\n+                                   types.CPointer(F_INT_nbtype)  # ipiv\n+                                   )\n+\n+    numba_xxgetrf = types.ExternalFunction(\"numba_xxgetrf\",\n+                                           numba_xxgetrf_sig)\n+\n+    kind = ord(get_blas_kind(a.dtype, \"slogdet\"))\n+\n+    F_layout = a.layout == 'F'\n+\n+    diag_walker = _get_slogdet_diag_walker(a)\n+\n+    def slogdet_impl(a):\n+        n = a.shape[-1]\n+        if a.shape[-2] != n:\n+            msg = \"Last 2 dimensions of the array must be square.\"\n+            raise np.linalg.LinAlgError(msg)\n+\n+        _check_finite_matrix(a)\n+\n+        if F_layout:\n+            acpy = np.copy(a)\n+        else:\n+            acpy = np.asfortranarray(a)\n+\n+        ipiv = np.empty(n, dtype=F_INT_nptype)\n+\n+        r = numba_xxgetrf(kind, n, n, acpy.ctypes, n, ipiv.ctypes)\n+\n+        if r > 0:\n+            # factorisation failed, return same defaults as np\n+            return (0., -np.inf)\n+        _inv_err_handler(r)  # catch input-to-lapack problem\n+\n+        sgn = 1\n+        for k in range(n):\n+            sgn = sgn + (ipiv[k] != (k + 1))","path":"numba/targets/linalg.py","position":88,"original_position":88,"commit_id":"72c5cb801894fbabd2b121becab59e1735abcfef","original_commit_id":"72c5cb801894fbabd2b121becab59e1735abcfef","user":{"login":"stuartarchibald","id":907717,"avatar_url":"https://avatars.githubusercontent.com/u/907717?v=3","gravatar_id":"","url":"https://api.github.com/users/stuartarchibald","html_url":"https://github.com/stuartarchibald","followers_url":"https://api.github.com/users/stuartarchibald/followers","following_url":"https://api.github.com/users/stuartarchibald/following{/other_user}","gists_url":"https://api.github.com/users/stuartarchibald/gists{/gist_id}","starred_url":"https://api.github.com/users/stuartarchibald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stuartarchibald/subscriptions","organizations_url":"https://api.github.com/users/stuartarchibald/orgs","repos_url":"https://api.github.com/users/stuartarchibald/repos","events_url":"https://api.github.com/users/stuartarchibald/events{/privacy}","received_events_url":"https://api.github.com/users/stuartarchibald/received_events","type":"User","site_admin":false},"body":"Nothing I can find directly. Suppose there were no row interchanges taking place and the target was to return the determinant itself, the determinant could then just be calculated directly as the product of the diagonal values of the decomposition. However, as this needs to return the sign and log of the determinant we use that `log(a*b)=log(a)+log(b)` so the determinant is the sum of the `log` of the values on the diagonal but obviously the sign needs to be accounted for. Recalling multiplication order matters to the sign, assuming again there's no pivoting, the `_get_slogdet_diag_walker` executes this idea (make values positive if they aren't already and adjust the accumulated sign, then accumulate the log value). Finally, if we introduce partial pivoting with row interchanges as in the LAPACK routine above, the above line and the modulo operator is deciding if the number of interchanges performed is odd or even, this taking into account the affect of the permutation on the multiplication order over the diagonal, this sign is then carried forward into the `_get_slogdet_diag_walker` routine and we can pretend the permutations didn't happen knowing the adjustment has been made.","created_at":"2016-07-19T17:20:52Z","updated_at":"2016-07-19T17:20:52Z","html_url":"https://github.com/numba/numba/pull/1987#discussion_r71382945","pull_request_url":"https://api.github.com/repos/numba/numba/pulls/1987","_links":{"self":{"href":"https://api.github.com/repos/numba/numba/pulls/comments/71382945"},"html":{"href":"https://github.com/numba/numba/pull/1987#discussion_r71382945"},"pull_request":{"href":"https://api.github.com/repos/numba/numba/pulls/1987"}}},"pull_request":{"url":"https://api.github.com/repos/numba/numba/pulls/1987","id":77644008,"html_url":"https://github.com/numba/numba/pull/1987","diff_url":"https://github.com/numba/numba/pull/1987.diff","patch_url":"https://github.com/numba/numba/pull/1987.patch","issue_url":"https://api.github.com/repos/numba/numba/issues/1987","number":1987,"state":"open","locked":false,"title":"Implement np.linalg.{det, slogdet}.","user":{"login":"stuartarchibald","id":907717,"avatar_url":"https://avatars.githubusercontent.com/u/907717?v=3","gravatar_id":"","url":"https://api.github.com/users/stuartarchibald","html_url":"https://github.com/stuartarchibald","followers_url":"https://api.github.com/users/stuartarchibald/followers","following_url":"https://api.github.com/users/stuartarchibald/following{/other_user}","gists_url":"https://api.github.com/users/stuartarchibald/gists{/gist_id}","starred_url":"https://api.github.com/users/stuartarchibald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stuartarchibald/subscriptions","organizations_url":"https://api.github.com/users/stuartarchibald/orgs","repos_url":"https://api.github.com/users/stuartarchibald/repos","events_url":"https://api.github.com/users/stuartarchibald/events{/privacy}","received_events_url":"https://api.github.com/users/stuartarchibald/received_events","type":"User","site_admin":false},"body":"This patch implements and tests:\r\n * `np.linalg.det`\r\n * `np.linalg.slogdet`","created_at":"2016-07-15T16:40:03Z","updated_at":"2016-07-19T17:20:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"81a24c9fdd7ce2dd40c12ff02c530f89b3e15977","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/numba/numba/pulls/1987/commits","review_comments_url":"https://api.github.com/repos/numba/numba/pulls/1987/comments","review_comment_url":"https://api.github.com/repos/numba/numba/pulls/comments{/number}","comments_url":"https://api.github.com/repos/numba/numba/issues/1987/comments","statuses_url":"https://api.github.com/repos/numba/numba/statuses/72c5cb801894fbabd2b121becab59e1735abcfef","head":{"label":"stuartarchibald:stage/linalg_slogdet_det","ref":"stage/linalg_slogdet_det","sha":"72c5cb801894fbabd2b121becab59e1735abcfef","user":{"login":"stuartarchibald","id":907717,"avatar_url":"https://avatars.githubusercontent.com/u/907717?v=3","gravatar_id":"","url":"https://api.github.com/users/stuartarchibald","html_url":"https://github.com/stuartarchibald","followers_url":"https://api.github.com/users/stuartarchibald/followers","following_url":"https://api.github.com/users/stuartarchibald/following{/other_user}","gists_url":"https://api.github.com/users/stuartarchibald/gists{/gist_id}","starred_url":"https://api.github.com/users/stuartarchibald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stuartarchibald/subscriptions","organizations_url":"https://api.github.com/users/stuartarchibald/orgs","repos_url":"https://api.github.com/users/stuartarchibald/repos","events_url":"https://api.github.com/users/stuartarchibald/events{/privacy}","received_events_url":"https://api.github.com/users/stuartarchibald/received_events","type":"User","site_admin":false},"repo":{"id":44982242,"name":"numba","full_name":"stuartarchibald/numba","owner":{"login":"stuartarchibald","id":907717,"avatar_url":"https://avatars.githubusercontent.com/u/907717?v=3","gravatar_id":"","url":"https://api.github.com/users/stuartarchibald","html_url":"https://github.com/stuartarchibald","followers_url":"https://api.github.com/users/stuartarchibald/followers","following_url":"https://api.github.com/users/stuartarchibald/following{/other_user}","gists_url":"https://api.github.com/users/stuartarchibald/gists{/gist_id}","starred_url":"https://api.github.com/users/stuartarchibald/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stuartarchibald/subscriptions","organizations_url":"https://api.github.com/users/stuartarchibald/orgs","repos_url":"https://api.github.com/users/stuartarchibald/repos","events_url":"https://api.github.com/users/stuartarchibald/events{/privacy}","received_events_url":"https://api.github.com/users/stuartarchibald/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/stuartarchibald/numba","description":"NumPy aware dynamic Python compiler using LLVM","fork":true,"url":"https://api.github.com/repos/stuartarchibald/numba","forks_url":"https://api.github.com/repos/stuartarchibald/numba/forks","keys_url":"https://api.github.com/repos/stuartarchibald/numba/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stuartarchibald/numba/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stuartarchibald/numba/teams","hooks_url":"https://api.github.com/repos/stuartarchibald/numba/hooks","issue_events_url":"https://api.github.com/repos/stuartarchibald/numba/issues/events{/number}","events_url":"https://api.github.com/repos/stuartarchibald/numba/events","assignees_url":"https://api.github.com/repos/stuartarchibald/numba/assignees{/user}","branches_url":"https://api.github.com/repos/stuartarchibald/numba/branches{/branch}","tags_url":"https://api.github.com/repos/stuartarchibald/numba/tags","blobs_url":"https://api.github.com/repos/stuartarchibald/numba/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stuartarchibald/numba/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stuartarchibald/numba/git/refs{/sha}","trees_url":"https://api.github.com/repos/stuartarchibald/numba/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stuartarchibald/numba/statuses/{sha}","languages_url":"https://api.github.com/repos/stuartarchibald/numba/languages","stargazers_url":"https://api.github.com/repos/stuartarchibald/numba/stargazers","contributors_url":"https://api.github.com/repos/stuartarchibald/numba/contributors","subscribers_url":"https://api.github.com/repos/stuartarchibald/numba/subscribers","subscription_url":"https://api.github.com/repos/stuartarchibald/numba/subscription","commits_url":"https://api.github.com/repos/stuartarchibald/numba/commits{/sha}","git_commits_url":"https://api.github.com/repos/stuartarchibald/numba/git/commits{/sha}","comments_url":"https://api.github.com/repos/stuartarchibald/numba/comments{/number}","issue_comment_url":"https://api.github.com/repos/stuartarchibald/numba/issues/comments{/number}","contents_url":"https://api.github.com/repos/stuartarchibald/numba/contents/{+path}","compare_url":"https://api.github.com/repos/stuartarchibald/numba/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stuartarchibald/numba/merges","archive_url":"https://api.github.com/repos/stuartarchibald/numba/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stuartarchibald/numba/downloads","issues_url":"https://api.github.com/repos/stuartarchibald/numba/issues{/number}","pulls_url":"https://api.github.com/repos/stuartarchibald/numba/pulls{/number}","milestones_url":"https://api.github.com/repos/stuartarchibald/numba/milestones{/number}","notifications_url":"https://api.github.com/repos/stuartarchibald/numba/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stuartarchibald/numba/labels{/name}","releases_url":"https://api.github.com/repos/stuartarchibald/numba/releases{/id}","deployments_url":"https://api.github.com/repos/stuartarchibald/numba/deployments","created_at":"2015-10-26T16:36:31Z","updated_at":"2016-01-15T11:19:31Z","pushed_at":"2016-07-19T11:49:11Z","git_url":"git://github.com/stuartarchibald/numba.git","ssh_url":"git@github.com:stuartarchibald/numba.git","clone_url":"https://github.com/stuartarchibald/numba.git","svn_url":"https://github.com/stuartarchibald/numba","homepage":"http://numba.pydata.org/","size":29635,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"numba:master","ref":"master","sha":"eee2f86e63c0c8f3eb085a6550900f1b1d502e54","user":{"login":"numba","id":1628082,"avatar_url":"https://avatars.githubusercontent.com/u/1628082?v=3","gravatar_id":"","url":"https://api.github.com/users/numba","html_url":"https://github.com/numba","followers_url":"https://api.github.com/users/numba/followers","following_url":"https://api.github.com/users/numba/following{/other_user}","gists_url":"https://api.github.com/users/numba/gists{/gist_id}","starred_url":"https://api.github.com/users/numba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/numba/subscriptions","organizations_url":"https://api.github.com/users/numba/orgs","repos_url":"https://api.github.com/users/numba/repos","events_url":"https://api.github.com/users/numba/events{/privacy}","received_events_url":"https://api.github.com/users/numba/received_events","type":"Organization","site_admin":false},"repo":{"id":3659275,"name":"numba","full_name":"numba/numba","owner":{"login":"numba","id":1628082,"avatar_url":"https://avatars.githubusercontent.com/u/1628082?v=3","gravatar_id":"","url":"https://api.github.com/users/numba","html_url":"https://github.com/numba","followers_url":"https://api.github.com/users/numba/followers","following_url":"https://api.github.com/users/numba/following{/other_user}","gists_url":"https://api.github.com/users/numba/gists{/gist_id}","starred_url":"https://api.github.com/users/numba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/numba/subscriptions","organizations_url":"https://api.github.com/users/numba/orgs","repos_url":"https://api.github.com/users/numba/repos","events_url":"https://api.github.com/users/numba/events{/privacy}","received_events_url":"https://api.github.com/users/numba/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/numba/numba","description":"NumPy aware dynamic Python compiler using LLVM","fork":false,"url":"https://api.github.com/repos/numba/numba","forks_url":"https://api.github.com/repos/numba/numba/forks","keys_url":"https://api.github.com/repos/numba/numba/keys{/key_id}","collaborators_url":"https://api.github.com/repos/numba/numba/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/numba/numba/teams","hooks_url":"https://api.github.com/repos/numba/numba/hooks","issue_events_url":"https://api.github.com/repos/numba/numba/issues/events{/number}","events_url":"https://api.github.com/repos/numba/numba/events","assignees_url":"https://api.github.com/repos/numba/numba/assignees{/user}","branches_url":"https://api.github.com/repos/numba/numba/branches{/branch}","tags_url":"https://api.github.com/repos/numba/numba/tags","blobs_url":"https://api.github.com/repos/numba/numba/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/numba/numba/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/numba/numba/git/refs{/sha}","trees_url":"https://api.github.com/repos/numba/numba/git/trees{/sha}","statuses_url":"https://api.github.com/repos/numba/numba/statuses/{sha}","languages_url":"https://api.github.com/repos/numba/numba/languages","stargazers_url":"https://api.github.com/repos/numba/numba/stargazers","contributors_url":"https://api.github.com/repos/numba/numba/contributors","subscribers_url":"https://api.github.com/repos/numba/numba/subscribers","subscription_url":"https://api.github.com/repos/numba/numba/subscription","commits_url":"https://api.github.com/repos/numba/numba/commits{/sha}","git_commits_url":"https://api.github.com/repos/numba/numba/git/commits{/sha}","comments_url":"https://api.github.com/repos/numba/numba/comments{/number}","issue_comment_url":"https://api.github.com/repos/numba/numba/issues/comments{/number}","contents_url":"https://api.github.com/repos/numba/numba/contents/{+path}","compare_url":"https://api.github.com/repos/numba/numba/compare/{base}...{head}","merges_url":"https://api.github.com/repos/numba/numba/merges","archive_url":"https://api.github.com/repos/numba/numba/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/numba/numba/downloads","issues_url":"https://api.github.com/repos/numba/numba/issues{/number}","pulls_url":"https://api.github.com/repos/numba/numba/pulls{/number}","milestones_url":"https://api.github.com/repos/numba/numba/milestones{/number}","notifications_url":"https://api.github.com/repos/numba/numba/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/numba/numba/labels{/name}","releases_url":"https://api.github.com/repos/numba/numba/releases{/id}","deployments_url":"https://api.github.com/repos/numba/numba/deployments","created_at":"2012-03-08T11:12:43Z","updated_at":"2016-07-19T16:20:11Z","pushed_at":"2016-07-19T11:49:11Z","git_url":"git://github.com/numba/numba.git","ssh_url":"git@github.com:numba/numba.git","clone_url":"https://github.com/numba/numba.git","svn_url":"https://github.com/numba/numba","homepage":"http://numba.pydata.org/","size":35354,"stargazers_count":1884,"watchers_count":1884,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":206,"mirror_url":null,"open_issues_count":197,"forks":206,"open_issues":197,"watchers":1884,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/numba/numba/pulls/1987"},"html":{"href":"https://github.com/numba/numba/pull/1987"},"issue":{"href":"https://api.github.com/repos/numba/numba/issues/1987"},"comments":{"href":"https://api.github.com/repos/numba/numba/issues/1987/comments"},"review_comments":{"href":"https://api.github.com/repos/numba/numba/pulls/1987/comments"},"review_comment":{"href":"https://api.github.com/repos/numba/numba/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/numba/numba/pulls/1987/commits"},"statuses":{"href":"https://api.github.com/repos/numba/numba/statuses/72c5cb801894fbabd2b121becab59e1735abcfef"}}}}
{"actor":{"display_login":"stuartarchibald"}}