@@ -140,7 +140,7 @@ additional loop unrolling and inlining, and consequentially (c) their mutating
140140methods like ` map! ` are extremely fast. Benchmarking shows that operations such
141141as addition and matrix multiplication are faster for ` MMatrix ` than ` Matrix ` ,
142142at least for sizes up to 14 × 14, though keep in mind that optimal speed will
143- be obtained by using mutating functions (like ` map! ` or ` A_mul_B !` ) where
143+ be obtained by using mutating functions (like ` map! ` or ` mul !` ) where
144144possible, rather than reallocating new memory.
145145
146146Mutable static arrays also happen to be very useful containers that can be
@@ -247,12 +247,14 @@ could be represented as a `Vector{SVector{3,Float64}}`.
247247Another common way of storing the same data is as a 3×` N ` ` Matrix{Float64} ` .
248248Rather conveniently, such types have * exactly* the same binary layout in memory,
249249and therefore we can use ` reinterpret ` to convert between the two formats
250- ``` julia
250+ ``` @example copy
251+ using StaticArrays # hide
251252function svectors(x::Matrix{T}, ::Val{N}) where {T,N}
252253 size(x,1) == N || error("sizes mismatch")
253254 isbitstype(T) || error("use for bitstypes only")
254255 reinterpret(SVector{N,T}, vec(x))
255256end
257+ nothing # hide
256258```
257259Such a conversion does not copy the data, rather it refers to the * same* memory.
258260Arguably, a ` Vector ` of ` SVector ` s is often preferable to a ` Matrix ` because it
@@ -263,31 +265,19 @@ However, the resulting object is a Base.ReinterpretArray, not an Array, which
263265carries some runtime penalty on every single access. If you can afford the
264266memory for a copy and can live with the non-shared mutation semantics, then it
265267is better to pull a copy by e.g.
266- ``` julia
268+ ``` @example copy
267269function svectorscopy(x::Matrix{T}, ::Val{N}) where {T,N}
268270 size(x,1) == N || error("sizes mismatch")
269271 isbitstype(T) || error("use for bitstypes only")
270272 copy(reinterpret(SVector{N,T}, vec(x)))
271273end
274+ nothing # hide
272275```
273276For example:
274- ```
275- julia> M=reshape(collect(1:6), (2,3))
276- 2×3 Array{Int64,2}:
277- 1 3 5
278- 2 4 6
279-
280- julia> svectors(M, Val{2}())
281- 3-element reinterpret(SArray{Tuple{2},Int64,1,2}, ::Array{Int64,1}):
282- [1, 2]
283- [3, 4]
284- [5, 6]
285-
286- julia> svectorscopy(M, Val{2}())
287- 3-element Array{SArray{Tuple{2},Int64,1,2},1}:
288- [1, 2]
289- [3, 4]
290- [5, 6]
277+ ``` @repl copy
278+ M = reshape(collect(1:6), (2,3))
279+ svectors(M, Val{2}())
280+ svectorscopy(M, Val{2}())
291281```
292282
293283### Working with mutable and immutable arrays
0 commit comments