Skip to content

Commit

Permalink
ENH: add routines for complete_polynomial(z, ::Degree, [::Derivative]) (
Browse files Browse the repository at this point in the history
#33)

* ENH: add routines for complete_polynomial(z, ::Degree, [::Derivative])

* TEST: clean up a testing bug
  • Loading branch information
sglyon authored Jul 18, 2017
1 parent 8bdb25e commit fa9b868
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 40 deletions.
41 changes: 29 additions & 12 deletions src/complete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function n_complete(n::Int, D::Int)
out
end

n_complete{D}(n::Int, ::Union{Degree{D},Type{Degree{D}}}) = n_complete(n, D)

#
# Generating basis functions
#
Expand Down Expand Up @@ -87,12 +89,15 @@ function complete_polynomial_impl!{T,N}(out::Type{Vector{T}}, z::Type{Vector{T}}
end
end

function complete_polynomial{T}(z::Vector{T}, d::Int)
function complete_polynomial{T,N}(z::AbstractVector{T}, d::Degree{N})
nvar = length(z)
out = Array{T}(n_complete(nvar, d))
complete_polynomial!(out, z, Degree{d}())
complete_polynomial!(out, z, d)
out
end

return out
function complete_polynomial(z::AbstractVector, d::Int)
complete_polynomial(z, Degree{d}())
end

#
Expand Down Expand Up @@ -131,12 +136,16 @@ function complete_polynomial_impl!{T,N}(out::Type{Matrix{T}}, z::Type{Matrix{T}}
end
end

function complete_polynomial{T}(z::Matrix{T}, d::Int)

function complete_polynomial{T,N}(z::AbstractMatrix{T}, d::Degree{N})
nobs, nvar = size(z)
out = Array{T}(nobs, n_complete(nvar, d))
complete_polynomial!(out, z, Degree{d}())
complete_polynomial!(out, z, d)
out
end

return out
function complete_polynomial(z::AbstractMatrix, d::Int)
complete_polynomial(z, Degree{d}())
end

#
Expand Down Expand Up @@ -196,12 +205,16 @@ function complete_polynomial_impl!{T,N,D}(out::Type{Vector{T}}, z::Type{Vector{T
end
end

function complete_polynomial{T}(z::Vector{T}, d::Int, der::Int)
function complete_polynomial{N,D,T}(
z::AbstractVector{T}, d::Degree{N}, der::Derivative{D}
)
nvar = length(z)
out = Array{T}(n_complete(nvar, d))
complete_polynomial!(out, z, Degree{d}(), Derivative{der}())
complete_polynomial!(out, z, d, der)
end

return out
function complete_polynomial(z::AbstractVector, d::Int, der::Int)
complete_polynomial(z, Degree{d}(), Derivative{der}())
end

#
Expand Down Expand Up @@ -246,10 +259,14 @@ function complete_polynomial_impl!{T,N,D}(out::Type{Matrix{T}}, z::Type{Matrix{T
end
end

function complete_polynomial{T}(z::Matrix{T}, d::Int, der::Int)
function complete_polynomial{N,D,T}(
z::AbstractMatrix{T}, d::Degree{N}, der::Derivative{D}
)
nobs, nvar = size(z)
out = Array{T}(nobs, n_complete(nvar, d))
complete_polynomial!(out, z, Degree{d}(), Derivative{der}())
complete_polynomial!(out, z, d, der)
end

return out
function complete_polynomial(z::AbstractMatrix, d::Int, der::Int)
complete_polynomial(z, Degree{d}(), Derivative{der}())
end
62 changes: 34 additions & 28 deletions test/complete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,40 @@ end
z2 = z[1, :]
all_zero = zeros(10)
all_ones = ones(10)
out_2 = complete_polynomial(z, 2)
# need to test columns size(z, 2) + 2:end
@test all(isapprox.(out_2[:, 1], all_ones))
@test all(isapprox.(out_2[:, 2], z[:, 1]))
@test all(isapprox.(out_2[:, 3], z[:, 1].*z[:, 1]))
@test all(isapprox.(out_2[:, 4], z[:, 1].*z[:, 2]))
@test all(isapprox.(out_2[:, 5], z[:, 1].*z[:, 3]))
@test all(isapprox.(out_2[:, 6], z[:, 2]))
@test all(isapprox.(out_2[:, 7], z[:, 2].*z[:, 2]))
@test all(isapprox.(out_2[:, 8], z[:, 2].*z[:, 3]))
@test all(isapprox.(out_2[:, 9], z[:, 3]))
@test all(isapprox.(out_2[:, 10], z[:, 3].*z[:, 3]))
# Test vector methods to make sure generates same thing as matrix methods
@test all(isapprox.(out_2[1, :], complete_polynomial(z2, 2)))
out_21 = complete_polynomial(z, 2)
out_22 = complete_polynomial(z, BasisMatrices.Degree{2}())
for out_2 in (out_21, out_22)
# need to test columns size(z, 2) + 2:end
@test all(isapprox.(out_2[:, 1], all_ones))
@test all(isapprox.(out_2[:, 2], z[:, 1]))
@test all(isapprox.(out_2[:, 3], z[:, 1].*z[:, 1]))
@test all(isapprox.(out_2[:, 4], z[:, 1].*z[:, 2]))
@test all(isapprox.(out_2[:, 5], z[:, 1].*z[:, 3]))
@test all(isapprox.(out_2[:, 6], z[:, 2]))
@test all(isapprox.(out_2[:, 7], z[:, 2].*z[:, 2]))
@test all(isapprox.(out_2[:, 8], z[:, 2].*z[:, 3]))
@test all(isapprox.(out_2[:, 9], z[:, 3]))
@test all(isapprox.(out_2[:, 10], z[:, 3].*z[:, 3]))
# Test vector methods to make sure generates same thing as matrix methods
@test all(isapprox.(out_2[1, :], complete_polynomial(z2, 2)))
end

out_der_2 = complete_polynomial(z, 2, 2)
# need to test columns size(z, 2) + 2:end
@test all(isapprox.(out_der_2[:, 1], all_zero))
@test all(isapprox.(out_der_2[:, 2], all_zero))
@test all(isapprox.(out_der_2[:, 3], all_zero))
@test all(isapprox.(out_der_2[:, 4], z[:, 1]))
@test all(isapprox.(out_der_2[:, 5], all_zero))
@test all(isapprox.(out_der_2[:, 6], all_ones))
@test all(isapprox.(out_der_2[:, 7], 2 .* z[:, 2]))
@test all(isapprox.(out_der_2[:, 8], z[:, 3]))
@test all(isapprox.(out_der_2[:, 9], all_zero))
@test all(isapprox.(out_der_2[:, 10], all_zero))
# Test vector methods to make sure generates same thing as matrix methods
@test all(isapprox.(out_2[1, :], complete_polynomial(z2, 2)))
out_der_21 = complete_polynomial(z, 2, 2)
out_der_22 = complete_polynomial(
z, BasisMatrices.Degree{2}(), BasisMatrices.Derivative{2}()
)
for out_der_2 in (out_der_21, out_der_22)
# need to test columns size(z, 2) + 2:end
@test all(isapprox.(out_der_2[:, 1], all_zero))
@test all(isapprox.(out_der_2[:, 2], all_zero))
@test all(isapprox.(out_der_2[:, 3], all_zero))
@test all(isapprox.(out_der_2[:, 4], z[:, 1]))
@test all(isapprox.(out_der_2[:, 5], all_zero))
@test all(isapprox.(out_der_2[:, 6], all_ones))
@test all(isapprox.(out_der_2[:, 7], 2 .* z[:, 2]))
@test all(isapprox.(out_der_2[:, 8], z[:, 3]))
@test all(isapprox.(out_der_2[:, 9], all_zero))
@test all(isapprox.(out_der_2[:, 10], all_zero))
end

end

0 comments on commit fa9b868

Please sign in to comment.