{"action":"created","issue":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/13731","labels_url":"https://api.github.com/repos/JuliaLang/julia/issues/13731/labels{/name}","comments_url":"https://api.github.com/repos/JuliaLang/julia/issues/13731/comments","events_url":"https://api.github.com/repos/JuliaLang/julia/issues/13731/events","html_url":"https://github.com/JuliaLang/julia/issues/13731","id":112908187,"number":13731,"title":"How best to fix similar for triangular matrices?","user":{"login":"artkuo","id":2893800,"avatar_url":"https://avatars.githubusercontent.com/u/2893800?v=3","gravatar_id":"","url":"https://api.github.com/users/artkuo","html_url":"https://github.com/artkuo","followers_url":"https://api.github.com/users/artkuo/followers","following_url":"https://api.github.com/users/artkuo/following{/other_user}","gists_url":"https://api.github.com/users/artkuo/gists{/gist_id}","starred_url":"https://api.github.com/users/artkuo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/artkuo/subscriptions","organizations_url":"https://api.github.com/users/artkuo/orgs","repos_url":"https://api.github.com/users/artkuo/repos","events_url":"https://api.github.com/users/artkuo/events{/privacy}","received_events_url":"https://api.github.com/users/artkuo/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/JuliaLang/julia/labels/decision","name":"decision","color":"F08000"},{"url":"https://api.github.com/repos/JuliaLang/julia/labels/linear%20algebra","name":"linear algebra","color":"aa8800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-10-22T22:57:34Z","updated_at":"2015-11-25T09:31:11Z","closed_at":null,"body":"In response to #13174, the bug was traced to `mean` which relies on `similar` (through reducedim) to prepare an array for its output, e.g. `mean` of a 4x3 matrix along dim 1 should yield a 1x3. It ends up calling something like `similar(triangularmatrix, LowerTriangular, (1,3))` (the template matrix, the desired type, and the dimensions), which fails because a 1x3 triangular matrix doesn't make sense. In this context, `similar` means \"give me a matrix with the same eltype\" not \"give me another triangular just like this one.\"\r\n\r\n1. An easy fix (@tkelman) is to adjust `similar` so that when optional dimensions are used, it automatically drops the triangular, which results in AbstractMatrix. When no dimension is given, it could yield a lower triangular. This actually works, and even preserves sparsity automatically. I find this a bit yucky, because the return type depends implicitly on how the call is made.\r\n\r\n2. Another fix is to change `similar` to always drop the triangular, again preserving sparsity where applicable. I've tried this fix and it doesn't break any existing tests (other than needing a new test for `similar`). Downside is it is a potentially breaking change for user code where `similar` is intended to mean \"give me another of same structure.\" Yucky as well.\r\n\r\n3. Yet another possibility is to delve into the details of how `similar` is called and root out the problem at the level of reducedim. Where appropriate, it could be replaced by `sameeltype`, meaning a more explicit version of what is meant already. In other cases, the replacement might be `samecontainer` or `samestructure` or some such. Idea is to make explicit what you really want. This seems like a longer term solution, because `similar` occurs 600+ times in julia project (between method calls and definitions) and it may not be straightforward to know what is intended when.\r\n\r\nPrevious discussions have complained about `similar` #11574 (comment) and #10064. The problem is that `similar` is vague. Present docs say that behavior for special types should be decided case by case. Right now `similar` for triangulars is undocumented, so the user must guess or look at the source code. Might be better to clarify and perhaps split off a `sameeltype` or `samestructure` in the long run.\r\n\r\nThere are safe, conservative ways to do option 2, such as when triangular is input, to return a matrix that's not triangular but whose off-triangle has been zeroed. This makes it safe for the user expecting a triangular back and for the most part won't blow up on them. (Triangulars store a full matrix in their .data field, so there is potential danger if things are not zeroed.) \r\n\r\nWhat is the right solution?"},"comment":{"url":"https://api.github.com/repos/JuliaLang/julia/issues/comments/159548321","html_url":"https://github.com/JuliaLang/julia/issues/13731#issuecomment-159548321","issue_url":"https://api.github.com/repos/JuliaLang/julia/issues/13731","id":159548321,"user":{"login":"nalimilan","id":1120448,"avatar_url":"https://avatars.githubusercontent.com/u/1120448?v=3","gravatar_id":"","url":"https://api.github.com/users/nalimilan","html_url":"https://github.com/nalimilan","followers_url":"https://api.github.com/users/nalimilan/followers","following_url":"https://api.github.com/users/nalimilan/following{/other_user}","gists_url":"https://api.github.com/users/nalimilan/gists{/gist_id}","starred_url":"https://api.github.com/users/nalimilan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nalimilan/subscriptions","organizations_url":"https://api.github.com/users/nalimilan/orgs","repos_url":"https://api.github.com/users/nalimilan/repos","events_url":"https://api.github.com/users/nalimilan/events{/privacy}","received_events_url":"https://api.github.com/users/nalimilan/received_events","type":"User","site_admin":false},"created_at":"2015-11-25T09:31:11Z","updated_at":"2015-11-25T09:31:11Z","body":"I agree with @tkelman. Even if in practice it more or less works, we'd better clarify what `similar` means. (In most cases, I suspect it's used to create an `Array` of the same eltype and (sometimes) size. Everything would be clearer if a separate function was used for that.)"}}