Skip to content

Commit

Permalink
feat: add show methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sathvikbhagavan committed Oct 20, 2023
1 parent e385f5f commit d4f9c76
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
9 changes: 7 additions & 2 deletions ext/DataInterpolationsOptimExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module DataInterpolationsOptimExt

if isdefined(Base, :get_extension)
using DataInterpolations: AbstractInterpolation, munge_data
import DataInterpolations: Curvefit, _interpolate
import DataInterpolations: Curvefit, _interpolate, get_show
using Reexport
else
using ..DataInterpolations: AbstractInterpolation, munge_data
import ..DataInterpolations: Curvefit, _interpolate
import ..DataInterpolations: Curvefit, _interpolate, get_show
using Reexport
end

Expand Down Expand Up @@ -75,4 +75,9 @@ function _interpolate(A::CurvefitCache{<:AbstractVector{<:Number}},
_interpolate(A, t), i
end

function get_show(interp::CurvefitCache)
return "Curvefit" * " with $(length(interp.t)) points, using $(nameof(typeof(interp.alg)))\n"
end


end # module
6 changes: 5 additions & 1 deletion ext/DataInterpolationsRegularizationToolsExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DataInterpolationsRegularizationToolsExt

using DataInterpolations
using DataInterpolations: munge_data, _interpolate, RegularizationSmooth
using DataInterpolations: munge_data, _interpolate, RegularizationSmooth, get_show
using LinearAlgebra

isdefined(Base, :get_extension) ? (import RegularizationTools as RT) :
Expand Down Expand Up @@ -269,4 +269,8 @@ function DataInterpolations._interpolate(A::RegularizationSmooth{
DataInterpolations._interpolate(A.Aitp, t)
end

function DataInterpolations.get_show(interp::RegularizationSmooth)
return "RegularizationSmooth" * " with $(length(interp.t)) points, with regularization coefficient $(interp.λ)\n"
end

end # module
2 changes: 2 additions & 0 deletions src/DataInterpolations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Base.setindex!(A::AbstractInterpolation{true}, x, i)
end

using LinearAlgebra, RecursiveArrayTools, RecipesBase
using PrettyTables

include("interpolation_caches.jl")
include("interpolation_utils.jl")
Expand All @@ -24,6 +25,7 @@ include("plot_rec.jl")
include("derivatives.jl")
include("integrals.jl")
include("online.jl")
include("show.jl")

(interp::AbstractInterpolation)(t::Number) = _interpolate(interp, t)
(interp::AbstractInterpolation)(t::Number, i::Integer) = _interpolate(interp, t, i)
Expand Down
65 changes: 65 additions & 0 deletions src/show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
###################### Generic Dispatches ######################

function Base.show(io::IO, mime::MIME"text/plain", interp::AbstractInterpolation)
print(io, get_show(interp))
header = ["time", get_names(interp.u)...]
data = hcat(interp.t, get_data(interp.u))
pretty_table(io, data; header = header, vcrop_mode = :middle)
end

function get_show(interp::AbstractInterpolation)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points\n"
end

function get_data(u::AbstractVector)
return u
end

function get_data(u::AbstractVector{<:AbstractVector})
return reduce(hcat, u)'
end

function get_data(u::AbstractMatrix)
return u'
end

function get_names(u::AbstractVector)
return ["u"]
end

function get_names(u::AbstractVector{<:AbstractVector})
return ["u$i" for i in eachindex(first(u))]
end

function get_names(u::AbstractMatrix)
return ["u$i" for i in axes(u, 1)]
end

###################### Specific Dispatches ######################

function get_show(interp::QuadraticInterpolation)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, $(interp.mode) mode\n"
end

function get_show(interp::LagrangeInterpolation)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with order $(interp.n)\n"
end

function get_show(interp::ConstantInterpolation)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, in $(interp.dir) direction\n"
end

function get_show(interp::BSplineInterpolation)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with degree $(interp.d)\n"
end

function get_show(interp::BSplineApprox)
return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with degree $(interp.d), number of control points $(interp.h)\n"
end







0 comments on commit d4f9c76

Please sign in to comment.