Skip to content

Commit

Permalink
use isone/iszero, allow zero-sized uniform scalings
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch committed Sep 26, 2019
1 parent dd84018 commit 67c762d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/LinearMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ Base.length(A::LinearMap) = size(A)[1] * size(A)[2]
Base.:(*)(A::LinearMap, x::AbstractVector) = mul!(similar(x, promote_type(eltype(A), eltype(x)), size(A, 1)), A, x)
function LinearAlgebra.mul!(y::AbstractVector, A::LinearMap, x::AbstractVector, α::Number=true, β::Number=false)
length(y) == size(A, 1) || throw(DimensionMismatch("mul!"))
if α == 1
β == 0 && (A_mul_B!(y, A, x); return y)
β == 1 && (y .+= A * x; return y)
if isone(α)
iszero(β) && (A_mul_B!(y, A, x); return y)
isone(β) && (y .+= A * x; return y)
# β != 0, 1
rmul!(y, β)
y .+= A * x
return y
elseif α == 0
β == 0 && (fill!(y, zero(eltype(y))); return y)
β == 1 && return y
elseif iszero(α)
iszero(β) && (fill!(y, zero(eltype(y))); return y)
isone(β) && return y
# β != 0, 1
rmul!(y, β)
return y
else # α != 0, 1
β == 0 && (A_mul_B!(y, A, x); rmul!(y, α); return y)
β == 1 && (y .+= rmul!(A * x, α); return y)
iszero(β) && (A_mul_B!(y, A, x); rmul!(y, α); return y)
isone(β) && (y .+= rmul!(A * x, α); return y)
# β != 0, 1
rmul!(y, β)
y .+= rmul!(A * x, α)
Expand Down
2 changes: 1 addition & 1 deletion src/uniformscalingmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct UniformScalingMap{T} <: LinearMap{T}
λ::T
M::Int
function UniformScalingMap::Number, M::Int)
M 0 && throw(ArgumentError("size of UniformScalingMap must be (strictly) positive, got $M"))
M < 0 && throw(ArgumentError("size of UniformScalingMap must be non-negative, got $M"))
return new{typeof(λ)}(λ, M)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/uniformscalingmap.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Test, LinearMaps, LinearAlgebra, BenchmarkTools

@testset "identity/scaling map" begin
@test_throws ArgumentError LinearMaps.UniformScalingMap(true, 0)
@test_throws ArgumentError LinearMaps.UniformScalingMap(true, -1)
A = 2 * rand(ComplexF64, (10, 10)) .- 1
B = rand(size(A)...)
M = @inferred 1 * LinearMap(A)
Expand Down

0 comments on commit 67c762d

Please sign in to comment.