-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add matrix, an operation to convert an array to a matrix (like vec) #23790
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be useful to have. I think I'd prefer matrix
instead of mat
even though mat
is similar to vec
. However, vec
is standard whereas mat
is not and I prefer to spell things out.
base/abstractarraymath.jl
Outdated
""" | ||
vec(a::AbstractArray) = reshape(a,_length(a)) | ||
vec(a::AbstractVector) = a | ||
|
||
|
||
""" | ||
mat(a::AbstractArray) -> Matrix |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be AbstractMatrix
instead of Matrix
.
Makes sense to spell it out. |
Would it be reasonable to expect this to convert a |
What do you mean? julia> supertype(RowVector) == AbstractMatrix
true
julia> x = RowVector([1,2,3])
1×3 RowVector{Int64,Array{Int64,1}}:
1 2 3
julia> size(x)==(1,3)
true
julia> matrix(x)
1×3 RowVector{Int64,Array{Int64,1}}:
1 2 3
julia> matrix(x) == x
true
julia> Matrix(x)
1×3 Array{Int64,2}:
1 2 3 |
Yes, I wanted a concrete Matrix without copying the data. Maybe I should just look the other way to make the receivers of the data more open to abstract matrices. |
I like the direction. There are a bunch of cases where you want to cast your quasi-1d collection into a specific shape, so that's cool. I totally haven't got around to this, but I was hoping to rather have this little family of functions:
since sometimes you want a column matrix and sometimes a row matrix. |
There's still something bothering me with the pseudomatrixness of RowVectors. If I say matrix(x) I would expect to get something that is matrixy enough that I can transpose it and always have a matrix, not something that occasionally reverts to a vector. |
@andreasnoack is this good to go now? Should I add @andyferris 's stuff? |
The same, yes. @GunnarFarneback I agree. |
Now that we have a |
With the present direction of array constructors, |
I don't think constructors can fill this role since I'd expect them to copy the data; Of course, |
Ah, right! Thank you :).
👍 |
@andyferris's set of functions is progressively growing on me. |
@andyferris Shouldn't that operation go row major, i.e. |
@jebej I wouldn't have thought so... the idea here is to turn any iterable into a |
I think we can close this. We have matrix literals even for matrixes of length 1, and we have reshape. |
We have
vec
which is a short-hand forreshape(x, Val(1))
.I propose that we should have
mat
as well, as a shorthand forreshape(x,Val(2))
.Obviously
vec
is the most common case of reshaping,but I suggest that
mat
comes in second, and is much more common than the generalreshape
.In particular for converting a Vector into a single column matrix.
Very common for interfacing with various libraries.
My old trick for that was to double transpose it (
x''
), but since as of 0.6 we take vector transpose seriously, that won't work. (Plus this way is nonallocating).