Skip to content

Commit 56565b1

Browse files
committed
Add SA_F64, SA_F32 for constructing common eltype
This gives a way to construct an `SVector` and `SMatrix` with a precise eltype, but without typing a verbose type name. I think this fixes #24 to the extent that it can be fixed. Also add some quickstart docs for `SA` and the new type aliases.
1 parent 98d35ff commit 56565b1

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ Pkg.add("StaticArrays") # or Pkg.clone("https://github.com/JuliaArrays/StaticAr
6262
using StaticArrays
6363
using LinearAlgebra
6464

65+
# Use the convenience constructor type `SA` to create vectors and matrices
66+
SA[1, 2, 3] isa SVector{3,Int}
67+
SA_F64[1, 2, 3] isa SVector{3,Float64}
68+
SA_F32[1, 2, 3] isa SVector{3,Float32}
69+
SA[1 2; 3 4] isa SMatrix{2,2,Int}
70+
SA_F64[1 2; 3 4] isa SMatrix{2,2,Float64}
71+
6572
# Create an SVector using various forms, using constructors, functions or macros
6673
v1 = SVector(1, 2, 3)
6774
v1.data === (1, 2, 3) # SVector uses a tuple for internal storage

docs/src/pages/quickstart.md

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ Pkg.add("StaticArrays") # or Pkg.clone("https://github.com/JuliaArrays/StaticAr
55
using StaticArrays
66
using LinearAlgebra
77

8+
# Use the convenience constructor type `SA` to create vectors and matrices
9+
SA[1, 2, 3] isa SVector{3,Int}
10+
SA_F64[1, 2, 3] isa SVector{3,Float64}
11+
SA_F32[1, 2, 3] isa SVector{3,Float32}
12+
SA[1 2; 3 4] isa SMatrix{2,2,Int}
13+
SA_F64[1 2; 3 4] isa SMatrix{2,2,Float64}
14+
815
# Create an SVector using various forms, using constructors, functions or macros
916
v1 = SVector(1, 2, 3)
1017
v1.data === (1, 2, 3) # SVector uses a tuple for internal storage

src/StaticArrays.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export SHermitianCompact
3636

3737
export Size, Length
3838

39-
export SA
39+
export SA, SA_F32, SA_F64
4040
export @SVector, @SMatrix, @SArray
4141
export @MVector, @MMatrix, @MArray
4242

src/initializers.jl

+8
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ provided explicitly.
1212
* `SA[1 2; 3 4]` creates a 2×2 SMatrix of `Int`s.
1313
* `SA[1 2]` creates a 1×2 SMatrix of `Int`s.
1414
* `SA{Float32}[1, 2]` creates a length-2 `SVector` of `Float32` elements.
15+
16+
A couple of helpful type aliases are also provided:
17+
18+
* `SA_F64[1, 2]` creates a lenght-2 `SVector` of `Float64` elements
19+
* `SA_F32[1, 2]` creates a lenght-2 `SVector` of `Float32` elements
1520
"""
1621
struct SA{T} ; end
1722

23+
const SA_F32 = SA{Float32}
24+
const SA_F64 = SA{Float64}
25+
1826
@inline similar_type(::Type{SA}, ::Size{S}) where {S} = SArray{Tuple{S...}}
1927
@inline similar_type(::Type{SA{T}}, ::Size{S}) where {T,S} = SArray{Tuple{S...}, T}
2028

test/initializers.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@testset "Initialization with SA" begin
2+
13
SA_test_ref(x) = SA[1,x,x]
24
SA_test_ref(x,T) = SA{T}[1,x,x]
35
@test @inferred(SA_test_ref(2)) === SVector{3,Int}((1,2,2))
@@ -31,3 +33,7 @@ SA_test_hvcat(x,T) = SA{T}[1 x x;
3133
4 5]
3234
@test_throws ArgumentError("SA[...] matrix rows of length (2, 3) are inconsistent") SA[1 2;
3335
3 4 5]
36+
@test SA_F64[1, 2] === SVector{2,Float64}((1,2))
37+
@test SA_F32[1, 2] === SVector{2,Float32}((1,2))
38+
39+
end

0 commit comments

Comments
 (0)