Skip to content

Commit 2c30f05

Browse files
committed
use IdentityMap constructor more often
1 parent 902923c commit 2c30f05

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

src/concrete/basic.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ IdentityMap() = StaticIdentityMap()
77
IdentityMap(::Val{N}) where {N} = StaticIdentityMap(Val(N))
88

99
IdentityMap{T}(n::Int) where {T} = DynamicIdentityMap{T}(n)
10-
IdentityMap{T}(n::Int) where {T<:StaticTypes} = StaticIdentityMap{T}()
10+
IdentityMap{T}(n::Int) where {T<:StaticTypes} = StaticIdentityMap{T}(n)
1111
IdentityMap{T}(::Val{N}) where {N,T} = StaticIdentityMap{T}(Val(N))
1212
IdentityMap{T}() where {T} = StaticIdentityMap{T}()
1313

@@ -50,10 +50,14 @@ end
5050
StaticIdentityMap() = StaticIdentityMap{Float64}()
5151
StaticIdentityMap(::Val{N}) where {N} = StaticIdentityMap{SVector{N,Float64}}()
5252

53-
StaticIdentityMap{T}(n::Int) where {T} =
54-
(@assert n == euclideandimension(T); StaticIdentityMap{T}())
55-
StaticIdentityMap{T}(::Val{N}) where {N,T} =
56-
(@assert N == euclideandimension(T); StaticIdentityMap{T}())
53+
function StaticIdentityMap{T}(n::Int) where {T}
54+
n == euclideandimension(T) || throw(ArgumentError("Provided dimension of static map is inconsistent with static type"))
55+
StaticIdentityMap{T}()
56+
end
57+
function StaticIdentityMap{T}(::Val{N}) where {N,T}
58+
N == euclideandimension(T) || throw(ArgumentError("Provided dimension of static map is inconsistent with static type"))
59+
StaticIdentityMap{T}()
60+
end
5761

5862
similarmap(m::StaticIdentityMap, ::Type{T}) where {T<:StaticTypes} = StaticIdentityMap{T}()
5963
similarmap(m::StaticIdentityMap, ::Type{T}) where {T} =

test/test_affine.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function test_linearmap(T)
5050
@test mw1.A isa widen(T)
5151
@test jacobian(m1) isa ConstantMap{T}
5252
@test jacobian(m1, 1) == 2
53-
@test LinearMap(one(T)) == StaticIdentityMap{T}()
53+
@test LinearMap(one(T)) == IdentityMap{T}()
5454

5555
m2 = LinearMap(2)
5656
@test domaintype(m2) == Int
@@ -60,15 +60,15 @@ function test_linearmap(T)
6060
@test jacobian(m2, 1) == 2
6161
@test jacobian(m2) isa ConstantMap{Int}
6262
@test jacobian(m2, 1) == 2
63-
@test LinearMap(1) == StaticIdentityMap{T}()
63+
@test LinearMap(1) == IdentityMap{T}()
6464

6565
m3 = LinearMap(SMatrix{2,2}(one(T), 2one(T), 3one(T), 4one(T)))
6666
@test m3 isa LinearMap{SVector{2,T}}
6767
@test m3 === StaticLinearMap(m3.A)
6868
@test m3(SVector(1,2)) == SVector(7, 10)
6969
@test m3(SVector{2,T}(1,2)) == SVector{2,T}(7, 10)
7070
@test m3 m3 isa LinearMap
71-
@test LinearMap(SA[1 0; 0 1]) == StaticIdentityMap{domaintype(m3)}()
71+
@test LinearMap(SA[1 0; 0 1]) == IdentityMap{domaintype(m3)}()
7272

7373
A = rand(T,2,2)
7474
m4 = LinearMap(A)

test/test_basic.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ function test_scaling_maps(T)
5858
end
5959

6060
function test_identity_map(T)
61-
i1 = StaticIdentityMap{T}()
62-
i2 = StaticIdentityMap{SVector{2,T}}()
61+
i1 = IdentityMap{T}()
62+
@test i1 isa FunctionMaps.StaticIdentityMap
63+
i2 = IdentityMap{SVector{2,T}}()
64+
@test i2 isa FunctionMaps.StaticIdentityMap
6365
test_generic_map(i1)
6466
test_generic_map(i2)
6567
@test i1 == i2
6668
@test hash(i1) == hash(i2)
6769
@test islinearmap(i1)
6870
@test isaffinemap(i1)
69-
@test convert(StaticIdentityMap{SVector{2,T}}, i1) === i2
71+
@test convert(FunctionMaps.StaticIdentityMap{SVector{2,T}}, i1) === i2
7072
@test jacobian(i1) isa ConstantMap
7173
@test jacobian(i1, 1) == 1
7274
@test jacdet(i1, 1) == 1
@@ -78,13 +80,19 @@ function test_identity_map(T)
7880
@test m2 isa LinearMap{T}
7981
@test jacdet(m2, 1) == 1
8082

81-
i3 = VectorIdentityMap{T}(10)
83+
i3 = IdentityMap{Vector{T}}(10)
84+
@test i3 isa FunctionMaps.VectorIdentityMap
8285
test_generic_map(i3)
8386
r = rand(T, 10)
8487
@test i3(r) r
85-
@test hash(i3) == hash(VectorIdentityMap{Int}(10))
88+
@test hash(i3) == hash(IdentityMap{Vector{Int}}(10))
8689

8790
@test IdentityMap() LinearMap(2) == LinearMap(2.0)
91+
92+
@test_throws ArgumentError IdentityMap{Float64}(10)
93+
@test_throws ArgumentError IdentityMap{Float64}(Val(2))
94+
@test IdentityMap{Float64}(1) isa Map
95+
@test IdentityMap{Float64}(Val(1)) isa Map
8896
end
8997

9098
function test_basic_maps(T)

test/test_maps.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
maps_to_test(T) = [
3-
StaticIdentityMap{T}(),
4-
VectorIdentityMap{T}(10),
3+
IdentityMap{T}(),
4+
IdentityMap{Vector{T}}(10),
55
ConstantMap{T}(one(T)),
66
ConstantMap{T}(SVector{2,T}(1,2)),
77
ZeroMap{T}(),
@@ -108,7 +108,7 @@ function test_composite_map(T)
108108
b = T(1)
109109
c = T(2)
110110
d = T(3)
111-
ma = StaticIdentityMap{T}()
111+
ma = IdentityMap{T}()
112112
mb = interval_map(a, b, c, d)
113113

114114
r = suitable_point_to_map(ma)

test/test_product.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function test_product_map(T)
2-
ma = StaticIdentityMap{T}()
2+
ma = IdentityMap{T}()
33
mb = interval_map(T(0), T(1), T(2), T(3))
44

55
r1 = suitable_point_to_map(ma)

0 commit comments

Comments
 (0)