Skip to content

Commit a8db302

Browse files
committed
numbers as a map
1 parent b3d9f3d commit a8db302

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ makedocs(;
2222
)
2323

2424
deploydocs(;
25-
repo="github.com/JuliaApproximation/FunctionMaps.jl.git", devbranch="master",
25+
repo="github.com/JuliaApproximation/FunctionMaps.jl.git", devbranch="main",
2626
)

src/generic/interface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ MapStyle(::Type{<:Map}) = IsMap()
3131
functionmap(m)
3232
3333
Return a map associated with the object `m`.
34+
35+
The result need not have type `Map`, but its `MapStyle` is `IsMap`.
3436
"""
3537
functionmap(m::Map) = m
3638

src/types/affine.jl

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -292,32 +292,6 @@ StaticLinearMap{T,N,M}(A::AbstractMatrix) where {T,N,M} =
292292
convert(::Type{Map{SVector{N,T}}}, m::VectorLinearMap) where {N,T} = StaticLinearMap{T,N,N}(m.A)
293293

294294

295-
# Implement the interface for abstract arrays,
296-
# representing the linear map x->A*x
297-
MapStyle(A::AbstractArray) = IsMap()
298-
299-
Map(A::AbstractArray) = LinearMap(A)
300-
Map{T}(A::AbstractArray) where T = LinearMap{T}(A)
301-
302-
domaintype(A::AbstractArray) = domaintype(Map(A))
303-
304-
applymap(A::AbstractArray, x) = A*x
305-
mapsize(A::AbstractArray) = size(A)
306-
307-
islinearmap(A::AbstractArray) = true
308-
isaffinemap(A::AbstractArray) = true
309-
affinematrix(A::AbstractArray) = A
310-
affinevector(A::AbstractArray) = zerovector(A)
311-
312-
inverse(A::AbstractMatrix) = inv(A)
313-
inverse(A::AbstractMatrix, x) = A \ x
314-
315-
jacobian(A::AbstractMatrix) = ConstantMap{glm_domaintype(A)}(A)
316-
jacobian(A::AbstractMatrix, x) = A
317-
318-
canonicalmap(A::AbstractArray) = Map(A)
319-
canonicalmap(::Equal, A::AbstractArray) = Map(A)
320-
321295

322296

323297

@@ -618,10 +592,68 @@ convert(::Type{Map{SVector{N,T}}}, m::VectorAffineMap) where {N,T} =
618592
StaticAffineMap{T,N}(m.A, m.b)
619593

620594

595+
##############################
596+
# Numbers and arrays as a map
597+
##############################
598+
599+
# Arrays represent the linear map A*x
600+
MapStyle(::Type{<:AbstractArray}) = IsMap()
601+
602+
Map(A::AbstractArray) = LinearMap(A)
603+
Map{T}(A::AbstractArray) where T = LinearMap{T}(A)
604+
605+
domaintype(A::AbstractArray) = domaintype(Map(A))
606+
607+
applymap(A::AbstractArray, x) = A*x
608+
mapsize(A::AbstractArray) = size(A)
609+
610+
islinearmap(A::AbstractArray) = true
611+
isaffinemap(A::AbstractArray) = true
612+
affinematrix(A::AbstractArray) = A
613+
affinevector(A::AbstractArray) = zerovector(A)
614+
615+
inverse(A::AbstractMatrix) = inv(A)
616+
inverse(A::AbstractMatrix, x) = A \ x
617+
618+
jacobian(A::AbstractMatrix) = ConstantMap{glm_domaintype(A)}(A)
619+
jacobian(A::AbstractMatrix, x) = A
620+
621+
canonicalmap(A::AbstractArray) = Map(A)
622+
canonicalmap(::Equal, A::AbstractArray) = Map(A)
623+
624+
# Numbers represent the linear map a*x
625+
626+
MapStyle(::Type{<:Number}) = IsMap()
627+
628+
applymap(a::Number, x) = a*x
629+
630+
Map(a::Number) = LinearMap(a)
631+
Map{T}(a::Number) where T = LinearMap{T}(a)
632+
domaintype(a::Number) = typeof(a)
633+
634+
mapsize(a::Number) = ()
635+
636+
islinearmap(a::Number) = true
637+
isaffinemap(a::Number) = true
638+
affinematrix(a::Number) = a
639+
affinevector(a::Number) = zero(a)
640+
641+
inverse(a::Number) = inv(a)
642+
inverse(a::Number, x) = a \ x
643+
644+
jacobian(a::Number) = ConstantMap(a)
645+
jacobian(a::Number, x) = a
646+
647+
canonicalmap(a::Number) = Map(a)
648+
canonicalmap(::Equal, a::Number) = Map(a)
649+
650+
621651
###########################
622652
# Uniform scaling as a map
623653
###########################
624654

655+
MapStyle(::Type{<:UniformScaling}) = IsMap()
656+
625657
convert(::Type{Map}, A::UniformScaling) = GenericLinearMap{Vector{Any}}(A)
626658
convert(::Type{Map{T}}, A::UniformScaling) where {T} = GenericLinearMap{T}(A)
627659

src/util/common.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
isrealtype(::Type{<:Real}) = true
33
isrealtype(::Type{<:Complex}) = false
44
isrealtype(::Type{T}) where {T} = isrealtype(eltype(T))
5+
isrealtype(::Type{Any}) = false
56

67
const StaticTypes = Union{Number,<:StaticVector{N} where N,<:NTuple{N,Any} where N}
78

test/test_common.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ function test_numtype()
8484
@test promote_numtype(2, 3.0+im, big(4)) isa Tuple{Complex{BigFloat},Complex{BigFloat},Complex{BigFloat}}
8585
end
8686

87+
using FunctionMaps: isrealtype
88+
89+
function test_realtype()
90+
@test isrealtype(Any) == false
91+
@test isrealtype(Int) == true
92+
@test isrealtype(ComplexF64) == false
93+
@test isrealtype(Matrix{Float64}) == true
94+
end
95+
8796
@testset "common functionality" begin
8897
@testset "dimension" begin
8998
test_dimension()
@@ -94,4 +103,6 @@ end
94103
@testset "numtype" begin
95104
test_numtype()
96105
end
106+
107+
test_realtype()
97108
end

test/test_interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ struct MySimpleMap end
33
FunctionMaps.MapStyle(::Type{MySimpleMap}) = IsMap()
44

55
@testset "map interface" begin
6-
@test MapStyle(2.0) isa NotMap
6+
@test_throws ArgumentError checkmap((5,)) # a tuple is not a map
7+
@test MapStyle(2.0) isa IsMap
78
@test MapStyle(2.0) == MapStyle(typeof(2.0))
8-
@test_throws ArgumentError checkmap(2.0)
99
m = LinearMap(2)
1010
@test MapStyle(m) isa IsMap
1111
@test functionmap(m) === m

0 commit comments

Comments
 (0)