1
- struct ScaledMap{T, S<: RealOrComplex , A<: LinearMap } <: LinearMap{T}
1
+ """
2
+ Lazy representation of a scaled map `λ * A = A * λ` with real or complex map
3
+ `A <: LinearMap{RealOrComplex}` and real or complex scaling factor
4
+ `λ <: RealOrComplex`.
5
+ """
6
+ struct ScaledMap{T, S<: RealOrComplex , L<: LinearMap } <: LinearMap{T}
2
7
λ:: S
3
- lmap:: A
4
- function ScaledMap {T} (λ:: S , lmap :: A ) where {T, S <: RealOrComplex , A <: LinearMap }
5
- @assert Base. promote_op (* , S, eltype (lmap )) == T " target type $T cannot hold products of $S and $(eltype (lmap )) objects"
6
- new {T,S,A } (λ, lmap )
8
+ lmap:: L
9
+ function ScaledMap {T} (λ:: S , A :: L ) where {T, S <: RealOrComplex , L <: LinearMap{<:RealOrComplex} }
10
+ @assert Base. promote_op (* , S, eltype (A )) == T " target type $T cannot hold products of $S and $(eltype (A )) objects"
11
+ new {T,S,L } (λ, A )
7
12
end
8
13
end
9
14
10
15
# constructor
11
- ScaledMap (λ:: S , lmap:: A ) where {S <: RealOrComplex ,A <: LinearMap } =
12
- ScaledMap {Base.promote_op(*, S , eltype(lmap))} (λ, lmap)
16
+ ScaledMap (λ:: RealOrComplex , lmap:: LinearMap{ <:RealOrComplex} ) =
17
+ ScaledMap {Base.promote_op(*, typeof(λ) , eltype(lmap))} (λ, lmap)
13
18
14
19
# basic methods
15
20
Base. size (A:: ScaledMap ) = size (A. lmap)
@@ -26,8 +31,8 @@ Base.:(==)(A::ScaledMap, B::ScaledMap) =
26
31
eltype (A) == eltype (B) && A. lmap == B. lmap && A. λ == B. λ
27
32
28
33
# scalar multiplication and division
29
- Base.:(* )(α:: RealOrComplex , A:: LinearMap ) = ScaledMap (α, A)
30
- Base.:(* )(A:: LinearMap , α:: RealOrComplex ) = ScaledMap (α, A)
34
+ Base.:(* )(α:: RealOrComplex , A:: LinearMap{<:RealOrComplex} ) = ScaledMap (α, A)
35
+ Base.:(* )(A:: LinearMap{<:RealOrComplex} , α:: RealOrComplex ) = ScaledMap (α, A)
31
36
32
37
Base.:(* )(α:: Number , A:: ScaledMap ) = (α * A. λ) * A. lmap
33
38
Base.:(* )(A:: ScaledMap , α:: Number ) = A. lmap * (A. λ * α)
@@ -42,13 +47,19 @@ Base.:(*)(A::ScaledMap, B::LinearMap) = A.λ * (A.lmap * B)
42
47
Base.:(* )(A:: LinearMap , B:: ScaledMap ) = (A * B. lmap) * B. λ
43
48
44
49
# multiplication with vectors/matrices
45
- for (In, Out) in ((AbstractVector, AbstractVecOrMat), (AbstractMatrix, AbstractMatrix))
50
+ for (In, Out) in ((AbstractVector, AbstractVecOrMat),
51
+ (AbstractMatrix, AbstractMatrix))
46
52
@eval begin
47
- function _unsafe_mul! (y:: $Out , A:: ScaledMap , x:: $In )
53
+ # commutative case
54
+ function _unsafe_mul! (y:: $Out , A:: ScaledMap , x:: $In{<:RealOrComplex} )
48
55
return _unsafe_mul! (y, A. lmap, x, A. λ, false )
49
56
end
50
- function _unsafe_mul! (y:: $Out , A:: ScaledMap , x:: $In , α:: Number , β:: Number )
57
+ function _unsafe_mul! (y:: $Out , A:: ScaledMap , x:: $In{<:RealOrComplex} , α:: Number , β:: Number )
51
58
return _unsafe_mul! (y, A. lmap, x, A. λ * α, β)
52
59
end
60
+ # non-commutative case
61
+ function _unsafe_mul! (y:: $Out , A:: ScaledMap , x:: $In )
62
+ return lmul! (A. λ, _unsafe_mul! (y, A. lmap, x))
63
+ end
53
64
end
54
65
end
0 commit comments