|
| 1 | +""" |
| 2 | + AffineMap{T} <: AbstractAffineMap{T} |
| 3 | +
|
| 4 | + The supertype of all affine maps that store `A` and `b`. |
| 5 | +Concrete subtypes differ in how `A` and `b` are represented. |
| 6 | +""" |
| 7 | +abstract type AffineMap{T} <: AbstractAffineMap{T} end |
| 8 | + |
| 9 | +""" |
| 10 | + AffineMap(A, b) |
| 11 | +
|
| 12 | +Return an affine map with an appropriate concrete type depending on the arguments |
| 13 | +`A` and `b`. |
| 14 | +
|
| 15 | +# Examples |
| 16 | +```julia |
| 17 | +julia> AffineMap(2, 3) |
| 18 | +x -> 2 * x + 3 |
| 19 | +``` |
| 20 | +""" |
| 21 | +AffineMap(A::Number, b::Number) = ScalarAffineMap(A, b) |
| 22 | +AffineMap(A::StaticMatrix, b::StaticVector) = StaticAffineMap(A, b) |
| 23 | +AffineMap(A::Matrix, b::Vector) = VectorAffineMap(A, b) |
| 24 | +AffineMap(A::UniformScaling{Bool}, b::Number) = ScalarAffineMap(one(b), b) |
| 25 | +AffineMap(A, b) = GenericAffineMap(A, b) |
| 26 | + |
| 27 | +AffineMap{T}(A::Number, b::Number) where {T<:Number} = ScalarAffineMap{T}(A, b) |
| 28 | +AffineMap{T}(A::AbstractMatrix, b::AbstractVector) where {N,S,T<:SVector{N,S}} = StaticAffineMap{S,N}(A, b) |
| 29 | +AffineMap{T}(A::Matrix, b::Vector) where {S,T<:Vector{S}} = VectorAffineMap{S}(A, b) |
| 30 | +AffineMap{T}(A::UniformScaling{Bool}, b::Number) where {T} = ScalarAffineMap{T}(one(T), b) |
| 31 | +AffineMap{T}(A, b) where {T} = GenericAffineMap{T}(A, b) |
| 32 | + |
| 33 | +similarmap(m::AffineMap, ::Type{T}) where {T} = AffineMap{T}(m.A, m.b) |
| 34 | + |
| 35 | +convert(::Type{AffineMap}, m) = (@assert isaffinemap(m); AffineMap(affinematrix(m), affinevector(m))) |
| 36 | +convert(::Type{AffineMap{T}}, m) where {T} = (@assert isaffinemap(m); AffineMap{T}(affinematrix(m), affinevector(m))) |
| 37 | +# avoid ambiguity errors with convert(::Type{T}, x::T) in Base: |
| 38 | +convert(::Type{AffineMap}, m::AffineMap) = m |
| 39 | +convert(::Type{AffineMap{T}}, m::AffineMap{T}) where T = m |
| 40 | + |
| 41 | +# If y = A*x+b, then x = inv(A)*(y-b) = inv(A)*y - inv(A)*b |
| 42 | +inverse(m::AffineMap) = (@assert issquaremap(m); AffineMap(inv(m.A), -inv(m.A)*m.b)) |
| 43 | +inverse(m::AffineMap, x) = (@assert issquaremap(m); m.A \ (x-m.b)) |
| 44 | + |
| 45 | +function leftinverse(m::AffineMap) |
| 46 | + @assert isoverdetermined(m) |
| 47 | + pA = matrix_pinv(m.A) |
| 48 | + AffineMap(pA, -pA*m.b) |
| 49 | +end |
| 50 | +function rightinverse(m::AffineMap) |
| 51 | + @assert isunderdetermined(m) |
| 52 | + pA = matrix_pinv(m.A) |
| 53 | + AffineMap(pA, -pA*m.b) |
| 54 | +end |
| 55 | +function leftinverse(m::AffineMap, x) |
| 56 | + @assert isoverdetermined(m) |
| 57 | + m.A \ (x-m.b) |
| 58 | +end |
| 59 | +function rightinverse(m::AffineMap, x) |
| 60 | + @assert isunderdetermined(m) |
| 61 | + m.A \ (x-m.b) |
| 62 | +end |
| 63 | + |
| 64 | + |
| 65 | +"An affine map for any combination of types of `A` and `b`." |
| 66 | +struct GenericAffineMap{T,AA,B} <: AffineMap{T} |
| 67 | + A :: AA |
| 68 | + b :: B |
| 69 | +end |
| 70 | + |
| 71 | +GenericAffineMap(A, b) = GenericAffineMap{typeof(b)}(A, b) |
| 72 | +GenericAffineMap(A::AbstractVector{S}, b::AbstractVector{T}) where {S,T} = |
| 73 | + GenericAffineMap{promote_type(S,T)}(A, b) |
| 74 | +GenericAffineMap(A::AbstractArray{S}, b::AbstractVector{T}) where {S,T} = |
| 75 | + GenericAffineMap{Vector{promote_type(S,T)}}(A, b) |
| 76 | +GenericAffineMap(A::StaticMatrix{M,N,S}, b::StaticVector{M,T}) where {M,N,S,T} = |
| 77 | + GenericAffineMap{SVector{N,promote_type(S,T)}}(A, b) |
| 78 | +GenericAffineMap(A::StaticMatrix{M,N,S}, b::AbstractVector{T}) where {M,N,S,T} = |
| 79 | + GenericAffineMap{SVector{N,promote_type(S,T)}}(A, b) |
| 80 | +GenericAffineMap(A::S, b::AbstractVector{T}) where {S<:Number,T} = |
| 81 | + GenericAffineMap{Vector{promote_type(S,T)}}(A, b) |
| 82 | +GenericAffineMap(A::S, b::StaticVector{N,T}) where {S<:Number,N,T} = |
| 83 | + GenericAffineMap{SVector{N,promote_type(S,T)}}(A, b) |
| 84 | +GenericAffineMap(A::UniformScaling{Bool}, b) = |
| 85 | + GenericAffineMap(UniformScaling{eltype(b)}(1), b) |
| 86 | + |
| 87 | + |
| 88 | +# Fallback routine for generic A and b, special cases follow |
| 89 | +GenericAffineMap{T}(A, b) where {T} = GenericAffineMap{T,typeof(A),typeof(b)}(A, b) |
| 90 | + |
| 91 | +GenericAffineMap{T}(A::AbstractVector{S}, b::AbstractVector{U}) where {T<:Number,S,U} = |
| 92 | + GenericAffineMap{T}(convert(AbstractVector{T}, A), convert(AbstractVector{T}, b)) |
| 93 | +GenericAffineMap{T}(A::AbstractVector{T}, b::AbstractVector{T}) where {T<:Number} = |
| 94 | + GenericAffineMap{T,typeof(A),typeof(b)}(A, b) |
| 95 | +GenericAffineMap{T}(A::Number, b) where {T} = GenericAffineMap{T,eltype(T),typeof(b)}(A, b) |
| 96 | +GenericAffineMap{T}(A::Number, b::AbstractVector) where {N,S,T <: StaticVector{N,S}} = |
| 97 | + GenericAffineMap{T,S,SVector{N,S}}(A, b) |
| 98 | +# Promote element types of abstract arrays |
| 99 | +GenericAffineMap{T}(A::AbstractArray, b::AbstractVector) where {S,T<:AbstractVector{S}} = |
| 100 | + GenericAffineMap{T}(convert(AbstractArray{eltype(T)},A), convert(AbstractVector{eltype(T)}, b)) |
| 101 | +GenericAffineMap{T}(A::AbstractArray{S}, b::AbstractVector{S}) where {S,T<:AbstractVector{S}} = |
| 102 | + GenericAffineMap{T,typeof(A),typeof(b)}(A, b) |
| 103 | +GenericAffineMap{T}(A::UniformScaling{Bool}, b::AbstractVector) where {S,T<:AbstractVector{S}} = |
| 104 | + GenericAffineMap{T}(A*one(S), convert(AbstractVector{S}, b)) |
| 105 | +GenericAffineMap{T}(A::UniformScaling{S}, b::AbstractVector{S}) where {S,T<:AbstractVector{S}} = |
| 106 | + GenericAffineMap{T,typeof(A),typeof(b)}(A, b) |
| 107 | + |
| 108 | + |
| 109 | +similarmap(m::GenericAffineMap, ::Type{T}) where {T} = AffineMap{T}(m.A, m.b) |
| 110 | + |
| 111 | +convert(::Type{GenericAffineMap{T}}, m::GenericAffineMap) where {T} = |
| 112 | + GenericAffineMap{T}(m.A, m.b) |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +"An affine map with scalar representation." |
| 117 | +struct ScalarAffineMap{T} <: AffineMap{T} |
| 118 | + A :: T |
| 119 | + b :: T |
| 120 | +end |
| 121 | + |
| 122 | +ScalarAffineMap(A, b) = ScalarAffineMap(promote(A, b)...) |
| 123 | + |
| 124 | +isrealmap(m::ScalarAffineMap{T}) where {T} = isrealtype(T) |
| 125 | + |
| 126 | +show(io::IO, m::ScalarAffineMap) = show_scalar_affine_map(io, m.A, m.b) |
| 127 | +show_scalar_affine_map(io, A::Real, b::Real) = print(io, "x -> $(A) * x", b < 0 ? " - " : " + ", abs(b)) |
| 128 | +show_scalar_affine_map(io, A::Complex, b::Complex) = print(io, "x -> ($(A)) * x + ", b) |
| 129 | +show_scalar_affine_map(io, A, b) = print(io, "x -> ($(A)) * x + $(b)") |
| 130 | + |
| 131 | + |
| 132 | +convert(::Type{ScalarAffineMap{T}}, m::ScalarAffineMap) where {T} = |
| 133 | + ScalarAffineMap{T}(m.A, m.b) |
| 134 | + |
| 135 | +"An affine map with array and vector representation." |
| 136 | +struct VectorAffineMap{T} <: AffineMap{Vector{T}} |
| 137 | + A :: Matrix{T} |
| 138 | + b :: Vector{T} |
| 139 | +end |
| 140 | + |
| 141 | +VectorAffineMap(A::AbstractArray{T}, b::AbstractVector{T}) where {T} = |
| 142 | + VectorAffineMap{T}(A, b) |
| 143 | +function VectorAffineMap(A::AbstractArray{S}, b::AbstractVector{T}) where {S,T} |
| 144 | + U = promote_type(S,T) |
| 145 | + VectorAffineMap(convert(AbstractArray{U}, A), convert(AbstractVector{U}, b)) |
| 146 | +end |
| 147 | + |
| 148 | +convert(::Type{VectorAffineMap{T}}, m::VectorAffineMap) where {T} = |
| 149 | + VectorAffineMap{T}(m.A, m.b) |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +"An affine map with representation using static arrays." |
| 154 | +struct StaticAffineMap{T,N,M,L} <: AffineMap{SVector{N,T}} |
| 155 | + A :: SMatrix{M,N,T,L} |
| 156 | + b :: SVector{M,T} |
| 157 | +end |
| 158 | + |
| 159 | +# Constructors: |
| 160 | +# - first, we deduce T |
| 161 | +StaticAffineMap(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T} = |
| 162 | + StaticAffineMap{T}(A, b) |
| 163 | +function StaticAffineMap(A::AbstractMatrix{S}, b::AbstractVector{T}) where {S,T} |
| 164 | + U = promote_type(S,T) |
| 165 | + StaticAffineMap(convert(AbstractMatrix{U}, A), convert(AbstractVector{U}, b)) |
| 166 | +end |
| 167 | + |
| 168 | +StaticAffineMap{T}(A::AbstractMatrix, b::AbstractVector) where {T} = |
| 169 | + StaticAffineMap{T}(convert(AbstractMatrix{T}, A), convert(AbstractVector{T}, b)) |
| 170 | + |
| 171 | +# - then, we determine N and/or M, from the arguments |
| 172 | +function StaticAffineMap{T}(A::AbstractMatrix{T}, b::StaticVector{M,T}) where {T,M} |
| 173 | + @assert size(A) == (M,M) |
| 174 | + StaticAffineMap{T,M,M}(A, b) |
| 175 | +end |
| 176 | +StaticAffineMap{T}(A::StaticMatrix{M,N,T}, b::AbstractVector) where {T,N,M} = |
| 177 | + StaticAffineMap{T,N,M}(A, b) |
| 178 | +StaticAffineMap{T}(A::StaticMatrix{M,N,T}, b::StaticVector{M,T}) where {T,N,M} = |
| 179 | + StaticAffineMap{T,N,M}(A, b) |
| 180 | +# line below catches ambiguity error |
| 181 | +StaticAffineMap{T}(A::StaticMatrix{M1,N,T}, b::StaticVector{M2,T}) where {T,N,M1,M2} = |
| 182 | + throw(ArgumentError("Non-matching dimensions")) |
| 183 | +StaticAffineMap{T,N}(A::AbstractMatrix, b::AbstractVector) where {T,N} = |
| 184 | + StaticAffineMap{T,N,N}(A, b) |
| 185 | +StaticAffineMap{T,N}(A::StaticMatrix{M,N}, b::AbstractVector) where {T,N,M} = |
| 186 | + StaticAffineMap{T,N,M}(A, b) |
| 187 | + |
| 188 | +# - finally invoke the constructor (and implicitly convert the data if necessary) |
| 189 | +StaticAffineMap{T,N,M}(A::AbstractMatrix, b::AbstractVector) where {T,N,M} = |
| 190 | + StaticAffineMap{T,N,M,M*N}(A, b) |
| 191 | + |
| 192 | +convert(::Type{Map{SVector{N,T}}}, m::VectorAffineMap) where {N,T} = |
| 193 | + StaticAffineMap{T,N}(m.A, m.b) |
0 commit comments