Skip to content

Commit

Permalink
Add getindex, setindex! methods for ::Colon (#101)
Browse files Browse the repository at this point in the history
* Add getindex, setindex! methods with ::Colon; add tests

* Add corresponding methods for TaylorN, HomogeneousPolynomials with tests; add new ::UnitRange setindex! method for Taylor1

* Fix typo

* Turn getindex using `view` (for ::UnitRange and ::Colon); fix and add tests

* Add missing tests for setindex! (Taylor1)

* Add setindex! tests for HomogeneousPolynomial

* Add missing setindex! test for HomogeneousPolynomial

* Add 4 setindex! tests (TaylorN); 4 tests still pending

* Add pending setindex! tests for TaylorN

* Add another test for `setindex!`
  • Loading branch information
PerezHz authored and lbenet committed Mar 30, 2017
1 parent 0697deb commit 47b201c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ function getindex(a::Taylor1, n::Int)
(1 n length(a.coeffs)) && return a.coeffs[n]
return zero(a.coeffs[1])
end
getindex(a::Taylor1, n::UnitRange) = a.coeffs[n]
getindex(a::Taylor1, n::UnitRange) = view(a.coeffs, n)
getindex(a::Taylor1, c::Colon) = view(a.coeffs, c)
setindex!{T<:Number}(a::Taylor1{T}, x::T, n::Int) = a.coeffs[n] = x
setindex!{T<:Number}(a::Taylor1{T}, x::T, n::UnitRange) = a.coeffs[n] = x

setindex!{T<:Number}(a::Taylor1{T}, x::Array{T,1}, n::UnitRange) = a.coeffs[n] = x
setindex!{T<:Number}(a::Taylor1{T}, x::T, c::Colon) = a.coeffs[c] = x
setindex!{T<:Number}(a::Taylor1{T}, x::Array{T,1}, c::Colon) = a.coeffs[c] = x


"""
Expand All @@ -95,11 +98,18 @@ function get_coeff(a::HomogeneousPolynomial, v::Array{Int,1})
end

getindex(a::HomogeneousPolynomial, n::Int) = a.coeffs[n]
getindex(a::HomogeneousPolynomial, n::UnitRange) = a.coeffs[n]
getindex(a::HomogeneousPolynomial, n::UnitRange) = view(a.coeffs, n)
getindex(a::HomogeneousPolynomial, c::Colon) = view(a.coeffs, c)
setindex!{T<:Number}(a::HomogeneousPolynomial{T}, x::T, n::Int) =
a.coeffs[n] = x
setindex!{T<:Number}(a::HomogeneousPolynomial{T}, x::T, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::HomogeneousPolynomial{T}, x::Array{T,1}, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::HomogeneousPolynomial{T}, x::T, c::Colon) =
a.coeffs[c] = x
setindex!{T<:Number}(a::HomogeneousPolynomial{T}, x::Array{T,1}, c::Colon) =
a.coeffs[c] = x


"""
Expand All @@ -119,7 +129,8 @@ function getindex(a::TaylorN, n::Int)
n get_order()+1 && return zero_korder(a, n-1)
throw(BoundsError(a.coeffs,n))
end
getindex(a::TaylorN, n::UnitRange) = a.coeffs[n]
getindex(a::TaylorN, n::UnitRange) = view(a.coeffs, n)
getindex(a::TaylorN, c::Colon) = view(a.coeffs, c)
setindex!{T<:Number}(a::TaylorN{T}, x::HomogeneousPolynomial{T}, n::Int) =
a.coeffs[n] = x
setindex!{T<:Number}(a::TaylorN{T}, x::T, n::Int) =
Expand All @@ -128,6 +139,18 @@ setindex!{T<:Number}(a::TaylorN{T}, x::HomogeneousPolynomial{T}, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::TaylorN{T}, x::T, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::TaylorN{T}, x::Array{T,1}, n::UnitRange) =
a.coeffs[n] = x
setindex!{T<:Number}(a::TaylorN{T}, x::HomogeneousPolynomial{T}, c::Colon) =
a.coeffs[c] = x
setindex!{T<:Number}(a::TaylorN{T}, x::T, c::Colon) =
a.coeffs[c] = x
setindex!{T<:Number}(a::TaylorN{T}, x::Array{HomogeneousPolynomial{T},1}, c::Colon) =
a.coeffs[c] = x
setindex!{T<:Number}(a::TaylorN{T}, x::Array{T,1}, c::Colon) =
a.coeffs[c] = x


## eltype, length, endof, get_order ##
Expand Down
84 changes: 84 additions & 0 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,90 @@ using Base.Test
@test xT*xT^3 == xT^4
txy = 1.0 + xT*yT - 0.5*xT^2*yT + (1/3)*xT^3*yT + 0.5*xT^2*yT^2
@test getindex((1+TaylorN(1))^TaylorN(2),1:5) == txy.coeffs[1:5]
@test ( (1+TaylorN(1))^TaylorN(2) )[:] == ( (1+TaylorN(1))^TaylorN(2) ).coeffs[:]
@test txy.coeffs[:] == txy[:]
@test txy.coeffs[:] == txy[1:end]
txy[:] .= ( -1.0 + 3xT*yT - xT^2*yT + (4/3)*xT^3*yT + (1/3)*xT*yT^3 + 0.5*xT^2*yT^2 + 0.5*xT*yT^3 )[:]
@test txy[:] == ( -1.0 + 3xT*yT - xT^2*yT + (4/3)*xT^3*yT + (1/3)*xT*yT^3 + 0.5*xT^2*yT^2 + 0.5*xT*yT^3 )[:]
txy[2:end-1] .= ( 1.0 - xT*yT + 0.5*xT^2*yT - (2/3)*xT*yT^3 - 0.5*xT^2*yT^2 + 7*xT^3*yT )[2:end-1]
@test txy[2:end-1] == ( 1.0 - xT*yT + 0.5*xT^2*yT - (2/3)*xT*yT^3 - 0.5*xT^2*yT^2 + 7*xT^3*yT )[2:end-1]

a = -5.0 + sin(xT+yT^2)
b = deepcopy(a)
@test a[:] == a[1:end]
@test a[:] == b[:]
@test a[1:end] == b[1:end]
@test a[end][:] == b[end][:]
@test a[end][1:end] == b[end][1:end]
rv = a[end][:] .= rand.()
@test a[end][:] == rv
@test a[end][:] != b[end][:]
rv = a[end][1:end] .= rand.()
@test a[end][1:end] == rv
@test a[end][1:end] != b[end][1:end]

hp = HomogeneousPolynomial(1)^8
rv1 = rand( length(hp) )
hp[:] = rv1
@test rv1 == hp[:]
rv2 = rand( length(hp)-2 )
hp[1:end-2] = rv2
@test hp[1:end-2] == rv2
@test hp[end-1:end] == rv1[end-1:end]
hp[3:4] = 0.0
@test hp[1:2] == rv2[1:2]
@test hp[3:4] == zeros(2)
@test hp[5:end-2] == rv2[5:end]
@test hp[end-1:end] == rv1[end-1:end]
hp[:] = 0.0
@test hp[:] == zero(rv1)

pol = sin(xT+yT*xT)+yT^2-(1-xT)^3
q = deepcopy(pol)
q[:] = 0.0
@test q[:] == zero(q[:])
q[:] = pol.coeffs
@test q == pol
@test q[:] == pol[:]
q[2:end-1] = 0.0
@test q[2:end-1] == zero(q[2:end-1])
@test q[1] == pol[1]
@test q[end] == pol[end]
q[:] = pol.coeffs
zH0 = zero(HomogeneousPolynomial{Float64})
q[:] = zH0
@test q[:] == zero(q[:])
q[:] = pol.coeffs
q[2:end-1] = zH0
@test q[2:end-1] == zero(q[2:end-1])
@test q[1] == pol[1]
@test q[end] == pol[end]
q[:] = pol.coeffs
zHall = zeros(HomogeneousPolynomial{Float64}, q.order)
q[:] = zHall
@test q[:] == zHall
q[:] = pol.coeffs
q[2:end-1] = zHall[2:end-1]
@test q[2:end-1] == zHall[2:end-1]
q[:] = pol.coeffs
@test q[:] != zeros(q.order+1)
q[:] = zeros(q.order+1)
@test q[:] == zeros(q.order+1)
q[:] = pol.coeffs
q[2:end-1] = zeros(q.order+1)[2:end-1]
@test q != pol
@test q[:] != pol[:]
@test q[2:end-1] == zeros(q.order+1)[2:end-1]
@test q[1] == pol[1]
@test q[end] == pol[end]
q[:] = pol.coeffs
pol2 = cos(sin(xT)-yT^3*xT)-3yT^2+sqrt(1-xT)
q[3:end-2] = pol2.coeffs[3:end-2]
@test q[1:2] == pol[1:2]
@test q[3:end-2] == pol2[3:end-2]
@test q[end-1:end] == pol[end-1:end]


@test_throws DomainError yT^(-2)
@test_throws DomainError yT^(-2.0)
@test (1+xT)^(3//2) == ((1+xT)^0.5)^3
Expand Down
2 changes: 1 addition & 1 deletion test/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using TaylorSeries
using Base.Test

@testset "Tests with mixures of Taylor1 and TaylorN" begin
@testset "Tests with mixtures of Taylor1 and TaylorN" begin
@test TaylorSeries.NumberNotSeries == Union{Real,Complex}
@test TaylorSeries.NumberNotSeriesN == Union{Real,Complex,Taylor1}

Expand Down
32 changes: 32 additions & 0 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,40 @@ using Base.Test
@test v == [1,2,0,0]
setindex!(Taylor1(v),3,3)
@test v == [1,2,3,0]
@test Taylor1(v)[:] == [1,2,3,0]
@test Taylor1(v)[:] == Taylor1(v).coeffs[:]
setindex!(Taylor1(v),0,1:3)
@test v == zero(v)
setindex!(Taylor1(v),1,:)
@test v == [1,1,1,1]
Taylor1(v)[:] = 0
@test v == zero(v)
rv = [rand(0:3) for i in 1:4]
@test Taylor1(rv)[:] == Taylor1(rv)[1:end]
y = sin(Taylor1(16))
@test y[:] == sin(Taylor1(16))[:]
y[:] .= cos(Taylor1(16))[:]
@test y == cos(Taylor1(16))
@test y[:] == cos(Taylor1(16))[:]
y = sin(Taylor1(16))
rv = rand(5)
y[1:5] = rv
@test y[1:5] == rv
@test y[6:end] == sin(Taylor1(16))[6:end]
rv = rand( length(y.coeffs) )
y[:] = rv
@test y[:] == rv
y[:] = cos(Taylor1(16)).coeffs
@test y == cos(Taylor1(16))
@test y[:] == cos(Taylor1(16))[:]
y[:] = 0.0
@test y[:] == zero(y[:])
y = sin(Taylor1(16))
rv = y[1:5] .= rand.()
@test y[1:5] == rv
@test y[6:end] == sin(Taylor1(16))[6:end]
rv = y[:] .= rand.()
@test y[:] == rv

@test Taylor1([0,1,0,0]) == Taylor1(3)
@test get_coeff(Taylor1(Complex128,3),1) == complex(1.0,0.0)
Expand Down

0 comments on commit 47b201c

Please sign in to comment.