Skip to content

Commit

Permalink
Add methods for getcoeff involving tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Benet committed Apr 15, 2018
1 parent 82b1e22 commit c3b660c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/src/examples.md
Expand Up @@ -112,7 +112,7 @@ We note that the above functions use expansions in `Int128`. This is actually
required, since some coefficients are larger than `typemax(Int)`:

```@repl fateman
getcoeff(f2, [1,6,7,20]) # coefficient of x y^6 z^7 w^{20}
getcoeff(f2, (1,6,7,20)) # coefficient of x y^6 z^7 w^{20}
ans > typemax(Int)
length(f2)
sum(TaylorSeries.size_table)
Expand Down
7 changes: 4 additions & 3 deletions docs/src/userguide.md
Expand Up @@ -304,11 +304,12 @@ exy = exp(x+y)

The function [`getcoeff`](@ref)
gives the normalized coefficient of the polynomial that corresponds to the
monomial specified by a vector `v` containing the powers. For instance, for
monomial specified by the tuple or vector `v` containing the powers.
For instance, for
the polynomial `exy` above, the coefficient of the monomial ``x^3 y^5`` is

obtained using `getcoeff(exy, (3,5))` or `getcoeff(exy, [3,5])`.
```@repl userguide
getcoeff(exy, [3,5])
getcoeff(exy, (3,5))
rationalize(ans)
```

Expand Down
18 changes: 11 additions & 7 deletions src/auxiliary.jl
Expand Up @@ -94,15 +94,17 @@ setindex!(a::Taylor1{T}, x::Array{T,1}, c::Colon) where {T<:Number} = a.coeffs[c
"""
getcoeff(a, v)
Return the coefficient of `a::HomogeneousPolynomial`, specified by
`v::Array{Int,1}` which has the indices of the specific monomial.
Return the coefficient of `a::HomogeneousPolynomial`, specified by `v`,
which is a tuple (or vector) with the indices of the specific
monomial.
"""
function getcoeff(a::HomogeneousPolynomial, v::Array{Int,1})
@assert length(v) == get_numvars()
function getcoeff(a::HomogeneousPolynomial, v::NTuple{N,Int}) where {N}
@assert N == get_numvars() && all(v .>= 0)
kdic = in_base(get_order(),v)
@inbounds n = pos_table[a.order+1][kdic]
a[n]
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)
Expand All @@ -123,14 +125,16 @@ setindex!(a::HomogeneousPolynomial{T}, x::Array{T,1}, c::Colon) where {T<:Number
"""
getcoeff(a, v)
Return the coefficient of `a::TaylorN`, specified by
`v::Array{Int,1}` which has the indices of the specific monomial.
Return the coefficient of `a::TaylorN`, specified by `v`,
which is a tuple (or vector) with the indices of the specific
monomial.
"""
function getcoeff(a::TaylorN, v::Array{Int,1})
function getcoeff(a::TaylorN, v::NTuple{N, Int}) where {N}
order = sum(v)
@assert order a.order
getcoeff(a[order], v)
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)
Expand Down
10 changes: 5 additions & 5 deletions test/manyvariables.jl
Expand Up @@ -97,8 +97,8 @@ end
@test get_order(zeroT) == 1
@test xT[1][1] == 1
@test yH[2] == 1
@test getcoeff(xT,[1,0]) == 1
@test getcoeff(yH,[1,0]) == 0
@test getcoeff(xT,(1,0)) == getcoeff(xT,[1,0]) == 1
@test getcoeff(yH,(1,0)) == getcoeff(yH,[1,0]) == 0
@test typeof(convert(HomogeneousPolynomial,1im)) ==
HomogeneousPolynomial{Complex{Int}}
@test convert(HomogeneousPolynomial,1im) ==
Expand Down Expand Up @@ -307,10 +307,10 @@ end
@test conj(im*yH) == (im*yH)'
@test conj(im*yT) == (im*yT)'
@test real( exp(1im * xT)) == cos(xT)
@test getcoeff(convert(TaylorN{Rational{Int}},cos(xT)),[4,0]) ==
@test getcoeff(convert(TaylorN{Rational{Int}},cos(xT)),(4,0)) ==
1//factorial(4)
cr = convert(TaylorN{Rational{Int}},cos(xT))
@test getcoeff(cr,[4,0]) == 1//factorial(4)
@test getcoeff(cr,(4,0)) == 1//factorial(4)
@test imag((exp(yT))^(-1im)') == sin(yT)
exy = exp( xT+yT )
@test evaluate(exy) == 1
Expand All @@ -323,7 +323,7 @@ end
@test isapprox(evaluate(exy, (1,1)), eeuler^2)
@test exy(:x₁, 0.0) == exp(yT)
txy = tan(xT+yT)
@test getcoeff(txy,[8,7]) == 929569/99225
@test getcoeff(txy,(8,7)) == 929569/99225
ptxy = xT + yT + (1/3)*( xT^3 + yT^3 ) + xT^2*yT + xT*yT^2
@test getindex(tan(TaylorN(1)+TaylorN(2)),0:4) == ptxy.coeffs[1:5]
@test evaluate(xH*yH, 1.0, 2.0) == (xH*yH)(1.0, 2.0) == 2.0
Expand Down

0 comments on commit c3b660c

Please sign in to comment.