Skip to content
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

Type changes from MArray to SArray upon using broadcast (vectorization) #403

Closed
eeshan9815 opened this issue May 14, 2018 · 3 comments
Closed
Labels
design speculative design related issue

Comments

@eeshan9815
Copy link

julia> a = @MMatrix randn(2, 2)
2×2 StaticArrays.MArray{Tuple{2,2},Float64,2,4}:
 1.16721   -1.01285
 0.221507   0.00572563
julia> sa = sin.(a)
2×2 StaticArrays.SArray{Tuple{2,2},Float64,2,4}:
 0.919658  -0.848346
 0.2197     0.0057256

Why is sa of the type SArray whereas a is of the type MArray?

@eeshan9815 eeshan9815 changed the title Type changes from MArray to SArray upon using broadcast (vectorization) Type changes from MArray to SArray upon using broadcast (vectorization) May 14, 2018
@tkoolen
Copy link
Contributor

tkoolen commented May 14, 2018

(Note: just a contributor). I think the current convention is to avoid allocation whenever possible.
There's something to be said for either option, see also

# should mutable things stay mutable?
#similar_type{SA<:Union{MVector,MMatrix,MArray},T,S}(::Type{SA},::Type{T},s::Size{S}) = mutable_similar_type(T,s,length_val(s))
but I think the current behavior would be frequently desirable (what with StaticArrays being all about getting the best performance possible).

Note that you can always do

julia> a = @MMatrix randn(2, 2)
2×2 StaticArrays.MArray{Tuple{2,2},Float64,2,4}:                                                                                                                                                                                                                                
 -0.266148  -0.146905                                                                                                                                                                                                                                                           
  0.67229    1.57561                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                
julia> sa = MMatrix{2, 2, Float64}()
2×2 StaticArrays.MArray{Tuple{2,2},Float64,2,4}:                                                                                                                                                                                                                                
 1.4822e-323  6.9015e-310                                                                                                                                                                                                                                                       
 6.9015e-310  0.0        

julia> sa .= sin.(a)
2×2 StaticArrays.MArray{Tuple{2,2},Float64,2,4}:
 -0.263017  -0.146378
  0.622779   0.999988

There would not be an easy workaround like the one above if an MArray were returned by default and you wanted an SArray without allocating.

@andyferris
Copy link
Member

See also #327

@c42f
Copy link
Member

c42f commented Jul 31, 2019

Was solved in #536, which I'm now fairly confident was the right choice :-)

@c42f c42f closed this as completed Jul 31, 2019
@c42f c42f added the design speculative design related issue label Jul 31, 2019
@c42f c42f mentioned this issue Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design speculative design related issue
Projects
None yet
Development

No branches or pull requests

4 participants