From 97de6be50b68fb0cb6e8a3059ddc1004a73b993b Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Sat, 1 Jun 2019 19:51:30 -0500 Subject: [PATCH] Add getindex and setindex! for StepRanges --- src/auxiliary.jl | 60 +++++++++++++++++++++++++++++++++---------- test/manyvariables.jl | 21 ++++++++++++--- test/onevariable.jl | 21 +++++++++++---- 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/auxiliary.jl b/src/auxiliary.jl index 66a8a0c0..fe2e4485 100644 --- a/src/auxiliary.jl +++ b/src/auxiliary.jl @@ -74,14 +74,15 @@ Return the coefficient of order `n::Int` of a `a::Taylor1` polynomial. getcoeff(a::Taylor1, n::Int) = (@assert 0 ≤ n ≤ a.order; return a[n]) getindex(a::Taylor1, n::Int) = a.coeffs[n+1] -getindex(a::Taylor1, u::UnitRange) = view(a.coeffs, u .+ 1 ) +getindex(a::Taylor1, u::UnitRange{Int}) = view(a.coeffs, u .+ 1 ) getindex(a::Taylor1, c::Colon) = view(a.coeffs, c) +getindex(a::Taylor1{T}, u::StepRange{Int,Int}) where {T<:Number} = + view(a.coeffs, u[:] .+ 1) setindex!(a::Taylor1{T}, x::T, n::Int) where {T<:Number} = a.coeffs[n+1] = x -setindex!(a::Taylor1{T}, x::T, u::UnitRange) where {T<:Number} = +setindex!(a::Taylor1{T}, x::T, u::UnitRange{Int}) where {T<:Number} = a.coeffs[u .+ 1] .= x -function setindex!(a::Taylor1{T}, x::Array{T,1}, u::UnitRange) where {T<:Number} - # a.coeffs[u .+ 1] .= x +function setindex!(a::Taylor1{T}, x::Array{T,1}, u::UnitRange{Int}) where {T<:Number} @assert length(u) == length(x) for ind in eachindex(x) a.coeffs[u[ind]+1] = x[ind] @@ -89,6 +90,14 @@ function setindex!(a::Taylor1{T}, x::Array{T,1}, u::UnitRange) where {T<:Number} end setindex!(a::Taylor1{T}, x::T, c::Colon) where {T<:Number} = a.coeffs[c] .= x setindex!(a::Taylor1{T}, x::Array{T,1}, c::Colon) where {T<:Number} = a.coeffs[c] .= x +setindex!(a::Taylor1{T}, x::T, u::StepRange{Int,Int}) where {T<:Number} = + a.coeffs[u[:] .+ 1] .= x +function setindex!(a::Taylor1{T}, x::Array{T,1}, u::StepRange{Int,Int}) where {T<:Number} + @assert length(u) == length(x) + for ind in eachindex(x) + a.coeffs[u[ind]+1] = x[ind] + end +end """ @@ -107,19 +116,25 @@ end getcoeff(a::HomogeneousPolynomial, v::Array{Int,1}) = getcoeff(a, (v...,)) getindex(a::HomogeneousPolynomial, n::Int) = a.coeffs[n] -getindex(a::HomogeneousPolynomial, n::UnitRange) = view(a.coeffs, n) +getindex(a::HomogeneousPolynomial, n::UnitRange{Int}) = view(a.coeffs, n) getindex(a::HomogeneousPolynomial, c::Colon) = view(a.coeffs, c) +getindex(a::HomogeneousPolynomial, u::StepRange{Int,Int}) where {T<:Number} = + view(a.coeffs, u[:]) setindex!(a::HomogeneousPolynomial{T}, x::T, n::Int) where {T<:Number} = a.coeffs[n] = x -setindex!(a::HomogeneousPolynomial{T}, x::T, n::UnitRange) where {T<:Number} = +setindex!(a::HomogeneousPolynomial{T}, x::T, n::UnitRange{Int}) where {T<:Number} = a.coeffs[n] .= x -setindex!(a::HomogeneousPolynomial{T}, x::Array{T,1}, n::UnitRange) where {T<:Number} = +setindex!(a::HomogeneousPolynomial{T}, x::Array{T,1}, n::UnitRange{Int}) where {T<:Number} = a.coeffs[n] .= x setindex!(a::HomogeneousPolynomial{T}, x::T, c::Colon) where {T<:Number} = a.coeffs[c] .= x setindex!(a::HomogeneousPolynomial{T}, x::Array{T,1}, c::Colon) where {T<:Number} = a.coeffs[c] = x +setindex!(a::HomogeneousPolynomial{T}, x::T, u::StepRange{Int,Int}) where {T<:Number} = + a.coeffs[u[:]] .= x +setindex!(a::HomogeneousPolynomial{T}, x::Array{T,1}, u::StepRange{Int,Int}) where {T<:Number} = + a.coeffs[u[:]] .= x[:] """ @@ -137,8 +152,10 @@ end getcoeff(a::TaylorN, v::Array{Int,1}) = getcoeff(a, (v...,)) getindex(a::TaylorN, n::Int) = a.coeffs[n+1] -getindex(a::TaylorN, u::UnitRange) = view(a.coeffs, u .+ 1) +getindex(a::TaylorN, u::UnitRange{Int}) = view(a.coeffs, u .+ 1) getindex(a::TaylorN, c::Colon) = view(a.coeffs, c) +getindex(a::TaylorN, u::StepRange{Int,Int}) where {T<:Number} = + view(a.coeffs, u[:] .+ 1) function setindex!(a::TaylorN{T}, x::HomogeneousPolynomial{T}, n::Int) where {T<:Number} @@ -147,21 +164,19 @@ function setindex!(a::TaylorN{T}, x::HomogeneousPolynomial{T}, n::Int) where end setindex!(a::TaylorN{T}, x::T, n::Int) where {T<:Number} = a.coeffs[n+1] = HomogeneousPolynomial(x, n) -function setindex!(a::TaylorN{T}, x::T, u::UnitRange) where {T<:Number} +function setindex!(a::TaylorN{T}, x::T, u::UnitRange{Int}) where {T<:Number} for ind in u a[ind] = x end a[u] end -function setindex!(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, u::UnitRange) where {T<:Number} - # a[u[:]] .= x[:] +function setindex!(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, u::UnitRange{Int}) where {T<:Number} @assert length(u) == length(x) for ind in eachindex(x) a[u[ind]] = x[ind] end end -function setindex!(a::TaylorN{T}, x::Array{T,1}, u::UnitRange) where {T<:Number} - # a[u] .= x +function setindex!(a::TaylorN{T}, x::Array{T,1}, u::UnitRange{Int}) where {T<:Number} @assert length(u) == length(x) for ind in eachindex(x) a[u[ind]] = x[ind] @@ -173,6 +188,25 @@ setindex!(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, ::Colon) where {T<:Number} = (a[0:end] = x; a[:]) setindex!(a::TaylorN{T}, x::Array{T,1}, ::Colon) where {T<:Number} = (a[0:end] = x; a[:]) +function setindex!(a::TaylorN{T}, x::T, u::StepRange{Int,Int}) where {T<:Number} + for ind in u + a[ind] = x + end + a[u] +end +function setindex!(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, u::StepRange{Int,Int}) where {T<:Number} + # a[u[:]] .= x[:] + @assert length(u) == length(x) + for ind in eachindex(x) + a[u[ind]] = x[ind] + end +end +function setindex!(a::TaylorN{T}, x::Array{T,1}, u::StepRange{Int,Int}) where {T<:Number} + @assert length(u) == length(x) + for ind in eachindex(x) + a[u[ind]] = x[ind] + end +end ## eltype, length, get_order ## diff --git a/test/manyvariables.jl b/test/manyvariables.jl index ea787ced..e70cc173 100644 --- a/test/manyvariables.jl +++ b/test/manyvariables.jl @@ -71,10 +71,16 @@ eeuler = Base.MathConstants.e @test typeof(TaylorSeries.resize_coeffsHP!(v,2)) == Nothing @test v == [1,2,0] @test_throws AssertionError TaylorSeries.resize_coeffsHP!(v,1) - HomogeneousPolynomial(v)[3] = 3 + hpol_v = HomogeneousPolynomial(v) + hpol_v[3] = 3 @test v == [1,2,3] - HomogeneousPolynomial(v)[1:3] = 3 + hpol_v[1:3] = 3 @test v == [3,3,3] + hpol_v[1:2:2] = 0 + @test v == [0,3,3] + hpol_v[1:1:2] = [1,2] + @test all(hpol_v[1:1:2] .== [1,2]) + @test v == [1,2,3] xH = HomogeneousPolynomial([1,0]) yH = HomogeneousPolynomial([0,1],1) @@ -241,6 +247,9 @@ eeuler = Base.MathConstants.e rv = a[end][1:end] @test a[end][1:end] == rv @test a[end][1:end] != b[end][1:end] + @test a[0:2:end] == a.coeffs[1:2:end] + a[0:1:end] = 0.0 + @test a == zero(a) hp = HomogeneousPolynomial(1)^8 rv1 = rand( length(hp) ) @@ -257,6 +266,7 @@ eeuler = Base.MathConstants.e @test hp[end-1:end] == rv1[end-1:end] hp[:] = 0.0 @test hp[:] == zero(rv1) + @test all(hp[end-1:1:end] .== 0.0) pol = sin(xT+yT*xT)+yT^2-(1-xT)^3 q = deepcopy(pol) @@ -295,7 +305,7 @@ eeuler = Base.MathConstants.e q[:] = pol.coeffs q[1:end-1] = zeros(q.order+1)[2:end-1] @test q != pol - @test q[:] != pol[:] + @test all(q[1:1:end-1] .== 0.0) @test q[1:end-1] == zeros(q.order+1)[2:end-1] @test q[0] == pol[0] @test q[end] == pol[end] @@ -305,6 +315,11 @@ eeuler = Base.MathConstants.e @test q[0:1] == pol[0:1] @test q[2:end-2] == pol2[2:end-2] @test q[end-1:end] == pol[end-1:end] + @test q[2:2:end-2] == pol2[2:2:end-2] + @test q[end-1:1:end] == pol[end-1:1:end] + q[end-2:2:end] = [0.0, 0.0] + @test q[end-2] == 0.0 + @test_throws AssertionError q[end-2:2:end] = [0.0, 0.0, 0.0] @test_throws AssertionError yT^(-2) @test_throws AssertionError yT^(-2.0) diff --git a/test/onevariable.jl b/test/onevariable.jl index 32b064d4..b9b2ff3f 100644 --- a/test/onevariable.jl +++ b/test/onevariable.jl @@ -43,14 +43,20 @@ eeuler = Base.MathConstants.e TaylorSeries.resize_coeffs1!(v,3) setindex!(Taylor1(v),3,2) @test v == [1,0,3,0] - @test Taylor1(v)[:] == [1,0,3,0] - @test Taylor1(v)[:] == Taylor1(v).coeffs[:] - setindex!(Taylor1(v),0,0:2) + pol_int = Taylor1(v) + @test pol_int[:] == [1,0,3,0] + @test pol_int[:] == pol_int.coeffs[:] + @test pol_int[1:2:3] == pol_int.coeffs[2:2:4] + setindex!(pol_int,0,0:2) @test v == zero(v) - setindex!(Taylor1(v),1,:) + setindex!(pol_int,1,:) @test v == ones(Int, 4) - Taylor1(v)[:] = 0 + pol_int[:] = 0 @test v == zero(v) + pol_int[0:2:end] = 2 + @test all(v[1:2:end] .== 2) + pol_int[0:2:3] = [0, 1] + @test all(v[1:2:3] .== [0, 1]) rv = [rand(0:3) for i in 1:4] @test Taylor1(rv)[:] == rv y = sin(Taylor1(16)) @@ -79,6 +85,11 @@ eeuler = Base.MathConstants.e rv = rand.(length(y)) y[:] = rv @test y[:] == rv + y[0:4:end] = 1.0 + @test all(y.coeffs[1:4:end] .== 1.0) + y[0:2:8] = rv[1:5] + @test y.coeffs[1:2:9] == rv[1:5] + @test_throws AssertionError y[0:2:3] = rv @test Taylor1([0,1,0,0]) == Taylor1(3) @test getcoeff(Taylor1(Complex{Float64},3),1) == complex(1.0,0.0)