Skip to content

Commit fd4100b

Browse files
authored
Fix getindex for LazyMap with Vector (#272)
* Fix getindex for LazyMap with Vector * Fix format * Fix * import as
1 parent 0268a1c commit fd4100b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/lazy_iterators.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Iterator over the elements of `data` mapped by `f`. This is similar to
3232
`Base.Generator(f, data)` except that the `eltype` of a `LazyMap` is given at
3333
construction while the `eltype` of `Base.Generator(f, data)` is `Any`.
3434
"""
35-
struct LazyMap{T,VT,F} <: AbstractVector{T}
35+
struct LazyMap{T,VT<:AbstractVector,F} <: AbstractVector{T}
3636
f::F
3737
data::VT
3838
end
@@ -58,7 +58,10 @@ Base.IteratorSize(it::LazyMap) = Base.IteratorSize(it.data)
5858

5959
Base.eltype(::LazyMap{T}) where {T} = T
6060

61-
Base.getindex(it::LazyMap, i) = it.f(getindex(it.data, i))
61+
Base.getindex(it::LazyMap, i::Integer) = it.f(getindex(it.data, i))
62+
function Base.getindex(it::LazyMap{T}, I::AbstractVector) where {T}
63+
return LazyMap{T}(it.f, getindex(it.data, I))
64+
end
6265

6366
Base.eachindex(it::LazyMap) = Base.eachindex(it.data)
6467
Base.lastindex(it::LazyMap) = Base.lastindex(it.data)

test/polynomial.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Test
22
using LinearAlgebra
3-
import MutableArithmetics
4-
const MA = MutableArithmetics
3+
import MutableArithmetics as MA
54
using MultivariatePolynomials
65
const MP = MultivariatePolynomials
76
@testset "Polynomial" begin
@@ -108,11 +107,15 @@ const MP = MultivariatePolynomials
108107
@test collect(coefficients(4x^2 * y + x * y + 2x)) == [2, 1, 4]
109108
@test collect(coefficients(4x^2 * y + x * y + 2x + 3, [x, 1, x * y, y])) ==
110109
[2, 3, 1, 0]
110+
@test monomials(4x^2 * y + x * y + 2x + 3)[1:1] ==
111+
[constant_monomial(x * y)]
111112

112113
for p in [polynomial([4, 9], [x, x * x]), polynomial([9, 4], [x * x, x])]
113114
@test collect(coefficients(p)) == [4, 9]
114115
@test monomials(p)[1] == x
115116
@test monomials(p)[2] == x^2
117+
@test monomials(p)[1:2][1] == x
118+
@test monomials(p)[1:2][2] == x^2
116119
@test p == dot([4, 9], [x, x * x])
117120
end
118121

0 commit comments

Comments
 (0)