Skip to content

Commit 5dc045a

Browse files
authored
Overload lmul!/rmul! (#92)
* Overload lmul!/rmul! * add comment
1 parent 03087c3 commit 5dc045a

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "0.8.7"
3+
version = "0.8.8"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
77
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero
88

99
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
10-
norm2, norm1, normInf, normMinusInf, normp
10+
norm2, norm1, normInf, normMinusInf, normp, lmul!, rmul!
1111

1212
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
1313

src/fillalgebra.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,24 @@ norm2(a::AbstractFill) = sqrt(length(a))*norm(getindex_value(a))
211211
normp(a::AbstractFill, p) = (length(a))^(1/p)*norm(getindex_value(a))
212212
normInf(a::AbstractFill) = norm(getindex_value(a))
213213
normMinusInf(a::AbstractFill) = norm(getindex_value(a))
214+
215+
216+
###
217+
# lmul!/rmul!
218+
###
219+
220+
function lmul!(x::Number, z::AbstractFill)
221+
λ = getindex_value(z)
222+
# Following check ensures consistency w/ lmul!(x, Array(z))
223+
# for, e.g., lmul!(NaN, z)
224+
x*λ == λ || throw(ArgumentError("Cannot scale by $x"))
225+
z
226+
end
227+
228+
function rmul!(z::AbstractFill, x::Number)
229+
λ = getindex_value(z)
230+
# Following check ensures consistency w/ lmul!(x, Array(z))
231+
# for, e.g., lmul!(NaN, z)
232+
λ*x == λ || throw(ArgumentError("Cannot scale by $x"))
233+
z
234+
end

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,3 +952,17 @@ end
952952
@test reshape(Fill(2,2,3),Val(1)) Fill(2,6)
953953
@test reshape(Fill(2, 2), (2, )) Fill(2, 2)
954954
end
955+
956+
@testset "lmul!/rmul!" begin
957+
z = Zeros(1_000_000_000_000)
958+
@test lmul!(2.0,z) === z
959+
@test rmul!(z,2.0) === z
960+
@test_throws ArgumentError lmul!(Inf,z)
961+
@test_throws ArgumentError rmul!(z,Inf)
962+
963+
x = Fill([1,2],1_000_000_000_000)
964+
@test lmul!(1.0,x) === x
965+
@test rmul!(x,1.0) === x
966+
@test_throws ArgumentError lmul!(2.0,x)
967+
@test_throws ArgumentError rmul!(x,2.0)
968+
end

0 commit comments

Comments
 (0)