From 469d5ddee5451d9deb2906fc2eef11f238577398 Mon Sep 17 00:00:00 2001 From: RJDennis Date: Mon, 20 Dec 2021 23:14:34 +0000 Subject: [PATCH] Generalize function-types --- Project.toml | 2 +- src/piecewise_linear.jl | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Project.toml b/Project.toml index 851380d..114b7e8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PiecewiseLinearApprox" uuid = "fcfa6960-8e2e-11e9-2cde-29dd09221fb3" authors = ["Richard Dennis "] -version = "0.1.3" +version = "0.1.4" [compat] julia = "^1.1" diff --git a/src/piecewise_linear.jl b/src/piecewise_linear.jl index f270e24..040879e 100644 --- a/src/piecewise_linear.jl +++ b/src/piecewise_linear.jl @@ -44,7 +44,7 @@ end const linear_nodes = piecewise_linear_nodes -function bracket_nodes(x::Array{T,1},point::T) where {T <: AbstractFloat} +function bracket_nodes(x::Array{T,1},point::R) where {T <: AbstractFloat,R<:Number} if point <= x[1] return (1,2) @@ -64,7 +64,7 @@ function bracket_nodes(x::Array{T,1},point::T) where {T <: AbstractFloat} end -function bracket_nodes(x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Array{T,1}) where {T <: AbstractFloat, N} +function bracket_nodes(x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Array{R,1}) where {T <: AbstractFloat, N,R<:Number} bracketing_nodes = Array{Int64,2}(undef,2,length(point)) for i = 1:length(point) @@ -75,7 +75,7 @@ function bracket_nodes(x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point: end -function piecewise_linear_weight(x::Array{T,1},point::T) where {T <: AbstractFloat} +function piecewise_linear_weight(x::Array{T,1},point::R) where {T<:AbstractFloat,R<:Number} bracketing_nodes = bracket_nodes(x,point) weight = (point-x[bracketing_nodes[1]])/(x[bracketing_nodes[2]]-x[bracketing_nodes[1]]) @@ -84,9 +84,9 @@ function piecewise_linear_weight(x::Array{T,1},point::T) where {T <: AbstractFlo end -function piecewise_linear_weights(x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Array{T,1}) where {T <: AbstractFloat, N} +function piecewise_linear_weights(x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Array{R,1}) where {T<:AbstractFloat,N,R<:Number} - weights = Array{T,1}(undef,length(point)) + weights = Array{R,1}(undef,length(point)) for i = 1:length(point) weights[i] = piecewise_linear_weight(x[i],point[i]) end @@ -121,7 +121,7 @@ function select_relevant_data(y::AbstractArray{T,N},bracketing_grid_points::Arra end -function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Union{T,Array{T,1}}) where {T <: AbstractFloat, N} +function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Union{R,Array{R,1}}) where {T<:AbstractFloat,N,R<:Number} b = bracket_nodes(x,point) w = piecewise_linear_weights(x,point) @@ -148,7 +148,7 @@ end function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}}) where {T <: AbstractFloat, N} - function approximating_function(point::Union{T,Array{T,1}}) where {T <: AbstractFloat} + function approximating_function(point::Union{R,Array{R,1}}) where {R<:Number} return piecewise_linear_evaluate(y,x,point) @@ -158,7 +158,7 @@ function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array end -function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Union{T,Array{T,1}},integrals::Union{T,Array{T,1}}) where {T <: AbstractFloat, N} +function piecewise_linear_evaluate(y::AbstractArray{T,N},x::Union{NTuple{N,Array{T,1}},Array{Array{T,1},1}},point::Union{R,Array{R,1}},integrals::Union{T,Array{T,1}}) where {T<:AbstractFloat,N,R<:Number} # This function is only needed to facilitate compatibility with SolveDSGE @@ -188,7 +188,7 @@ end # Functions to handle the 1-D case -function piecewise_linear_evaluate(y::Array{T,1},x::Array{T,1},point::T) where {T <: AbstractFloat} +function piecewise_linear_evaluate(y::Array{T,1},x::Array{T,1},point::R) where {T<:AbstractFloat,R<:Number} b = bracket_nodes(x,point) w = piecewise_linear_weight(x,point) @@ -201,7 +201,7 @@ end function piecewise_linear_evaluate(y::Array{T,1},nodes::Array{T,1}) where {T <: AbstractFloat} - function approximating_function(point::T) where {T <: AbstractFloat} + function approximating_function(point::R) where {R<:Number} return piecewise_linear_evaluate(y,nodes,point) @@ -213,7 +213,7 @@ end # Function to transform from one uniform grid to another -function grid_reshape(f::Array{T,N},grid::NTuple{N,Array{T,1}}) where {T<:AbstractFloat,N} +function grid_reshape(f::AbstractArray{T,N},grid::NTuple{N,Array{T,1}}) where {T<:AbstractFloat,N} #1. Construct the old grid