Skip to content

Commit

Permalink
Fix getindex for LazyMap with Vector (#272)
Browse files Browse the repository at this point in the history
* Fix getindex for LazyMap with Vector

* Fix format

* Fix

* import as
  • Loading branch information
blegat authored Jul 20, 2023
1 parent 0268a1c commit fd4100b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/lazy_iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Iterator over the elements of `data` mapped by `f`. This is similar to
`Base.Generator(f, data)` except that the `eltype` of a `LazyMap` is given at
construction while the `eltype` of `Base.Generator(f, data)` is `Any`.
"""
struct LazyMap{T,VT,F} <: AbstractVector{T}
struct LazyMap{T,VT<:AbstractVector,F} <: AbstractVector{T}
f::F
data::VT
end
Expand All @@ -58,7 +58,10 @@ Base.IteratorSize(it::LazyMap) = Base.IteratorSize(it.data)

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

Base.getindex(it::LazyMap, i) = it.f(getindex(it.data, i))
Base.getindex(it::LazyMap, i::Integer) = it.f(getindex(it.data, i))
function Base.getindex(it::LazyMap{T}, I::AbstractVector) where {T}
return LazyMap{T}(it.f, getindex(it.data, I))
end

Base.eachindex(it::LazyMap) = Base.eachindex(it.data)
Base.lastindex(it::LazyMap) = Base.lastindex(it.data)
Expand Down
7 changes: 5 additions & 2 deletions test/polynomial.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Test
using LinearAlgebra
import MutableArithmetics
const MA = MutableArithmetics
import MutableArithmetics as MA
using MultivariatePolynomials
const MP = MultivariatePolynomials
@testset "Polynomial" begin
Expand Down Expand Up @@ -108,11 +107,15 @@ const MP = MultivariatePolynomials
@test collect(coefficients(4x^2 * y + x * y + 2x)) == [2, 1, 4]
@test collect(coefficients(4x^2 * y + x * y + 2x + 3, [x, 1, x * y, y])) ==
[2, 3, 1, 0]
@test monomials(4x^2 * y + x * y + 2x + 3)[1:1] ==
[constant_monomial(x * y)]

for p in [polynomial([4, 9], [x, x * x]), polynomial([9, 4], [x * x, x])]
@test collect(coefficients(p)) == [4, 9]
@test monomials(p)[1] == x
@test monomials(p)[2] == x^2
@test monomials(p)[1:2][1] == x
@test monomials(p)[1:2][2] == x^2
@test p == dot([4, 9], [x, x * x])
end

Expand Down

0 comments on commit fd4100b

Please sign in to comment.